Skip to content

Commit

Permalink
chore(client/ts): port widgets/buttons/launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
eliandoran committed Jan 10, 2025
1 parent 7e00b88 commit c94346c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/public/app/menus/link_context_menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import contextMenu from "./context_menu.js";
import appContext from "../components/app_context.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({
x: e.pageX,
y: e.pageY,
Expand Down
2 changes: 1 addition & 1 deletion src/public/app/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function isMac() {
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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
import shortcutService from "../../../services/shortcuts.js";
import attributesService from "../../../services/attributes.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 {
constructor(launcherNote) {
export default abstract class AbstractLauncher extends OnClickButtonWidget {

protected launcherNote: FNote;

constructor(launcherNote: FNote) {
super();

this.class("launcher-button");

/** @type {FNote} */
this.launcherNote = launcherNote;

for (const label of launcherNote.getOwnedLabels("keyboardShortcut")) {
this.bindNoteShortcutHandler(label);
}
}

launch() {
throw new Error("Abstract implementation");
}
abstract launch(): void;

bindNoteShortcutHandler(labelOrRow) {
bindNoteShortcutHandler(labelOrRow: FAttribute | AttributeRow) {
const namespace = labelOrRow.attributeId;

if (labelOrRow.isDeleted) {
if ("isDeleted" in labelOrRow && labelOrRow.isDeleted) {
// only applicable if row
shortcutService.removeGlobalShortcut(namespace);
} else {
} else if (labelOrRow.value) {
shortcutService.bindGlobalShortcut(labelOrRow.value, () => this.launch(), namespace);
}
}

entitiesReloadedEvent({ loadResults }) {
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
for (const attr of loadResults.getAttributeRows()) {
if (attr.noteId === this.launcherNote.noteId && attr.type === "label" && attr.name === "keyboardShortcut") {
this.bindNoteShortcutHandler(attr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dialogService from "../../../services/dialog.js";
import appContext from "../../../components/app_context.js";
import utils from "../../../services/utils.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,
// e.g. you want to make launchers to 2 mermaid diagrams which both have mermaid icon (ok),
Expand All @@ -13,16 +14,17 @@ import linkContextMenuService from "../../../menus/link_context_menu.js";
// 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
export default class NoteLauncher extends AbstractLauncher {
constructor(launcherNote) {
constructor(launcherNote: FNote) {
super(launcherNote);

this.title(() => this.launcherNote.title)
.icon(() => this.launcherNote.getIcon())
.onClick((widget, evt) => this.launch(evt))
.onAuxClick((widget, evt) => this.launch(evt))
.onContextMenu((evt) => {
const targetNoteId = this.getTargetNoteId();
if (!targetNoteId) {
.onContextMenu(async (evt) => {
let targetNoteId = await Promise.resolve(this.getTargetNoteId());

if (!targetNoteId || !evt) {
return;
}

Expand All @@ -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
const targetNoteId = await this.getTargetNoteId();
if (!targetNoteId || evt.which === 3) {
if (!targetNoteId || evt?.which === 3) {
return;
}

const hoistedNoteId = await this.getHoistedNoteId();
if (!hoistedNoteId) {
return;
}

if (!evt) {
// keyboard shortcut
// TODO: Fix once tabManager is ported.
//@ts-ignore
await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteId);
} else {
const ctrlKey = utils.isCtrlKey(evt);

if ((evt.which === 1 && ctrlKey) || evt.which === 2) {
// TODO: Fix once tabManager is ported.
//@ts-ignore
await appContext.tabManager.openInNewTab(targetNoteId, hoistedNoteId);
} else {
// TODO: Fix once tabManager is ported.
//@ts-ignore
await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteId);
}
}
}

getTargetNoteId() {
getTargetNoteId(): void | string | Promise<string | undefined> {
const targetNoteId = this.launcherNote.getRelationValue("target");

if (!targetNoteId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type FNote from "../../../entities/fnote.js";
import AbstractLauncher from "./abstract_launcher.js";

export default class ScriptLauncher extends AbstractLauncher {
constructor(launcherNote) {
constructor(launcherNote: FNote) {
super(launcherNote);

this.title(() => this.launcherNote.title)
Expand All @@ -14,8 +15,9 @@ export default class ScriptLauncher extends AbstractLauncher {
await this.launcherNote.executeScript();
} else {
const script = await this.launcherNote.getRelationTarget("script");

await script.executeScript();
if (script) {
await script.executeScript();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class TodayLauncher extends NoteLauncher {
async getTargetNoteId() {
const todayNote = await dateNotesService.getTodayNote();

return todayNote.noteId;
return todayNote?.noteId;
}

getHoistedNoteId() {
Expand Down

0 comments on commit c94346c

Please sign in to comment.