mirror of
https://github.com/felvesthe/Note-Locker.git
synced 2026-07-22 12:30:24 +00:00
Compare commits
No commits in common. "master" and "1.3.1" have entirely different histories.
5 changed files with 42 additions and 63 deletions
58
src/main.ts
58
src/main.ts
|
|
@ -8,7 +8,6 @@ import {
|
|||
WorkspaceLeaf,
|
||||
TAbstractFile,
|
||||
TFolder,
|
||||
setIcon,
|
||||
} from "obsidian";
|
||||
|
||||
import { NoteLockerSettings, DEFAULT_SETTINGS } from "./models/types";
|
||||
|
|
@ -45,7 +44,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
// hotkey
|
||||
this.addCommand({
|
||||
id: 'toggle-note-lock',
|
||||
name: 'Toggle lock for current note',
|
||||
name: 'Toggle Lock for current note',
|
||||
checkCallback: (checking: boolean) => {
|
||||
const file = this.app.workspace.getActiveFile();
|
||||
if (file && file.extension === 'md') {
|
||||
|
|
@ -66,7 +65,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
|
||||
this.addCommand({
|
||||
id: 'switch-to-edit-mode-intercept',
|
||||
name: 'Switch to edit mode (strict lock check)',
|
||||
name: 'Switch to Edit Mode (Strict Lock Check)',
|
||||
hotkeys: [{ modifiers: ["Mod"], key: "e" }],
|
||||
checkCallback: (checking: boolean) => {
|
||||
const file = this.app.workspace.getActiveFile();
|
||||
|
|
@ -114,7 +113,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
|
||||
this.registerEvent(
|
||||
this.app.workspace.on("active-leaf-change", (leaf) => {
|
||||
this.updateLeafMode(leaf, true);
|
||||
this.updateLeafMode(leaf);
|
||||
this.statusBarUI.updateStatusBarButton();
|
||||
})
|
||||
);
|
||||
|
|
@ -148,7 +147,6 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
this.app.workspace.on("file-open", () => {
|
||||
this.statusBarUI.updateStatusBarButton();
|
||||
this.fileExplorerUI.updateFileExplorerIcons();
|
||||
this.updateLeafMode(this.app.workspace.getLeaf(false), true);
|
||||
})
|
||||
);
|
||||
|
||||
|
|
@ -163,7 +161,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
private initializeExistingLeaves() {
|
||||
this.app.workspace
|
||||
.getLeavesOfType("markdown")
|
||||
.forEach((leaf) => this.updateLeafMode(leaf, true));
|
||||
.forEach((leaf) => this.updateLeafMode(leaf));
|
||||
}
|
||||
|
||||
private addBulkLockMenuItem(menu: Menu, files: TAbstractFile[]) {
|
||||
|
|
@ -203,7 +201,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
new StrictUnlockModal(
|
||||
this.app,
|
||||
() => this.toggleBulkLock(validItems, false),
|
||||
"Strictly locked items",
|
||||
"Strictly Locked Items",
|
||||
"At least one of the selected items is strictly locked."
|
||||
).open();
|
||||
} else {
|
||||
|
|
@ -225,7 +223,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
if (allUnlocked || allLocked) {
|
||||
menu.addItem((item) =>
|
||||
item
|
||||
.setTitle(`Strict lock ${validItems.length} items`)
|
||||
.setTitle(`Strict Lock ${validItems.length} items`)
|
||||
.setIcon("lock")
|
||||
.onClick(() => this.toggleBulkStrictLock(validItems, true))
|
||||
);
|
||||
|
|
@ -312,14 +310,14 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
if (!isStrictLocked) {
|
||||
menu.addItem((item) =>
|
||||
item
|
||||
.setTitle("Strict lock")
|
||||
.setTitle("Strict Lock")
|
||||
.setIcon("lock")
|
||||
.onClick(() => this.toggleStrictLock(path))
|
||||
);
|
||||
} else {
|
||||
menu.addItem((item) =>
|
||||
item
|
||||
.setTitle("Strict unlock")
|
||||
.setTitle("Strict Unlock")
|
||||
.setIcon("unlock")
|
||||
.onClick(() => {
|
||||
new StrictUnlockModal(this.app, () => {
|
||||
|
|
@ -411,26 +409,15 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
async toggleStrictLock(notePath: string) {
|
||||
const isStrictLocked = this.settings.strictLockedNotes.has(notePath);
|
||||
|
||||
isStrictLocked
|
||||
? this.settings.strictLockedNotes.delete(notePath)
|
||||
: this.settings.strictLockedNotes.add(notePath);
|
||||
if (isStrictLocked) {
|
||||
this.settings.strictLockedNotes.delete(notePath);
|
||||
} else {
|
||||
this.settings.strictLockedNotes.add(notePath);
|
||||
}
|
||||
|
||||
await this.saveSettings();
|
||||
|
||||
const file = this.app.vault.getAbstractFileByPath(notePath);
|
||||
|
||||
if (!file) {
|
||||
new Notice("Error: Note not found");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.settings.showNotifications) {
|
||||
const fileName = file instanceof TFile ? file.basename :
|
||||
notePath.split('/').pop()?.replace(/\..+$/, '') || notePath;
|
||||
const displayName = this.truncateFileName(fileName);
|
||||
|
||||
new Notice(`${isStrictLocked ? '🔓 Strictly unlocked' : '🔒 Strictly locked'}: ${displayName}`);
|
||||
}
|
||||
new Notice(`${isStrictLocked ? '🔓 Strictly Unlocked' : '🔒 Strictly Locked'}: ${notePath}`);
|
||||
|
||||
this.updateAllNoteInstances(notePath);
|
||||
this.statusBarUI.updateStatusBarButton();
|
||||
|
|
@ -472,7 +459,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
return false;
|
||||
}
|
||||
|
||||
public updateLeafMode(leaf: WorkspaceLeaf | null, fromNavigation: boolean = false) {
|
||||
public updateLeafMode(leaf: WorkspaceLeaf | null) {
|
||||
if (!leaf || !(leaf.view instanceof MarkdownView)) return;
|
||||
|
||||
const { view } = leaf;
|
||||
|
|
@ -489,11 +476,17 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
...state,
|
||||
state: { ...state.state, mode: 'preview' },
|
||||
});
|
||||
|
||||
if (this.app.workspace.activeLeaf === leaf) {
|
||||
new StrictUnlockModal(this.app, () => {
|
||||
this.toggleStrictLock(path);
|
||||
}).open();
|
||||
}
|
||||
return;
|
||||
}
|
||||
} else if (isLocked) {
|
||||
const state = leaf.getViewState();
|
||||
if ((fromNavigation || this.settings.preventEditInLockedNotes) && state.state?.mode === 'source') {
|
||||
if (this.settings.preventEditInLockedNotes && state.state?.mode === 'source') {
|
||||
leaf.setViewState({
|
||||
...state,
|
||||
state: { ...state.state, mode: 'preview' },
|
||||
|
|
@ -545,12 +538,12 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
|
||||
private addStrictLockButton(view: MarkdownView, path: string) {
|
||||
const container = view.containerEl;
|
||||
let lockBtn = container.querySelector('.note-locker-strict-btn') as HTMLElement;
|
||||
let lockBtn = container.querySelector('.note-locker-strict-btn');
|
||||
if (!lockBtn) {
|
||||
lockBtn = document.createElement('div');
|
||||
lockBtn.addClass('view-action', 'clickable-icon', 'note-locker-strict-btn');
|
||||
lockBtn.setAttribute('aria-label', 'Strictly locked');
|
||||
setIcon(lockBtn, 'lock-keyhole');
|
||||
lockBtn.setAttribute('aria-label', 'Strictly Locked');
|
||||
lockBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon lucide-lock"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>';
|
||||
|
||||
lockBtn.addEventListener('click', () => {
|
||||
new StrictUnlockModal(this.app, () => {
|
||||
|
|
@ -571,6 +564,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
if (lockBtn) lockBtn.remove();
|
||||
}
|
||||
|
||||
|
||||
async loadSettings() {
|
||||
const loaded = await this.loadData();
|
||||
if (loaded) {
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ export class NoteLockerSettingTab extends PluginSettingTab {
|
|||
});
|
||||
hotkeyInfo.style.fontStyle = 'italic';
|
||||
|
||||
containerEl.createEl('h3', { text: 'Locked notes & folders statistics' });
|
||||
containerEl.createEl('h3', { text: 'Locked Notes & Folders Statistics' });
|
||||
const lockedNotesCount = this.plugin.settings.lockedNotes.size;
|
||||
const lockedFoldersCount = this.plugin.settings.lockedFolders.size;
|
||||
const strictLockedNotesCount = this.plugin.settings.strictLockedNotes.size;
|
||||
|
|
|
|||
|
|
@ -141,20 +141,14 @@ export class FileExplorerUI {
|
|||
const path = titleEl.getAttribute('data-path');
|
||||
if (!path) return;
|
||||
|
||||
let iconName: string | null = null;
|
||||
let shouldHaveIcon = false;
|
||||
if (item.classList.contains('nav-file')) {
|
||||
if (this.plugin.settings.strictLockedNotes.has(path)) {
|
||||
iconName = 'lock-keyhole';
|
||||
} else if (this.plugin.settings.lockedNotes.has(path)) {
|
||||
iconName = 'lock';
|
||||
}
|
||||
shouldHaveIcon = this.plugin.settings.lockedNotes.has(path) || this.plugin.settings.strictLockedNotes.has(path);
|
||||
} else if (item.classList.contains('nav-folder')) {
|
||||
if (this.plugin.settings.lockedFolders.has(path)) {
|
||||
iconName = 'lock';
|
||||
}
|
||||
shouldHaveIcon = this.plugin.settings.lockedFolders.has(path);
|
||||
}
|
||||
|
||||
this.updateIconState(titleEl, iconName);
|
||||
this.updateIconState(titleEl, shouldHaveIcon);
|
||||
}
|
||||
|
||||
public updateFileExplorerIcons(): void {
|
||||
|
|
@ -172,13 +166,8 @@ export class FileExplorerUI {
|
|||
const filePath = titleEl.getAttribute('data-path');
|
||||
if (!filePath) return;
|
||||
|
||||
let iconName: string | null = null;
|
||||
if (this.plugin.settings.strictLockedNotes.has(filePath)) {
|
||||
iconName = 'lock-keyhole';
|
||||
} else if (this.plugin.settings.lockedNotes.has(filePath)) {
|
||||
iconName = 'lock';
|
||||
}
|
||||
this.updateIconState(titleEl, iconName);
|
||||
const shouldHaveIcon = this.plugin.settings.lockedNotes.has(filePath) || this.plugin.settings.strictLockedNotes.has(filePath);
|
||||
this.updateIconState(titleEl, shouldHaveIcon);
|
||||
});
|
||||
|
||||
// Handle folders
|
||||
|
|
@ -190,25 +179,20 @@ export class FileExplorerUI {
|
|||
const folderPath = titleEl.getAttribute('data-path');
|
||||
if (!folderPath) return;
|
||||
|
||||
let iconName: string | null = null;
|
||||
if (this.plugin.settings.lockedFolders.has(folderPath)) {
|
||||
iconName = 'lock';
|
||||
}
|
||||
this.updateIconState(titleEl, iconName);
|
||||
const shouldHaveIcon = this.plugin.settings.lockedFolders.has(folderPath);
|
||||
this.updateIconState(titleEl, shouldHaveIcon);
|
||||
});
|
||||
}
|
||||
|
||||
private updateIconState(targetEl: Element, iconName: string | null): void {
|
||||
private updateIconState(targetEl: Element, shouldHaveIcon: boolean): void {
|
||||
const existingIcon = targetEl.querySelector('.note-locker-icon');
|
||||
|
||||
if (iconName) {
|
||||
if (shouldHaveIcon) {
|
||||
if (!existingIcon) {
|
||||
const iconEl = document.createElement('div');
|
||||
iconEl.addClass('note-locker-icon');
|
||||
setIcon(iconEl, iconName);
|
||||
setIcon(iconEl, 'lock');
|
||||
targetEl.appendChild(iconEl);
|
||||
} else {
|
||||
setIcon(existingIcon as HTMLElement, iconName);
|
||||
}
|
||||
} else {
|
||||
if (existingIcon) {
|
||||
|
|
|
|||
|
|
@ -56,8 +56,9 @@ export class StatusBarUI {
|
|||
const iconSpan = this.statusBarItemEl.createSpan({ cls: 'locker-icon' });
|
||||
|
||||
if (isStrictLocked) {
|
||||
setIcon(iconSpan, 'lock-keyhole');
|
||||
this.statusBarItemEl.createSpan({ text: ' Strictly locked' });
|
||||
setIcon(iconSpan, 'lock');
|
||||
iconSpan.setText('🔓');
|
||||
this.statusBarItemEl.createSpan({ text: ' Strictly Locked' });
|
||||
this.statusBarItemEl.setAttribute('aria-label', 'Strictly locked. Click to unlock.');
|
||||
} else {
|
||||
setIcon(iconSpan, isLocked ? 'lock' : 'unlock');
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ export class StrictUnlockModal extends Modal {
|
|||
constructor(
|
||||
app: App,
|
||||
onUnlock: () => void,
|
||||
title: string = "Strictly locked note",
|
||||
title: string = "Strictly Locked Note",
|
||||
message: string = "This note is strictly locked to prevent accidental editing.") {
|
||||
super(app);
|
||||
this.onUnlock = onUnlock;
|
||||
|
|
|
|||
Loading…
Reference in a new issue