mirror of
https://github.com/felvesthe/Note-Locker.git
synced 2026-07-22 12:30:24 +00:00
Compare commits
6 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a2fbbbb9d | ||
|
|
1bbbb63f90 | ||
|
|
7d377dc99d | ||
|
|
cd4ecd7821 | ||
|
|
cf02579f86 | ||
|
|
f990abb7ef |
7 changed files with 139 additions and 80 deletions
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"id": "note-locker",
|
||||
"name": "Note Locker",
|
||||
"version": "1.3.0",
|
||||
"version": "1.3.1",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "Lock notes to open in preview mode by default.",
|
||||
"author": "Felvesthe",
|
||||
|
|
|
|||
149
src/main.ts
149
src/main.ts
|
|
@ -8,6 +8,7 @@ import {
|
|||
WorkspaceLeaf,
|
||||
TAbstractFile,
|
||||
TFolder,
|
||||
setIcon,
|
||||
} from "obsidian";
|
||||
|
||||
import { NoteLockerSettings, DEFAULT_SETTINGS } from "./models/types";
|
||||
|
|
@ -44,7 +45,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') {
|
||||
|
|
@ -65,7 +66,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();
|
||||
|
|
@ -113,7 +114,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
|
||||
this.registerEvent(
|
||||
this.app.workspace.on("active-leaf-change", (leaf) => {
|
||||
this.updateLeafMode(leaf);
|
||||
this.updateLeafMode(leaf, true);
|
||||
this.statusBarUI.updateStatusBarButton();
|
||||
})
|
||||
);
|
||||
|
|
@ -147,6 +148,7 @@ 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);
|
||||
})
|
||||
);
|
||||
|
||||
|
|
@ -161,7 +163,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
private initializeExistingLeaves() {
|
||||
this.app.workspace
|
||||
.getLeavesOfType("markdown")
|
||||
.forEach((leaf) => this.updateLeafMode(leaf));
|
||||
.forEach((leaf) => this.updateLeafMode(leaf, true));
|
||||
}
|
||||
|
||||
private addBulkLockMenuItem(menu: Menu, files: TAbstractFile[]) {
|
||||
|
|
@ -201,7 +203,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 {
|
||||
|
|
@ -223,7 +225,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))
|
||||
);
|
||||
|
|
@ -310,14 +312,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, () => {
|
||||
|
|
@ -409,15 +411,26 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
async toggleStrictLock(notePath: string) {
|
||||
const isStrictLocked = this.settings.strictLockedNotes.has(notePath);
|
||||
|
||||
if (isStrictLocked) {
|
||||
this.settings.strictLockedNotes.delete(notePath);
|
||||
} else {
|
||||
this.settings.strictLockedNotes.add(notePath);
|
||||
}
|
||||
isStrictLocked
|
||||
? this.settings.strictLockedNotes.delete(notePath)
|
||||
: this.settings.strictLockedNotes.add(notePath);
|
||||
|
||||
await this.saveSettings();
|
||||
|
||||
new Notice(`${isStrictLocked ? '🔓 Strictly Unlocked' : '🔒 Strictly Locked'}: ${notePath}`);
|
||||
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}`);
|
||||
}
|
||||
|
||||
this.updateAllNoteInstances(notePath);
|
||||
this.statusBarUI.updateStatusBarButton();
|
||||
|
|
@ -459,7 +472,7 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
return false;
|
||||
}
|
||||
|
||||
private updateLeafMode(leaf: WorkspaceLeaf | null) {
|
||||
public updateLeafMode(leaf: WorkspaceLeaf | null, fromNavigation: boolean = false) {
|
||||
if (!leaf || !(leaf.view instanceof MarkdownView)) return;
|
||||
|
||||
const { view } = leaf;
|
||||
|
|
@ -476,17 +489,11 @@ 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 (state.state?.mode === 'source') {
|
||||
if ((fromNavigation || this.settings.preventEditInLockedNotes) && state.state?.mode === 'source') {
|
||||
leaf.setViewState({
|
||||
...state,
|
||||
state: { ...state.state, mode: 'preview' },
|
||||
|
|
@ -495,55 +502,75 @@ export default class NoteLockerPlugin extends Plugin {
|
|||
}
|
||||
|
||||
if (isStrictLocked) {
|
||||
const container = view.containerEl;
|
||||
const allActions = container.querySelectorAll('.view-action');
|
||||
this.hideEditButtons(view);
|
||||
this.addStrictLockButton(view, path);
|
||||
} else if (isLocked && this.settings.preventEditInLockedNotes) {
|
||||
this.hideEditButtons(view);
|
||||
this.removeStrictLockButton(view);
|
||||
} else {
|
||||
this.showEditButtons(view);
|
||||
this.removeStrictLockButton(view);
|
||||
}
|
||||
}
|
||||
|
||||
allActions.forEach(btn => {
|
||||
if (btn instanceof HTMLElement) {
|
||||
const ariaLabel = btn.getAttribute('aria-label') || '';
|
||||
const hasPencilIcon = !!btn.querySelector('.lucide-pencil');
|
||||
private hideEditButtons(view: MarkdownView) {
|
||||
const container = view.containerEl;
|
||||
const allActions = container.querySelectorAll('.view-action');
|
||||
|
||||
if (hasPencilIcon || ariaLabel.includes('Edit') || ariaLabel.includes('edit') || ariaLabel.includes('Switch to editing')) {
|
||||
btn.style.display = 'none';
|
||||
}
|
||||
}
|
||||
});
|
||||
allActions.forEach(btn => {
|
||||
if (btn instanceof HTMLElement) {
|
||||
const ariaLabel = btn.getAttribute('aria-label') || '';
|
||||
const hasPencilIcon = !!btn.querySelector('.lucide-pencil');
|
||||
|
||||
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');
|
||||
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, () => {
|
||||
this.toggleStrictLock(path);
|
||||
}).open();
|
||||
});
|
||||
|
||||
const actionsContainer = container.querySelector('.view-actions');
|
||||
if (actionsContainer) {
|
||||
actionsContainer.prepend(lockBtn);
|
||||
if (hasPencilIcon || ariaLabel.includes('Edit') || ariaLabel.includes('edit') || ariaLabel.includes('Switch to editing')) {
|
||||
btn.style.display = 'none';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const container = view.containerEl;
|
||||
const editBtns = container.querySelectorAll('.view-action');
|
||||
editBtns.forEach(btn => {
|
||||
if (btn instanceof HTMLElement) {
|
||||
if (btn.getAttribute('aria-label')?.includes('Edit') ||
|
||||
btn.getAttribute('aria-label')?.includes('edit') ||
|
||||
btn.querySelector('.lucide-pencil')) {
|
||||
btn.style.display = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private showEditButtons(view: MarkdownView) {
|
||||
const container = view.containerEl;
|
||||
const editBtns = container.querySelectorAll('.view-action');
|
||||
editBtns.forEach(btn => {
|
||||
if (btn instanceof HTMLElement) {
|
||||
if (btn.getAttribute('aria-label')?.includes('Edit') ||
|
||||
btn.getAttribute('aria-label')?.includes('edit') ||
|
||||
btn.querySelector('.lucide-pencil')) {
|
||||
btn.style.display = '';
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private addStrictLockButton(view: MarkdownView, path: string) {
|
||||
const container = view.containerEl;
|
||||
let lockBtn = container.querySelector('.note-locker-strict-btn') as HTMLElement;
|
||||
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.addEventListener('click', () => {
|
||||
new StrictUnlockModal(this.app, () => {
|
||||
this.toggleStrictLock(path);
|
||||
}).open();
|
||||
});
|
||||
const lockBtn = container.querySelector('.note-locker-strict-btn');
|
||||
if (lockBtn) lockBtn.remove();
|
||||
|
||||
const actionsContainer = container.querySelector('.view-actions');
|
||||
if (actionsContainer) {
|
||||
actionsContainer.prepend(lockBtn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private removeStrictLockButton(view: MarkdownView) {
|
||||
const container = view.containerEl;
|
||||
const lockBtn = container.querySelector('.note-locker-strict-btn');
|
||||
if (lockBtn) lockBtn.remove();
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
const loaded = await this.loadData();
|
||||
if (loaded) {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ export interface NoteLockerSettings {
|
|||
showFileExplorerIcons: boolean;
|
||||
showStatusBarButton: boolean;
|
||||
showNotifications: boolean;
|
||||
preventEditInLockedNotes: boolean;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: NoteLockerSettings = {
|
||||
|
|
@ -17,5 +18,6 @@ export const DEFAULT_SETTINGS: NoteLockerSettings = {
|
|||
desktopNotificationMaxLength: 22,
|
||||
showFileExplorerIcons: true,
|
||||
showStatusBarButton: true,
|
||||
showNotifications: true
|
||||
showNotifications: true,
|
||||
preventEditInLockedNotes: false
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { App, Platform, PluginSettingTab, Setting } from "obsidian";
|
||||
import { App, Platform, PluginSettingTab, Setting, MarkdownView } from "obsidian";
|
||||
import type NoteLockerPlugin from "./main";
|
||||
|
||||
export class NoteLockerSettingTab extends PluginSettingTab {
|
||||
|
|
@ -41,6 +41,21 @@ export class NoteLockerSettingTab extends PluginSettingTab {
|
|||
await this.plugin.saveSettings();
|
||||
}));
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Prevent editing in locked notes')
|
||||
.setDesc('If enabled, normally locked notes will be read-only. If disabled, you can switch to edit mode.')
|
||||
.addToggle(toggle => toggle
|
||||
.setValue(this.plugin.settings.preventEditInLockedNotes)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.preventEditInLockedNotes = value;
|
||||
await this.plugin.saveSettings();
|
||||
this.plugin.app.workspace.iterateAllLeaves((leaf) => {
|
||||
if (leaf.view instanceof MarkdownView && leaf.view.file) {
|
||||
this.plugin.updateLeafMode(leaf);
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
const mobileNotificationSetting = new Setting(containerEl)
|
||||
.setName('Mobile notification max length')
|
||||
.setDesc('Maximum length of file names in notifications on mobile devices')
|
||||
|
|
@ -80,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,14 +141,20 @@ export class FileExplorerUI {
|
|||
const path = titleEl.getAttribute('data-path');
|
||||
if (!path) return;
|
||||
|
||||
let shouldHaveIcon = false;
|
||||
let iconName: string | null = null;
|
||||
if (item.classList.contains('nav-file')) {
|
||||
shouldHaveIcon = this.plugin.settings.lockedNotes.has(path) || this.plugin.settings.strictLockedNotes.has(path);
|
||||
if (this.plugin.settings.strictLockedNotes.has(path)) {
|
||||
iconName = 'lock-keyhole';
|
||||
} else if (this.plugin.settings.lockedNotes.has(path)) {
|
||||
iconName = 'lock';
|
||||
}
|
||||
} else if (item.classList.contains('nav-folder')) {
|
||||
shouldHaveIcon = this.plugin.settings.lockedFolders.has(path);
|
||||
if (this.plugin.settings.lockedFolders.has(path)) {
|
||||
iconName = 'lock';
|
||||
}
|
||||
}
|
||||
|
||||
this.updateIconState(titleEl, shouldHaveIcon);
|
||||
this.updateIconState(titleEl, iconName);
|
||||
}
|
||||
|
||||
public updateFileExplorerIcons(): void {
|
||||
|
|
@ -166,8 +172,13 @@ export class FileExplorerUI {
|
|||
const filePath = titleEl.getAttribute('data-path');
|
||||
if (!filePath) return;
|
||||
|
||||
const shouldHaveIcon = this.plugin.settings.lockedNotes.has(filePath) || this.plugin.settings.strictLockedNotes.has(filePath);
|
||||
this.updateIconState(titleEl, shouldHaveIcon);
|
||||
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);
|
||||
});
|
||||
|
||||
// Handle folders
|
||||
|
|
@ -179,20 +190,25 @@ export class FileExplorerUI {
|
|||
const folderPath = titleEl.getAttribute('data-path');
|
||||
if (!folderPath) return;
|
||||
|
||||
const shouldHaveIcon = this.plugin.settings.lockedFolders.has(folderPath);
|
||||
this.updateIconState(titleEl, shouldHaveIcon);
|
||||
let iconName: string | null = null;
|
||||
if (this.plugin.settings.lockedFolders.has(folderPath)) {
|
||||
iconName = 'lock';
|
||||
}
|
||||
this.updateIconState(titleEl, iconName);
|
||||
});
|
||||
}
|
||||
|
||||
private updateIconState(targetEl: Element, shouldHaveIcon: boolean): void {
|
||||
private updateIconState(targetEl: Element, iconName: string | null): void {
|
||||
const existingIcon = targetEl.querySelector('.note-locker-icon');
|
||||
|
||||
if (shouldHaveIcon) {
|
||||
if (iconName) {
|
||||
if (!existingIcon) {
|
||||
const iconEl = document.createElement('div');
|
||||
iconEl.addClass('note-locker-icon');
|
||||
setIcon(iconEl, 'lock');
|
||||
setIcon(iconEl, iconName);
|
||||
targetEl.appendChild(iconEl);
|
||||
} else {
|
||||
setIcon(existingIcon as HTMLElement, iconName);
|
||||
}
|
||||
} else {
|
||||
if (existingIcon) {
|
||||
|
|
|
|||
|
|
@ -56,9 +56,8 @@ export class StatusBarUI {
|
|||
const iconSpan = this.statusBarItemEl.createSpan({ cls: 'locker-icon' });
|
||||
|
||||
if (isStrictLocked) {
|
||||
setIcon(iconSpan, 'lock');
|
||||
iconSpan.setText('🔓');
|
||||
this.statusBarItemEl.createSpan({ text: ' Strictly Locked' });
|
||||
setIcon(iconSpan, 'lock-keyhole');
|
||||
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