From eec6cb8de2f08332c2ca4e4e6c289ac6b97699f6 Mon Sep 17 00:00:00 2001 From: Felvesthe <23108389+Felvesthe@users.noreply.github.com> Date: Sun, 23 Nov 2025 00:38:17 +0100 Subject: [PATCH] Implement possibility to lock/unlock multiple notes at once Signed-off-by: Felvesthe <23108389+Felvesthe@users.noreply.github.com> --- src/main.ts | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/main.ts b/src/main.ts index 394f754..6008dfe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,8 @@ import { Plugin, TFile, WorkspaceLeaf, + TAbstractFile, + TFolder, } from "obsidian"; import { NoteLockerSettings, DEFAULT_SETTINGS } from "./models/types"; @@ -70,6 +72,12 @@ export default class NoteLockerPlugin extends Plugin { ) ); + this.registerEvent( + this.app.workspace.on("files-menu", (menu, files) => + this.addBulkLockMenuItem(menu, files) + ) + ); + this.registerEvent( this.app.workspace.on("editor-menu", (menu, _, view) => view.file && this.addLockMenuItem(menu, view.file.path) @@ -130,6 +138,74 @@ export default class NoteLockerPlugin extends Plugin { .forEach((leaf) => this.updateLeafMode(leaf)); } + private addBulkLockMenuItem(menu: Menu, files: TAbstractFile[]) { + const validItems = files.filter(f => + (f instanceof TFile && f.extension === 'md') || + (f instanceof TFolder) + ); + + if (validItems.length < 2) return; + + let allLocked = true; + let allUnlocked = true; + + for (const item of validItems) { + const isLocked = this.isPathLocked(item.path); + if (isLocked) { + allUnlocked = false; + } else { + allLocked = false; + } + } + + if (allLocked) { + menu.addItem((item) => + item + .setTitle(`Unlock ${validItems.length} items`) + .setIcon("unlock") + .onClick(() => this.toggleBulkLock(validItems, false)) + ); + } else if (allUnlocked) { + menu.addItem((item) => + item + .setTitle(`Lock ${validItems.length} items`) + .setIcon("lock") + .onClick(() => this.toggleBulkLock(validItems, true)) + ); + } + // if mixed, do nothing + } + + private async toggleBulkLock(files: TAbstractFile[], shouldLock: boolean) { + for (const file of files) { + if (file instanceof TFile) { + if (shouldLock) { + this.settings.lockedNotes.add(file.path); + } else { + this.settings.lockedNotes.delete(file.path); + } + } else if (file instanceof TFolder) { + if (shouldLock) { + this.settings.lockedFolders.add(file.path); + } else { + this.settings.lockedFolders.delete(file.path); + } + } + } + + await this.saveSettings(); + this.fileExplorerUI.updateFileExplorerIcons(); + + this.app.workspace.iterateAllLeaves((leaf) => { + if (leaf.view instanceof MarkdownView && leaf.view.file) { + const path = leaf.view.file.path; + if (files.some(f => f.path === path || (f instanceof TFolder && path.startsWith(f.path + '/')))) { + this.updateLeafMode(leaf); + } + } + }); + } + private addLockMenuItem(menu: Menu, path: string) { if (this.isParentFolderLocked(path)) { return;