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

306 lines
9.7 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";
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";
import ProtectedSessionTypeWidget from "./type_widgets/protected_session.js";
import BookTypeWidget from "./type_widgets/book.js";
import appContext from "../services/app_context.js";
2020-02-16 20:09:59 +01:00
import keyboardActionsService from "../services/keyboard_actions.js";
import noteCreateService from "../services/note_create.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-08-25 22:49:24 +02:00
import attributeService from "../services/attributes.js";
2021-09-20 22:19:47 +02:00
import NoteMapTypeWidget from "./type_widgets/note_map.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
}
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,
'protected-session': ProtectedSessionTypeWidget,
2021-09-16 23:09:48 +02:00
'book': BookTypeWidget,
2021-09-29 22:10:15 +02:00
'note-map': NoteMapTypeWidget
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
const dto = note.dto;
dto.content = this.getTypeWidget().getContent();
2020-01-19 21:40:23 +01:00
2021-09-18 14:29:41 +02:00
// for read only notes
if (dto.content === undefined) {
return;
}
protectedSessionHolder.touchProtectedSessionIfNecessary(note);
await server.put('notes/' + noteId, dto, 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();
2020-02-25 12:24:37 +01:00
this.mime = this.note ? this.note.mime : null;
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
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
2020-03-07 15:01:48 +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
}
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';
}
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();
}
}
2021-05-22 12:26:45 +02:00
async beforeTabRemoveEvent({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);
this.$widget.find('.note-detail-printable:visible').printThis({
header: $("<h2>").text(this.note && this.note.title).prop('outerHTML'),
footer: `
<script src="libraries/katex/katex.min.js"></script>
<script src="libraries/katex/mhchem.min.js"></script>
<script src="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: [
"libraries/codemirror/codemirror.css",
2020-03-07 22:29:49 +01:00
"libraries/ckeditor/ckeditor-content.css",
"libraries/bootstrap/css/bootstrap.min.css",
"libraries/katex/katex.min.css",
2020-03-07 22:29:49 +01:00
"stylesheets/print.css",
2020-03-08 09:38:49 +01:00
"stylesheets/relation_map.css",
"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}) {
2020-02-18 22:16:20 +01:00
if (loadResults.isNoteContentReloaded(this.noteId, this.componentId)
|| (loadResults.isNoteReloaded(this.noteId, this.componentId) && (this.type !== await this.getWidgetType() || this.mime !== this.note.mime))) {
this.handleEvent('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'].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) {
// probably incorrect event
// calling this.refresh() is not enough since the event needs to be propagated to children as well
this.handleEvent('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 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,
saveSelection: true
});
}
// 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();
}
}
}