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

387 lines
13 KiB
JavaScript
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";
2022-12-01 13:07:23 +01:00
import appContext 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";
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
}
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,
mindMap: MindMapWidget
2020-01-13 21:48:44 +01:00
};
2021-05-22 12:35:41 +02:00
export default class NoteDetailWidget extends NoteContextAwareWidget {
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-01-09 18:07:02 +02:00
const { note } = this.noteContext;
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);
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-01-09 18:07:02 +02:00
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-01-09 18:07:02 +02:00
await typeWidget.handleEvent("noteSwitched", {
2021-05-22 12:26:45 +02:00
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();
}
/**
* 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
2025-01-09 18:07:02 +02:00
this.$widget.toggleClass(
"full-height",
(!this.noteContext.hasNoteList() && ["canvas", "webView", "noteMap", "mindMap"].includes(this.type) && this.mime !== "text/x-sqlite;schema=trilium") ||
this.noteContext.viewScope.viewMode === "attachments"
2023-05-21 18:14:17 +02:00
);
2020-01-13 21:48:44 +01:00
}
2020-01-19 11:03:34 +01:00
getTypeWidget() {
if (!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
2020-02-02 11:44:08 +01:00
async getWidgetType() {
2020-03-06 22:17:07 +01:00
const note = this.note;
if (!note) {
2020-01-13 21:48:44 +01:00
return "empty";
}
2020-03-06 22:17:07 +01:00
let type = note.type;
2023-04-03 23:47:24 +02:00
const viewScope = this.noteContext.viewScope;
2020-01-13 21:48:44 +01:00
2025-01-09 18:07:02 +02:00
if (viewScope.viewMode === "source") {
type = "readOnlyCode";
} else if (viewScope.viewMode === "attachments") {
type = viewScope.attachmentId ? "attachmentDetail" : "attachmentList";
} else if (type === "text" && (await this.noteContext.isReadOnly())) {
type = "readOnlyText";
} else if ((type === "code" || type === "mermaid") && (await this.noteContext.isReadOnly())) {
type = "readOnlyCode";
} else if (type === "text") {
type = "editableText";
} else if (type === "code" || type === "mermaid") {
type = "editableCode";
} else if (type === "launcher") {
type = "doc";
2022-08-06 15:00:56 +02:00
}
2020-03-06 22:17:07 +01:00
if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
2025-01-09 18:07:02 +02:00
type = "protectedSession";
2020-01-13 21:48:44 +01:00
}
return type;
}
2020-01-19 19:33:35 +01:00
2025-01-09 18:07:02 +02:00
async focusOnDetailEvent({ ntxId }) {
if (this.noteContext.ntxId !== ntxId) {
return;
}
await this.refresh();
const widget = this.getTypeWidget();
await widget.initialized;
widget.focus();
}
2025-01-09 18:07:02 +02:00
async scrollToEndEvent({ ntxId }) {
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-01-09 18:07:02 +02:00
async beforeNoteSwitchEvent({ noteContext }) {
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-01-09 18:07:02 +02:00
async beforeNoteContextRemoveEvent({ ntxIds }) {
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
async runActiveNoteCommand(params) {
if (this.isNoteContext(params.ntxId)) {
// make sure that script is saved before running it #4028
await this.spacedUpdate.updateNowIfNecessary();
}
2025-01-09 18:07:02 +02:00
return await this.parent.triggerCommand("runActiveNote", params);
}
2020-02-16 19:23:49 +01:00
async printActiveNoteEvent() {
2021-05-22 12:26:45 +02:00
if (!this.noteContext.isActive()) {
2020-01-21 21:43:23 +01:00
return;
}
await libraryLoader.requireLibrary(libraryLoader.PRINT_THIS);
let $promotedAttributes = $("");
if (this.note.getPromotedDefinitionAttributes().length > 0) {
$promotedAttributes = (await attributeRenderer.renderNormalAttributes(this.note)).$renderedAttributes;
}
2025-01-09 18:07:02 +02:00
const { assetPath } = window.glob;
const cssToLoad = [
`${assetPath}/node_modules/codemirror/lib/codemirror.css`,
`${assetPath}/libraries/ckeditor/ckeditor-content.css`,
`${assetPath}/node_modules/bootstrap/dist/css/bootstrap.min.css`,
`${assetPath}/node_modules/katex/dist/katex.min.css`,
`${assetPath}/stylesheets/print.css`,
`${assetPath}/stylesheets/relation_map.css`,
`${assetPath}/stylesheets/ckeditor-theme.css`
];
if (isSyntaxHighlightEnabled()) {
cssToLoad.push(getStylesheetUrl("default:vs"));
}
2025-01-09 18:07:02 +02:00
this.$widget.find(".note-detail-printable:visible").printThis({
header: $("<div>").append($("<h2>").text(this.note.title)).append($promotedAttributes).prop("outerHTML"),
2023-11-22 20:22:16 +01:00
footer: `
2023-11-22 20:22:16 +01:00
<script src="${assetPath}/node_modules/katex/dist/katex.min.js"></script>
<script src="${assetPath}/node_modules/katex/dist/contrib/mhchem.min.js"></script>
<script src="${assetPath}/node_modules/katex/dist/contrib/auto-render.min.js"></script>
<script>
document.body.className += ' ck-content printed-content';
renderMathInElement(document.body, {trust: true});
</script>
`,
2020-01-21 21:43:23 +01:00
importCSS: false,
loadCSS: cssToLoad,
2020-01-21 21:43:23 +01:00
debug: true
});
}
2020-01-24 15:44:24 +01:00
2025-01-09 18:07:02 +02:00
hoistedNoteChangedEvent({ ntxId }) {
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-01-09 18:07:02 +02:00
async entitiesReloadedEvent({ loadResults }) {
// 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
if (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 });
} else if (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) =>
attr.type === "label" && ["readOnly", "autoReadOnlyDisabled", "cssClass", "displayRelations", "hideRelations"].includes(attr.name) && attributeService.isAffecting(attr, this.note)
);
2025-01-09 18:07:02 +02:00
const relation = attrs.find((attr) => attr.type === "relation" && ["template", "inherit", "renderNote"].includes(attr.name) && attributeService.isAffecting(attr, this.note));
if (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-01-09 18:07:02 +02:00
readOnlyTemporarilyDisabledEvent({ noteContext }) {
2021-05-22 12:42:34 +02:00
if (this.isNoteContext(noteContext.ntxId)) {
2020-04-06 22:08:54 +02:00
this.refresh();
}
}
2025-01-09 18:07:02 +02:00
async executeInActiveNoteDetailWidgetEvent({ callback }) {
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
2021-05-22 12:35:41 +02:00
noteCreateService.createNote(appContext.tabManager.getActiveContextNotePath(), {
isProtected: note.isProtected,
2022-06-03 22:05:18 +02:00
saveSelection: true,
textEditor: await this.noteContext.getTextEditor()
});
}
// used by cutToNote in CKEditor build
async saveNoteDetailNowCommand() {
await this.spacedUpdate.updateNowIfNecessary();
}
renderActiveNoteEvent() {
2021-05-22 12:26:45 +02:00
if (this.noteContext.isActive()) {
this.refresh();
}
}
2022-06-03 22:05:18 +02:00
2025-01-09 18:07:02 +02:00
async executeWithTypeWidgetEvent({ resolve, ntxId }) {
2022-06-03 22:05:18 +02:00
if (!this.isNoteContext(ntxId)) {
return;
}
await this.initialized;
await this.getWidgetType();
resolve(this.getTypeWidget());
}
}