Skip to content

Build your personal knowledge base with Trilium Notes

chore(client/ts): port components/shortcut_component

Elian Doran018be8c926e7
diff --git a/src/public/app/components/main_tree_executors.ts b/src/public/app/components/main_tree_executors.ts
index 9446176d4e..add809a08c 100644
--- a/src/public/app/components/main_tree_executors.ts
+++ b/src/public/app/components/main_tree_executors.ts
@@ -37,7 +37,7 @@ export default class MainTreeExecutors extends Component {
async createNoteIntoCommand() {
const activeNoteContext = appContext.tabManager.getActiveContext();
- if (!activeNoteContext) {
+ if (!activeNoteContext || !activeNoteContext.notePath || !activeNoteContext.note) {
return;
}
diff --git a/src/public/app/components/note_context.ts b/src/public/app/components/note_context.ts
index cb49864cff..41727f89a2 100644
--- a/src/public/app/components/note_context.ts
+++ b/src/public/app/components/note_context.ts
@@ -25,7 +25,7 @@ class NoteContext extends Component
hoistedNoteId: string;
private mainNtxId: string | null;
- private notePath?: string | null;
+ notePath?: string | null;
private noteId?: string | null;
private parentNoteId?: string | null;
private viewScope?: ViewScope;
@@ -62,11 +62,15 @@ class NoteContext extends Component
return !this.noteId;
}
- async setNote(inputNotePath: string, opts: SetNoteOpts = {}) {
+ async setNote(inputNotePath: string | undefined, opts: SetNoteOpts = {}) {
opts.triggerSwitchEvent = opts.triggerSwitchEvent !== undefined ? opts.triggerSwitchEvent : true;
opts.viewScope = opts.viewScope || {};
opts.viewScope.viewMode = opts.viewScope.viewMode || "default";
+ if (!inputNotePath) {
+ return;
+ }
+
const resolvedNotePath = await this.getResolvedNotePath(inputNotePath);
if (!resolvedNotePath) {
@@ -301,7 +305,7 @@ class NoteContext extends Component
&& !this.note.isLabelTruthy('hideChildrenOverview');
}
- async getTextEditor(callback: GetTextEditorCallback) {
+ async getTextEditor(callback?: GetTextEditorCallback) {
return this.timeout(new Promise(resolve => appContext.triggerCommand('executeWithTextEditor', {
callback,
resolve,
diff --git a/src/public/app/components/shortcut_component.js b/src/public/app/components/shortcut_component.ts
similarity index 59%
rename from src/public/app/components/shortcut_component.js
rename to src/public/app/components/shortcut_component.ts
index f19a718853..4d01a05889 100644
--- a/src/public/app/components/shortcut_component.js
+++ b/src/public/app/components/shortcut_component.ts
@@ -1,37 +1,42 @@
-import appContext from "./app_context.js";
+import appContext, { EventData, EventListener } from "./app_context.js";
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";
-export default class ShortcutComponent extends Component {
+export default class ShortcutComponent extends Component
+ implements EventListener<"entitiesReloaded">
+{
constructor() {
super();
- server.get('keyboard-shortcuts-for-notes').then(shortcutAttributes => {
+ server.get<AttributeRow[]>('keyboard-shortcuts-for-notes').then(shortcutAttributes => {
for (const attr of shortcutAttributes) {
this.bindNoteShortcutHandler(attr);
}
});
}
- bindNoteShortcutHandler(labelOrRow) {
+ bindNoteShortcutHandler(labelOrRow: AttributeRow) {
const handler = () => appContext.tabManager.getActiveContext().setNote(labelOrRow.noteId);
const namespace = labelOrRow.attributeId;
if (labelOrRow.isDeleted) { // only applicable if row
- shortcutService.removeGlobalShortcut(namespace);
- } else {
+ if (namespace) {
+ shortcutService.removeGlobalShortcut(namespace);
+ }
+ } else if (labelOrRow.value) {
shortcutService.bindGlobalShortcut(labelOrRow.value, handler, namespace);
}
}
- async entitiesReloadedEvent({loadResults}) {
+ async entitiesReloadedEvent({loadResults}: EventData<"entitiesReloaded">) {
for (const attr of loadResults.getAttributeRows()) {
- if (attr.type === 'label' && attr.name === 'keyboardShortcut') {
+ if (attr.type === 'label' && attr.name === 'keyboardShortcut' && attr.noteId) {
const note = await froca.getNote(attr.noteId);
// launcher shortcuts are handled specifically
- if (note && note.type !== 'launcher') {
+ if (note && attr && note.type !== 'launcher') {
this.bindNoteShortcutHandler(attr);
}
}
diff --git a/src/public/app/services/load_results.ts b/src/public/app/services/load_results.ts
index d06e9636e6..86ee7d4c8c 100644
--- a/src/public/app/services/load_results.ts
+++ b/src/public/app/services/load_results.ts
@@ -1,4 +1,5 @@
import { NoteRow } from "../../../becca/entities/rows.js";
+import { AttributeType } from "../entities/fattribute.js";
import { EntityChange } from "../server_types.js";
interface BranchRow {
@@ -11,6 +12,10 @@ export interface AttributeRow {
attributeId: string;
componentId: string;
isInheritable?: boolean;
+ isDeleted?: boolean;
+ name?: string;
+ value?: string;
+ type?: AttributeType;
}
interface RevisionRow {
@@ -26,6 +31,10 @@ interface ContentNoteIdToComponentIdRow {
interface AttachmentRow {}
+interface OptionRow {}
+
+interface NoteReorderingRow {}
+
interface ContentNoteIdToComponentIdRow {
noteId: string;
componentId: string;
@@ -34,7 +43,10 @@ interface ContentNoteIdToComponentIdRow {
type EntityRowMappings = {
"notes": NoteRow,
"branches": BranchRow,
- "attributes": AttributeRow
+ "attributes": AttributeRow,
+ "options": OptionRow,
+ "revisions": RevisionRow,
+ "note_reordering": NoteReorderingRow
}
export type EntityRowNames = keyof EntityRowMappings;