mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
various widget refactorings
This commit is contained in:
parent
7c6cd63a53
commit
d5ae3802d1
@ -1,60 +0,0 @@
|
|||||||
import server from "./server.js";
|
|
||||||
import ws from "./ws.js";
|
|
||||||
import treeService from "./tree.js";
|
|
||||||
import noteAutocompleteService from "./note_autocomplete.js";
|
|
||||||
import Component from "../widgets/component.js";
|
|
||||||
import utils from "./utils.js";
|
|
||||||
|
|
||||||
class Attributes extends Component {
|
|
||||||
/**
|
|
||||||
* @param {AppContext} appContext
|
|
||||||
* @param {TabContext} tabContext
|
|
||||||
*/
|
|
||||||
constructor(appContext, tabContext) {
|
|
||||||
super(appContext);
|
|
||||||
this.tabContext = tabContext;
|
|
||||||
this.attributePromise = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
invalidateAttributes() {
|
|
||||||
this.attributePromise = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
reloadAttributes() {
|
|
||||||
if (this.tabContext.note) {
|
|
||||||
this.attributePromise = server.get(`notes/${this.tabContext.note.noteId}/attributes`);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.invalidateAttributes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async refreshAttributes() {
|
|
||||||
this.reloadAttributes();
|
|
||||||
}
|
|
||||||
|
|
||||||
async getAttributes() {
|
|
||||||
if (!this.attributePromise) {
|
|
||||||
this.reloadAttributes();
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.attributePromise;
|
|
||||||
}
|
|
||||||
|
|
||||||
syncDataListener({data}) {
|
|
||||||
if (this.tabContext.note && data.find(sd => sd.entityName === 'attributes' && sd.noteId === this.tabContext.note.noteId)) {
|
|
||||||
this.reloadAttributes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tabNoteSwitchedListener() {
|
|
||||||
if (utils.isDesktop()) {
|
|
||||||
this.refreshAttributes();
|
|
||||||
} else {
|
|
||||||
// mobile usually doesn't need attributes so we just invalidate
|
|
||||||
this.invalidateAttributes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Attributes;
|
|
@ -2,11 +2,10 @@ import server from "./server.js";
|
|||||||
import bundleService from "./bundle.js";
|
import bundleService from "./bundle.js";
|
||||||
|
|
||||||
async function render(note, $el, ctx) {
|
async function render(note, $el, ctx) {
|
||||||
const attributes = await note.getAttributes();
|
const relations = await note.getRelations('renderNote');
|
||||||
const renderNoteIds = attributes.filter(attr =>
|
const renderNoteIds = relations
|
||||||
attr.type === 'relation'
|
.map(rel => rel.value)
|
||||||
&& attr.name === 'renderNote'
|
.filter(noteId => noteId);
|
||||||
&& !!attr.value).map(rel => rel.value);
|
|
||||||
|
|
||||||
$el.empty().toggle(renderNoteIds.length > 0);
|
$el.empty().toggle(renderNoteIds.length > 0);
|
||||||
|
|
||||||
@ -18,11 +17,8 @@ async function render(note, $el, ctx) {
|
|||||||
|
|
||||||
$scriptContainer.append(bundle.html);
|
$scriptContainer.append(bundle.html);
|
||||||
|
|
||||||
const $result = await bundleService.executeBundle(bundle, note, ctx, $scriptContainer);
|
// async so that scripts cannot block trilium execution
|
||||||
|
bundleService.executeBundle(bundle, note, ctx, $scriptContainer);
|
||||||
if ($result) {
|
|
||||||
$scriptContainer.append($result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return renderNoteIds.length > 0;
|
return renderNoteIds.length > 0;
|
||||||
|
@ -3,7 +3,7 @@ import linkService from "../services/link.js";
|
|||||||
import ws from "../services/ws.js";
|
import ws from "../services/ws.js";
|
||||||
import CollapsibleWidget from "./collapsible_widget.js";
|
import CollapsibleWidget from "./collapsible_widget.js";
|
||||||
|
|
||||||
class AttributesWidget extends CollapsibleWidget {
|
export default class AttributesWidget extends CollapsibleWidget {
|
||||||
getWidgetTitle() { return "Attributes"; }
|
getWidgetTitle() { return "Attributes"; }
|
||||||
|
|
||||||
getHelp() {
|
getHelp() {
|
||||||
@ -94,5 +94,3 @@ class AttributesWidget extends CollapsibleWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default AttributesWidget;
|
|
@ -24,7 +24,7 @@ const TPL = `
|
|||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
class CalendarWidget extends CollapsibleWidget {
|
export default class CalendarWidget extends CollapsibleWidget {
|
||||||
getWidgetTitle() { return "Calendar"; }
|
getWidgetTitle() { return "Calendar"; }
|
||||||
|
|
||||||
async isEnabled() {
|
async isEnabled() {
|
||||||
@ -162,5 +162,3 @@ class CalendarWidget extends CollapsibleWidget {
|
|||||||
][monthIndex];
|
][monthIndex];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CalendarWidget;
|
|
@ -8,7 +8,7 @@ const TPL = `
|
|||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
class LinkMapWidget extends CollapsibleWidget {
|
export default class LinkMapWidget extends CollapsibleWidget {
|
||||||
getWidgetTitle() { return "Link map"; }
|
getWidgetTitle() { return "Link map"; }
|
||||||
|
|
||||||
getHelp() {
|
getHelp() {
|
||||||
@ -61,5 +61,3 @@ class LinkMapWidget extends CollapsibleWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default LinkMapWidget;
|
|
@ -35,7 +35,7 @@ const TPL = `
|
|||||||
</table>
|
</table>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
class NoteInfoWidget extends CollapsibleWidget {
|
export default class NoteInfoWidget extends CollapsibleWidget {
|
||||||
getWidgetTitle() { return "Note info"; }
|
getWidgetTitle() { return "Note info"; }
|
||||||
|
|
||||||
doRenderBody() {
|
doRenderBody() {
|
||||||
@ -67,11 +67,9 @@ class NoteInfoWidget extends CollapsibleWidget {
|
|||||||
.attr("title", note.mime);
|
.attr("title", note.mime);
|
||||||
}
|
}
|
||||||
|
|
||||||
syncDataListener({data}) {
|
entitiesReloadedListener({loadResults}) {
|
||||||
if (data.find(sd => sd.entityName === 'notes' && this.isNote(sd.entityId))) {
|
if (loadResults.isNoteReloaded(this.noteId)) {
|
||||||
this.refresh();
|
this.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NoteInfoWidget;
|
|
@ -26,8 +26,7 @@ class NoteRevisionsWidget extends CollapsibleWidget {
|
|||||||
return [$showFullButton];
|
return [$showFullButton];
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshWithNote() {
|
async refreshWithNote(note) {
|
||||||
const note = this.tabContext.note;
|
|
||||||
const revisionItems = await server.get(`notes/${note.noteId}/revisions`);
|
const revisionItems = await server.get(`notes/${note.noteId}/revisions`);
|
||||||
|
|
||||||
if (revisionItems.length === 0) {
|
if (revisionItems.length === 0) {
|
||||||
@ -35,6 +34,10 @@ class NoteRevisionsWidget extends CollapsibleWidget {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (note.noteId !== this.noteId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.$body.html(TPL);
|
this.$body.html(TPL);
|
||||||
|
|
||||||
const $list = this.$body.find('.note-revision-list');
|
const $list = this.$body.find('.note-revision-list');
|
||||||
@ -60,12 +63,6 @@ class NoteRevisionsWidget extends CollapsibleWidget {
|
|||||||
this.refresh();
|
this.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
syncDataListener({data}) {
|
|
||||||
if (data.find(sd => sd.entityName === 'note_revisions' && sd.noteId === this.tabContext.note.noteId)) {
|
|
||||||
this.refresh();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NoteRevisionsWidget;
|
export default NoteRevisionsWidget;
|
@ -3,7 +3,7 @@ import linkService from "../services/link.js";
|
|||||||
import server from "../services/server.js";
|
import server from "../services/server.js";
|
||||||
import treeCache from "../services/tree_cache.js";
|
import treeCache from "../services/tree_cache.js";
|
||||||
|
|
||||||
class SimilarNotesWidget extends CollapsibleWidget {
|
export default class SimilarNotesWidget extends CollapsibleWidget {
|
||||||
getWidgetTitle() { return "Similar notes"; }
|
getWidgetTitle() { return "Similar notes"; }
|
||||||
|
|
||||||
getHelp() {
|
getHelp() {
|
||||||
@ -14,9 +14,9 @@ class SimilarNotesWidget extends CollapsibleWidget {
|
|||||||
|
|
||||||
getMaxHeight() { return "200px"; }
|
getMaxHeight() { return "200px"; }
|
||||||
|
|
||||||
async refreshWithNote() {
|
async refreshWithNote(note) {
|
||||||
// remember which title was when we found the similar notes
|
// remember which title was when we found the similar notes
|
||||||
this.title = this.tabContext.note.title;
|
this.title = note.title;
|
||||||
|
|
||||||
const similarNotes = await server.get('similar-notes/' + this.tabContext.note.noteId);
|
const similarNotes = await server.get('similar-notes/' + this.tabContext.note.noteId);
|
||||||
|
|
||||||
@ -47,13 +47,11 @@ class SimilarNotesWidget extends CollapsibleWidget {
|
|||||||
this.$body.empty().append($list);
|
this.$body.empty().append($list);
|
||||||
}
|
}
|
||||||
|
|
||||||
noteSavedListener({data}) {
|
entitiesReloadedListener({loadResults}) {
|
||||||
if (this.title !== this.tabContext.note.title) {
|
if (this.note && this.title !== this.note.title) {
|
||||||
this.rendered = false;
|
this.rendered = false;
|
||||||
|
|
||||||
this.renderBody();
|
this.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SimilarNotesWidget;
|
|
@ -225,18 +225,33 @@ export default class BookTypeWidget extends TypeWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const childNote of childNotes) {
|
for (const childNote of childNotes) {
|
||||||
|
if (note.noteId !== this.noteId) {
|
||||||
|
// rendering can take a long time and the note might be switched during the rendering
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const childNotePath = this.tabContext.notePath + '/' + childNote.noteId;
|
const childNotePath = this.tabContext.notePath + '/' + childNote.noteId;
|
||||||
|
|
||||||
const {type, renderedContent} = await noteContentRenderer.getRenderedContent(childNote);
|
const $content = $('<div class="note-book-content">')
|
||||||
|
.css("max-height", ZOOMS[this.zoomLevel].height);
|
||||||
|
|
||||||
const $card = $('<div class="note-book-card">')
|
const $card = $('<div class="note-book-card">')
|
||||||
.attr('data-note-id', childNote.noteId)
|
.attr('data-note-id', childNote.noteId)
|
||||||
.css("flex-basis", ZOOMS[this.zoomLevel].width)
|
.css("flex-basis", ZOOMS[this.zoomLevel].width)
|
||||||
.addClass("type-" + type)
|
|
||||||
.append($('<h5 class="note-book-title">').append(await linkService.createNoteLink(childNotePath, {showTooltip: false})))
|
.append($('<h5 class="note-book-title">').append(await linkService.createNoteLink(childNotePath, {showTooltip: false})))
|
||||||
.append($('<div class="note-book-content">')
|
.append($content);
|
||||||
.css("max-height", ZOOMS[this.zoomLevel].height)
|
|
||||||
.append(renderedContent));
|
try {
|
||||||
|
const {type, renderedContent} = await noteContentRenderer.getRenderedContent(childNote);
|
||||||
|
|
||||||
|
$card.addClass("type-" + type);
|
||||||
|
$content.append(renderedContent);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.log(`Caught error while rendering note ${note.noteId} of type ${note.type}: ${e.message}, stack: ${e.stack}`);
|
||||||
|
|
||||||
|
$content.append("rendering error");
|
||||||
|
}
|
||||||
|
|
||||||
const childCount = childNote.getChildNoteIds().length;
|
const childCount = childNote.getChildNoteIds().length;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import CollapsibleWidget from "./collapsible_widget.js";
|
import CollapsibleWidget from "./collapsible_widget.js";
|
||||||
import linkService from "../services/link.js";
|
import linkService from "../services/link.js";
|
||||||
|
|
||||||
class WhatLinksHereWidget extends CollapsibleWidget {
|
export default class WhatLinksHereWidget extends CollapsibleWidget {
|
||||||
getWidgetTitle() { return "What links here"; }
|
getWidgetTitle() { return "What links here"; }
|
||||||
|
|
||||||
getMaxHeight() { return "200px"; }
|
getMaxHeight() { return "200px"; }
|
||||||
@ -56,5 +56,3 @@ class WhatLinksHereWidget extends CollapsibleWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default WhatLinksHereWidget;
|
|
Loading…
x
Reference in New Issue
Block a user