1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Fixed note serialization and edition

This commit is contained in:
Laurent Cozic 2017-07-13 22:26:45 +01:00
parent 1b50bfe960
commit 4b07092a75
5 changed files with 37 additions and 15 deletions

View File

@ -64,7 +64,7 @@ class Application {
async loadItems(type, pattern, options = null) { async loadItems(type, pattern, options = null) {
if (!options) options = {}; if (!options) options = {};
const parent = options.parent ? options.parent : app().currentFolder(); const parent = options.parent ? options.parent : app().currentFolder();
const ItemClass = BaseItem.itemClass(type); const ItemClass = BaseItem.itemClass(type);
@ -185,6 +185,9 @@ class Application {
let cmd = new CommandClass(); let cmd = new CommandClass();
let vorpalCmd = this.vorpal().command(cmd.usage(), cmd.description()); let vorpalCmd = this.vorpal().command(cmd.usage(), cmd.description());
// TODO: maybe remove if the PR is not merged
if ('disableTypeCasting' in vorpalCmd) vorpalCmd.disableTypeCasting();
for (let i = 0; i < cmd.aliases().length; i++) { for (let i = 0; i < cmd.aliases().length; i++) {
vorpalCmd.alias(cmd.aliases()[i]); vorpalCmd.alias(cmd.aliases()[i]);
} }

View File

@ -24,10 +24,16 @@ class Command extends BaseCommand {
async action(args) { async action(args) {
let watcher = null; let watcher = null;
let newNote = null;
let hasSaved = false;
const onFinishedEditing = () => { const onFinishedEditing = async () => {
if (watcher) watcher.close(); if (watcher) watcher.close();
app().vorpal().show(); app().vorpal().show();
if (!hasSaved && newNote) {
await Note.delete(newNote.id);
newNote = null;
}
this.log(_('Done editing.')); this.log(_('Done editing.'));
} }
@ -43,7 +49,10 @@ class Command extends BaseCommand {
if (!app().currentFolder()) throw new Error(_('No active notebook.')); if (!app().currentFolder()) throw new Error(_('No active notebook.'));
let note = await app().loadItem(BaseModel.TYPE_NOTE, title); let note = await app().loadItem(BaseModel.TYPE_NOTE, title);
if (!note) throw new Error(_('No note with title "%s" found.', title)); if (!note) {
newNote = await Note.save({ parent_id: app().currentFolder().id });
note = newNote;
}
let editorPath = textEditorPath(); let editorPath = textEditorPath();
let editorArgs = editorPath.split(' '); let editorArgs = editorPath.split(' ');
@ -71,6 +80,7 @@ class Command extends BaseCommand {
if (watchTimeout) return; if (watchTimeout) return;
watchTimeout = setTimeout(async () => { watchTimeout = setTimeout(async () => {
hasSaved = true;
let updatedNote = await fs.readFile(tempFilePath, 'utf8'); let updatedNote = await fs.readFile(tempFilePath, 'utf8');
updatedNote = await Note.unserializeForEdit(updatedNote); updatedNote = await Note.unserializeForEdit(updatedNote);
updatedNote.id = note.id; updatedNote.id = note.id;
@ -80,11 +90,11 @@ class Command extends BaseCommand {
}); });
const childProcess = spawn(editorPath, editorArgs, { stdio: 'inherit' }); const childProcess = spawn(editorPath, editorArgs, { stdio: 'inherit' });
childProcess.on('exit', (error, code) => { childProcess.on('exit', async (error, code) => {
onFinishedEditing(); await onFinishedEditing();
}); });
} catch(error) { } catch(error) {
onFinishedEditing(); await onFinishedEditing();
throw error; throw error;
} }
} }

View File

@ -93,7 +93,7 @@ class Command extends BaseCommand {
} }
let title = item.title + suffix; let title = item.title + suffix;
if (seenTitles.indexOf(item.title) >= 0) { if (seenTitles.indexOf(item.title) >= 0 || !item.title) {
title += ' (' + item.id.substr(0,4) + ')'; title += ' (' + item.id.substr(0,4) + ')';
} else { } else {
seenTitles.push(item.title); seenTitles.push(item.title);

View File

@ -205,18 +205,18 @@ class BaseItem extends BaseModel {
static async serialize(item, type = null, shownKeys = null) { static async serialize(item, type = null, shownKeys = null) {
item = this.filter(item); item = this.filter(item);
let output = []; let output = {};
if ('title' in item && shownKeys.indexOf('title') >= 0) { if ('title' in item && shownKeys.indexOf('title') >= 0) {
output.push(item.title); output.title = item.title;
output.push('');
} }
if ('body' in item && shownKeys.indexOf('body') >= 0) { if ('body' in item && shownKeys.indexOf('body') >= 0) {
output.push(item.body); output.body = item.body;
if (shownKeys.length) output.push('');
} }
output.props = [];
for (let i = 0; i < shownKeys.length; i++) { for (let i = 0; i < shownKeys.length; i++) {
let key = shownKeys[i]; let key = shownKeys[i];
if (key == 'title' || key == 'body') continue; if (key == 'title' || key == 'body') continue;
@ -230,10 +230,16 @@ class BaseItem extends BaseModel {
value = this.serialize_format(key, item[key]); value = this.serialize_format(key, item[key]);
} }
output.push(key + ': ' + value); output.props.push(key + ': ' + value);
} }
return output.join("\n"); let temp = [];
if (output.title) temp.push(output.title);
if (output.body) temp.push(output.body);
if (output.props.length) temp.push(output.props.join("\n"));
return temp.join("\n\n");
} }
static async unserialize(content) { static async unserialize(content) {

View File

@ -27,7 +27,10 @@ class Note extends BaseItem {
static async unserializeForEdit(content) { static async unserializeForEdit(content) {
content += "\n\ntype_: " + BaseModel.TYPE_NOTE; content += "\n\ntype_: " + BaseModel.TYPE_NOTE;
return super.unserialize(content); let output = await super.unserialize(content);
if (!output.title) output.title = '';
if (!output.body) output.body = '';
return output;
} }
static async serializeAllProps(note) { static async serializeAllProps(note) {