Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
compatibility with existing scripts
zadam03d7ee9abbb9
diff --git a/package-lock.json b/package-lock.jsonindex 67ee1b50a2..52f6d5af49 100644--- a/package-lock.json+++ b/package-lock.json@@ -3065,9 +3065,9 @@}},"electron": {- "version": "10.0.0-beta.21",- "resolved": "https://registry.npmjs.org/electron/-/electron-10.0.0-beta.21.tgz",- "integrity": "sha512-r2qIQ9lzq2FBeEpitBWJ5WO0V+TtrEvrgAXc2sUCrZqKfS1kUoJadmrDy69j4R3qNfifRrxuR2wISMmvNL2SAQ==",+ "version": "10.0.0-beta.23",+ "resolved": "https://registry.npmjs.org/electron/-/electron-10.0.0-beta.23.tgz",+ "integrity": "sha512-jMav5NXZUN8YdcCfASy0Jimms3VoFEPa2nYGZTN/19nlryEedaZksHKJM9afVX3w9AnUv8xPCWdyXUQiRg0YWA==","dev": true,"requires": {"@electron/get": "^1.0.1",diff --git a/package.json b/package.jsonindex c79bf5311d..31ed07be51 100644--- a/package.json+++ b/package.json@@ -77,7 +77,7 @@},"devDependencies": {"cross-env": "7.0.2",- "electron": "10.0.0-beta.21",+ "electron": "10.0.0-beta.23","electron-builder": "22.8.0","electron-packager": "15.0.0","electron-rebuild": "1.11.0",diff --git a/src/app.js b/src/app.jsindex a99571f1e4..f18324f549 100644--- a/src/app.js+++ b/src/app.js@@ -23,7 +23,7 @@ app.use(helmet({hidePoweredBy: false, // deactivated because electron 4.0 crashes on this right after startupcontentSecurityPolicy: {directives: {- defaultSrc: ["*", "'unsafe-inline'", "'unsafe-eval'"]+ defaultSrc: ["*", "'unsafe-inline'", "'unsafe-eval'", "img-src 'self' data:"]}}}));diff --git a/src/public/app/widgets/search_box.js b/src/public/app/widgets/search_box.jsindex 2f4cb8cc58..cc9ce03441 100644--- a/src/public/app/widgets/search_box.js+++ b/src/public/app/widgets/search_box.js@@ -44,6 +44,7 @@ const TPL = `export default class SearchBoxWidget extends BasicWidget {doRender() {this.$widget = $(TPL);+ this.contentSized();this.$searchBox = this.$widget;this.$closeSearchButton = this.$widget.find(".close-search-button");diff --git a/src/services/attributes.js b/src/services/attributes.jsindex 9f10b79634..116e7e5c8d 100644--- a/src/services/attributes.js+++ b/src/services/attributes.js@@ -13,7 +13,6 @@ const BUILTIN_ATTRIBUTES = [{ type: 'label', name: 'calendarRoot' },{ type: 'label', name: 'archived' },{ type: 'label', name: 'excludeFromExport' },- { type: 'label', name: 'manualTransactionHandling' },{ type: 'label', name: 'disableInclusion' },{ type: 'label', name: 'appCss' },{ type: 'label', name: 'appTheme' },diff --git a/src/services/backend_script_api.js b/src/services/backend_script_api.jsindex c5805de992..56c45c8c01 100644--- a/src/services/backend_script_api.js+++ b/src/services/backend_script_api.js@@ -68,13 +68,6 @@ function BackendScriptApi(currentNote, apiParams) {*/this.getAttribute = repository.getAttribute;- /**- * @method- * @param {string} imageId- * @returns {Image|null}- */- this.getImage = repository.getImage;-/*** Retrieves first entity from the SQL's result set.*@@ -275,19 +268,21 @@ function BackendScriptApi(currentNote, apiParams) {extraOptions.content = content;}- const {note, branch} = noteService.createNewNote(extraOptions);-- for (const attr of extraOptions.attributes || []) {- attributeService.createAttribute({- noteId: note.noteId,- type: attr.type,- name: attr.name,- value: attr.value,- isInheritable: !!attr.isInheritable- });- }-- return {note, branch};+ sql.transactional(() => {+ const {note, branch} = noteService.createNewNote(extraOptions);++ for (const attr of extraOptions.attributes || []) {+ attributeService.createAttribute({+ noteId: note.noteId,+ type: attr.type,+ name: attr.name,+ value: attr.value,+ isInheritable: !!attr.isInheritable+ });+ }++ return {note, branch};+ });};/**@@ -374,9 +369,6 @@ function BackendScriptApi(currentNote, apiParams) {* This functions wraps code which is supposed to be running in transaction. If transaction already* exists, then we'll use that transaction.*- * This method is required only when script has label manualTransactionHandling, all other scripts are- * transactional by default.- ** @method* @param {function} func* @returns {?} result of func callbackdiff --git a/src/services/date_notes.js b/src/services/date_notes.jsindex 1c3027b272..ee33d33cf4 100644--- a/src/services/date_notes.js+++ b/src/services/date_notes.js@@ -4,6 +4,7 @@ const noteService = require('./notes');const attributeService = require('./attributes');const dateUtils = require('./date_utils');const repository = require('./repository');+const sql = require('./sql');const CALENDAR_ROOT_LABEL = 'calendarRoot';const YEAR_LABEL = 'yearNote';@@ -36,17 +37,19 @@ function getRootCalendarNote() {let rootNote = attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL);if (!rootNote) {- rootNote = noteService.createNewNote({- parentNoteId: 'root',- title: 'Calendar',- target: 'into',- isProtected: false,- type: 'text',- content: ''- }).note;-- attributeService.createLabel(rootNote.noteId, CALENDAR_ROOT_LABEL);- attributeService.createLabel(rootNote.noteId, 'sorted');+ sql.transactional(() => {+ rootNote = noteService.createNewNote({+ parentNoteId: 'root',+ title: 'Calendar',+ target: 'into',+ isProtected: false,+ type: 'text',+ content: ''+ }).note;++ attributeService.createLabel(rootNote.noteId, CALENDAR_ROOT_LABEL);+ attributeService.createLabel(rootNote.noteId, 'sorted');+ });}return rootNote;@@ -66,16 +69,18 @@ function getYearNote(dateStr, rootNote) {yearNote = getNoteStartingWith(rootNote.noteId, yearStr);if (!yearNote) {- yearNote = createNote(rootNote.noteId, yearStr);+ sql.transactional(() => {+ yearNote = createNote(rootNote.noteId, yearStr);- attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr);- attributeService.createLabel(yearNote.noteId, 'sorted');+ attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr);+ attributeService.createLabel(yearNote.noteId, 'sorted');- const yearTemplateAttr = rootNote.getOwnedAttribute('relation', 'yearTemplate');+ const yearTemplateAttr = rootNote.getOwnedAttribute('relation', 'yearTemplate');- if (yearTemplateAttr) {- attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value);- }+ if (yearTemplateAttr) {+ attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value);+ }+ });}}@@ -112,16 +117,18 @@ function getMonthNote(dateStr, rootNote) {const noteTitle = getMonthNoteTitle(rootNote, monthNumber, dateObj);- monthNote = createNote(yearNote.noteId, noteTitle);+ sql.transactional(() => {+ monthNote = createNote(yearNote.noteId, noteTitle);- attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr);- attributeService.createLabel(monthNote.noteId, 'sorted');+ attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr);+ attributeService.createLabel(monthNote.noteId, 'sorted');- const monthTemplateAttr = rootNote.getOwnedAttribute('relation', 'monthTemplate');+ const monthTemplateAttr = rootNote.getOwnedAttribute('relation', 'monthTemplate');- if (monthTemplateAttr) {- attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value);- }+ if (monthTemplateAttr) {+ attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value);+ }+ });}}@@ -157,15 +164,17 @@ function getDateNote(dateStr) {const noteTitle = getDateNoteTitle(rootNote, dayNumber, dateObj);- dateNote = createNote(monthNote.noteId, noteTitle);+ sql.transactional(() => {+ dateNote = createNote(monthNote.noteId, noteTitle);- attributeService.createLabel(dateNote.noteId, DATE_LABEL, dateStr.substr(0, 10));+ attributeService.createLabel(dateNote.noteId, DATE_LABEL, dateStr.substr(0, 10));- const dateTemplateAttr = rootNote.getOwnedAttribute('relation', 'dateTemplate');+ const dateTemplateAttr = rootNote.getOwnedAttribute('relation', 'dateTemplate');- if (dateTemplateAttr) {- attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value);- }+ if (dateTemplateAttr) {+ attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value);+ }+ });}}diff --git a/src/services/notes.js b/src/services/notes.jsindex 212d75df19..e2db234f17 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -105,35 +105,37 @@ function createNewNote(params) {throw new Error(`Note title must not be empty`);}- const note = new Note({- noteId: params.noteId, // optionally can force specific noteId- title: params.title,- isProtected: !!params.isProtected,- type: params.type,- mime: deriveMime(params.type, params.mime)- }).save();+ sql.transactional(() => {+ const note = new Note({+ noteId: params.noteId, // optionally can force specific noteId+ title: params.title,+ isProtected: !!params.isProtected,+ type: params.type,+ mime: deriveMime(params.type, params.mime)+ }).save();- note.setContent(params.content);+ note.setContent(params.content);- const branch = new Branch({- noteId: note.noteId,- parentNoteId: params.parentNoteId,- notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(params.parentNoteId),- prefix: params.prefix,- isExpanded: !!params.isExpanded- }).save();+ const branch = new Branch({+ noteId: note.noteId,+ parentNoteId: params.parentNoteId,+ notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(params.parentNoteId),+ prefix: params.prefix,+ isExpanded: !!params.isExpanded+ }).save();- scanForLinks(note);+ scanForLinks(note);- copyChildAttributes(parentNote, note);+ copyChildAttributes(parentNote, note);- triggerNoteTitleChanged(note);- triggerChildNoteCreated(note, parentNote);+ triggerNoteTitleChanged(note);+ triggerChildNoteCreated(note, parentNote);- return {- note,- branch- };+ return {+ note,+ branch+ };+ });}function createNewNoteWithTarget(target, targetBranchId, params) {diff --git a/src/services/script.js b/src/services/script.jsindex 96a1d1da2f..b588fdfcdf 100644--- a/src/services/script.js+++ b/src/services/script.js@@ -39,12 +39,7 @@ async function executeBundle(bundle, apiParams = {}) {const ctx = new ScriptContext(bundle.allNotes, apiParams);try {- if (bundle.note.hasOwnedLabel('manualTransactionHandling')) {- return execute(ctx, script);- }- else {- return sql.transactional(() => execute(ctx, script));- }+ return execute(ctx, script);}catch (e) {log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`);@@ -159,7 +154,7 @@ function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds =if (note.isJavaScript()) {bundle.script += `apiContext.modules['${note.noteId}'] = {};-${root ? 'return ' : ''}((function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') ++${root ? 'return ' : ''}await ((async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {try {${note.getContent()};