Notes/src/public/app/widgets/note_detail.ts

417 lines
14 KiB
TypeScript
Raw Normal View History

2024-09-24 09:57:16 +08:00
import { t } from "../services/i18n.js";
2021-05-22 12:35:41 +02:00
import NoteContextAwareWidget from "./note_context_aware_widget.js";
2020-01-13 21:48:44 +01:00
import protectedSessionHolder from "../services/protected_session_holder.js";
2020-01-19 21:40:23 +01:00
import SpacedUpdate from "../services/spaced_update.js";
import server from "../services/server.js";
2020-01-21 21:43:23 +01:00
import libraryLoader from "../services/library_loader.js";
2025-02-05 21:06:21 +02:00
import appContext, { type CommandListenerData, type EventData } from "../components/app_context.js";
2022-05-28 22:19:29 +02:00
import keyboardActionsService from "../services/keyboard_actions.js";
import noteCreateService from "../services/note_create.js";
import attributeService from "../services/attributes.js";
import attributeRenderer from "../services/attribute_renderer.js";
2020-02-12 22:09:25 +01:00
import EmptyTypeWidget from "./type_widgets/empty.js";
2020-04-07 21:04:28 +02:00
import EditableTextTypeWidget from "./type_widgets/editable_text.js";
2020-04-26 11:38:30 +02:00
import EditableCodeTypeWidget from "./type_widgets/editable_code.js";
2020-02-12 22:09:25 +01:00
import FileTypeWidget from "./type_widgets/file.js";
import ImageTypeWidget from "./type_widgets/image.js";
import RenderTypeWidget from "./type_widgets/render.js";
import RelationMapTypeWidget from "./type_widgets/relation_map.js";
2022-05-10 13:43:05 +02:00
import CanvasTypeWidget from "./type_widgets/canvas.js";
2020-02-12 22:09:25 +01:00
import ProtectedSessionTypeWidget from "./type_widgets/protected_session.js";
import BookTypeWidget from "./type_widgets/book.js";
2020-04-06 22:21:09 +02:00
import ReadOnlyTextTypeWidget from "./type_widgets/read_only_text.js";
2020-04-26 11:38:30 +02:00
import ReadOnlyCodeTypeWidget from "./type_widgets/read_only_code.js";
2020-11-26 23:00:27 +01:00
import NoneTypeWidget from "./type_widgets/none.js";
2021-09-20 22:19:47 +02:00
import NoteMapTypeWidget from "./type_widgets/note_map.js";
2022-05-30 20:59:54 +02:00
import WebViewTypeWidget from "./type_widgets/web_view.js";
2022-08-06 15:00:56 +02:00
import DocTypeWidget from "./type_widgets/doc.js";
import ContentWidgetTypeWidget from "./type_widgets/content_widget.js";
2023-04-03 23:47:24 +02:00
import AttachmentListTypeWidget from "./type_widgets/attachment_list.js";
import AttachmentDetailTypeWidget from "./type_widgets/attachment_detail.js";
import MindMapWidget from "./type_widgets/mind_map.js";
import { getStylesheetUrl, isSyntaxHighlightEnabled } from "../services/syntax_highlight.js";
2025-01-20 18:45:56 +02:00
import GeoMapTypeWidget from "./type_widgets/geo_map.js";
import utils from "../services/utils.js";
2025-02-05 21:06:21 +02:00
import type { NoteType } from "../entities/fnote.js";
import type TypeWidget from "./type_widgets/type_widget.js";
import TaskListWidget from "./type_widgets/task_list.js";
2020-01-13 21:48:44 +01:00
const TPL = `
<div class="note-detail">
<style>
2020-01-19 13:19:40 +01:00
.note-detail {
2020-03-15 21:40:26 +01:00
font-family: var(--detail-font-family);
font-size: var(--detail-font-size);
2020-03-06 22:17:07 +01:00
}
2025-01-20 18:45:56 +02:00
2022-01-07 19:33:59 +01:00
.note-detail.full-height {
height: 100%;
}
2020-01-13 21:48:44 +01:00
</style>
</div>
`;
2020-01-19 11:03:34 +01:00
const typeWidgetClasses = {
2025-01-09 18:07:02 +02:00
empty: EmptyTypeWidget,
editableText: EditableTextTypeWidget,
readOnlyText: ReadOnlyTextTypeWidget,
editableCode: EditableCodeTypeWidget,
readOnlyCode: ReadOnlyCodeTypeWidget,
file: FileTypeWidget,
image: ImageTypeWidget,
search: NoneTypeWidget,
render: RenderTypeWidget,
relationMap: RelationMapTypeWidget,
canvas: CanvasTypeWidget,
protectedSession: ProtectedSessionTypeWidget,
book: BookTypeWidget,
noteMap: NoteMapTypeWidget,
webView: WebViewTypeWidget,
doc: DocTypeWidget,
contentWidget: ContentWidgetTypeWidget,
attachmentDetail: AttachmentDetailTypeWidget,
attachmentList: AttachmentListTypeWidget,
2025-01-20 18:45:56 +02:00
mindMap: MindMapWidget,
geoMap: GeoMapTypeWidget,
taskList: TaskListWidget
2020-01-13 21:48:44 +01:00
};
2025-02-05 21:06:21 +02:00
/**
* A `NoteType` altered by the note detail widget, taking into consideration whether the note is editable or not and adding special note types such as an empty one,
* for protected session or attachment information.
*/
2025-03-02 20:47:57 +01:00
type ExtendedNoteType =
| Exclude<NoteType, "mermaid" | "launcher" | "text" | "code">
| "empty"
| "readOnlyCode"
| "readOnlyText"
| "editableText"
| "editableCode"
| "attachmentDetail"
| "attachmentList"
| "protectedSession";
2025-02-05 21:06:21 +02:00
2021-05-22 12:35:41 +02:00
export default class NoteDetailWidget extends NoteContextAwareWidget {
2025-02-05 21:06:21 +02:00
private typeWidgets: Record<string, TypeWidget>;
private spacedUpdate: SpacedUpdate;
private type?: ExtendedNoteType;
private mime?: string;
2020-02-27 10:03:14 +01:00
constructor() {
super();
2020-01-13 21:48:44 +01:00
2020-01-19 11:03:34 +01:00
this.typeWidgets = {};
2020-01-19 21:40:23 +01:00
this.spacedUpdate = new SpacedUpdate(async () => {
2025-02-05 21:06:21 +02:00
if (!this.noteContext) {
return;
}
2025-01-09 18:07:02 +02:00
const { note } = this.noteContext;
2025-02-05 21:06:21 +02:00
if (!note) {
return;
}
2025-01-09 18:07:02 +02:00
const { noteId } = note;
2020-01-19 21:40:23 +01:00
2023-01-24 09:19:49 +01:00
const data = await this.getTypeWidget().getData();
2020-01-19 21:40:23 +01:00
2021-09-18 14:29:41 +02:00
// for read only notes
2023-01-24 09:19:49 +01:00
if (data === undefined) {
2021-09-18 14:29:41 +02:00
return;
}
protectedSessionHolder.touchProtectedSessionIfNecessary(note);
2023-01-24 09:19:49 +01:00
await server.put(`notes/${noteId}/data`, data, this.componentId);
2025-02-05 21:06:21 +02:00
this.getTypeWidget().dataSaved();
});
appContext.addBeforeUnloadListener(this);
2020-01-13 21:48:44 +01:00
}
2020-03-06 22:17:07 +01:00
isEnabled() {
return true;
}
2020-01-13 21:48:44 +01:00
doRender() {
this.$widget = $(TPL);
2021-06-15 21:53:22 +02:00
this.contentSized();
2020-01-13 21:48:44 +01:00
}
async refresh() {
2020-02-12 22:09:25 +01:00
this.type = await this.getWidgetType();
this.mime = this.note?.mime;
2020-01-15 21:36:01 +01:00
2020-02-12 22:09:25 +01:00
if (!(this.type in this.typeWidgets)) {
const clazz = typeWidgetClasses[this.type];
2020-01-24 20:15:53 +01:00
if (!clazz) {
throw new Error(`Cannot find type widget for type '${this.type}'`);
}
2025-01-09 18:07:02 +02:00
const typeWidget = (this.typeWidgets[this.type] = new clazz());
2020-02-12 22:09:25 +01:00
typeWidget.spacedUpdate = this.spacedUpdate;
2020-03-06 23:34:39 +01:00
typeWidget.setParent(this);
2020-02-16 20:09:59 +01:00
const $renderedWidget = typeWidget.render();
keyboardActionsService.updateDisplayedShortcuts($renderedWidget);
this.$widget.append($renderedWidget);
2020-02-12 22:09:25 +01:00
2025-02-05 21:06:21 +02:00
if (this.noteContext) {
await typeWidget.handleEvent("setNoteContext", { noteContext: this.noteContext });
}
2020-02-29 22:04:46 +01:00
2023-05-05 23:41:11 +02:00
// this is happening in update(), so note has been already set, and we need to reflect this
2025-02-07 22:37:43 +02:00
if (this.noteContext) {
2025-02-05 21:06:21 +02:00
await typeWidget.handleEvent("noteSwitched", {
noteContext: this.noteContext,
notePath: this.noteContext.notePath
});
}
2020-03-07 15:01:48 +01:00
2020-03-06 23:34:39 +01:00
this.child(typeWidget);
2020-02-12 22:09:25 +01:00
}
2022-01-07 19:33:59 +01:00
this.checkFullHeight();
if (utils.isMobile()) {
const hasFixedTree = this.noteContext?.hoistedNoteId === "_lbMobileRoot";
$("body").toggleClass("force-fixed-tree", hasFixedTree);
}
2022-01-07 19:33:59 +01:00
}
/**
* sets full height of container that contains note content for a subset of note-types
*/
2022-01-07 19:33:59 +01:00
checkFullHeight() {
// https://github.com/zadam/trilium/issues/2522
const isBackendNote = this.noteContext?.noteId === "_backendLog";
const isSqlNote = this.mime === "text/x-sqlite;schema=trilium";
2025-02-05 21:06:21 +02:00
const isFullHeightNoteType = ["canvas", "webView", "noteMap", "mindMap", "geoMap"].includes(this.type ?? "");
const isFullHeight = (!this.noteContext?.hasNoteList() && isFullHeightNoteType && !isSqlNote)
|| this.noteContext?.viewScope?.viewMode === "attachments"
|| isBackendNote;
this.$widget.toggleClass("full-height", isFullHeight);
2020-01-13 21:48:44 +01:00
}
2020-01-19 11:03:34 +01:00
getTypeWidget() {
2025-02-05 21:06:21 +02:00
if (!this.type || !this.typeWidgets[this.type]) {
2024-09-24 09:57:16 +08:00
throw new Error(t(`note_detail.could_not_find_typewidget`, { type: this.type }));
2020-01-18 18:01:16 +01:00
}
2020-01-13 21:48:44 +01:00
2020-01-19 11:03:34 +01:00
return this.typeWidgets[this.type];
2020-01-18 18:01:16 +01:00
}
2020-01-13 21:48:44 +01:00
2025-02-05 21:06:21 +02:00
async getWidgetType(): Promise<ExtendedNoteType> {
2020-03-06 22:17:07 +01:00
const note = this.note;
if (!note) {
2020-01-13 21:48:44 +01:00
return "empty";
}
2025-02-05 21:06:21 +02:00
let type: NoteType = note.type;
let resultingType: ExtendedNoteType;
const viewScope = this.noteContext?.viewScope;
if (viewScope?.viewMode === "source") {
resultingType = "readOnlyCode";
} else if (viewScope && viewScope.viewMode === "attachments") {
resultingType = viewScope.attachmentId ? "attachmentDetail" : "attachmentList";
} else if (type === "text" && (await this.noteContext?.isReadOnly())) {
resultingType = "readOnlyText";
} else if ((type === "code" || type === "mermaid") && (await this.noteContext?.isReadOnly())) {
resultingType = "readOnlyCode";
2025-01-09 18:07:02 +02:00
} else if (type === "text") {
2025-02-05 21:06:21 +02:00
resultingType = "editableText";
2025-01-09 18:07:02 +02:00
} else if (type === "code" || type === "mermaid") {
2025-02-05 21:06:21 +02:00
resultingType = "editableCode";
2025-01-09 18:07:02 +02:00
} else if (type === "launcher") {
2025-02-05 21:06:21 +02:00
resultingType = "doc";
} else {
resultingType = type;
2022-08-06 15:00:56 +02:00
}
2020-03-06 22:17:07 +01:00
if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
2025-02-05 21:06:21 +02:00
resultingType = "protectedSession";
2020-01-13 21:48:44 +01:00
}
2025-02-05 21:06:21 +02:00
return resultingType;
2020-01-13 21:48:44 +01:00
}
2020-01-19 19:33:35 +01:00
2025-02-05 21:06:21 +02:00
async focusOnDetailEvent({ ntxId }: EventData<"focusOnDetail">) {
if (this.noteContext?.ntxId !== ntxId) {
return;
}
await this.refresh();
const widget = this.getTypeWidget();
await widget.initialized;
widget.focus();
}
2025-02-05 21:06:21 +02:00
async scrollToEndEvent({ ntxId }: EventData<"scrollToEnd">) {
if (this.noteContext?.ntxId !== ntxId) {
return;
}
await this.refresh();
const widget = this.getTypeWidget();
await widget.initialized;
2020-01-19 19:33:35 +01:00
if (widget.scrollToEnd) {
widget.scrollToEnd();
2020-01-19 19:33:35 +01:00
}
}
2020-01-19 21:40:23 +01:00
2025-02-05 21:06:21 +02:00
async beforeNoteSwitchEvent({ noteContext }: EventData<"beforeNoteSwitch">) {
2021-05-22 12:42:34 +02:00
if (this.isNoteContext(noteContext.ntxId)) {
2020-01-19 21:40:23 +01:00
await this.spacedUpdate.updateNowIfNecessary();
}
}
2025-02-05 21:06:21 +02:00
async beforeNoteContextRemoveEvent({ ntxIds }: EventData<"beforeNoteContextRemove">) {
2021-05-22 12:42:34 +02:00
if (this.isNoteContext(ntxIds)) {
2020-01-19 21:40:23 +01:00
await this.spacedUpdate.updateNowIfNecessary();
}
}
2020-01-21 21:43:23 +01:00
2025-02-05 21:06:21 +02:00
async runActiveNoteCommand(params: CommandListenerData<"runActiveNote">) {
if (this.isNoteContext(params.ntxId)) {
// make sure that script is saved before running it #4028
await this.spacedUpdate.updateNowIfNecessary();
}
2025-02-05 21:06:21 +02:00
return await this.parent?.triggerCommand("runActiveNote", params);
}
2020-02-16 19:23:49 +01:00
async printActiveNoteEvent() {
2025-02-05 21:06:21 +02:00
if (!this.noteContext?.isActive()) {
2020-01-21 21:43:23 +01:00
return;
}
2025-02-01 00:52:46 +02:00
window.print();
2020-01-21 21:43:23 +01:00
}
2020-01-24 15:44:24 +01:00
async exportAsPdfEvent() {
2025-02-05 21:06:21 +02:00
if (!this.noteContext?.isActive() || !this.note) {
return;
}
const { ipcRenderer } = utils.dynamicRequire("electron");
2025-01-31 21:58:52 +02:00
ipcRenderer.send("export-as-pdf", {
title: this.note.title,
pageSize: this.note.getAttributeValue("label", "printPageSize") ?? "Letter",
landscape: this.note.hasAttribute("label", "printLandscape")
2025-01-31 21:58:52 +02:00
});
}
2025-02-05 21:06:21 +02:00
hoistedNoteChangedEvent({ ntxId }: EventData<"hoistedNoteChanged">) {
2021-05-22 12:42:34 +02:00
if (this.isNoteContext(ntxId)) {
2020-11-22 23:05:02 +01:00
this.refresh();
}
2020-01-24 15:44:24 +01:00
}
2025-02-05 21:06:21 +02:00
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
// we're detecting note type change on the note_detail level, but triggering the noteTypeMimeChanged
// globally, so it gets also to e.g. ribbon components. But this means that the event can be generated multiple
// times if the same note is open in several tabs.
2020-02-18 22:16:20 +01:00
2025-02-05 21:06:21 +02:00
if (this.noteId && loadResults.isNoteContentReloaded(this.noteId, this.componentId)) {
// probably incorrect event
// calling this.refresh() is not enough since the event needs to be propagated to children as well
// FIXME: create a separate event to force hierarchical refresh
// this uses handleEvent to make sure that the ordinary content updates are propagated only in the subtree
2023-05-05 23:41:11 +02:00
// to avoid the problem in #3365
2025-01-09 18:07:02 +02:00
this.handleEvent("noteTypeMimeChanged", { noteId: this.noteId });
2025-02-05 21:06:21 +02:00
} else if (this.noteId && loadResults.isNoteReloaded(this.noteId, this.componentId) && (this.type !== (await this.getWidgetType()) || this.mime !== this.note?.mime)) {
2023-05-05 23:41:11 +02:00
// this needs to have a triggerEvent so that e.g., note type (not in the component subtree) is updated
2025-01-09 18:07:02 +02:00
this.triggerEvent("noteTypeMimeChanged", { noteId: this.noteId });
} else {
const attrs = loadResults.getAttributeRows();
2025-01-09 18:07:02 +02:00
const label = attrs.find(
(attr) =>
2025-03-02 20:47:57 +01:00
attr.type === "label" &&
["readOnly", "autoReadOnlyDisabled", "cssClass", "displayRelations", "hideRelations"].includes(attr.name ?? "") &&
attributeService.isAffecting(attr, this.note)
2025-01-09 18:07:02 +02:00
);
2025-02-05 21:06:21 +02:00
const relation = attrs.find((attr) => attr.type === "relation" && ["template", "inherit", "renderNote"].includes(attr.name ?? "") && attributeService.isAffecting(attr, this.note));
2025-02-05 21:06:21 +02:00
if (this.noteId && (label || relation)) {
// probably incorrect event
// calling this.refresh() is not enough since the event needs to be propagated to children as well
2025-01-09 18:07:02 +02:00
this.triggerEvent("noteTypeMimeChanged", { noteId: this.noteId });
}
}
2020-01-27 22:58:03 +01:00
}
2020-02-02 10:41:43 +01:00
2020-02-16 19:23:49 +01:00
beforeUnloadEvent() {
return this.spacedUpdate.isAllSavedAndTriggerUpdate();
2020-02-02 10:41:43 +01:00
}
2020-02-02 11:44:08 +01:00
2025-02-05 21:06:21 +02:00
readOnlyTemporarilyDisabledEvent({ noteContext }: EventData<"readOnlyTemporarilyDisabled">) {
2021-05-22 12:42:34 +02:00
if (this.isNoteContext(noteContext.ntxId)) {
2020-04-06 22:08:54 +02:00
this.refresh();
}
}
2025-02-05 21:06:21 +02:00
async executeInActiveNoteDetailWidgetEvent({ callback }: EventData<"executeInActiveNoteDetailWidget">) {
if (!this.isActiveNoteContext()) {
return;
}
await this.initialized;
callback(this);
}
async cutIntoNoteCommand() {
2021-05-22 12:35:41 +02:00
const note = appContext.tabManager.getActiveContextNote();
if (!note) {
return;
}
// without await as this otherwise causes deadlock through component mutex
2025-02-05 21:06:21 +02:00
const parentNotePath = appContext.tabManager.getActiveContextNotePath();
if (this.noteContext && parentNotePath) {
noteCreateService.createNote(parentNotePath, {
isProtected: note.isProtected,
saveSelection: true,
textEditor: await this.noteContext.getTextEditor()
});
}
}
// used by cutToNote in CKEditor build
async saveNoteDetailNowCommand() {
await this.spacedUpdate.updateNowIfNecessary();
}
renderActiveNoteEvent() {
2025-02-05 21:06:21 +02:00
if (this.noteContext?.isActive()) {
this.refresh();
}
}
2022-06-03 22:05:18 +02:00
2025-02-05 21:06:21 +02:00
async executeWithTypeWidgetEvent({ resolve, ntxId }: EventData<"executeWithTypeWidget">) {
2022-06-03 22:05:18 +02:00
if (!this.isNoteContext(ntxId)) {
return;
}
await this.initialized;
await this.getWidgetType();
resolve(this.getTypeWidget());
}
}