chore(client/ts): port components/shortcut_component

This commit is contained in:
Elian Doran 2024-12-23 21:47:36 +02:00
parent 8a47b2f5a8
commit 018be8c926
No known key found for this signature in database
4 changed files with 35 additions and 14 deletions

View File

@ -37,7 +37,7 @@ export default class MainTreeExecutors extends Component {
async createNoteIntoCommand() { async createNoteIntoCommand() {
const activeNoteContext = appContext.tabManager.getActiveContext(); const activeNoteContext = appContext.tabManager.getActiveContext();
if (!activeNoteContext) { if (!activeNoteContext || !activeNoteContext.notePath || !activeNoteContext.note) {
return; return;
} }

View File

@ -25,7 +25,7 @@ class NoteContext extends Component
hoistedNoteId: string; hoistedNoteId: string;
private mainNtxId: string | null; private mainNtxId: string | null;
private notePath?: string | null; notePath?: string | null;
private noteId?: string | null; private noteId?: string | null;
private parentNoteId?: string | null; private parentNoteId?: string | null;
private viewScope?: ViewScope; private viewScope?: ViewScope;
@ -62,11 +62,15 @@ class NoteContext extends Component
return !this.noteId; 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.triggerSwitchEvent = opts.triggerSwitchEvent !== undefined ? opts.triggerSwitchEvent : true;
opts.viewScope = opts.viewScope || {}; opts.viewScope = opts.viewScope || {};
opts.viewScope.viewMode = opts.viewScope.viewMode || "default"; opts.viewScope.viewMode = opts.viewScope.viewMode || "default";
if (!inputNotePath) {
return;
}
const resolvedNotePath = await this.getResolvedNotePath(inputNotePath); const resolvedNotePath = await this.getResolvedNotePath(inputNotePath);
if (!resolvedNotePath) { if (!resolvedNotePath) {
@ -301,7 +305,7 @@ class NoteContext extends Component
&& !this.note.isLabelTruthy('hideChildrenOverview'); && !this.note.isLabelTruthy('hideChildrenOverview');
} }
async getTextEditor(callback: GetTextEditorCallback) { async getTextEditor(callback?: GetTextEditorCallback) {
return this.timeout(new Promise(resolve => appContext.triggerCommand('executeWithTextEditor', { return this.timeout(new Promise(resolve => appContext.triggerCommand('executeWithTextEditor', {
callback, callback,
resolve, resolve,

View File

@ -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 shortcutService from "../services/shortcuts.js";
import server from "../services/server.js"; import server from "../services/server.js";
import Component from "./component.js"; import Component from "./component.js";
import froca from "../services/froca.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() { constructor() {
super(); super();
server.get('keyboard-shortcuts-for-notes').then(shortcutAttributes => { server.get<AttributeRow[]>('keyboard-shortcuts-for-notes').then(shortcutAttributes => {
for (const attr of shortcutAttributes) { for (const attr of shortcutAttributes) {
this.bindNoteShortcutHandler(attr); this.bindNoteShortcutHandler(attr);
} }
}); });
} }
bindNoteShortcutHandler(labelOrRow) { bindNoteShortcutHandler(labelOrRow: AttributeRow) {
const handler = () => appContext.tabManager.getActiveContext().setNote(labelOrRow.noteId); const handler = () => appContext.tabManager.getActiveContext().setNote(labelOrRow.noteId);
const namespace = labelOrRow.attributeId; const namespace = labelOrRow.attributeId;
if (labelOrRow.isDeleted) { // only applicable if row if (labelOrRow.isDeleted) { // only applicable if row
if (namespace) {
shortcutService.removeGlobalShortcut(namespace); shortcutService.removeGlobalShortcut(namespace);
} else { }
} else if (labelOrRow.value) {
shortcutService.bindGlobalShortcut(labelOrRow.value, handler, namespace); shortcutService.bindGlobalShortcut(labelOrRow.value, handler, namespace);
} }
} }
async entitiesReloadedEvent({loadResults}) { async entitiesReloadedEvent({loadResults}: EventData<"entitiesReloaded">) {
for (const attr of loadResults.getAttributeRows()) { 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); const note = await froca.getNote(attr.noteId);
// launcher shortcuts are handled specifically // launcher shortcuts are handled specifically
if (note && note.type !== 'launcher') { if (note && attr && note.type !== 'launcher') {
this.bindNoteShortcutHandler(attr); this.bindNoteShortcutHandler(attr);
} }
} }

View File

@ -1,4 +1,5 @@
import { NoteRow } from "../../../becca/entities/rows.js"; import { NoteRow } from "../../../becca/entities/rows.js";
import { AttributeType } from "../entities/fattribute.js";
import { EntityChange } from "../server_types.js"; import { EntityChange } from "../server_types.js";
interface BranchRow { interface BranchRow {
@ -11,6 +12,10 @@ export interface AttributeRow {
attributeId: string; attributeId: string;
componentId: string; componentId: string;
isInheritable?: boolean; isInheritable?: boolean;
isDeleted?: boolean;
name?: string;
value?: string;
type?: AttributeType;
} }
interface RevisionRow { interface RevisionRow {
@ -26,6 +31,10 @@ interface ContentNoteIdToComponentIdRow {
interface AttachmentRow {} interface AttachmentRow {}
interface OptionRow {}
interface NoteReorderingRow {}
interface ContentNoteIdToComponentIdRow { interface ContentNoteIdToComponentIdRow {
noteId: string; noteId: string;
componentId: string; componentId: string;
@ -34,7 +43,10 @@ interface ContentNoteIdToComponentIdRow {
type EntityRowMappings = { type EntityRowMappings = {
"notes": NoteRow, "notes": NoteRow,
"branches": BranchRow, "branches": BranchRow,
"attributes": AttributeRow "attributes": AttributeRow,
"options": OptionRow,
"revisions": RevisionRow,
"note_reordering": NoteReorderingRow
} }
export type EntityRowNames = keyof EntityRowMappings; export type EntityRowNames = keyof EntityRowMappings;