mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	chore(client/ts): port widgets/buttons/launcher
This commit is contained in:
		| @@ -3,7 +3,7 @@ import contextMenu from "./context_menu.js"; | |||||||
| import appContext from "../components/app_context.js"; | import appContext from "../components/app_context.js"; | ||||||
| import type { ViewScope } from "../services/link.js"; | import type { ViewScope } from "../services/link.js"; | ||||||
|  |  | ||||||
| function openContextMenu(notePath: string, e: PointerEvent, viewScope: ViewScope = {}, hoistedNoteId: string | null = null) { | function openContextMenu(notePath: string, e: PointerEvent | JQuery.ContextMenuEvent, viewScope: ViewScope = {}, hoistedNoteId: string | null = null) { | ||||||
|     contextMenu.show({ |     contextMenu.show({ | ||||||
|         x: e.pageX, |         x: e.pageX, | ||||||
|         y: e.pageY, |         y: e.pageY, | ||||||
|   | |||||||
| @@ -97,7 +97,7 @@ function isMac() { | |||||||
|     return navigator.platform.indexOf("Mac") > -1; |     return navigator.platform.indexOf("Mac") > -1; | ||||||
| } | } | ||||||
|  |  | ||||||
| function isCtrlKey(evt: KeyboardEvent | MouseEvent | JQuery.ClickEvent) { | function isCtrlKey(evt: KeyboardEvent | MouseEvent | JQuery.ClickEvent | JQuery.ContextMenuEvent | JQuery.TriggeredEvent) { | ||||||
|     return (!isMac() && evt.ctrlKey) || (isMac() && evt.metaKey); |     return (!isMac() && evt.ctrlKey) || (isMac() && evt.metaKey); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,14 +1,20 @@ | |||||||
| import shortcutService from "../../../services/shortcuts.js"; | import shortcutService from "../../../services/shortcuts.js"; | ||||||
| import attributesService from "../../../services/attributes.js"; | import attributesService from "../../../services/attributes.js"; | ||||||
| import OnClickButtonWidget from "../onclick_button.js"; | import OnClickButtonWidget from "../onclick_button.js"; | ||||||
|  | import type FNote from "../../../entities/fnote.js"; | ||||||
|  | import type FAttribute from "../../../entities/fattribute.js"; | ||||||
|  | import type { EventData } from "../../../components/app_context.js"; | ||||||
|  | import type { AttributeRow } from "../../../services/load_results.js"; | ||||||
| 
 | 
 | ||||||
| export default class AbstractLauncher extends OnClickButtonWidget { | export default abstract class AbstractLauncher extends OnClickButtonWidget { | ||||||
|     constructor(launcherNote) { | 
 | ||||||
|  |     protected launcherNote: FNote; | ||||||
|  | 
 | ||||||
|  |     constructor(launcherNote: FNote) { | ||||||
|         super(); |         super(); | ||||||
| 
 | 
 | ||||||
|         this.class("launcher-button"); |         this.class("launcher-button"); | ||||||
| 
 | 
 | ||||||
|         /** @type {FNote} */ |  | ||||||
|         this.launcherNote = launcherNote; |         this.launcherNote = launcherNote; | ||||||
| 
 | 
 | ||||||
|         for (const label of launcherNote.getOwnedLabels("keyboardShortcut")) { |         for (const label of launcherNote.getOwnedLabels("keyboardShortcut")) { | ||||||
| @@ -16,22 +22,20 @@ export default class AbstractLauncher extends OnClickButtonWidget { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     launch() { |     abstract launch(): void; | ||||||
|         throw new Error("Abstract implementation"); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     bindNoteShortcutHandler(labelOrRow) { |     bindNoteShortcutHandler(labelOrRow: FAttribute | AttributeRow) { | ||||||
|         const namespace = labelOrRow.attributeId; |         const namespace = labelOrRow.attributeId; | ||||||
| 
 | 
 | ||||||
|         if (labelOrRow.isDeleted) { |         if ("isDeleted" in labelOrRow && labelOrRow.isDeleted) { | ||||||
|             // only applicable if row
 |             // only applicable if row
 | ||||||
|             shortcutService.removeGlobalShortcut(namespace); |             shortcutService.removeGlobalShortcut(namespace); | ||||||
|         } else { |         } else if (labelOrRow.value) { | ||||||
|             shortcutService.bindGlobalShortcut(labelOrRow.value, () => this.launch(), namespace); |             shortcutService.bindGlobalShortcut(labelOrRow.value, () => this.launch(), namespace); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     entitiesReloadedEvent({ loadResults }) { |     entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { | ||||||
|         for (const attr of loadResults.getAttributeRows()) { |         for (const attr of loadResults.getAttributeRows()) { | ||||||
|             if (attr.noteId === this.launcherNote.noteId && attr.type === "label" && attr.name === "keyboardShortcut") { |             if (attr.noteId === this.launcherNote.noteId && attr.type === "label" && attr.name === "keyboardShortcut") { | ||||||
|                 this.bindNoteShortcutHandler(attr); |                 this.bindNoteShortcutHandler(attr); | ||||||
| @@ -4,6 +4,7 @@ import dialogService from "../../../services/dialog.js"; | |||||||
| import appContext from "../../../components/app_context.js"; | import appContext from "../../../components/app_context.js"; | ||||||
| import utils from "../../../services/utils.js"; | import utils from "../../../services/utils.js"; | ||||||
| import linkContextMenuService from "../../../menus/link_context_menu.js"; | import linkContextMenuService from "../../../menus/link_context_menu.js"; | ||||||
|  | import type FNote from "../../../entities/fnote.js"; | ||||||
| 
 | 
 | ||||||
| // we're intentionally displaying the launcher title and icon instead of the target,
 | // we're intentionally displaying the launcher title and icon instead of the target,
 | ||||||
| // e.g. you want to make launchers to 2 mermaid diagrams which both have mermaid icon (ok),
 | // e.g. you want to make launchers to 2 mermaid diagrams which both have mermaid icon (ok),
 | ||||||
| @@ -13,16 +14,17 @@ import linkContextMenuService from "../../../menus/link_context_menu.js"; | |||||||
| // The only downside is more work in setting up the typical case
 | // The only downside is more work in setting up the typical case
 | ||||||
| // where you actually want to have both title and icon in sync, but for those cases there are bookmarks
 | // where you actually want to have both title and icon in sync, but for those cases there are bookmarks
 | ||||||
| export default class NoteLauncher extends AbstractLauncher { | export default class NoteLauncher extends AbstractLauncher { | ||||||
|     constructor(launcherNote) { |     constructor(launcherNote: FNote) { | ||||||
|         super(launcherNote); |         super(launcherNote); | ||||||
| 
 | 
 | ||||||
|         this.title(() => this.launcherNote.title) |         this.title(() => this.launcherNote.title) | ||||||
|             .icon(() => this.launcherNote.getIcon()) |             .icon(() => this.launcherNote.getIcon()) | ||||||
|             .onClick((widget, evt) => this.launch(evt)) |             .onClick((widget, evt) => this.launch(evt)) | ||||||
|             .onAuxClick((widget, evt) => this.launch(evt)) |             .onAuxClick((widget, evt) => this.launch(evt)) | ||||||
|             .onContextMenu((evt) => { |             .onContextMenu(async (evt) => { | ||||||
|                 const targetNoteId = this.getTargetNoteId(); |                 let targetNoteId = await Promise.resolve(this.getTargetNoteId()); | ||||||
|                 if (!targetNoteId) { | 
 | ||||||
|  |                 if (!targetNoteId || !evt) { | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
| @@ -32,30 +34,39 @@ export default class NoteLauncher extends AbstractLauncher { | |||||||
|             }); |             }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async launch(evt) { |     async launch(evt?: JQuery.ClickEvent | JQuery.ContextMenuEvent | JQuery.TriggeredEvent) { | ||||||
|         // await because subclass overrides can be async
 |         // await because subclass overrides can be async
 | ||||||
|         const targetNoteId = await this.getTargetNoteId(); |         const targetNoteId = await this.getTargetNoteId(); | ||||||
|         if (!targetNoteId || evt.which === 3) { |         if (!targetNoteId || evt?.which === 3) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const hoistedNoteId = await this.getHoistedNoteId(); |         const hoistedNoteId = await this.getHoistedNoteId(); | ||||||
|  |         if (!hoistedNoteId) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         if (!evt) { |         if (!evt) { | ||||||
|             // keyboard shortcut
 |             // keyboard shortcut
 | ||||||
|  |             // TODO: Fix once tabManager is ported.
 | ||||||
|  |             //@ts-ignore
 | ||||||
|             await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteId); |             await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteId); | ||||||
|         } else { |         } else { | ||||||
|             const ctrlKey = utils.isCtrlKey(evt); |             const ctrlKey = utils.isCtrlKey(evt); | ||||||
| 
 | 
 | ||||||
|             if ((evt.which === 1 && ctrlKey) || evt.which === 2) { |             if ((evt.which === 1 && ctrlKey) || evt.which === 2) { | ||||||
|  |                 // TODO: Fix once tabManager is ported.
 | ||||||
|  |                 //@ts-ignore
 | ||||||
|                 await appContext.tabManager.openInNewTab(targetNoteId, hoistedNoteId); |                 await appContext.tabManager.openInNewTab(targetNoteId, hoistedNoteId); | ||||||
|             } else { |             } else { | ||||||
|  |                 // TODO: Fix once tabManager is ported.
 | ||||||
|  |                 //@ts-ignore
 | ||||||
|                 await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteId); |                 await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteId); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     getTargetNoteId() { |     getTargetNoteId(): void | string | Promise<string | undefined> { | ||||||
|         const targetNoteId = this.launcherNote.getRelationValue("target"); |         const targetNoteId = this.launcherNote.getRelationValue("target"); | ||||||
| 
 | 
 | ||||||
|         if (!targetNoteId) { |         if (!targetNoteId) { | ||||||
| @@ -1,7 +1,8 @@ | |||||||
|  | import type FNote from "../../../entities/fnote.js"; | ||||||
| import AbstractLauncher from "./abstract_launcher.js"; | import AbstractLauncher from "./abstract_launcher.js"; | ||||||
| 
 | 
 | ||||||
| export default class ScriptLauncher extends AbstractLauncher { | export default class ScriptLauncher extends AbstractLauncher { | ||||||
|     constructor(launcherNote) { |     constructor(launcherNote: FNote) { | ||||||
|         super(launcherNote); |         super(launcherNote); | ||||||
| 
 | 
 | ||||||
|         this.title(() => this.launcherNote.title) |         this.title(() => this.launcherNote.title) | ||||||
| @@ -14,8 +15,9 @@ export default class ScriptLauncher extends AbstractLauncher { | |||||||
|             await this.launcherNote.executeScript(); |             await this.launcherNote.executeScript(); | ||||||
|         } else { |         } else { | ||||||
|             const script = await this.launcherNote.getRelationTarget("script"); |             const script = await this.launcherNote.getRelationTarget("script"); | ||||||
| 
 |             if (script) { | ||||||
|                 await script.executeScript(); |                 await script.executeScript(); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | } | ||||||
| @@ -6,7 +6,7 @@ export default class TodayLauncher extends NoteLauncher { | |||||||
|     async getTargetNoteId() { |     async getTargetNoteId() { | ||||||
|         const todayNote = await dateNotesService.getTodayNote(); |         const todayNote = await dateNotesService.getTodayNote(); | ||||||
| 
 | 
 | ||||||
|         return todayNote.noteId; |         return todayNote?.noteId; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     getHoistedNoteId() { |     getHoistedNoteId() { | ||||||
		Reference in New Issue
	
	Block a user