Merge pull request #1281 from TriliumNext/porting_js

port toc widget button
This commit is contained in:
Elian Doran 2025-02-26 17:28:36 +02:00 committed by GitHub
commit bb8277d035
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 67 additions and 54 deletions

View File

@ -312,6 +312,9 @@ type EventMappings = {
showHighlightsListWidget: { showHighlightsListWidget: {
noteId: string; noteId: string;
}; };
showTocWidget: {
noteId: string;
};
showSearchError: { showSearchError: {
error: string; error: string;
}; };
@ -350,9 +353,6 @@ type EventMappings = {
refreshNoteList: { refreshNoteList: {
noteId: string; noteId: string;
}; };
showToc: {
noteId: string;
};
noteTypeMimeChanged: { noteId: string }; noteTypeMimeChanged: { noteId: string };
zenModeChanged: { isEnabled: boolean }; zenModeChanged: { isEnabled: boolean };
}; };

View File

@ -16,7 +16,7 @@ export default class ShowHighlightsListWidgetButton extends OnClickButtonWidget
this.icon("bx-highlight") this.icon("bx-highlight")
.title(t("show_highlights_list_widget_button.show_highlights_list")) .title(t("show_highlights_list_widget_button.show_highlights_list"))
.titlePlacement("bottom") .titlePlacement("bottom")
.onClick((widget) => { .onClick(() => {
if (this.noteContext?.viewScope && this.noteId) { if (this.noteContext?.viewScope && this.noteId) {
this.noteContext.viewScope.highlightsListTemporarilyHidden = false; this.noteContext.viewScope.highlightsListTemporarilyHidden = false;
appContext.triggerEvent("showHighlightsListWidget", { noteId: this.noteId }); appContext.triggerEvent("showHighlightsListWidget", { noteId: this.noteId });

View File

@ -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();
}
}
}

View File

@ -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<void> {
if (this.noteContext?.viewScope) {
this.toggleInt(this.noteContext.viewScope.tocTemporarilyHidden);
}
}
async reEvaluateTocWidgetVisibilityEvent({ noteId }: { noteId: string }): Promise<void> {
if (noteId === this.noteId) {
await this.refresh();
}
}
async entitiesReloadedEvent({ loadResults }: { loadResults: LoadResults }): Promise<void> {
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<void> {
if (this.isNote(noteId)) {
await this.refresh();
}
}
}

View File

@ -329,7 +329,7 @@ export default class TocWidget extends RightPanelWidget {
appContext.triggerEvent("reEvaluateTocWidgetVisibility", { noteId: this.noteId }); appContext.triggerEvent("reEvaluateTocWidgetVisibility", { noteId: this.noteId });
} }
async showTocWidgetEvent({ noteId }: EventData<"showToc">) { async showTocWidgetEvent({ noteId }: EventData<"showTocWidget">) {
if (this.noteId === noteId) { if (this.noteId === noteId) {
await this.refresh(); await this.refresh();
this.triggerCommand("reEvaluateRightPaneVisibility"); this.triggerCommand("reEvaluateRightPaneVisibility");