1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Create new notes with minimum set of properties to prevent a few minor bugs

This commit is contained in:
Laurent Cozic 2020-06-02 22:27:36 +01:00
parent e117b6f732
commit dd557f66a5
4 changed files with 44 additions and 5 deletions

View File

@ -139,11 +139,15 @@ class MainScreenComponent extends React.Component {
const body = template ? TemplateUtils.render(template) : ''; 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, parent_id: folderId,
is_todo: isTodo ? 1 : 0, is_todo: isTodo ? 1 : 0,
body: body, body: body,
}, { provisional: true }); });
newNote = await Note.save(newNote, { provisional: true });
this.props.dispatch({ this.props.dispatch({
type: 'NOTE_SELECT', type: 'NOTE_SELECT',

View File

@ -47,6 +47,14 @@ class BaseModel {
return null; 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) { static modelIndexById(items, id) {
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
if (items[i].id == id) return i; if (items[i].id == id) return i;

View File

@ -215,6 +215,18 @@ class JoplinDatabase extends Database {
return row; 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) { fieldDescription(tableName, fieldName) {
const sp = sprintf; const sp = sprintf;

View File

@ -249,9 +249,24 @@ class Note extends BaseItem {
}); });
} }
static previewFields() { static previewFieldsWithDefaultValues(options = null) {
// return ['id', 'title', 'body', 'is_todo', 'todo_completed', 'parent_id', 'updated_time', 'user_updated_time', 'user_created_time', 'encryption_applied']; return Note.defaultValues(this.previewFields(options));
return ['id', 'title', 'is_todo', 'todo_completed', 'parent_id', 'updated_time', 'user_updated_time', 'user_created_time', 'encryption_applied']; }
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) { static previewFieldsSql(fields = null) {