rename folder feature added. 4s edit wait, 6s cooldown adjust
This commit is contained in:
57
src/main.ts
57
src/main.ts
@@ -19,11 +19,19 @@
|
||||
// * rcb@beco.cc *
|
||||
// *************************************************************************
|
||||
|
||||
import { Plugin, TFile, TAbstractFile, Menu, moment } from 'obsidian';
|
||||
import {
|
||||
Plugin,
|
||||
TFile,
|
||||
TAbstractFile,
|
||||
TFolder,
|
||||
Menu,
|
||||
Notice,
|
||||
moment,
|
||||
} from 'obsidian';
|
||||
import { extractTimeId, generateSlug, buildFilename } from './slug';
|
||||
|
||||
const SETTLE_MS = 5000; // rename ~5s after the H1 stops changing
|
||||
const COOLDOWN_MS = 10000; // do not rename the same path again within 10s
|
||||
const SETTLE_MS = 4000; // rename ~4s after the H1 stops changing
|
||||
const COOLDOWN_MS = 6000; // do not rename the same path again within 6s
|
||||
|
||||
export default class ZettelcleanPlugin extends Plugin {
|
||||
private renameTimers = new Map<string, number>();
|
||||
@@ -37,11 +45,12 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
this.scheduleSync(file),
|
||||
),
|
||||
);
|
||||
// Retrofit entry in the file-explorer right-click menu, near "Rename".
|
||||
// File-explorer right-click menu: retrofit item for files, slug item for folders.
|
||||
this.registerEvent(
|
||||
this.app.workspace.on('file-menu', (menu, file) =>
|
||||
this.addTimeIdMenu(menu, file),
|
||||
),
|
||||
this.app.workspace.on('file-menu', (menu, file) => {
|
||||
this.addTimeIdMenu(menu, file);
|
||||
this.addFolderSlugMenu(menu, file);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -56,8 +65,8 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
return `${dir}${base}.md`;
|
||||
}
|
||||
|
||||
// Guard layer: only the actively edited zettel, honoring the 10s cooldown,
|
||||
// (re)arming the 5s settle timer on every cache change.
|
||||
// Guard layer: only the actively edited zettel, honoring the 6s cooldown,
|
||||
// (re)arming the 4s settle timer on every cache change.
|
||||
private scheduleSync(file: TAbstractFile) {
|
||||
if (this.isRenameInProgress) return; // our own rename, ignore
|
||||
if (!(file instanceof TFile) || file.extension !== 'md') return;
|
||||
@@ -65,7 +74,7 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
if (this.app.workspace.getActiveFile() !== file) return; // only the edited note
|
||||
|
||||
const last = this.lastRenamedAt.get(file.path);
|
||||
if (last && Date.now() - last < COOLDOWN_MS) return; // 10s cooldown
|
||||
if (last && Date.now() - last < COOLDOWN_MS) return; // 6s cooldown
|
||||
|
||||
const existing = this.renameTimers.get(file.path);
|
||||
if (existing) window.clearTimeout(existing);
|
||||
@@ -128,4 +137,32 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
}
|
||||
await this.app.fileManager.renameFile(file, target);
|
||||
}
|
||||
|
||||
// Folder right-click: slug the folder's own name (this folder only, instant).
|
||||
private addFolderSlugMenu(menu: Menu, file: TAbstractFile) {
|
||||
if (!(file instanceof TFolder) || file.isRoot()) return; // never the vault root
|
||||
menu.addItem((item) =>
|
||||
item
|
||||
.setTitle('Slug folder name')
|
||||
.setIcon('folder')
|
||||
.onClick(() => void this.slugFolder(file)),
|
||||
);
|
||||
}
|
||||
|
||||
private async slugFolder(folder: TFolder) {
|
||||
const newName = generateSlug(folder.name);
|
||||
if (!newName || newName === folder.name) {
|
||||
new Notice('Folder name is already clean');
|
||||
return;
|
||||
}
|
||||
const parent = folder.parent;
|
||||
const dir = parent && parent.path !== '/' ? `${parent.path}/` : '';
|
||||
const newPath = `${dir}${newName}`; // folders have no extension
|
||||
if (this.app.vault.getAbstractFileByPath(newPath)) {
|
||||
new Notice(`Cannot rename: "${newName}" already exists`);
|
||||
return;
|
||||
}
|
||||
await this.app.fileManager.renameFile(folder, newPath); // updates [[links]]
|
||||
new Notice(`Renamed folder "${folder.name}" -> "${newName}"`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user