2024-08-02 09:09:49 +08:00
|
|
|
import { t } from "../../../services/i18n.js";
|
2024-08-02 09:21:15 +08:00
|
|
|
import AbstractLauncher from "./abstract_launcher.js";
|
2022-12-02 16:46:14 +01:00
|
|
|
import dialogService from "../../../services/dialog.js";
|
|
|
|
|
import appContext from "../../../components/app_context.js";
|
2022-12-09 16:48:00 +01:00
|
|
|
import utils from "../../../services/utils.js";
|
|
|
|
|
import linkContextMenuService from "../../../menus/link_context_menu.js";
|
2025-01-11 01:46:00 +02:00
|
|
|
import type FNote from "../../../entities/fnote.js";
|
2022-12-02 16:46:14 +01:00
|
|
|
|
2023-06-30 11:18:34 +02:00
|
|
|
// we're intentionally displaying the launcher title and icon instead of the target,
|
2022-12-09 16:48:00 +01:00
|
|
|
// e.g. you want to make launchers to 2 mermaid diagrams which both have mermaid icon (ok),
|
|
|
|
|
// but on the launchpad you want them distinguishable.
|
|
|
|
|
// for titles, the note titles may follow a different scheme than maybe desirable on the launchpad
|
|
|
|
|
// another reason is the discrepancy between what user sees on the launchpad and in the config (esp. icons).
|
|
|
|
|
// 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
|
2022-12-02 16:46:14 +01:00
|
|
|
export default class NoteLauncher extends AbstractLauncher {
|
2025-01-11 01:46:00 +02:00
|
|
|
constructor(launcherNote: FNote) {
|
2022-12-02 16:46:14 +01:00
|
|
|
super(launcherNote);
|
|
|
|
|
|
2022-12-18 22:05:06 +01:00
|
|
|
this.title(() => this.launcherNote.title)
|
|
|
|
|
.icon(() => this.launcherNote.getIcon())
|
2022-12-09 16:48:00 +01:00
|
|
|
.onClick((widget, evt) => this.launch(evt))
|
|
|
|
|
.onAuxClick((widget, evt) => this.launch(evt))
|
2025-01-11 01:46:00 +02:00
|
|
|
.onContextMenu(async (evt) => {
|
|
|
|
|
let targetNoteId = await Promise.resolve(this.getTargetNoteId());
|
|
|
|
|
|
|
|
|
|
if (!targetNoteId || !evt) {
|
2022-12-09 16:48:00 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 22:05:06 +01:00
|
|
|
const hoistedNoteId = this.getHoistedNoteId();
|
|
|
|
|
|
2023-04-03 23:47:24 +02:00
|
|
|
linkContextMenuService.openContextMenu(targetNoteId, evt, {}, hoistedNoteId);
|
2022-12-09 16:48:00 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-11 01:46:00 +02:00
|
|
|
async launch(evt?: JQuery.ClickEvent | JQuery.ContextMenuEvent | JQuery.TriggeredEvent) {
|
2022-12-18 23:25:35 +01:00
|
|
|
// await because subclass overrides can be async
|
|
|
|
|
const targetNoteId = await this.getTargetNoteId();
|
2025-01-11 01:46:00 +02:00
|
|
|
if (!targetNoteId || evt?.which === 3) {
|
2022-12-09 16:48:00 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-18 23:25:35 +01:00
|
|
|
const hoistedNoteId = await this.getHoistedNoteId();
|
2025-01-11 01:46:00 +02:00
|
|
|
if (!hoistedNoteId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-18 22:05:06 +01:00
|
|
|
|
2022-12-09 16:48:00 +01:00
|
|
|
if (!evt) {
|
|
|
|
|
// keyboard shortcut
|
2025-01-11 01:46:00 +02:00
|
|
|
// TODO: Fix once tabManager is ported.
|
|
|
|
|
//@ts-ignore
|
2022-12-19 23:19:47 +01:00
|
|
|
await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteId);
|
2022-12-09 16:48:00 +01:00
|
|
|
} else {
|
2022-12-18 22:05:06 +01:00
|
|
|
const ctrlKey = utils.isCtrlKey(evt);
|
|
|
|
|
|
|
|
|
|
if ((evt.which === 1 && ctrlKey) || evt.which === 2) {
|
2025-01-11 01:46:00 +02:00
|
|
|
// TODO: Fix once tabManager is ported.
|
|
|
|
|
//@ts-ignore
|
2022-12-19 23:19:47 +01:00
|
|
|
await appContext.tabManager.openInNewTab(targetNoteId, hoistedNoteId);
|
2022-12-18 22:05:06 +01:00
|
|
|
} else {
|
2025-01-11 01:46:00 +02:00
|
|
|
// TODO: Fix once tabManager is ported.
|
|
|
|
|
//@ts-ignore
|
2022-12-19 23:19:47 +01:00
|
|
|
await appContext.tabManager.openInSameTab(targetNoteId, hoistedNoteId);
|
2022-12-18 22:05:06 +01:00
|
|
|
}
|
2022-12-09 16:48:00 +01:00
|
|
|
}
|
2022-12-02 16:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 01:46:00 +02:00
|
|
|
getTargetNoteId(): void | string | Promise<string | undefined> {
|
2025-01-09 18:07:02 +02:00
|
|
|
const targetNoteId = this.launcherNote.getRelationValue("target");
|
2022-12-02 16:46:14 +01:00
|
|
|
|
|
|
|
|
if (!targetNoteId) {
|
2024-08-02 09:09:49 +08:00
|
|
|
dialogService.info(t("note_launcher.this_launcher_doesnt_define_target_note"));
|
2022-12-02 16:46:14 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 16:48:00 +01:00
|
|
|
return targetNoteId;
|
2022-12-02 16:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-18 22:05:06 +01:00
|
|
|
getHoistedNoteId() {
|
2025-01-09 18:07:02 +02:00
|
|
|
return this.launcherNote.getRelationValue("hoistedNote") || appContext.tabManager.getActiveContext().hoistedNoteId;
|
2022-12-18 22:05:06 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-02 16:46:14 +01:00
|
|
|
getTitle() {
|
2025-01-09 18:07:02 +02:00
|
|
|
const shortcuts = this.launcherNote
|
|
|
|
|
.getLabels("keyboardShortcut")
|
|
|
|
|
.map((l) => l.value)
|
|
|
|
|
.filter((v) => !!v)
|
2022-12-02 16:46:14 +01:00
|
|
|
.join(", ");
|
|
|
|
|
|
|
|
|
|
let title = super.getTitle();
|
|
|
|
|
if (shortcuts) {
|
|
|
|
|
title += ` (${shortcuts})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return title;
|
|
|
|
|
}
|
2022-12-18 22:05:06 +01:00
|
|
|
}
|