From 15c63f52dc8342dc68dd7219da362731146f376d Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 28 Jan 2025 14:07:56 +0200 Subject: [PATCH] feat(client/ts): port note_title --- src/public/app/components/app_context.ts | 5 ++- src/public/app/services/load_results.ts | 2 +- .../app/widgets/note_context_aware_widget.ts | 4 +-- .../widgets/{note_title.js => note_title.ts} | 33 +++++++++++-------- 4 files changed, 27 insertions(+), 17 deletions(-) rename src/public/app/widgets/{note_title.js => note_title.ts} (76%) diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.ts index eb58409be..d3868eac3 100644 --- a/src/public/app/components/app_context.ts +++ b/src/public/app/components/app_context.ts @@ -71,7 +71,7 @@ export interface ExecuteCommandData extends CommandData { export type CommandMappings = { "api-log-messages": CommandData; focusTree: CommandData, - focusOnDetail: Required; + focusOnDetail: CommandData; focusOnSearchDefinition: Required; searchNotes: CommandData & { searchString?: string; @@ -238,6 +238,9 @@ type EventMappings = { beforeNoteSwitch: { noteContext: NoteContext; }; + beforeNoteContextRemove: { + ntxIds: string[]; + }; noteSwitched: { noteContext: NoteContext; notePath: string | null; diff --git a/src/public/app/services/load_results.ts b/src/public/app/services/load_results.ts index b7cdf5545..f87f62ea2 100644 --- a/src/public/app/services/load_results.ts +++ b/src/public/app/services/load_results.ts @@ -158,7 +158,7 @@ export default class LoadResults { return Object.keys(this.noteIdToComponentId); } - isNoteReloaded(noteId: string, componentId = null) { + isNoteReloaded(noteId: string | undefined, componentId: string | null = null) { if (!noteId) { return false; } diff --git a/src/public/app/widgets/note_context_aware_widget.ts b/src/public/app/widgets/note_context_aware_widget.ts index 4684c3079..7db5c1027 100644 --- a/src/public/app/widgets/note_context_aware_widget.ts +++ b/src/public/app/widgets/note_context_aware_widget.ts @@ -10,9 +10,9 @@ import type NoteContext from "../components/note_context.js"; class NoteContextAwareWidget extends BasicWidget { protected noteContext?: NoteContext; - isNoteContext(ntxId: string | null | undefined) { + isNoteContext(ntxId: string | string[] | null | undefined) { if (Array.isArray(ntxId)) { - return this.noteContext && ntxId.includes(this.noteContext.ntxId); + return this.noteContext && this.noteContext.ntxId && ntxId.includes(this.noteContext.ntxId); } else { return this.noteContext && this.noteContext.ntxId === ntxId; } diff --git a/src/public/app/widgets/note_title.js b/src/public/app/widgets/note_title.ts similarity index 76% rename from src/public/app/widgets/note_title.js rename to src/public/app/widgets/note_title.ts index 570b38b56..8cb333af9 100644 --- a/src/public/app/widgets/note_title.js +++ b/src/public/app/widgets/note_title.ts @@ -3,10 +3,11 @@ import NoteContextAwareWidget from "./note_context_aware_widget.js"; import protectedSessionHolder from "../services/protected_session_holder.js"; import server from "../services/server.js"; import SpacedUpdate from "../services/spaced_update.js"; -import appContext from "../components/app_context.js"; +import appContext, { type EventData } from "../components/app_context.js"; import branchService from "../services/branches.js"; import shortcutService from "../services/shortcuts.js"; import utils from "../services/utils.js"; +import type FNote from "../entities/fnote.js"; const TPL = `
@@ -33,13 +34,20 @@ const TPL = `
`; export default class NoteTitleWidget extends NoteContextAwareWidget { + + private $noteTitle!: JQuery; + private deleteNoteOnEscape: boolean; + private spacedUpdate: SpacedUpdate; + constructor() { super(); this.spacedUpdate = new SpacedUpdate(async () => { const title = this.$noteTitle.val(); - protectedSessionHolder.touchProtectedSessionIfNecessary(this.note); + if (this.note) { + protectedSessionHolder.touchProtectedSessionIfNecessary(this.note); + } await server.put(`notes/${this.noteId}/title`, { title }, this.componentId); }); @@ -62,37 +70,36 @@ export default class NoteTitleWidget extends NoteContextAwareWidget { }); shortcutService.bindElShortcut(this.$noteTitle, "esc", () => { - if (this.deleteNoteOnEscape && this.noteContext.isActive()) { + if (this.deleteNoteOnEscape && this.noteContext?.isActive() && this.noteContext?.note) { branchService.deleteNotes(Object.values(this.noteContext.note.parentToBranch)); } }); shortcutService.bindElShortcut(this.$noteTitle, "return", () => { - this.triggerCommand("focusOnDetail", { ntxId: this.noteContext.ntxId }); + this.triggerCommand("focusOnDetail", { ntxId: this.noteContext?.ntxId }); }); } - async refreshWithNote(note) { - const isReadOnly = (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) || utils.isLaunchBarConfig(note.noteId) || this.noteContext.viewScope.viewMode !== "default"; + async refreshWithNote(note: FNote) { + const isReadOnly = (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) || utils.isLaunchBarConfig(note.noteId) || this.noteContext?.viewScope?.viewMode !== "default"; - this.$noteTitle.val(isReadOnly ? await this.noteContext.getNavigationTitle() : note.title); + this.$noteTitle.val(isReadOnly ? await this.noteContext?.getNavigationTitle() || "" : note.title); this.$noteTitle.prop("readonly", isReadOnly); this.setProtectedStatus(note); } - /** @param {FNote} note */ - setProtectedStatus(note) { + setProtectedStatus(note: FNote) { this.$noteTitle.toggleClass("protected", !!note.isProtected); } - async beforeNoteSwitchEvent({ noteContext }) { + async beforeNoteSwitchEvent({ noteContext }: EventData<"beforeNoteSwitch">) { if (this.isNoteContext(noteContext.ntxId)) { await this.spacedUpdate.updateNowIfNecessary(); } } - async beforeNoteContextRemoveEvent({ ntxIds }) { + async beforeNoteContextRemoveEvent({ ntxIds }: EventData<"beforeNoteContextRemove">) { if (this.isNoteContext(ntxIds)) { await this.spacedUpdate.updateNowIfNecessary(); } @@ -112,8 +119,8 @@ export default class NoteTitleWidget extends NoteContextAwareWidget { } } - entitiesReloadedEvent({ loadResults }) { - if (loadResults.isNoteReloaded(this.noteId)) { + entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { + if (loadResults.isNoteReloaded(this.noteId) && this.note) { // not updating the title specifically since the synced title might be older than what the user is currently typing this.setProtectedStatus(this.note); }