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

390 lines
13 KiB
JavaScript
Raw Normal View History

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-03-23 16:39:03 +01:00
import DeletedTypeWidget from "./type_widgets/deleted.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";
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 = {
2020-02-12 22:09:25 +01:00
'empty': EmptyTypeWidget,
2020-03-23 16:39:03 +01:00
'deleted': DeletedTypeWidget,
2020-04-07 21:04:28 +02:00
'editable-text': EditableTextTypeWidget,
2020-04-06 22:21:09 +02:00
'read-only-text': ReadOnlyTextTypeWidget,
2020-04-26 11:38:30 +02:00
'editable-code': EditableCodeTypeWidget,
'read-only-code': ReadOnlyCodeTypeWidget,
2020-02-12 22:09:25 +01:00
'file': FileTypeWidget,
'image': ImageTypeWidget,
2020-11-26 23:00:27 +01:00
'search': NoneTypeWidget,
2020-02-12 22:09:25 +01:00
'render': RenderTypeWidget,
'relation-map': RelationMapTypeWidget,
2022-05-10 13:43:05 +02:00
'canvas': CanvasTypeWidget,
2020-02-12 22:09:25 +01:00
'protected-session': ProtectedSessionTypeWidget,
2021-09-16 23:09:48 +02:00
'book': BookTypeWidget,
2022-05-28 22:19:29 +02:00
'note-map': NoteMapTypeWidget,
2022-08-06 15:00:56 +02:00
'web-view': WebViewTypeWidget,
2022-12-05 23:57:29 +01:00
'doc': DocTypeWidget,
'content-widget': ContentWidgetTypeWidget
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 () => {
2021-05-22 12:26:45 +02:00
const {note} = this.noteContext;
2020-01-25 18:29:32 +01:00
const {noteId} = note;
2020-01-19 21:40:23 +01:00
2022-06-13 22:38:59 +02:00
const content = await this.getTypeWidget().getContent();
2020-01-19 21:40:23 +01:00
2021-09-18 14:29:41 +02:00
// for read only notes
2022-06-13 22:38:59 +02:00
if (content === undefined) {
2021-09-18 14:29:41 +02:00
return;
}
protectedSessionHolder.touchProtectedSessionIfNecessary(note);
2022-06-13 22:38:59 +02:00
await server.put(`notes/${noteId}/content`, {content}, this.componentId);
});
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
this.$widget.on("dragover", e => e.preventDefault());
this.$widget.on("dragleave", e => e.preventDefault());
this.$widget.on("drop", async e => {
2021-05-22 12:35:41 +02:00
const activeNote = appContext.tabManager.getActiveContextNote();
if (!activeNote) {
return;
}
const files = [...e.originalEvent.dataTransfer.files]; // chrome has issue that dataTransfer.files empties after async operation
const importService = await import("../services/import.js");
importService.uploadFiles(activeNote.noteId, files, {
safeImport: true,
shrinkImages: true,
textImportedAsText: true,
codeImportedAsCode: true,
explodeArchives: true,
replaceUnderscoresWithSpaces: true
});
});
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}'`);
}
2020-02-27 10:03:14 +01: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
2021-05-22 12:26:45 +02:00
await typeWidget.handleEvent('setNoteContext', {noteContext: this.noteContext});
2020-02-29 22:04:46 +01:00
2022-01-07 19:33:59 +01:00
// this is happening in update() so note has been already set, and we need to reflect this
2021-05-22 12:35:41 +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
this.$widget.toggleClass("full-height",
2022-01-07 19:33:59 +01:00
!this.noteContext.hasNoteList()
&& ['editable-text', 'editable-code', 'canvas', 'web-view', 'note-map'].includes(this.type)
2022-05-02 21:23:40 +02:00
&& this.mime !== 'text/x-sqlite;schema=trilium');
2020-01-13 21:48:44 +01:00
}
2020-01-19 11:03:34 +01:00
getTypeWidget() {
if (!this.typeWidgets[this.type]) {
throw new Error("Could not find typeWidget for 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-23 16:39:03 +01:00
} else if (note.isDeleted) {
return "deleted";
2020-01-13 21:48:44 +01:00
}
2020-03-06 22:17:07 +01:00
let type = note.type;
2020-01-13 21:48:44 +01:00
if (type === 'text' && await this.noteContext.isReadOnly()) {
type = 'read-only-text';
2020-04-06 22:08:54 +02:00
}
2021-09-29 22:10:15 +02:00
if ((type === 'code' || type === 'mermaid') && await this.noteContext.isReadOnly()) {
type = 'read-only-code';
2020-04-26 11:38:30 +02:00
}
2020-04-07 21:04:28 +02:00
if (type === 'text') {
type = 'editable-text';
}
2021-09-29 22:10:15 +02:00
if (type === 'code' || type === 'mermaid') {
2020-04-26 11:38:30 +02:00
type = 'editable-code';
}
2022-12-01 10:16:57 +01:00
if (type === 'launcher') {
2022-08-06 15:00:56 +02:00
type = 'doc';
}
2020-03-06 22:17:07 +01:00
if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
2020-01-19 11:03:34 +01:00
type = 'protected-session';
2020-01-13 21:48:44 +01:00
}
return type;
}
2020-01-19 19:33:35 +01:00
2021-05-22 12:26:45 +02:00
async focusOnDetailEvent({ntxId}) {
if (this.noteContext.ntxId === ntxId) {
2020-01-19 19:33:35 +01:00
await this.refresh();
const widget = this.getTypeWidget();
await widget.initialized;
2020-01-19 19:33:35 +01:00
widget.focus();
}
}
2020-01-19 21:40:23 +01:00
2021-05-22 12:26:45 +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();
}
}
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
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;
}
const {assetPath} = window.glob;
2020-01-21 21:43:23 +01:00
this.$widget.find('.note-detail-printable:visible').printThis({
header: $("<div>")
.append($("<h2>").text(this.note.title))
.append($promotedAttributes)
.prop('outerHTML'),
footer: `
<script src="${assetPath}/libraries/katex/katex.min.js"></script>
<script src="${assetPath}/libraries/katex/mhchem.min.js"></script>
<script src="${assetPath}/libraries/katex/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: [
assetPath + "/libraries/codemirror/codemirror.css",
assetPath + "/libraries/ckeditor/ckeditor-content.css",
assetPath + "/libraries/bootstrap/css/bootstrap.min.css",
assetPath + "/libraries/katex/katex.min.css",
assetPath + "/stylesheets/print.css",
assetPath + "/stylesheets/relation_map.css",
assetPath + "/stylesheets/ckeditor-theme.css"
2020-01-21 21:43:23 +01:00
],
debug: true
});
}
2020-01-24 15:44:24 +01:00
2021-05-22 12:26:45 +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
}
2020-02-16 19:23:49 +01: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
// to avoid problem in #3365
this.handleEvent('noteTypeMimeChanged', {noteId: this.noteId});
}
else if (loadResults.isNoteReloaded(this.noteId, this.componentId)
&& (this.type !== await this.getWidgetType() || this.mime !== this.note.mime)) {
// this needs to have a triggerEvent so that e.g. note type (not in the component subtree) is updated
this.triggerEvent('noteTypeMimeChanged', {noteId: this.noteId});
2020-01-29 21:38:58 +01:00
}
else {
const attrs = loadResults.getAttributes();
const label = attrs.find(attr =>
attr.type === 'label'
&& ['readOnly', 'autoReadOnlyDisabled', 'cssClass', 'displayRelations', 'hideRelations'].includes(attr.name)
2021-08-25 22:49:24 +02:00
&& attributeService.isAffecting(attr, this.note));
const relation = attrs.find(attr =>
attr.type === 'relation'
2020-06-14 14:30:57 +02:00
&& ['template', 'renderNote'].includes(attr.name)
2021-08-25 22:49:24 +02:00
&& attributeService.isAffecting(attr, this.note));
if (label || relation) {
console.log("OOOO");
// probably incorrect event
// calling this.refresh() is not enough since the event needs to be propagated to children as well
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
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();
}
}
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
async executeWithTypeWidgetEvent({resolve, ntxId}) {
if (!this.isNoteContext(ntxId)) {
return;
}
await this.initialized;
await this.getWidgetType();
resolve(this.getTypeWidget());
}
}