trilium/src/public/javascripts/widgets/note_detail.js

126 lines
3.5 KiB
JavaScript
Raw Normal View History

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";
const TPL = `
<div class="note-detail">
<style>
.note-detail-content {
display: flex;
flex-direction: column;
flex-grow: 100;
height: 100%;
width: 100%;
}
</style>
</div>
`;
2020-01-19 18:03:34 +08:00
const typeWidgetClasses = {
2020-01-19 18:20:02 +08:00
'empty': "./type_widgets/empty.js",
2020-01-19 18:03:34 +08:00
'text': "./type_widgets/text.js",
'code': "./type_widgets/code.js",
'file': "./type_widgets/file.js",
'image': "./type_widgets/note_detail_image.js",
'search': "./type_widgets/note_detail_search.js",
'render': "./type_widgets/note_detail_render.js",
'relation-map': "./type_widgets/note_detail_relation_map.js",
'protected-session': "./type_widgets/note_detail_protected_session.js",
'book': "./type_widgets/note_detail_book.js"
2020-01-14 04:48:44 +08:00
};
export default class NoteDetailWidget extends TabAwareWidget {
constructor(appContext) {
super(appContext);
2020-01-19 18:03:34 +08:00
this.typeWidgets = {};
this.typeWidgetPromises = {};
2020-01-14 04:48:44 +08:00
}
doRender() {
this.$widget = $(TPL);
return this.$widget;
}
2020-01-19 18:03:34 +08:00
async refresh() {
this.type = this.getWidgetType(/*disableAutoBook*/);
2020-01-14 04:48:44 +08:00
2020-01-19 18:03:34 +08:00
if (!(this.type in this.typeWidgetPromises)) {
this.typeWidgetPromises[this.type] = this.initWidgetType(this.type);
2020-01-14 04:48:44 +08:00
}
2020-01-19 18:03:34 +08:00
await this.typeWidgetPromises[this.type];
for (const typeWidget of Object.values(this.typeWidgets)) {
if (typeWidget.constructor.getType() !== this.type) {
typeWidget.cleanup();
typeWidget.toggle(false);
}
}
2020-01-14 04:48:44 +08:00
2020-01-19 18:03:34 +08:00
this.getTypeWidget().toggle(true);
2020-01-16 04:36:01 +08:00
this.setupClasses();
}
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-01-19 18:20:02 +08:00
const note = this.tabContext.note;
2020-01-16 04:36:01 +08:00
2020-01-19 18:20:02 +08:00
if (note) {
this.$widget.addClass(note.cssClass);
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-19 18:03:34 +08:00
async initWidgetType(type) {
const clazz = await import(typeWidgetClasses[type]);
2020-01-17 05:44:36 +08:00
2020-01-19 18:03:34 +08:00
this.typeWidgets[this.type] = new clazz.default(this.appContext);
this.children.push(this.typeWidgets[this.type]);
2020-01-19 01:01:16 +08:00
2020-01-19 18:03:34 +08:00
this.typeWidgets[this.type].renderTo(this.$widget);
2020-01-19 01:01:16 +08:00
2020-01-19 18:03:34 +08:00
this.typeWidgets[this.type].eventReceived('setTabContext', {tabContext: this.tabContext});
2020-01-14 04:48:44 +08:00
}
2020-01-19 18:03:34 +08:00
getWidgetType(disableAutoBook) {
2020-01-14 04:48:44 +08:00
const note = this.tabContext.note;
if (!note) {
return "empty";
}
let type = note.type;
2020-01-19 18:03:34 +08:00
if (type === 'text' && !disableAutoBook
&& utils.isHtmlEmpty(note.content)
&& note.hasChildren()) {
2020-01-14 04:48:44 +08:00
type = 'book';
}
2020-01-19 18:03:34 +08:00
if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
type = 'protected-session';
2020-01-14 04:48:44 +08:00
}
return type;
}
}