mirror of
https://github.com/NohamR/Pocket-Bird.git
synced 2026-05-24 19:59:36 +00:00
Add conditional menu items and disable sticky notes at root
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
25
src/menu.js
25
src/menu.js
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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, "");
|
||||
|
||||
Reference in New Issue
Block a user