From 253a6ef0816e745a0c5fada3473af616cce5fbce Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 14 Apr 2019 12:24:48 +0200 Subject: [PATCH] added date note APIs to frontend script API --- docs/backend_api/Note.html | 102 +- docs/backend_api/entities_note.js.html | 34 +- docs/frontend_api/FrontendScriptApi.html | 1133 ++++++++++++++++- docs/frontend_api/NoteFull.html | 120 +- docs/frontend_api/entities_note_full.js.html | 6 + docs/frontend_api/global.html | 2 +- .../services_frontend_script_api.js.html | 65 + .../services/frontend_script_api.js | 36 + 8 files changed, 1413 insertions(+), 85 deletions(-) diff --git a/docs/backend_api/Note.html b/docs/backend_api/Note.html index ceaa2bbd1..1cf2ff433 100644 --- a/docs/backend_api/Note.html +++ b/docs/backend_api/Note.html @@ -581,7 +581,7 @@
Source:
@@ -746,7 +746,7 @@
Source:
@@ -922,7 +922,7 @@
Source:
@@ -1026,7 +1026,7 @@
Source:
@@ -1126,7 +1126,7 @@
Source:
@@ -1230,7 +1230,7 @@
Source:
@@ -1334,7 +1334,7 @@
Source:
@@ -1434,7 +1434,7 @@
Source:
@@ -1665,7 +1665,7 @@
Source:
@@ -1861,7 +1861,7 @@
Source:
@@ -2057,7 +2057,7 @@
Source:
@@ -2157,7 +2157,7 @@
Source:
@@ -2306,7 +2306,7 @@
Source:
@@ -2471,7 +2471,7 @@
Source:
@@ -2636,7 +2636,7 @@
Source:
@@ -2789,7 +2789,7 @@
Source:
@@ -2897,7 +2897,7 @@
Source:
@@ -3001,7 +3001,7 @@
Source:
@@ -3101,7 +3101,7 @@
Source:
@@ -3205,7 +3205,7 @@
Source:
@@ -3358,7 +3358,7 @@
Source:
@@ -3523,7 +3523,7 @@
Source:
@@ -3688,7 +3688,7 @@
Source:
@@ -3841,7 +3841,7 @@
Source:
@@ -3997,7 +3997,7 @@
Source:
@@ -4105,7 +4105,7 @@
Source:
@@ -4205,7 +4205,7 @@
Source:
@@ -4313,7 +4313,7 @@
Source:
@@ -4413,7 +4413,7 @@
Source:
@@ -4589,7 +4589,7 @@
Source:
@@ -4693,7 +4693,7 @@
Source:
@@ -4846,7 +4846,7 @@
Source:
@@ -4999,7 +4999,7 @@
Source:
@@ -5108,7 +5108,7 @@ Cache is note instance scoped.
Source:
@@ -5190,7 +5190,7 @@ Cache is note instance scoped.
Source:
@@ -5294,7 +5294,7 @@ Cache is note instance scoped.
Source:
@@ -5398,7 +5398,7 @@ Cache is note instance scoped.
Source:
@@ -5502,7 +5502,7 @@ Cache is note instance scoped.
Source:
@@ -5606,7 +5606,7 @@ Cache is note instance scoped.
Source:
@@ -5710,7 +5710,7 @@ Cache is note instance scoped.
Source:
@@ -5937,7 +5937,7 @@ Cache is note instance scoped.
Source:
@@ -6133,7 +6133,7 @@ Cache is note instance scoped.
Source:
@@ -6329,7 +6329,7 @@ Cache is note instance scoped.
Source:
@@ -6556,7 +6556,7 @@ Cache is note instance scoped.
Source:
@@ -6656,7 +6656,7 @@ Cache is note instance scoped.
Source:
@@ -6756,7 +6756,7 @@ Cache is note instance scoped.
Source:
@@ -6952,7 +6952,7 @@ Cache is note instance scoped.
Source:
@@ -7148,7 +7148,7 @@ Cache is note instance scoped.
Source:
@@ -7406,7 +7406,7 @@ Cache is note instance scoped.
Source:
@@ -7633,7 +7633,7 @@ Cache is note instance scoped.
Source:
@@ -7860,7 +7860,7 @@ Cache is note instance scoped.
Source:
diff --git a/docs/backend_api/entities_note.js.html b/docs/backend_api/entities_note.js.html index 62eaad682..f1acd74b0 100644 --- a/docs/backend_api/entities_note.js.html +++ b/docs/backend_api/entities_note.js.html @@ -90,10 +90,31 @@ class Note extends Entity { } } + /* + * Note content has quite special handling - it's not a separate entity, but a lazily loaded + * part of Note entity with it's own sync. Reasons behind this hybrid design has been: + * + * - content can be quite large and it's not necessary to load it / fill memory for any note access even if we don't need a content, especially for bulk operations like search + * - changes in the note metadata or title should not trigger note content sync (so we keep separate utcDateModified and sync rows) + * - but to the user note content and title changes are one and the same - single dateModified (so all changes must go through Note and content is not a separate entity) + */ + /** @returns {Promise<*>} */ - async getContent() { + async getContent(silentNotFoundError = false) { if (this.content === undefined) { - this.content = await sql.getValue(`SELECT content FROM note_contents WHERE noteId = ?`, [this.noteId]); + const res = await sql.getRow(`SELECT content, hash FROM note_contents WHERE noteId = ?`, [this.noteId]); + + if (!res) { + if (silentNotFoundError) { + return undefined; + } + else { + throw new Error("Cannot find note content for noteId=" + this.noteId); + } + } + + this.content = res.content; + this.contentHash = res.contentHash; // used only for note_fulltext consistency check if (this.isProtected) { if (this.isContentAvailable) { @@ -123,6 +144,10 @@ class Note extends Entity { /** @returns {Promise} */ async setContent(content) { + // force updating note itself so that dateChanged is represented correctly even for the content + this.forcedChange = true; + await this.save(); + this.content = content; const pojo = { @@ -144,10 +169,6 @@ class Note extends Entity { await sql.upsert("note_contents", "noteId", pojo); await syncTableService.addNoteContentSync(this.noteId); - - this.forcedChange = true; - - await this.save(); } /** @returns {Promise} */ @@ -736,6 +757,7 @@ class Note extends Entity { delete pojo.isContentAvailable; delete pojo.__attributeCache; delete pojo.content; + delete pojo.contentHash; } async afterSaving() { diff --git a/docs/frontend_api/FrontendScriptApi.html b/docs/frontend_api/FrontendScriptApi.html index 73e9bb535..c2704ceac 100644 --- a/docs/frontend_api/FrontendScriptApi.html +++ b/docs/frontend_api/FrontendScriptApi.html @@ -81,7 +81,7 @@
Source:
@@ -221,7 +221,7 @@
Source:
@@ -334,7 +334,7 @@
Source:
@@ -444,7 +444,7 @@
Source:
@@ -573,7 +573,7 @@
Source:
@@ -726,7 +726,7 @@
Source:
@@ -879,7 +879,7 @@
Source:
@@ -1057,7 +1057,7 @@
Source:
@@ -1188,7 +1188,7 @@
Source:
@@ -1292,7 +1292,7 @@
Source:
@@ -1396,7 +1396,7 @@
Source:
@@ -1452,6 +1452,110 @@ +

getActiveNotePath() → {string}

+ + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+ returns note path of active note +
+ + + +
+
+ Type +
+
+ +string + + +
+
+ + + + + + + + + + + + +

getCodeMimeTypes() → {array}

@@ -1500,7 +1604,7 @@
Source:
@@ -1556,6 +1660,159 @@ +

getDateNote(date) → {Promise.<NoteShort>}

+ + + + + + +
+ Returns date-note. If it doesn't exist, it is automatically created. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
date + + +string + + + + e.g. "2019-04-29"
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +Promise.<NoteShort> + + +
+
+ + + + + + + + + + + + +

getDefaultCodeMimeTypes() → {array}

@@ -1604,7 +1861,7 @@
Source:
@@ -1713,7 +1970,7 @@ if some action needs to happen on only one specific instance.
Source:
@@ -1765,6 +2022,313 @@ if some action needs to happen on only one specific instance. +

getMonthNote(month) → {Promise.<NoteShort>}

+ + + + + + +
+ Returns month-note. If it doesn't exist, it is automatically created. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
month + + +string + + + + e.g. "2019-04"
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +Promise.<NoteShort> + + +
+
+ + + + + + + + + + + + + +

getNote(noteId) → {Promise.<NoteShort>}

+ + + + + + +
+ Returns note by given noteId. If note is missing from cache, it's loaded. +* +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
noteId + + +string + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +Promise.<NoteShort> + + +
+
+ + + + + + + + + + + + +

getNotes(noteIds, silentNotFoundErroropt) → {Promise.<Array.<NoteShort>>}

@@ -1912,7 +2476,7 @@ otherwise (by e.g. createNoteLink())
Source:
@@ -1964,6 +2528,263 @@ otherwise (by e.g. createNoteLink()) +

getTodayNote() → {Promise.<NoteShort>}

+ + + + + + +
+ Returns date-note for today. If it doesn't exist, it is automatically created. +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +Promise.<NoteShort> + + +
+
+ + + + + + + + + + + + + +

getYearNote(year) → {Promise.<NoteShort>}

+ + + + + + +
+ Returns year-note. If it doesn't exist, it is automatically created. +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
year + + +string + + + + e.g. "2019"
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +Promise.<NoteShort> + + +
+
+ + + + + + + + + + + + +

isNoteStillActive() → {boolean}

@@ -2019,7 +2840,7 @@ note.
Source:
@@ -2172,7 +2993,7 @@ note.
Source:
@@ -2303,7 +3124,7 @@ note.
Source:
@@ -2407,7 +3228,7 @@ note.
Source:
@@ -2493,7 +3314,7 @@ note.
Source:
@@ -2541,6 +3362,268 @@ note. + + + + +

reloadChildren(noteId)

+ + + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
noteId + + +string + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +

reloadParents(noteId)

+ + + + + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
noteId + + +string + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + @@ -2670,7 +3753,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
@@ -2823,7 +3906,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
@@ -2954,7 +4037,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
@@ -3089,7 +4172,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
@@ -3224,7 +4307,7 @@ Internally this serializes the anonymous function into string and sends it to ba
Source:
diff --git a/docs/frontend_api/NoteFull.html b/docs/frontend_api/NoteFull.html index 655bc3f93..50bedd792 100644 --- a/docs/frontend_api/NoteFull.html +++ b/docs/frontend_api/NoteFull.html @@ -199,7 +199,7 @@ -

utcDateCreated

+

dateCreated

@@ -257,7 +257,7 @@ -

utcDateModified

+

dateModified

@@ -314,6 +314,122 @@ + +

utcDateCreated

+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + +

utcDateModified

+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + diff --git a/docs/frontend_api/entities_note_full.js.html b/docs/frontend_api/entities_note_full.js.html index d052c4b01..535bbb7c4 100644 --- a/docs/frontend_api/entities_note_full.js.html +++ b/docs/frontend_api/entities_note_full.js.html @@ -38,6 +38,12 @@ class NoteFull extends NoteShort { /** @param {string} */ this.content = row.content; + /** @param {string} */ + this.dateCreated = row.dateCreated; + + /** @param {string} */ + this.dateModified = row.dateModified; + /** @param {string} */ this.utcDateCreated = row.utcDateCreated; diff --git a/docs/frontend_api/global.html b/docs/frontend_api/global.html index 643d96edf..104db4601 100644 --- a/docs/frontend_api/global.html +++ b/docs/frontend_api/global.html @@ -303,7 +303,7 @@
Source:
diff --git a/docs/frontend_api/services_frontend_script_api.js.html b/docs/frontend_api/services_frontend_script_api.js.html index 860717d74..eac242c9f 100644 --- a/docs/frontend_api/services_frontend_script_api.js.html +++ b/docs/frontend_api/services_frontend_script_api.js.html @@ -36,6 +36,7 @@ import noteDetailService from './note_detail.js'; import noteTypeService from './note_type.js'; import noteTooltipService from './note_tooltip.js'; import protectedSessionService from'./protected_session.js'; +import dateNotesService from'./date_notes.js'; /** * This is the main frontend API interface for scripts. It's published in the local "api" object. @@ -53,6 +54,9 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) { /** @property {object|null} entity whose event triggered this execution */ this.originEntity = originEntity; + // to keep consistency with backend API + this.dayjs = dayjs; + /** * Activates note in the tree and in the note detail. * @@ -159,6 +163,14 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) { } }; + /** + * Returns note by given noteId. If note is missing from cache, it's loaded. + ** + * @param {string} noteId + * @return {Promise<NoteShort>} + */ + this.getNote = async noteId => await treeCache.getNote(noteId); + /** * Returns list of notes. If note is missing from cache, it's loaded. * @@ -171,6 +183,18 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) { */ this.getNotes = async (noteIds, silentNotFoundError = false) => await treeCache.getNotes(noteIds, silentNotFoundError); + /** + * @param {string} noteId + * @method + */ + this.reloadChildren = async noteId => await treeCache.reloadChildren(noteId); + + /** + * @param {string} noteId + * @method + */ + this.reloadParents = async noteId => await treeCache.reloadParents(noteId); + /** * Instance name identifies particular Trilium instance. It can be useful for scripts * if some action needs to happen on only one specific instance. @@ -238,6 +262,12 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) { */ this.getActiveNote = noteDetailService.getActiveNote; + /** + * @method + * @returns {string} returns note path of active note + */ + this.getActiveNotePath = treeService.getActiveNotePath; + /** * This method checks whether user navigated away from the note from which the scripts has been started. * This is necessary because script execution is async and by the time it is finished, the user might have @@ -285,6 +315,41 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) { * @method */ this.protectActiveNote = protectedSessionService.protectNoteAndSendToServer; + + /** + * Returns date-note for today. If it doesn't exist, it is automatically created. + * + * @method + * @return {Promise<NoteShort>} + */ + this.getTodayNote = dateNotesService.getTodayNote; + + /** + * Returns date-note. If it doesn't exist, it is automatically created. + * + * @method + * @param {string} date - e.g. "2019-04-29" + * @return {Promise<NoteShort>} + */ + this.getDateNote = dateNotesService.getDateNote; + + /** + * Returns month-note. If it doesn't exist, it is automatically created. + * + * @method + * @param {string} month - e.g. "2019-04" + * @return {Promise<NoteShort>} + */ + this.getMonthNote = dateNotesService.getMonthNote; + + /** + * Returns year-note. If it doesn't exist, it is automatically created. + * + * @method + * @param {string} year - e.g. "2019" + * @return {Promise<NoteShort>} + */ + this.getYearNote = dateNotesService.getYearNote; } export default FrontendScriptApi; diff --git a/src/public/javascripts/services/frontend_script_api.js b/src/public/javascripts/services/frontend_script_api.js index a55e39649..7f63ddca3 100644 --- a/src/public/javascripts/services/frontend_script_api.js +++ b/src/public/javascripts/services/frontend_script_api.js @@ -8,6 +8,7 @@ import noteDetailService from './note_detail.js'; import noteTypeService from './note_type.js'; import noteTooltipService from './note_tooltip.js'; import protectedSessionService from'./protected_session.js'; +import dateNotesService from'./date_notes.js'; /** * This is the main frontend API interface for scripts. It's published in the local "api" object. @@ -286,6 +287,41 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) { * @method */ this.protectActiveNote = protectedSessionService.protectNoteAndSendToServer; + + /** + * Returns date-note for today. If it doesn't exist, it is automatically created. + * + * @method + * @return {Promise} + */ + this.getTodayNote = dateNotesService.getTodayNote; + + /** + * Returns date-note. If it doesn't exist, it is automatically created. + * + * @method + * @param {string} date - e.g. "2019-04-29" + * @return {Promise} + */ + this.getDateNote = dateNotesService.getDateNote; + + /** + * Returns month-note. If it doesn't exist, it is automatically created. + * + * @method + * @param {string} month - e.g. "2019-04" + * @return {Promise} + */ + this.getMonthNote = dateNotesService.getMonthNote; + + /** + * Returns year-note. If it doesn't exist, it is automatically created. + * + * @method + * @param {string} year - e.g. "2019" + * @return {Promise} + */ + this.getYearNote = dateNotesService.getYearNote; } export default FrontendScriptApi; \ No newline at end of file