From dd557f66a566f0198948be716876bcc0a2409b90 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 2 Jun 2020 22:27:36 +0100 Subject: [PATCH] Desktop: Create new notes with minimum set of properties to prevent a few minor bugs --- ElectronClient/gui/MainScreen.jsx | 8 ++++++-- ReactNativeClient/lib/BaseModel.js | 8 ++++++++ ReactNativeClient/lib/joplin-database.js | 12 ++++++++++++ ReactNativeClient/lib/models/Note.js | 21 ++++++++++++++++++--- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/ElectronClient/gui/MainScreen.jsx b/ElectronClient/gui/MainScreen.jsx index 932859d01..0524e09dd 100644 --- a/ElectronClient/gui/MainScreen.jsx +++ b/ElectronClient/gui/MainScreen.jsx @@ -139,11 +139,15 @@ class MainScreenComponent extends React.Component { const body = template ? TemplateUtils.render(template) : ''; - const newNote = await Note.save({ + const defaultValues = Note.previewFieldsWithDefaultValues({ includeTimestamps: false }); + + let newNote = Object.assign({}, defaultValues, { parent_id: folderId, is_todo: isTodo ? 1 : 0, body: body, - }, { provisional: true }); + }); + + newNote = await Note.save(newNote, { provisional: true }); this.props.dispatch({ type: 'NOTE_SELECT', diff --git a/ReactNativeClient/lib/BaseModel.js b/ReactNativeClient/lib/BaseModel.js index 0dfeea11d..2aa424ba7 100644 --- a/ReactNativeClient/lib/BaseModel.js +++ b/ReactNativeClient/lib/BaseModel.js @@ -47,6 +47,14 @@ class BaseModel { return null; } + static defaultValues(fieldNames) { + const output = {}; + for (const n of fieldNames) { + output[n] = this.db().fieldDefaultValue(this.tableName(), n); + } + return output; + } + static modelIndexById(items, id) { for (let i = 0; i < items.length; i++) { if (items[i].id == id) return i; diff --git a/ReactNativeClient/lib/joplin-database.js b/ReactNativeClient/lib/joplin-database.js index d720a5b7b..62f5a1a7e 100644 --- a/ReactNativeClient/lib/joplin-database.js +++ b/ReactNativeClient/lib/joplin-database.js @@ -215,6 +215,18 @@ class JoplinDatabase extends Database { return row; } + fieldByName(tableName, fieldName) { + const fields = this.tableFields(tableName); + for (const field of fields) { + if (field.name === fieldName) return field; + } + throw new Error(`No such field: ${tableName}: ${fieldName}`); + } + + fieldDefaultValue(tableName, fieldName) { + return this.fieldByName(tableName, fieldName).default; + } + fieldDescription(tableName, fieldName) { const sp = sprintf; diff --git a/ReactNativeClient/lib/models/Note.js b/ReactNativeClient/lib/models/Note.js index e96ba43ff..40a65cf67 100644 --- a/ReactNativeClient/lib/models/Note.js +++ b/ReactNativeClient/lib/models/Note.js @@ -249,9 +249,24 @@ class Note extends BaseItem { }); } - static previewFields() { - // return ['id', 'title', 'body', 'is_todo', 'todo_completed', 'parent_id', 'updated_time', 'user_updated_time', 'user_created_time', 'encryption_applied']; - return ['id', 'title', 'is_todo', 'todo_completed', 'parent_id', 'updated_time', 'user_updated_time', 'user_created_time', 'encryption_applied']; + static previewFieldsWithDefaultValues(options = null) { + return Note.defaultValues(this.previewFields(options)); + } + + static previewFields(options = null) { + options = Object.assign({ + includeTimestamps: true, + }, options); + + const output = ['id', 'title', 'is_todo', 'todo_completed', 'parent_id', 'encryption_applied']; + + if (options.includeTimestamps) { + output.push('updated_time'); + output.push('user_updated_time'); + output.push('user_created_time'); + } + + return output; } static previewFieldsSql(fields = null) {