1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +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) {
if (!options) options = {};
const parent = options.parent ? options.parent : app().currentFolder();
const ItemClass = BaseItem.itemClass(type);
@ -185,6 +185,9 @@ class Application {
let cmd = new CommandClass();
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++) {
vorpalCmd.alias(cmd.aliases()[i]);
}

View File

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

View File

@ -93,7 +93,7 @@ class Command extends BaseCommand {
}
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) + ')';
} else {
seenTitles.push(item.title);

View File

@ -205,18 +205,18 @@ class BaseItem extends BaseModel {
static async serialize(item, type = null, shownKeys = null) {
item = this.filter(item);
let output = [];
let output = {};
if ('title' in item && shownKeys.indexOf('title') >= 0) {
output.push(item.title);
output.push('');
output.title = item.title;
}
if ('body' in item && shownKeys.indexOf('body') >= 0) {
output.push(item.body);
if (shownKeys.length) output.push('');
output.body = item.body;
}
output.props = [];
for (let i = 0; i < shownKeys.length; i++) {
let key = shownKeys[i];
if (key == 'title' || key == 'body') continue;
@ -230,10 +230,16 @@ class BaseItem extends BaseModel {
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) {

View File

@ -27,7 +27,10 @@ class Note extends BaseItem {
static async unserializeForEdit(content) {
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) {