2025-02-16 18:09:01 +02:00
|
|
|
import appContext, { type EventData } from "../../components/app_context.js";
|
2025-02-02 20:44:36 +02:00
|
|
|
import type { NoteType } from "../../entities/fnote.js";
|
|
|
|
import { t } from "../../services/i18n.js";
|
2025-02-03 22:50:00 +02:00
|
|
|
import type { ViewScope } from "../../services/link.js";
|
2025-02-16 18:09:01 +02:00
|
|
|
import type { ViewTypeOptions } from "../../services/note_list_renderer.js";
|
2025-02-02 20:44:36 +02:00
|
|
|
import NoteContextAwareWidget from "../note_context_aware_widget.js";
|
|
|
|
|
|
|
|
const TPL = `
|
|
|
|
<button class="open-contextual-help-button" title="${t("help-button.title")}">
|
|
|
|
<span class="bx bx-help-circle"></span>
|
|
|
|
</button>
|
|
|
|
`;
|
|
|
|
|
2025-02-16 18:09:01 +02:00
|
|
|
const byNoteType: Record<Exclude<NoteType, "book">, string | null> = {
|
2025-02-02 20:44:36 +02:00
|
|
|
canvas: null,
|
|
|
|
code: null,
|
|
|
|
contentWidget: null,
|
|
|
|
doc: null,
|
|
|
|
file: null,
|
|
|
|
geoMap: "foPEtsL51pD2",
|
|
|
|
image: null,
|
|
|
|
launcher: null,
|
|
|
|
mermaid: null,
|
|
|
|
mindMap: null,
|
|
|
|
noteMap: null,
|
|
|
|
relationMap: null,
|
|
|
|
render: null,
|
|
|
|
search: null,
|
|
|
|
text: null,
|
2025-02-19 18:14:49 +02:00
|
|
|
webView: null,
|
|
|
|
taskList: null
|
2025-02-02 20:44:36 +02:00
|
|
|
};
|
|
|
|
|
2025-02-16 18:09:01 +02:00
|
|
|
const byBookType: Record<ViewTypeOptions, string | null> = {
|
|
|
|
list: null,
|
|
|
|
grid: null,
|
|
|
|
calendar: "fDGg7QcJg3Xm"
|
|
|
|
};
|
|
|
|
|
2025-02-02 20:44:36 +02:00
|
|
|
export default class ContextualHelpButton extends NoteContextAwareWidget {
|
|
|
|
|
|
|
|
private helpNoteIdToOpen?: string | null;
|
|
|
|
|
|
|
|
isEnabled() {
|
|
|
|
this.helpNoteIdToOpen = null;
|
|
|
|
|
|
|
|
if (!super.isEnabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-02-16 18:09:01 +02:00
|
|
|
if (this.note && this.note.type !== "book" && byNoteType[this.note.type]) {
|
2025-02-02 20:44:36 +02:00
|
|
|
this.helpNoteIdToOpen = byNoteType[this.note.type];
|
2025-02-16 18:09:01 +02:00
|
|
|
} else if (this.note && this.note.type === "book") {
|
2025-03-02 20:47:57 +01:00
|
|
|
this.helpNoteIdToOpen = byBookType[(this.note.getAttributeValue("label", "viewType") as ViewTypeOptions) ?? ""];
|
2025-02-02 20:44:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return !!this.helpNoteIdToOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
|
|
|
this.$widget.on("click", () => {
|
2025-03-03 21:02:18 +01:00
|
|
|
const subContexts = appContext.tabManager.getActiveContext()?.getSubContexts();
|
2025-02-03 22:50:00 +02:00
|
|
|
const targetNote = `_help_${this.helpNoteIdToOpen}`;
|
2025-03-03 21:02:18 +01:00
|
|
|
const helpSubcontext = subContexts?.find((s) => s.viewScope?.viewMode === "contextual-help");
|
2025-02-03 22:50:00 +02:00
|
|
|
const viewScope: ViewScope = {
|
2025-03-02 20:47:57 +01:00
|
|
|
viewMode: "contextual-help"
|
2025-02-03 22:50:00 +02:00
|
|
|
};
|
|
|
|
if (!helpSubcontext) {
|
|
|
|
// The help is not already open, open a new split with it.
|
2025-03-03 21:02:18 +01:00
|
|
|
const { ntxId } = subContexts?.[subContexts.length - 1] ?? {};
|
2025-02-03 22:50:00 +02:00
|
|
|
this.triggerCommand("openNewNoteSplit", {
|
|
|
|
ntxId,
|
|
|
|
notePath: targetNote,
|
|
|
|
hoistedNoteId: "_help",
|
|
|
|
viewScope
|
2025-03-02 20:47:57 +01:00
|
|
|
});
|
2025-02-03 22:50:00 +02:00
|
|
|
} else {
|
|
|
|
// There is already a help window open, make sure it opens on the right note.
|
|
|
|
helpSubcontext.setNote(targetNote, { viewScope });
|
|
|
|
}
|
2025-02-02 20:44:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-02-16 18:09:01 +02:00
|
|
|
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
|
|
|
|
if (this.note?.type === "book" && loadResults.getAttributeRows().find((attr) => attr.noteId === this.noteId && attr.name === "viewType")) {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-02 20:44:36 +02:00
|
|
|
}
|