mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	compatibility with existing scripts
This commit is contained in:
		
							
								
								
									
										6
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										6
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -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", | ||||||
|   | |||||||
| @@ -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", | ||||||
|   | |||||||
| @@ -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:"] | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| })); | })); | ||||||
|   | |||||||
| @@ -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"); | ||||||
|   | |||||||
| @@ -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' }, | ||||||
|   | |||||||
| @@ -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,19 +268,21 @@ function BackendScriptApi(currentNote, apiParams) { | |||||||
|             extraOptions.content = content; |             extraOptions.content = content; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         const {note, branch} = noteService.createNewNote(extraOptions); |         sql.transactional(() => { | ||||||
|  |             const {note, branch} = noteService.createNewNote(extraOptions); | ||||||
|  |  | ||||||
|         for (const attr of extraOptions.attributes || []) { |             for (const attr of extraOptions.attributes || []) { | ||||||
|             attributeService.createAttribute({ |                 attributeService.createAttribute({ | ||||||
|                 noteId: note.noteId, |                     noteId: note.noteId, | ||||||
|                 type: attr.type, |                     type: attr.type, | ||||||
|                 name: attr.name, |                     name: attr.name, | ||||||
|                 value: attr.value, |                     value: attr.value, | ||||||
|                 isInheritable: !!attr.isInheritable |                     isInheritable: !!attr.isInheritable | ||||||
|             }); |                 }); | ||||||
|         } |             } | ||||||
|  |  | ||||||
|         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 | ||||||
|   | |||||||
| @@ -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,17 +37,19 @@ function getRootCalendarNote() { | |||||||
|     let rootNote = attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL); |     let rootNote = attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL); | ||||||
|  |  | ||||||
|     if (!rootNote) { |     if (!rootNote) { | ||||||
|         rootNote = noteService.createNewNote({ |         sql.transactional(() => { | ||||||
|             parentNoteId: 'root', |             rootNote = noteService.createNewNote({ | ||||||
|             title: 'Calendar', |                 parentNoteId: 'root', | ||||||
|             target: 'into', |                 title: 'Calendar', | ||||||
|             isProtected: false, |                 target: 'into', | ||||||
|             type: 'text', |                 isProtected: false, | ||||||
|             content: '' |                 type: 'text', | ||||||
|         }).note; |                 content: '' | ||||||
|  |             }).note; | ||||||
|  |  | ||||||
|         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,16 +69,18 @@ function getYearNote(dateStr, rootNote) { | |||||||
|         yearNote = getNoteStartingWith(rootNote.noteId, yearStr); |         yearNote = getNoteStartingWith(rootNote.noteId, yearStr); | ||||||
|  |  | ||||||
|         if (!yearNote) { |         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, YEAR_LABEL, yearStr); | ||||||
|             attributeService.createLabel(yearNote.noteId, 'sorted'); |                 attributeService.createLabel(yearNote.noteId, 'sorted'); | ||||||
|  |  | ||||||
|             const yearTemplateAttr = rootNote.getOwnedAttribute('relation', 'yearTemplate'); |                 const yearTemplateAttr = rootNote.getOwnedAttribute('relation', 'yearTemplate'); | ||||||
|  |  | ||||||
|             if (yearTemplateAttr) { |                 if (yearTemplateAttr) { | ||||||
|                 attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value); |                     attributeService.createRelation(yearNote.noteId, 'template', yearTemplateAttr.value); | ||||||
|             } |                 } | ||||||
|  |             }); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -112,16 +117,18 @@ function getMonthNote(dateStr, rootNote) { | |||||||
|  |  | ||||||
|             const noteTitle = getMonthNoteTitle(rootNote, monthNumber, dateObj); |             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, MONTH_LABEL, monthStr); | ||||||
|             attributeService.createLabel(monthNote.noteId, 'sorted'); |                 attributeService.createLabel(monthNote.noteId, 'sorted'); | ||||||
|  |  | ||||||
|             const monthTemplateAttr = rootNote.getOwnedAttribute('relation', 'monthTemplate'); |                 const monthTemplateAttr = rootNote.getOwnedAttribute('relation', 'monthTemplate'); | ||||||
|  |  | ||||||
|             if (monthTemplateAttr) { |                 if (monthTemplateAttr) { | ||||||
|                 attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value); |                     attributeService.createRelation(monthNote.noteId, 'template', monthTemplateAttr.value); | ||||||
|             } |                 } | ||||||
|  |             }); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -157,15 +164,17 @@ function getDateNote(dateStr) { | |||||||
|  |  | ||||||
|             const noteTitle = getDateNoteTitle(rootNote, dayNumber, dateObj); |             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) { |                 if (dateTemplateAttr) { | ||||||
|                 attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value); |                     attributeService.createRelation(dateNote.noteId, 'template', dateTemplateAttr.value); | ||||||
|             } |                 } | ||||||
|  |             }); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -105,35 +105,37 @@ function createNewNote(params) { | |||||||
|         throw new Error(`Note title must not be empty`); |         throw new Error(`Note title must not be empty`); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const note = new Note({ |     sql.transactional(() => { | ||||||
|         noteId: params.noteId, // optionally can force specific noteId |         const note = new Note({ | ||||||
|         title: params.title, |             noteId: params.noteId, // optionally can force specific noteId | ||||||
|         isProtected: !!params.isProtected, |             title: params.title, | ||||||
|         type: params.type, |             isProtected: !!params.isProtected, | ||||||
|         mime: deriveMime(params.type, params.mime) |             type: params.type, | ||||||
|     }).save(); |             mime: deriveMime(params.type, params.mime) | ||||||
|  |         }).save(); | ||||||
|  |  | ||||||
|     note.setContent(params.content); |         note.setContent(params.content); | ||||||
|  |  | ||||||
|     const branch = new Branch({ |         const branch = new Branch({ | ||||||
|         noteId: note.noteId, |             noteId: note.noteId, | ||||||
|         parentNoteId: params.parentNoteId, |             parentNoteId: params.parentNoteId, | ||||||
|         notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(params.parentNoteId), |             notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(params.parentNoteId), | ||||||
|         prefix: params.prefix, |             prefix: params.prefix, | ||||||
|         isExpanded: !!params.isExpanded |             isExpanded: !!params.isExpanded | ||||||
|     }).save(); |         }).save(); | ||||||
|  |  | ||||||
|     scanForLinks(note); |         scanForLinks(note); | ||||||
|  |  | ||||||
|     copyChildAttributes(parentNote, note); |         copyChildAttributes(parentNote, note); | ||||||
|  |  | ||||||
|     triggerNoteTitleChanged(note); |         triggerNoteTitleChanged(note); | ||||||
|     triggerChildNoteCreated(note, parentNote); |         triggerChildNoteCreated(note, parentNote); | ||||||
|  |  | ||||||
|     return { |         return { | ||||||
|         note, |             note, | ||||||
|         branch |             branch | ||||||
|     }; |         }; | ||||||
|  |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
| function createNewNoteWithTarget(target, targetBranchId, params) { | function createNewNoteWithTarget(target, targetBranchId, params) { | ||||||
|   | |||||||
| @@ -39,12 +39,7 @@ 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()}; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user