Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(client/ts): port note_title
Elian Doran15c63f52dc83
diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.tsindex eb58409be6..d3868eac37 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<CommandData>;+ focusOnDetail: CommandData;focusOnSearchDefinition: Required<CommandData>;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.tsindex b7cdf55453..f87f62ea2e 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.tsindex 4684c3079f..7db5c1027e 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.tssimilarity index 76%rename from src/public/app/widgets/note_title.jsrename to src/public/app/widgets/note_title.tsindex 570b38b56c..8cb333af9c 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 = `<div class="note-title-widget">@@ -33,13 +34,20 @@ const TPL = `</div>`;export default class NoteTitleWidget extends NoteContextAwareWidget {++ private $noteTitle!: JQuery<HTMLElement>;+ 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 typingthis.setProtectedStatus(this.note);}