Notes/src/public/app/components/shortcut_component.ts

45 lines
1.7 KiB
TypeScript
Raw Normal View History

import appContext, { EventData, 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";
import froca from "../services/froca.js";
import { 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() {
super();
2025-01-09 18:07:02 +02:00
server.get<AttributeRow[]>("keyboard-shortcuts-for-notes").then((shortcutAttributes) => {
for (const attr of shortcutAttributes) {
this.bindNoteShortcutHandler(attr);
2022-12-01 13:07:23 +01:00
}
});
2022-12-01 13:07:23 +01:00
}
bindNoteShortcutHandler(labelOrRow: AttributeRow) {
const handler = () => appContext.tabManager.getActiveContext().setNote(labelOrRow.noteId);
const namespace = labelOrRow.attributeId;
2025-01-09 18:07:02 +02:00
if (labelOrRow.isDeleted) {
// only applicable if row
if (namespace) {
shortcutService.removeGlobalShortcut(namespace);
}
} else if (labelOrRow.value) {
shortcutService.bindGlobalShortcut(labelOrRow.value, handler, namespace);
}
}
2022-12-01 13:07:23 +01:00
2025-01-09 18:07:02 +02:00
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
for (const attr of loadResults.getAttributeRows()) {
2025-01-09 18:07:02 +02:00
if (attr.type === "label" && attr.name === "keyboardShortcut" && attr.noteId) {
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") {
this.bindNoteShortcutHandler(attr);
}
}
}
2022-12-01 13:07:23 +01:00
}
}