From d28dfc2b649dcb66fe53efe9d99983ec6f1732d6 Mon Sep 17 00:00:00 2001 From: Jin <22962980+JYC333@users.noreply.github.com> Date: Wed, 26 Feb 2025 00:53:15 +0100 Subject: [PATCH] port toc widget button --- src/public/app/components/app_context.ts | 6 +- .../show_highlights_list_widget_button.ts | 2 +- .../widgets/buttons/show_toc_widget_button.js | 49 --------------- .../widgets/buttons/show_toc_widget_button.ts | 62 +++++++++++++++++++ src/public/app/widgets/toc.ts | 2 +- 5 files changed, 67 insertions(+), 54 deletions(-) delete mode 100644 src/public/app/widgets/buttons/show_toc_widget_button.js create mode 100644 src/public/app/widgets/buttons/show_toc_widget_button.ts diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.ts index 39141b881..6dad4246d 100644 --- a/src/public/app/components/app_context.ts +++ b/src/public/app/components/app_context.ts @@ -312,6 +312,9 @@ type EventMappings = { showHighlightsListWidget: { noteId: string; }; + showTocWidget: { + noteId: string; + }; showSearchError: { error: string; }; @@ -350,9 +353,6 @@ type EventMappings = { refreshNoteList: { noteId: string; }; - showToc: { - noteId: string; - }; noteTypeMimeChanged: { noteId: string }; zenModeChanged: { isEnabled: boolean }; }; diff --git a/src/public/app/widgets/buttons/show_highlights_list_widget_button.ts b/src/public/app/widgets/buttons/show_highlights_list_widget_button.ts index b5ce7920b..622bf35ca 100644 --- a/src/public/app/widgets/buttons/show_highlights_list_widget_button.ts +++ b/src/public/app/widgets/buttons/show_highlights_list_widget_button.ts @@ -16,7 +16,7 @@ export default class ShowHighlightsListWidgetButton extends OnClickButtonWidget this.icon("bx-highlight") .title(t("show_highlights_list_widget_button.show_highlights_list")) .titlePlacement("bottom") - .onClick((widget) => { + .onClick(() => { if (this.noteContext?.viewScope && this.noteId) { this.noteContext.viewScope.highlightsListTemporarilyHidden = false; appContext.triggerEvent("showHighlightsListWidget", { noteId: this.noteId }); diff --git a/src/public/app/widgets/buttons/show_toc_widget_button.js b/src/public/app/widgets/buttons/show_toc_widget_button.js deleted file mode 100644 index 226d482a4..000000000 --- a/src/public/app/widgets/buttons/show_toc_widget_button.js +++ /dev/null @@ -1,49 +0,0 @@ -import OnClickButtonWidget from "./onclick_button.js"; -import appContext from "../../components/app_context.js"; -import attributeService from "../../services/attributes.js"; -import { t } from "../../services/i18n.js"; - -export default class ShowTocWidgetButton extends OnClickButtonWidget { - isEnabled() { - return super.isEnabled() && this.note && this.note.type === "text" && this.noteContext.viewScope.viewMode === "default"; - } - - constructor() { - super(); - - this.icon("bx-objects-horizontal-left") - .title(t("show_toc_widget_button.show_toc")) - .titlePlacement("bottom") - .onClick((widget) => { - this.noteContext.viewScope.tocTemporarilyHidden = false; - appContext.triggerEvent("showTocWidget", { noteId: this.noteId }); - this.toggleInt(false); - }); - } - - async refreshWithNote(note) { - this.toggleInt(this.noteContext.viewScope.tocTemporarilyHidden); - } - async reEvaluateTocWidgetVisibilityEvent({ noteId }) { - if (noteId === this.noteId) { - await this.refresh(); - } - } - async entitiesReloadedEvent({ loadResults }) { - if (loadResults.isNoteContentReloaded(this.noteId)) { - await this.refresh(); - } else if ( - loadResults - .getAttributeRows() - .find((attr) => attr.type === "label" && (attr.name.toLowerCase().includes("readonly") || attr.name === "toc") && attributeService.isAffecting(attr, this.note)) - ) { - await this.refresh(); - } - } - - async noteTypeMimeChangedEvent({ noteId }) { - if (this.isNote(noteId)) { - await this.refresh(); - } - } -} diff --git a/src/public/app/widgets/buttons/show_toc_widget_button.ts b/src/public/app/widgets/buttons/show_toc_widget_button.ts new file mode 100644 index 000000000..7b4dad89a --- /dev/null +++ b/src/public/app/widgets/buttons/show_toc_widget_button.ts @@ -0,0 +1,62 @@ +import OnClickButtonWidget from "./onclick_button.js"; +import appContext from "../../components/app_context.js"; +import attributeService from "../../services/attributes.js"; +import { t } from "../../services/i18n.js"; +import LoadResults from "../../services/load_results.js"; +import type { AttributeRow } from "../../services/load_results.js"; + +export default class ShowTocWidgetButton extends OnClickButtonWidget { + isEnabled(): boolean { + return Boolean(super.isEnabled() && this.note && this.note.type === "text" && this.noteContext?.viewScope?.viewMode === "default"); + } + + constructor() { + super(); + + this.icon("bx-objects-horizontal-left") + .title(t("show_toc_widget_button.show_toc")) + .titlePlacement("bottom") + .onClick(() => { + if (this.noteContext?.viewScope && this.noteId) { + this.noteContext.viewScope.tocTemporarilyHidden = false; + appContext.triggerEvent("showTocWidget", { noteId: this.noteId }); + } + this.toggleInt(false); + }); + } + + async refreshWithNote(): Promise { + if (this.noteContext?.viewScope) { + this.toggleInt(this.noteContext.viewScope.tocTemporarilyHidden); + } + } + + async reEvaluateTocWidgetVisibilityEvent({ noteId }: { noteId: string }): Promise { + if (noteId === this.noteId) { + await this.refresh(); + } + } + + async entitiesReloadedEvent({ loadResults }: { loadResults: LoadResults }): Promise { + if (this.noteId && loadResults.isNoteContentReloaded(this.noteId)) { + await this.refresh(); + } else if ( + loadResults + .getAttributeRows() + .find((attr: AttributeRow) => + attr.type === "label" && + (attr.name?.toLowerCase().includes("readonly") || attr.name === "toc") && + this.note && + attributeService.isAffecting(attr, this.note) + ) + ) { + await this.refresh(); + } + } + + async noteTypeMimeChangedEvent({ noteId }: { noteId: string }): Promise { + if (this.isNote(noteId)) { + await this.refresh(); + } + } +} diff --git a/src/public/app/widgets/toc.ts b/src/public/app/widgets/toc.ts index 1958da572..6a843a11f 100644 --- a/src/public/app/widgets/toc.ts +++ b/src/public/app/widgets/toc.ts @@ -329,7 +329,7 @@ export default class TocWidget extends RightPanelWidget { appContext.triggerEvent("reEvaluateTocWidgetVisibility", { noteId: this.noteId }); } - async showTocWidgetEvent({ noteId }: EventData<"showToc">) { + async showTocWidgetEvent({ noteId }: EventData<"showTocWidget">) { if (this.noteId === noteId) { await this.refresh(); this.triggerCommand("reEvaluateRightPaneVisibility");