1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Make title and body optional

This commit is contained in:
Laurent Cozic 2017-07-03 21:38:26 +01:00
parent c28323bfab
commit 1ecf5b626e
3 changed files with 130 additions and 21 deletions

View File

@ -5,6 +5,14 @@ import { time } from 'lib/time-utils.js';
class BaseModel {
static modelType() {
throw new Error('Must be overriden');
}
static tableName() {
throw new Error('Must be overriden');
}
static addModelMd(model) {
if (!model) return model;
@ -25,18 +33,10 @@ class BaseModel {
return this.db().logger();
}
static tableName() {
throw new Error('Must be overriden');
}
static useUuid() {
return false;
}
static modelType() {
throw new Error('Must be overriden');
}
static byId(items, id) {
for (let i = 0; i < items.length; i++) {
if (items[i].id == id) return items[i];
@ -284,8 +284,8 @@ BaseModel.TYPE_FOLDER = 2;
BaseModel.TYPE_SETTING = 3;
BaseModel.TYPE_RESOURCE = 4;
BaseModel.TYPE_TAG = 5;
BaseModel.tableInfo_ = null;
BaseModel.tableKeys_ = null;
BaseModel.TYPE_NOTE_TAG = 6;
BaseModel.db_ = null;
BaseModel.dispatch = function(o) {};

View File

@ -133,10 +133,16 @@ class BaseItem extends BaseModel {
let output = [];
if ('title' in item) {
output.push(item.title);
output.push('');
output.push(type == 'note' ? item.body : '');
}
if ('body' in item) {
output.push(item.body);
output.push('');
}
for (let i = 0; i < shownKeys.length; i++) {
let key = shownKeys[i];
let value = null;
@ -168,7 +174,6 @@ class BaseItem extends BaseModel {
if (line == '') {
state = 'readingBody';
continue;
}
let p = line.indexOf(':');
@ -181,15 +186,15 @@ class BaseItem extends BaseModel {
}
}
if (body.length < 3) throw new Error('Invalid body size: ' + body.length + ': ' + content);
let title = body.splice(0, 2);
output.title = title[0];
if (!output.type_) throw new Error('Missing required property: type_: ' + content);
output.type_ = Number(output.type_);
if (output.type_ == BaseModel.TYPE_NOTE) output.body = body.join("\n");
if (body.length) {
let title = body.splice(0, 2);
output.title = title[0];
}
if (body.length) output.body = body.join("\n");
for (let n in output) {
if (!output.hasOwnProperty(n)) continue;

104
lib/models/note-tag.js Normal file
View File

@ -0,0 +1,104 @@
import { Database } from 'lib/database.js';
import { BaseItem } from 'lib/models/base-item.js';
class Tag extends BaseItem {
static tableName() {
return 'note_tags';
}
static modelType() {
return BaseModel.TYPE_NOTE_TAG;
}
// static async serialize(item, type = null, shownKeys = null) {
// let fieldNames = this.fieldNames();
// fieldNames.push('type_');
// fieldNames.push(async () => {
// let noteIds = await this.tagNoteIds(item.id);
// return {
// key: 'notes_',
// value: noteIds.join(','),
// };
// });
// lodash.pull(fieldNames, 'sync_time');
// return super.serialize(item, 'tag', fieldNames);
// }
// static async tagNoteIds(tagId) {
// let rows = await this.db().selectAll('SELECT note_id FROM note_tags WHERE tag_id = ?', [tagId]);
// let output = [];
// for (let i = 0; i < rows.length; i++) {
// output.push(rows[i].note_id);
// }
// return output;
// }
// static async notes(tagId) {
// let noteIds = await this.tagNoteIds(tagId);
// if (!noteIds.length) return [];
// let noteIdsSql = noteIds.join('","');
// noteIdsSql = '"' + noteIdsSql + '"';
// let options = {
// conditions: ['id IN (' + noteIdsSql + ')'],
// };
// return Note.search(options);
// }
// static async addNote(tagId, noteId) {
// let hasIt = await this.hasNote(tagId, noteId);
// if (hasIt) return;
// let query = Database.insertQuery('note_tags', {
// tag_id: tagId,
// note_id: noteId,
// });
// await this.db().exec(query);
// await this.save({ id: tagId, updated_time: time.unixMs() });
// }
// static async removeNote(tagId, noteId) {
// await this.db().exec('DELETE FROM note_tags WHERE tag_id = ? AND note_id = ?', [tagId, noteId]);
// await this.save({ id: tagId, updated_time: time.unixMs() });
// }
// // Note: updated_time must not change here since this is only called from
// // save(), which already handles how the updated_time property is set.
// static async setAssociatedNotes_(tagId, noteIds) {
// let queries = [{
// sql: 'DELETE FROM note_tags WHERE tag_id = ?',
// params: [tagId],
// }];
// for (let i = 0; i < noteIds.length; i++) {
// queries.push(Database.insertQuery('note_tags', { tag_id: tagId, note_id: noteIds[i] }));
// }
// return this.db().transactionExecBatch(queries);
// }
// static async hasNote(tagId, noteId) {
// let r = await this.db().selectOne('SELECT note_id FROM note_tags WHERE tag_id = ? AND note_id = ? LIMIT 1', [tagId, noteId]);
// return !!r;
// }
// static async save(o, options = null) {
// let result = await super.save(o, options);
// if (options && options.applyMetadataChanges === true) {
// if (o.notes_) {
// let noteIds = o.notes_.split(',');
// await this.setAssociatedNotes_(o.id, noteIds);
// }
// }
// return result;
// }
}
export { NoteTag };