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) {
branchIdsToDelete = await filterRootNote(branchIdsToDelete);
@ -132,24 +115,14 @@ async function deleteNodes(branchIdsToDelete) {
if (deleteClones) {
await server.remove(`notes/${branch.noteId}` + query);
// FIXME
noteDetailService.noteDeleted(branch.noteId);
}
else {
const {noteDeleted} = await server.remove(`branches/${branchIdToDelete}` + query);
if (noteDeleted) {
// FIXME
noteDetailService.noteDeleted(branch.noteId);
}
await server.remove(`branches/${branchIdToDelete}` + query);
}
}
const noteIds = Array.from(new Set(nodes.map(node => node.getParent().data.noteId)));
appContext.trigger('reloadNotes', {noteIds});
return true;
}

View file

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

View file

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

View file

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

View file

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

View file

@ -35,7 +35,7 @@ export default class AttributesWidget extends CollapsibleWidget {
.text("+show inherited")
.on('click', async () => {
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) {
$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 utils from "../services/utils.js";
import dateNoteService from "../services/date_notes.js";
import treeService from "../services/tree.js";
import server from "../services/server.js";
import appContext from "../services/app_context.js";
@ -30,7 +29,7 @@ export default class CalendarWidget extends CollapsibleWidget {
async isEnabled() {
return await super.isEnabled()
&& this.tabContext.note.hasOwnedLabel("dateNote");
&& this.note.hasOwnedLabel("dateNote");
}
async doRenderBody() {

View file

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

View file

@ -112,7 +112,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
}
}
const note = this.tabContext.note;
const note = this.note;
if (note) {
this.$widget.addClass(note.cssClass);
@ -144,7 +144,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
}
async getWidgetType() {
const note = this.tabContext.note;
const note = this.note;
if (!note) {
return "empty";
@ -199,7 +199,7 @@ export default class NoteDetailWidget extends TabAwareWidget {
await libraryLoader.requireLibrary(libraryLoader.PRINT_THIS);
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,
loadCSS: [
"libraries/codemirror/codemirror.css",

View file

@ -30,10 +30,9 @@ export default class NoteTitleWidget extends TabAwareWidget {
super(appContext);
this.spacedUpdate = new SpacedUpdate(async () => {
const noteId = this.tabContext.note.noteId;
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);
});
if (this.tabContext.note.type === noteType.type) {
if (this.note.type === noteType.type) {
$typeLink.addClass("selected");
}
@ -93,7 +93,7 @@ export default class NoteTypeWidget extends TabAwareWidget {
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");
this.$noteTypeDesc.text(mimeType.title);
@ -118,11 +118,11 @@ export default class NoteTypeWidget extends TabAwareWidget {
}
async save(type, mime) {
if (type !== this.tabContext.note.type && !await this.confirmChangeIfContent()) {
if (type !== this.note.type && !await this.confirmChangeIfContent()) {
return;
}
await server.put('notes/' + this.tabContext.note.noteId
await server.put('notes/' + this.noteId
+ '/type/' + encodeURIComponent(type)
+ '/mime/' + encodeURIComponent(mime));

View file

@ -224,7 +224,7 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
.prop("title", "Remove this attribute")
.on('click', async () => {
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();
@ -253,7 +253,7 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
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"),
type: $attr.prop("attribute-type"),
name: $attr.prop("attribute-name"),
@ -261,13 +261,5 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
});
$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
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) {
this.$body.text("No similar notes found ...");

View file

@ -230,7 +230,7 @@ export default class BookTypeWidget extends TypeWidget {
return;
}
const childNotePath = this.tabContext.notePath + '/' + childNote.noteId;
const childNotePath = this.notePath + '/' + childNote.noteId;
const $content = $('<div class="note-book-content">')
.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 */
isAutoBook() {
return this.tabContext.note.type !== 'book';
return this.note.type !== 'book';
}
getDefaultZoomLevel() {

View file

@ -107,20 +107,19 @@ export default class CodeTypeWidget extends TypeWidget {
async executeCurrentNote() {
// 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;
}
// make sure note is saved so we load latest changes
// FIXME
await noteDetailService.saveNotesIfChanged();
await this.spacedUpdate.updateNowIfNecessary();
if (this.tabContext.note.mime.endsWith("env=frontend")) {
await bundleService.getAndExecuteBundle(this.tabContext.note.noteId);
if (this.note.mime.endsWith("env=frontend")) {
await bundleService.getAndExecuteBundle(this.noteId);
}
if (this.tabContext.note.mime.endsWith("env=backend")) {
await server.post('script/run/' + this.tabContext.note.noteId);
if (this.note.mime.endsWith("env=backend")) {
await server.post('script/run/' + this.noteId);
}
toastService.showMessage("Note executed");

View file

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

View file

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

View file

@ -157,7 +157,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
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,
content: '',
type: 'text'
@ -519,7 +519,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
}
saveData() {
this.tabContext.noteChanged();
this.spacedUpdate.scheduleUpdate();
}
async createNoteBox(noteId, title, x, y) {

View file

@ -30,8 +30,7 @@ export default class RenderTypeWidget extends TypeWidget {
this.$widget.show();
this.$noteDetailRenderHelp.hide();
// FIXME this.ctx
const renderNotesFound = await renderService.render(note, this.$noteDetailRenderContent, this.ctx);
const renderNotesFound = await renderService.render(note, this.$noteDetailRenderContent, this.tabContext);
if (!renderNotesFound) {
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.on('click', async () => {
// FIXME
await noteDetailService.saveNotesIfChanged();
await this.spacedUpdate.updateNowIfNecessary();
await searchNotesService.refreshSearch();
});

View file

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