Add conditional menu items and disable sticky notes at root

This commit is contained in:
Idrees Hassan
2025-11-14 00:28:14 -05:00
parent 7639c7c36a
commit 1175c40fa2
11 changed files with 144 additions and 58 deletions

View File

@@ -31,6 +31,7 @@ import {
} from './stickyNotes.js';
import {
MenuItem,
ConditionalMenuItem,
DebugMenuItem,
Separator,
insertMenu,
@@ -193,9 +194,7 @@ Promise.all([
const menuItems = [
new MenuItem(`Pet ${birdBirb()}`, pet),
new MenuItem("Field Guide", insertFieldGuide),
...(getContext().areStickyNotesEnabled() ? [
new MenuItem("Sticky Note", () => createNewStickyNote(stickyNotes, save, deleteStickyNote))
] : []),
new ConditionalMenuItem("Sticky Note", () => createNewStickyNote(stickyNotes, save, deleteStickyNote), () => getContext().areStickyNotesEnabled()),
new MenuItem(`Hide ${birdBirb()}`, () => birb.setVisible(false)),
new DebugMenuItem("Freeze/Unfreeze", () => {
frozen = !frozen;

View File

@@ -305,6 +305,11 @@ export class ObsidianContext extends Context {
return path === this.getPath();
}
/** @override */
areStickyNotesEnabled() {
return this.getPath() !== ROOT_PATH;
}
/** @returns {HTMLElement|null} */
getActiveEditorElement() {
// @ts-expect-error

View File

@@ -15,23 +15,34 @@ export class MenuItem {
* @param {string} text
* @param {() => void} action
* @param {boolean} [removeMenu]
* @param {boolean} [isDebug]
*/
constructor(text, action, removeMenu = true, isDebug = false) {
constructor(text, action, removeMenu = true) {
this.text = text;
this.action = action;
this.removeMenu = removeMenu;
this.isDebug = isDebug;
}
}
export class DebugMenuItem extends MenuItem {
export class ConditionalMenuItem extends MenuItem {
/**
* @param {string} text
* @param {() => void} action
* @param {() => boolean} condition
* @param {boolean} [removeMenu]
*/
constructor(text, action, condition, removeMenu = true) {
super(text, action, removeMenu);
this.condition = condition;
}
}
export class DebugMenuItem extends ConditionalMenuItem {
/**
* @param {string} text
* @param {() => void} action
*/
constructor(text, action, removeMenu = true) {
super(text, action, removeMenu, true);
super(text, action, () => isDebug(), removeMenu);
}
}
@@ -77,7 +88,7 @@ export function insertMenu(menuItems, title, updateLocationCallback) {
let content = makeElement("birb-window-content");
const removeCallback = () => removeMenu();
for (const item of menuItems) {
if (!item.isDebug || isDebug()) {
if (!(item instanceof ConditionalMenuItem) || item.condition()) {
content.appendChild(makeMenuItem(item, removeCallback));
}
}
@@ -134,7 +145,7 @@ export function switchMenuItems(menuItems, updateLocationCallback) {
}
const removeCallback = () => removeMenu();
for (const item of menuItems) {
if (!item.isDebug || isDebug()) {
if (!(item instanceof ConditionalMenuItem) || item.condition()) {
content.appendChild(makeMenuItem(item, removeCallback));
}
}

View File

@@ -129,6 +129,9 @@ export function drawStickyNotes(stickyNotes, onSave, onDelete) {
* @param {(note: StickyNote) => void} onDelete
*/
export function createNewStickyNote(stickyNotes, onSave, onDelete) {
if (getContext().areStickyNotesEnabled() === false) {
return;
}
const id = Date.now().toString();
const site = getContext().getPath();
const stickyNote = new StickyNote(id, site, "");