2020-01-14 04:48:44 +08:00
|
|
|
import TabAwareWidget from "./tab_aware_widget.js";
|
|
|
|
import utils from "../services/utils.js";
|
|
|
|
import protectedSessionHolder from "../services/protected_session_holder.js";
|
2020-01-20 04:40:23 +08:00
|
|
|
import SpacedUpdate from "../services/spaced_update.js";
|
|
|
|
import server from "../services/server.js";
|
2020-01-22 04:43:23 +08:00
|
|
|
import libraryLoader from "../services/library_loader.js";
|
2020-02-13 05:09:25 +08:00
|
|
|
import EmptyTypeWidget from "./type_widgets/empty.js";
|
|
|
|
import TextTypeWidget from "./type_widgets/text.js";
|
|
|
|
import CodeTypeWidget from "./type_widgets/code.js";
|
|
|
|
import FileTypeWidget from "./type_widgets/file.js";
|
|
|
|
import ImageTypeWidget from "./type_widgets/image.js";
|
|
|
|
import SearchTypeWidget from "./type_widgets/search.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";
|
2020-02-17 01:11:32 +08:00
|
|
|
import appContext from "../services/app_context.js";
|
2020-02-17 03:09:59 +08:00
|
|
|
import keyboardActionsService from "../services/keyboard_actions.js";
|
2020-02-17 05:56:40 +08:00
|
|
|
import noteCreateService from "../services/note_create.js";
|
2020-01-14 04:48:44 +08:00
|
|
|
|
|
|
|
const TPL = `
|
|
|
|
<div class="note-detail">
|
|
|
|
<style>
|
2020-01-19 20:19:40 +08:00
|
|
|
.note-detail {
|
2020-01-14 04:48:44 +08:00
|
|
|
height: 100%;
|
2020-02-26 02:19:10 +08:00
|
|
|
min-height: 0;
|
2020-01-14 04:48:44 +08:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
2020-01-19 18:03:34 +08:00
|
|
|
const typeWidgetClasses = {
|
2020-02-13 05:09:25 +08:00
|
|
|
'empty': EmptyTypeWidget,
|
|
|
|
'text': TextTypeWidget,
|
|
|
|
'code': CodeTypeWidget,
|
|
|
|
'file': FileTypeWidget,
|
|
|
|
'image': ImageTypeWidget,
|
|
|
|
'search': SearchTypeWidget,
|
|
|
|
'render': RenderTypeWidget,
|
|
|
|
'relation-map': RelationMapTypeWidget,
|
|
|
|
'protected-session': ProtectedSessionTypeWidget,
|
|
|
|
'book': BookTypeWidget
|
2020-01-14 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class NoteDetailWidget extends TabAwareWidget {
|
2020-02-17 01:11:32 +08:00
|
|
|
constructor(parent) {
|
|
|
|
super(parent);
|
2020-01-14 04:48:44 +08:00
|
|
|
|
2020-01-19 18:03:34 +08:00
|
|
|
this.typeWidgets = {};
|
2020-01-20 04:40:23 +08:00
|
|
|
|
|
|
|
this.spacedUpdate = new SpacedUpdate(async () => {
|
2020-02-01 18:33:31 +08:00
|
|
|
const {note} = this.tabContext;
|
2020-01-26 01:29:32 +08:00
|
|
|
const {noteId} = note;
|
2020-01-20 04:40:23 +08:00
|
|
|
|
2020-01-25 21:37:12 +08:00
|
|
|
const dto = note.dto;
|
2020-02-02 01:29:18 +08:00
|
|
|
dto.content = this.getTypeWidget().getContent();
|
2020-01-20 04:40:23 +08:00
|
|
|
|
2020-02-02 01:29:18 +08:00
|
|
|
await server.put('notes/' + noteId, dto, this.componentId);
|
2020-01-20 04:40:23 +08:00
|
|
|
});
|
2020-01-14 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
|
|
|
|
2020-01-20 01:05:06 +08:00
|
|
|
this.$widget.on("dragover", e => e.preventDefault());
|
|
|
|
|
|
|
|
this.$widget.on("dragleave", e => e.preventDefault());
|
|
|
|
|
|
|
|
this.$widget.on("drop", async e => {
|
2020-02-17 01:11:32 +08:00
|
|
|
const activeNote = appContext.tabManager.getActiveTabNote();
|
2020-01-20 01:05:06 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-14 04:48:44 +08:00
|
|
|
return this.$widget;
|
|
|
|
}
|
|
|
|
|
2020-02-17 03:09:59 +08:00
|
|
|
isEnabled() {
|
|
|
|
return this.tabContext && this.tabContext.isActive();
|
|
|
|
}
|
|
|
|
|
2020-02-17 05:14:28 +08:00
|
|
|
async refresh() {
|
2020-02-13 05:09:25 +08:00
|
|
|
if (!this.isEnabled()) {
|
|
|
|
this.toggle(false);
|
|
|
|
return;
|
2020-01-19 18:03:34 +08:00
|
|
|
}
|
2020-01-14 04:48:44 +08:00
|
|
|
|
2020-02-13 05:09:25 +08:00
|
|
|
this.toggle(true);
|
2020-01-16 04:36:01 +08:00
|
|
|
|
2020-02-13 05:09:25 +08:00
|
|
|
this.type = await this.getWidgetType();
|
2020-02-25 19:24:37 +08:00
|
|
|
this.mime = this.note ? this.note.mime : null;
|
2020-01-16 04:36:01 +08:00
|
|
|
|
2020-02-13 05:09:25 +08:00
|
|
|
if (!(this.type in this.typeWidgets)) {
|
|
|
|
const clazz = typeWidgetClasses[this.type];
|
2020-01-25 03:15:53 +08:00
|
|
|
|
2020-02-17 01:11:32 +08:00
|
|
|
const typeWidget = this.typeWidgets[this.type] = new clazz(this);
|
2020-02-13 05:09:25 +08:00
|
|
|
typeWidget.spacedUpdate = this.spacedUpdate;
|
2020-01-25 03:15:53 +08:00
|
|
|
|
2020-02-13 05:09:25 +08:00
|
|
|
this.children.push(typeWidget);
|
2020-02-17 03:09:59 +08:00
|
|
|
|
|
|
|
const $renderedWidget = typeWidget.render();
|
|
|
|
keyboardActionsService.updateDisplayedShortcuts($renderedWidget);
|
|
|
|
|
|
|
|
this.$widget.append($renderedWidget);
|
2020-02-13 05:09:25 +08:00
|
|
|
|
2020-02-17 02:21:17 +08:00
|
|
|
typeWidget.handleEvent('setTabContext', {tabContext: this.tabContext});
|
2020-02-13 05:09:25 +08:00
|
|
|
}
|
2020-01-25 03:15:53 +08:00
|
|
|
|
2020-02-13 05:09:25 +08:00
|
|
|
this.setupClasses();
|
2020-01-25 03:15:53 +08:00
|
|
|
}
|
|
|
|
|
2020-01-16 04:36:01 +08:00
|
|
|
setupClasses() {
|
|
|
|
for (const clazz of Array.from(this.$widget[0].classList)) { // create copy to safely iterate over while removing classes
|
|
|
|
if (clazz !== 'note-detail') {
|
|
|
|
this.$widget.removeClass(clazz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 04:56:45 +08:00
|
|
|
const note = this.note;
|
2020-01-16 04:36:01 +08:00
|
|
|
|
2020-01-19 18:20:02 +08:00
|
|
|
if (note) {
|
2020-02-25 16:40:49 +08:00
|
|
|
note.getCssClass().then(cssClass => this.$widget.addClass(cssClass));
|
|
|
|
|
2020-01-19 18:20:02 +08:00
|
|
|
this.$widget.addClass(utils.getNoteTypeClass(note.type));
|
|
|
|
this.$widget.addClass(utils.getMimeTypeClass(note.mime));
|
|
|
|
|
|
|
|
this.$widget.toggleClass("protected", note.isProtected);
|
|
|
|
}
|
2020-01-14 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2020-01-19 18:03:34 +08:00
|
|
|
getTypeWidget() {
|
|
|
|
if (!this.typeWidgets[this.type]) {
|
|
|
|
throw new Error("Could not find typeWidget for type: " + this.type);
|
2020-01-19 01:01:16 +08:00
|
|
|
}
|
2020-01-14 04:48:44 +08:00
|
|
|
|
2020-01-19 18:03:34 +08:00
|
|
|
return this.typeWidgets[this.type];
|
2020-01-19 01:01:16 +08:00
|
|
|
}
|
2020-01-14 04:48:44 +08:00
|
|
|
|
2020-02-02 18:44:08 +08:00
|
|
|
async getWidgetType() {
|
2020-02-26 23:37:17 +08:00
|
|
|
if (!this.note) {
|
2020-01-14 04:48:44 +08:00
|
|
|
return "empty";
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:37:17 +08:00
|
|
|
let type = this.note.type;
|
2020-01-14 04:48:44 +08:00
|
|
|
|
2020-02-02 18:44:08 +08:00
|
|
|
if (type === 'text' && !this.tabContext.autoBookDisabled
|
2020-02-26 23:37:17 +08:00
|
|
|
&& this.note.hasChildren()
|
2020-02-01 18:33:31 +08:00
|
|
|
&& utils.isDesktop()) {
|
2020-01-19 18:03:34 +08:00
|
|
|
|
2020-02-01 18:33:31 +08:00
|
|
|
const noteComplement = await this.tabContext.getNoteComplement();
|
|
|
|
|
|
|
|
if (utils.isHtmlEmpty(noteComplement.content)) {
|
|
|
|
type = 'book';
|
|
|
|
}
|
2020-01-14 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 23:37:17 +08:00
|
|
|
if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
|
2020-01-19 18:03:34 +08:00
|
|
|
type = 'protected-session';
|
2020-01-14 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
2020-01-20 02:33:35 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
async focusOnDetailEvent({tabId}) {
|
2020-01-20 02:33:35 +08:00
|
|
|
if (this.tabContext.tabId === tabId) {
|
|
|
|
await this.refresh();
|
|
|
|
|
|
|
|
const widget = this.getTypeWidget();
|
|
|
|
widget.focus();
|
|
|
|
}
|
|
|
|
}
|
2020-01-20 04:40:23 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
async beforeNoteSwitchEvent({tabId}) {
|
2020-01-20 04:40:23 +08:00
|
|
|
if (this.isTab(tabId)) {
|
|
|
|
await this.spacedUpdate.updateNowIfNecessary();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
async beforeTabRemoveEvent({tabId}) {
|
2020-01-20 04:40:23 +08:00
|
|
|
if (this.isTab(tabId)) {
|
|
|
|
await this.spacedUpdate.updateNowIfNecessary();
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 04:43:23 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
async printActiveNoteEvent() {
|
2020-01-22 04:43:23 +08:00
|
|
|
if (!this.tabContext.isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await libraryLoader.requireLibrary(libraryLoader.PRINT_THIS);
|
|
|
|
|
|
|
|
this.$widget.find('.note-detail-printable:visible').printThis({
|
2020-02-04 04:56:45 +08:00
|
|
|
header: $("<h2>").text(this.note && this.note.title).prop('outerHTML') ,
|
2020-01-22 04:43:23 +08:00
|
|
|
importCSS: false,
|
|
|
|
loadCSS: [
|
|
|
|
"libraries/codemirror/codemirror.css",
|
|
|
|
"libraries/ckeditor/ckeditor-content.css"
|
|
|
|
],
|
|
|
|
debug: true
|
|
|
|
});
|
|
|
|
}
|
2020-01-24 22:44:24 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
hoistedNoteChangedEvent() {
|
2020-01-24 22:44:24 +08:00
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
async entitiesReloadedEvent({loadResults}) {
|
2020-02-19 05:16:20 +08:00
|
|
|
// FIXME: we should test what happens when the loaded note is deleted
|
2020-02-01 05:32:24 +08:00
|
|
|
|
2020-02-19 05:16:20 +08: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-30 04:38:58 +08:00
|
|
|
}
|
2020-01-28 05:58:03 +08:00
|
|
|
}
|
2020-02-02 17:41:43 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
beforeUnloadEvent() {
|
2020-02-02 17:41:43 +08:00
|
|
|
this.spacedUpdate.updateNowIfNecessary();
|
|
|
|
}
|
2020-02-02 18:44:08 +08:00
|
|
|
|
2020-02-17 02:23:49 +08:00
|
|
|
autoBookDisabledEvent() {
|
2020-02-02 18:44:08 +08:00
|
|
|
this.refresh();
|
|
|
|
}
|
2020-02-13 05:09:25 +08:00
|
|
|
|
2020-02-17 02:21:17 +08:00
|
|
|
async handleEventInChildren(name, data) {
|
2020-02-13 05:09:25 +08:00
|
|
|
// done manually in refresh()
|
|
|
|
if (name !== 'setTabContext') {
|
2020-02-17 02:21:17 +08:00
|
|
|
await super.handleEventInChildren(name, data);
|
2020-02-13 05:09:25 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-17 05:56:40 +08:00
|
|
|
|
|
|
|
async cutIntoNoteCommand() {
|
|
|
|
const note = appContext.tabManager.getActiveTabNote();
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await noteCreateService.createNote(note.noteId, {
|
|
|
|
isProtected: note.isProtected,
|
|
|
|
saveSelection: true
|
|
|
|
});
|
|
|
|
}
|
2020-01-14 04:48:44 +08:00
|
|
|
}
|