various fixes and small refactorings

This commit is contained in:
zadam 2020-02-03 21:56:45 +01:00
parent 44ddcdd852
commit b757dfcf79
22 changed files with 41 additions and 87 deletions

View File

@ -67,23 +67,6 @@ async function moveToParentNote(branchIdsToMove, newParentNoteId) {
} }
} }
// FIXME used for finding a next note to activate after a delete
async function getNextNode(nodes) {
// following code assumes that nodes contain only top-most selected nodes - getSelectedNodes has been
// called with stopOnParent=true
let next = nodes[nodes.length - 1].getNextSibling();
if (!next) {
next = nodes[0].getPrevSibling();
}
if (!next && !await hoistedNoteService.isRootNode(nodes[0])) {
next = nodes[0].getParent();
}
return treeService.getNotePath(next);
}
async function deleteNodes(branchIdsToDelete) { async function deleteNodes(branchIdsToDelete) {
branchIdsToDelete = await filterRootNote(branchIdsToDelete); branchIdsToDelete = await filterRootNote(branchIdsToDelete);
@ -132,24 +115,14 @@ async function deleteNodes(branchIdsToDelete) {
if (deleteClones) { if (deleteClones) {
await server.remove(`notes/${branch.noteId}` + query); await server.remove(`notes/${branch.noteId}` + query);
// FIXME
noteDetailService.noteDeleted(branch.noteId);
} }
else { else {
const {noteDeleted} = await server.remove(`branches/${branchIdToDelete}` + query); await server.remove(`branches/${branchIdToDelete}` + query);
if (noteDeleted) {
// FIXME
noteDetailService.noteDeleted(branch.noteId);
}
} }
} }
const noteIds = Array.from(new Set(nodes.map(node => node.getParent().data.noteId))); const noteIds = Array.from(new Set(nodes.map(node => node.getParent().data.noteId)));
appContext.trigger('reloadNotes', {noteIds});
return true; return true;
} }

View File

@ -9,10 +9,7 @@ async function cloneNoteTo(childNoteId, parentNoteId, prefix) {
if (!resp.success) { if (!resp.success) {
alert(resp.message); alert(resp.message);
return;
} }
appContext.trigger('reloadNotes', {noteIds: [childNoteId, parentNoteId]});
} }
// beware that first arg is noteId and second is branchId! // beware that first arg is noteId and second is branchId!
@ -25,8 +22,6 @@ async function cloneNoteAfter(noteId, afterBranchId) {
} }
const afterBranch = treeCache.getBranch(afterBranchId); const afterBranch = treeCache.getBranch(afterBranchId);
appContext.trigger('reloadNotes', {noteIds: [noteId, afterBranch.parentNoteId]});
} }
export default { export default {

View File

@ -64,8 +64,6 @@ ws.subscribeToMessages(async message => {
toastService.showPersistent(toast); toastService.showPersistent(toast);
appContext.trigger('reloadNotes', {noteIds: [message.result.parentNoteId]});
if (message.result.importedNoteId) { if (message.result.importedNoteId) {
await appContext.getActiveTabContext.setNote(message.result.importedNoteId); await appContext.getActiveTabContext.setNote(message.result.importedNoteId);
} }

View File

@ -1,7 +1,7 @@
import server from "./server.js"; 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, tabContext) {
const relations = await note.getRelations('renderNote'); const relations = await note.getRelations('renderNote');
const renderNoteIds = relations const renderNoteIds = relations
.map(rel => rel.value) .map(rel => rel.value)
@ -18,7 +18,7 @@ async function render(note, $el, ctx) {
$scriptContainer.append(bundle.html); $scriptContainer.append(bundle.html);
// async so that scripts cannot block trilium execution // async so that scripts cannot block trilium execution
bundleService.executeBundle(bundle, note, ctx, $scriptContainer); bundleService.executeBundle(bundle, note, tabContext, $scriptContainer);
} }
return renderNoteIds.length > 0; return renderNoteIds.length > 0;

View File

@ -49,6 +49,8 @@ class TabContext extends Component {
await this.trigger('beforeNoteSwitch', {tabId: this.tabId}, true); await this.trigger('beforeNoteSwitch', {tabId: this.tabId}, true);
utils.closeActiveDialog();
this.notePath = notePath; this.notePath = notePath;
this.noteId = treeService.getNoteIdFromNotePath(notePath); this.noteId = treeService.getNoteIdFromNotePath(notePath);

View File

@ -35,7 +35,7 @@ export default class AttributesWidget extends CollapsibleWidget {
.text("+show inherited") .text("+show inherited")
.on('click', async () => { .on('click', async () => {
const attributes = await note.getAttributes(); const attributes = await note.getAttributes();
const inheritedAttributes = attributes.filter(attr => attr.noteId !== this.tabContext.note.noteId); const inheritedAttributes = attributes.filter(attr => attr.noteId !== this.noteId);
if (inheritedAttributes.length === 0) { if (inheritedAttributes.length === 0) {
$inheritedAttrs.text("No inherited attributes yet..."); $inheritedAttrs.text("No inherited attributes yet...");

View File

@ -2,7 +2,6 @@ import CollapsibleWidget from "./collapsible_widget.js";
import libraryLoader from "../services/library_loader.js"; import libraryLoader from "../services/library_loader.js";
import utils from "../services/utils.js"; import utils from "../services/utils.js";
import dateNoteService from "../services/date_notes.js"; import dateNoteService from "../services/date_notes.js";
import treeService from "../services/tree.js";
import server from "../services/server.js"; import server from "../services/server.js";
import appContext from "../services/app_context.js"; import appContext from "../services/app_context.js";
@ -30,7 +29,7 @@ export default class CalendarWidget extends CollapsibleWidget {
async isEnabled() { async isEnabled() {
return await super.isEnabled() return await super.isEnabled()
&& this.tabContext.note.hasOwnedLabel("dateNote"); && this.note.hasOwnedLabel("dateNote");
} }
async doRenderBody() { async doRenderBody() {

View File

@ -16,7 +16,7 @@ export default class EditedNotesWidget extends CollapsibleWidget {
async isEnabled() { async isEnabled() {
return await super.isEnabled() return await super.isEnabled()
&& await this.tabContext.note.hasLabel("dateNote"); && await this.note.hasOwnedLabel("dateNote");
} }
async refreshWithNote(note) { async refreshWithNote(note) {

View File

@ -34,7 +34,7 @@ export default class NoteActionsWidget extends TabAwareWidget {
}); });
this.$importNoteButton = this.$widget.find('.import-files-button'); this.$importNoteButton = this.$widget.find('.import-files-button');
this.$importNoteButton.on("click", () => import('../dialogs/import.js').then(d => d.showDialog(this.tabContext.note.noteId))); this.$importNoteButton.on("click", () => import('../dialogs/import.js').then(d => d.showDialog(this.noteId)));
return this.$widget; return this.$widget;
} }

View File

@ -112,7 +112,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
} }
} }
const note = this.tabContext.note; const note = this.note;
if (note) { if (note) {
this.$widget.addClass(note.cssClass); this.$widget.addClass(note.cssClass);
@ -144,7 +144,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
} }
async getWidgetType() { async getWidgetType() {
const note = this.tabContext.note; const note = this.note;
if (!note) { if (!note) {
return "empty"; return "empty";
@ -199,7 +199,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
await libraryLoader.requireLibrary(libraryLoader.PRINT_THIS); await libraryLoader.requireLibrary(libraryLoader.PRINT_THIS);
this.$widget.find('.note-detail-printable:visible').printThis({ this.$widget.find('.note-detail-printable:visible').printThis({
header: $("<h2>").text(this.tabContext.note && this.tabContext.note.title).prop('outerHTML') , header: $("<h2>").text(this.note && this.note.title).prop('outerHTML') ,
importCSS: false, importCSS: false,
loadCSS: [ loadCSS: [
"libraries/codemirror/codemirror.css", "libraries/codemirror/codemirror.css",

View File

@ -30,10 +30,9 @@ export default class NoteTitleWidget extends TabAwareWidget {
super(appContext); super(appContext);
this.spacedUpdate = new SpacedUpdate(async () => { this.spacedUpdate = new SpacedUpdate(async () => {
const noteId = this.tabContext.note.noteId;
const title = this.$noteTitle.val(); const title = this.$noteTitle.val();
await server.put(`notes/${noteId}/change-title`, {title}); await server.put(`notes/${this.noteId}/change-title`, {title});
}); });
} }

View File

@ -67,7 +67,7 @@ export default class NoteTypeWidget extends TabAwareWidget {
this.save(noteType.type, noteType.mime); this.save(noteType.type, noteType.mime);
}); });
if (this.tabContext.note.type === noteType.type) { if (this.note.type === noteType.type) {
$typeLink.addClass("selected"); $typeLink.addClass("selected");
} }
@ -93,7 +93,7 @@ export default class NoteTypeWidget extends TabAwareWidget {
this.save('code', $link.attr('data-mime-type')) this.save('code', $link.attr('data-mime-type'))
}); });
if (this.tabContext.note.type === 'code' && this.tabContext.note.mime === mimeType.mime) { if (this.note.type === 'code' && this.note.mime === mimeType.mime) {
$mimeLink.addClass("selected"); $mimeLink.addClass("selected");
this.$noteTypeDesc.text(mimeType.title); this.$noteTypeDesc.text(mimeType.title);
@ -118,11 +118,11 @@ export default class NoteTypeWidget extends TabAwareWidget {
} }
async save(type, mime) { async save(type, mime) {
if (type !== this.tabContext.note.type && !await this.confirmChangeIfContent()) { if (type !== this.note.type && !await this.confirmChangeIfContent()) {
return; return;
} }
await server.put('notes/' + this.tabContext.note.noteId await server.put('notes/' + this.noteId
+ '/type/' + encodeURIComponent(type) + '/type/' + encodeURIComponent(type)
+ '/mime/' + encodeURIComponent(mime)); + '/mime/' + encodeURIComponent(mime));

View File

@ -224,7 +224,7 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
.prop("title", "Remove this attribute") .prop("title", "Remove this attribute")
.on('click', async () => { .on('click', async () => {
if (valueAttr.attributeId) { if (valueAttr.attributeId) {
await server.remove("notes/" + this.tabContext.note.noteId + "/attributes/" + valueAttr.attributeId); await server.remove("notes/" + this.noteId + "/attributes/" + valueAttr.attributeId);
} }
$tr.remove(); $tr.remove();
@ -253,7 +253,7 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
value = $attr.val(); value = $attr.val();
} }
const result = await server.put(`notes/${this.tabContext.note.noteId}/attribute`, { const result = await server.put(`notes/${this.noteId}/attribute`, {
attributeId: $attr.prop("attribute-id"), attributeId: $attr.prop("attribute-id"),
type: $attr.prop("attribute-type"), type: $attr.prop("attribute-type"),
name: $attr.prop("attribute-name"), name: $attr.prop("attribute-name"),
@ -261,13 +261,5 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
}); });
$attr.prop("attribute-id", result.attributeId); $attr.prop("attribute-id", result.attributeId);
// FIXME
// animate only if it's not being animated already, this is important especially for e.g. number inputs
// which can be changed many times in a second by clicking on higher/lower buttons.
// if (this.$savedIndicator.queue().length === 0) {
// this.$savedIndicator.fadeOut();
// this.$savedIndicator.fadeIn();
// }
} }
} }

View File

@ -32,7 +32,7 @@ export default class SimilarNotesWidget extends CollapsibleWidget {
// remember which title was when we found the similar notes // remember which title was when we found the similar notes
this.title = 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.noteId);
if (similarNotes.length === 0) { if (similarNotes.length === 0) {
this.$body.text("No similar notes found ..."); this.$body.text("No similar notes found ...");

View File

@ -230,7 +230,7 @@ export default class BookTypeWidget extends TypeWidget {
return; return;
} }
const childNotePath = this.tabContext.notePath + '/' + childNote.noteId; const childNotePath = this.notePath + '/' + childNote.noteId;
const $content = $('<div class="note-book-content">') const $content = $('<div class="note-book-content">')
.css("max-height", ZOOMS[this.zoomLevel].height); .css("max-height", ZOOMS[this.zoomLevel].height);
@ -271,7 +271,7 @@ export default class BookTypeWidget extends TypeWidget {
/** @return {boolean} true if this is "auto book" activated (empty text note) and not explicit book note */ /** @return {boolean} true if this is "auto book" activated (empty text note) and not explicit book note */
isAutoBook() { isAutoBook() {
return this.tabContext.note.type !== 'book'; return this.note.type !== 'book';
} }
getDefaultZoomLevel() { getDefaultZoomLevel() {

View File

@ -107,20 +107,19 @@ export default class CodeTypeWidget extends TypeWidget {
async executeCurrentNote() { async executeCurrentNote() {
// ctrl+enter is also used elsewhere so make sure we're running only when appropriate // ctrl+enter is also used elsewhere so make sure we're running only when appropriate
if (this.tabContext.note.type !== 'code') { if (this.note.type !== 'code') {
return; return;
} }
// make sure note is saved so we load latest changes // make sure note is saved so we load latest changes
// FIXME await this.spacedUpdate.updateNowIfNecessary();
await noteDetailService.saveNotesIfChanged();
if (this.tabContext.note.mime.endsWith("env=frontend")) { if (this.note.mime.endsWith("env=frontend")) {
await bundleService.getAndExecuteBundle(this.tabContext.note.noteId); await bundleService.getAndExecuteBundle(this.noteId);
} }
if (this.tabContext.note.mime.endsWith("env=backend")) { if (this.note.mime.endsWith("env=backend")) {
await server.post('script/run/' + this.tabContext.note.noteId); await server.post('script/run/' + this.noteId);
} }
toastService.showMessage("Note executed"); toastService.showMessage("Note executed");

View File

@ -94,7 +94,7 @@ export default class FileTypeWidget extends TypeWidget {
formData.append('upload', fileToUpload); formData.append('upload', fileToUpload);
const result = await $.ajax({ const result = await $.ajax({
url: baseApiUrl + 'notes/' + this.tabContext.note.noteId + '/file', url: baseApiUrl + 'notes/' + this.noteId + '/file',
headers: server.getHeaders(), headers: server.getHeaders(),
data: formData, data: formData,
type: 'PUT', type: 'PUT',
@ -106,7 +106,7 @@ export default class FileTypeWidget extends TypeWidget {
if (result.uploaded) { if (result.uploaded) {
toastService.showMessage("New file revision has been uploaded."); toastService.showMessage("New file revision has been uploaded.");
// FIXME reload this.refresh();
} }
else { else {
toastService.showError("Upload of a new file revision failed."); toastService.showError("Upload of a new file revision failed.");
@ -142,7 +142,7 @@ export default class FileTypeWidget extends TypeWidget {
} }
getFileUrl() { getFileUrl() {
return utils.getUrlForDownload("api/notes/" + this.tabContext.note.noteId + "/download"); return utils.getUrlForDownload("api/notes/" + this.noteId + "/download");
} }
getContent() {} getContent() {}

View File

@ -97,7 +97,7 @@ class ImageTypeWidget extends TypeWidget {
formData.append('upload', fileToUpload); formData.append('upload', fileToUpload);
const result = await $.ajax({ const result = await $.ajax({
url: baseApiUrl + 'images/' + this.tabContext.note.noteId, url: baseApiUrl + 'images/' + this.noteId,
headers: server.getHeaders(), headers: server.getHeaders(),
data: formData, data: formData,
type: 'PUT', type: 'PUT',
@ -111,8 +111,7 @@ class ImageTypeWidget extends TypeWidget {
await utils.clearBrowserCache(); await utils.clearBrowserCache();
// FIXME this.refresh();
await noteDetailService.reload();
} }
else { else {
toastService.showError("Upload of a new image revision failed: " + result.message); toastService.showError("Upload of a new image revision failed: " + result.message);
@ -146,7 +145,7 @@ class ImageTypeWidget extends TypeWidget {
} }
getFileUrl() { getFileUrl() {
return utils.getUrlForDownload(`api/notes/${this.tabContext.note.noteId}/download`); return utils.getUrlForDownload(`api/notes/${this.noteId}/download`);
} }
getContent() {} getContent() {}

View File

@ -157,7 +157,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
return; return;
} }
const {note} = await server.post(`notes/${this.tabContext.note.noteId}/children?target=into`, { const {note} = await server.post(`notes/${this.noteId}/children?target=into`, {
title, title,
content: '', content: '',
type: 'text' type: 'text'
@ -519,7 +519,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
} }
saveData() { saveData() {
this.tabContext.noteChanged(); this.spacedUpdate.scheduleUpdate();
} }
async createNoteBox(noteId, title, x, y) { async createNoteBox(noteId, title, x, y) {

View File

@ -30,8 +30,7 @@ export default class RenderTypeWidget extends TypeWidget {
this.$widget.show(); this.$widget.show();
this.$noteDetailRenderHelp.hide(); this.$noteDetailRenderHelp.hide();
// FIXME this.ctx const renderNotesFound = await renderService.render(note, this.$noteDetailRenderContent, this.tabContext);
const renderNotesFound = await renderService.render(note, this.$noteDetailRenderContent, this.ctx);
if (!renderNotesFound) { if (!renderNotesFound) {
this.$noteDetailRenderHelp.show(); this.$noteDetailRenderHelp.show();

View File

@ -29,8 +29,7 @@ export default class SearchTypeWidget extends TypeWidget {
this.$refreshButton = this.$widget.find('.note-detail-search-refresh-results-button'); this.$refreshButton = this.$widget.find('.note-detail-search-refresh-results-button');
this.$refreshButton.on('click', async () => { this.$refreshButton.on('click', async () => {
// FIXME await this.spacedUpdate.updateNowIfNecessary();
await noteDetailService.saveNotesIfChanged();
await searchNotesService.refreshSearch(); await searchNotesService.refreshSearch();
}); });

View File

@ -22,8 +22,8 @@ export default class WhatLinksHereWidget extends CollapsibleWidget {
return [$showFullButton]; return [$showFullButton];
} }
async refreshWithNote() { async refreshWithNote(note) {
const targetRelations = this.tabContext.note.getTargetRelations(); const targetRelations = note.getTargetRelations();
if (targetRelations.length === 0) { if (targetRelations.length === 0) {
this.$body.text("Nothing links here yet ..."); this.$body.text("Nothing links here yet ...");
@ -44,7 +44,7 @@ export default class WhatLinksHereWidget extends CollapsibleWidget {
} }
if (i < targetRelations.length) { if (i < targetRelations.length) {
$list.append($("<li>").text(`${targetRelations.length - i} more links ...`)) $list.append($("<li>").text(`${targetRelations.length - i} more links ...`));
} }
this.$body.empty().append($list); this.$body.empty().append($list);