compatibility with existing scripts

This commit is contained in:
zadam 2020-08-18 21:32:45 +02:00
parent 3670fbff49
commit 03d7ee9abb
9 changed files with 85 additions and 87 deletions

6
package-lock.json generated
View File

@ -3065,9 +3065,9 @@
} }
}, },
"electron": { "electron": {
"version": "10.0.0-beta.21", "version": "10.0.0-beta.23",
"resolved": "https://registry.npmjs.org/electron/-/electron-10.0.0-beta.21.tgz", "resolved": "https://registry.npmjs.org/electron/-/electron-10.0.0-beta.23.tgz",
"integrity": "sha512-r2qIQ9lzq2FBeEpitBWJ5WO0V+TtrEvrgAXc2sUCrZqKfS1kUoJadmrDy69j4R3qNfifRrxuR2wISMmvNL2SAQ==", "integrity": "sha512-jMav5NXZUN8YdcCfASy0Jimms3VoFEPa2nYGZTN/19nlryEedaZksHKJM9afVX3w9AnUv8xPCWdyXUQiRg0YWA==",
"dev": true, "dev": true,
"requires": { "requires": {
"@electron/get": "^1.0.1", "@electron/get": "^1.0.1",

View File

@ -77,7 +77,7 @@
}, },
"devDependencies": { "devDependencies": {
"cross-env": "7.0.2", "cross-env": "7.0.2",
"electron": "10.0.0-beta.21", "electron": "10.0.0-beta.23",
"electron-builder": "22.8.0", "electron-builder": "22.8.0",
"electron-packager": "15.0.0", "electron-packager": "15.0.0",
"electron-rebuild": "1.11.0", "electron-rebuild": "1.11.0",

View File

@ -23,7 +23,7 @@ app.use(helmet({
hidePoweredBy: false, // deactivated because electron 4.0 crashes on this right after startup hidePoweredBy: false, // deactivated because electron 4.0 crashes on this right after startup
contentSecurityPolicy: { contentSecurityPolicy: {
directives: { directives: {
defaultSrc: ["*", "'unsafe-inline'", "'unsafe-eval'"] defaultSrc: ["*", "'unsafe-inline'", "'unsafe-eval'", "img-src 'self' data:"]
} }
} }
})); }));

View File

@ -44,6 +44,7 @@ const TPL = `
export default class SearchBoxWidget extends BasicWidget { export default class SearchBoxWidget extends BasicWidget {
doRender() { doRender() {
this.$widget = $(TPL); this.$widget = $(TPL);
this.contentSized();
this.$searchBox = this.$widget; this.$searchBox = this.$widget;
this.$closeSearchButton = this.$widget.find(".close-search-button"); this.$closeSearchButton = this.$widget.find(".close-search-button");

View File

@ -13,7 +13,6 @@ const BUILTIN_ATTRIBUTES = [
{ type: 'label', name: 'calendarRoot' }, { type: 'label', name: 'calendarRoot' },
{ type: 'label', name: 'archived' }, { type: 'label', name: 'archived' },
{ type: 'label', name: 'excludeFromExport' }, { type: 'label', name: 'excludeFromExport' },
{ type: 'label', name: 'manualTransactionHandling' },
{ type: 'label', name: 'disableInclusion' }, { type: 'label', name: 'disableInclusion' },
{ type: 'label', name: 'appCss' }, { type: 'label', name: 'appCss' },
{ type: 'label', name: 'appTheme' }, { type: 'label', name: 'appTheme' },

View File

@ -68,13 +68,6 @@ function BackendScriptApi(currentNote, apiParams) {
*/ */
this.getAttribute = repository.getAttribute; this.getAttribute = repository.getAttribute;
/**
* @method
* @param {string} imageId
* @returns {Image|null}
*/
this.getImage = repository.getImage;
/** /**
* Retrieves first entity from the SQL's result set. * Retrieves first entity from the SQL's result set.
* *
@ -275,6 +268,7 @@ function BackendScriptApi(currentNote, apiParams) {
extraOptions.content = content; extraOptions.content = content;
} }
sql.transactional(() => {
const {note, branch} = noteService.createNewNote(extraOptions); const {note, branch} = noteService.createNewNote(extraOptions);
for (const attr of extraOptions.attributes || []) { for (const attr of extraOptions.attributes || []) {
@ -288,6 +282,7 @@ function BackendScriptApi(currentNote, apiParams) {
} }
return {note, branch}; 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 * This functions wraps code which is supposed to be running in transaction. If transaction already
* exists, then we'll use that transaction. * 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 * @method
* @param {function} func * @param {function} func
* @returns {?} result of func callback * @returns {?} result of func callback

View File

@ -4,6 +4,7 @@ const noteService = require('./notes');
const attributeService = require('./attributes'); const attributeService = require('./attributes');
const dateUtils = require('./date_utils'); const dateUtils = require('./date_utils');
const repository = require('./repository'); const repository = require('./repository');
const sql = require('./sql');
const CALENDAR_ROOT_LABEL = 'calendarRoot'; const CALENDAR_ROOT_LABEL = 'calendarRoot';
const YEAR_LABEL = 'yearNote'; const YEAR_LABEL = 'yearNote';
@ -36,6 +37,7 @@ function getRootCalendarNote() {
let rootNote = attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL); let rootNote = attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL);
if (!rootNote) { if (!rootNote) {
sql.transactional(() => {
rootNote = noteService.createNewNote({ rootNote = noteService.createNewNote({
parentNoteId: 'root', parentNoteId: 'root',
title: 'Calendar', title: 'Calendar',
@ -47,6 +49,7 @@ function getRootCalendarNote() {
attributeService.createLabel(rootNote.noteId, CALENDAR_ROOT_LABEL); attributeService.createLabel(rootNote.noteId, CALENDAR_ROOT_LABEL);
attributeService.createLabel(rootNote.noteId, 'sorted'); attributeService.createLabel(rootNote.noteId, 'sorted');
});
} }
return rootNote; return rootNote;
@ -66,6 +69,7 @@ function getYearNote(dateStr, rootNote) {
yearNote = getNoteStartingWith(rootNote.noteId, yearStr); yearNote = getNoteStartingWith(rootNote.noteId, yearStr);
if (!yearNote) { if (!yearNote) {
sql.transactional(() => {
yearNote = createNote(rootNote.noteId, yearStr); yearNote = createNote(rootNote.noteId, yearStr);
attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr); attributeService.createLabel(yearNote.noteId, YEAR_LABEL, yearStr);
@ -76,6 +80,7 @@ function getYearNote(dateStr, rootNote) {
if (yearTemplateAttr) { if (yearTemplateAttr) {
attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value); attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value);
} }
});
} }
} }
@ -112,6 +117,7 @@ function getMonthNote(dateStr, rootNote) {
const noteTitle = getMonthNoteTitle(rootNote, monthNumber, dateObj); const noteTitle = getMonthNoteTitle(rootNote, monthNumber, dateObj);
sql.transactional(() => {
monthNote = createNote(yearNote.noteId, noteTitle); monthNote = createNote(yearNote.noteId, noteTitle);
attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr); attributeService.createLabel(monthNote.noteId, MONTH_LABEL, monthStr);
@ -122,6 +128,7 @@ function getMonthNote(dateStr, rootNote) {
if (monthTemplateAttr) { if (monthTemplateAttr) {
attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value); attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value);
} }
});
} }
} }
@ -157,6 +164,7 @@ function getDateNote(dateStr) {
const noteTitle = getDateNoteTitle(rootNote, dayNumber, dateObj); const noteTitle = getDateNoteTitle(rootNote, dayNumber, dateObj);
sql.transactional(() => {
dateNote = createNote(monthNote.noteId, noteTitle); 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));
@ -166,6 +174,7 @@ function getDateNote(dateStr) {
if (dateTemplateAttr) { if (dateTemplateAttr) {
attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value); attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value);
} }
});
} }
} }

View File

@ -105,6 +105,7 @@ function createNewNote(params) {
throw new Error(`Note title must not be empty`); throw new Error(`Note title must not be empty`);
} }
sql.transactional(() => {
const note = new Note({ const note = new Note({
noteId: params.noteId, // optionally can force specific noteId noteId: params.noteId, // optionally can force specific noteId
title: params.title, title: params.title,
@ -134,6 +135,7 @@ function createNewNote(params) {
note, note,
branch branch
}; };
});
} }
function createNewNoteWithTarget(target, targetBranchId, params) { function createNewNoteWithTarget(target, targetBranchId, params) {

View File

@ -39,13 +39,8 @@ async function executeBundle(bundle, apiParams = {}) {
const ctx = new ScriptContext(bundle.allNotes, apiParams); const ctx = new ScriptContext(bundle.allNotes, apiParams);
try { try {
if (bundle.note.hasOwnedLabel('manualTransactionHandling')) {
return execute(ctx, script); return execute(ctx, script);
} }
else {
return sql.transactional(() => execute(ctx, script));
}
}
catch (e) { catch (e) {
log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`); 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()) { if (note.isJavaScript()) {
bundle.script += ` bundle.script += `
apiContext.modules['${note.noteId}'] = {}; 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(', ') + `) { modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
try { try {
${note.getContent()}; ${note.getContent()};