2025-01-09 18:36:24 +02:00
|
|
|
import appContext, { type EventData, type EventListener } from "./app_context.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import shortcutService from "../services/shortcuts.js";
|
|
|
|
import server from "../services/server.js";
|
|
|
|
import Component from "./component.js";
|
2022-12-01 13:24:34 +01:00
|
|
|
import froca from "../services/froca.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { AttributeRow } from "../services/load_results.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
export default class ShortcutComponent extends Component implements EventListener<"entitiesReloaded"> {
|
2022-12-01 13:07:23 +01:00
|
|
|
constructor() {
|
2022-12-01 13:24:34 +01:00
|
|
|
super();
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
server.get<AttributeRow[]>("keyboard-shortcuts-for-notes").then((shortcutAttributes) => {
|
2022-12-01 13:24:34 +01:00
|
|
|
for (const attr of shortcutAttributes) {
|
|
|
|
this.bindNoteShortcutHandler(attr);
|
2022-12-01 13:07:23 +01:00
|
|
|
}
|
2022-12-01 13:24:34 +01:00
|
|
|
});
|
2022-12-01 13:07:23 +01:00
|
|
|
}
|
|
|
|
|
2024-12-23 21:47:36 +02:00
|
|
|
bindNoteShortcutHandler(labelOrRow: AttributeRow) {
|
2025-03-03 21:02:18 +01:00
|
|
|
const handler = () => appContext.tabManager.getActiveContext()?.setNote(labelOrRow.noteId);
|
2023-06-05 16:26:05 +02:00
|
|
|
const namespace = labelOrRow.attributeId;
|
2022-12-01 13:24:34 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (labelOrRow.isDeleted) {
|
|
|
|
// only applicable if row
|
2024-12-23 21:47:36 +02:00
|
|
|
if (namespace) {
|
|
|
|
shortcutService.removeGlobalShortcut(namespace);
|
|
|
|
}
|
|
|
|
} else if (labelOrRow.value) {
|
2023-06-05 16:26:05 +02:00
|
|
|
shortcutService.bindGlobalShortcut(labelOrRow.value, handler, namespace);
|
2022-12-01 13:24:34 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 13:07:23 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
|
2023-06-05 16:12:02 +02:00
|
|
|
for (const attr of loadResults.getAttributeRows()) {
|
2025-01-09 18:07:02 +02:00
|
|
|
if (attr.type === "label" && attr.name === "keyboardShortcut" && attr.noteId) {
|
2022-12-01 13:24:34 +01:00
|
|
|
const note = await froca.getNote(attr.noteId);
|
|
|
|
// launcher shortcuts are handled specifically
|
2025-01-09 18:07:02 +02:00
|
|
|
if (note && attr && note.type !== "launcher") {
|
2022-12-01 13:24:34 +01:00
|
|
|
this.bindNoteShortcutHandler(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-01 13:07:23 +01:00
|
|
|
}
|
|
|
|
}
|