2017-07-10 22:03:46 +02:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import { BaseCommand } from './base-command.js';
|
|
|
|
import { app } from './app.js';
|
|
|
|
import { _ } from 'lib/locale.js';
|
|
|
|
import { Folder } from 'lib/models/folder.js';
|
|
|
|
import { Note } from 'lib/models/note.js';
|
|
|
|
import { Setting } from 'lib/models/setting.js';
|
2017-07-10 22:47:01 +02:00
|
|
|
import { BaseModel } from 'lib/base-model.js';
|
2017-07-10 22:03:46 +02:00
|
|
|
import { autocompleteItems } from './autocomplete.js';
|
|
|
|
|
|
|
|
class Command extends BaseCommand {
|
|
|
|
|
|
|
|
usage() {
|
2017-08-04 19:51:01 +02:00
|
|
|
return 'edit <note>';
|
2017-07-10 22:03:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
description() {
|
2017-07-18 20:21:03 +02:00
|
|
|
return _('Edit note.');
|
2017-07-10 22:03:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
autocomplete() {
|
|
|
|
return { data: autocompleteItems };
|
|
|
|
}
|
|
|
|
|
|
|
|
async action(args) {
|
|
|
|
let watcher = null;
|
2017-07-13 23:26:45 +02:00
|
|
|
let newNote = null;
|
2017-07-10 22:03:46 +02:00
|
|
|
|
2017-07-13 23:26:45 +02:00
|
|
|
const onFinishedEditing = async () => {
|
2017-07-10 22:03:46 +02:00
|
|
|
if (watcher) watcher.close();
|
2017-08-03 19:48:14 +02:00
|
|
|
//app().vorpal().show();
|
2017-07-16 01:09:04 +02:00
|
|
|
newNote = null;
|
2017-07-10 22:03:46 +02:00
|
|
|
this.log(_('Done editing.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const textEditorPath = () => {
|
|
|
|
if (Setting.value('editor')) return Setting.value('editor');
|
|
|
|
if (process.env.EDITOR) return process.env.EDITOR;
|
|
|
|
throw new Error(_('No text editor is defined. Please set it using `config editor <editor-path>`'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-08-04 19:51:01 +02:00
|
|
|
let title = args['note'];
|
2017-07-10 22:03:46 +02:00
|
|
|
|
|
|
|
if (!app().currentFolder()) throw new Error(_('No active notebook.'));
|
2017-07-10 22:47:01 +02:00
|
|
|
let note = await app().loadItem(BaseModel.TYPE_NOTE, title);
|
2017-07-10 22:03:46 +02:00
|
|
|
|
2017-07-13 23:26:45 +02:00
|
|
|
if (!note) {
|
2017-08-03 19:48:14 +02:00
|
|
|
// TODO
|
|
|
|
throw new Error(_('Note does not exist.'));
|
|
|
|
|
2017-07-27 19:25:42 +02:00
|
|
|
let ok = await vorpalUtils.cmdPromptConfirm(this, _('Note does not exist: "%s". Create it?', title))
|
|
|
|
if (!ok) return;
|
2017-07-16 01:09:04 +02:00
|
|
|
newNote = await Note.save({ title: title, parent_id: app().currentFolder().id });
|
|
|
|
note = await Note.load(newNote.id);
|
2017-07-13 23:26:45 +02:00
|
|
|
}
|
2017-07-10 22:03:46 +02:00
|
|
|
|
|
|
|
let editorPath = textEditorPath();
|
|
|
|
let editorArgs = editorPath.split(' ');
|
|
|
|
|
|
|
|
editorPath = editorArgs[0];
|
|
|
|
editorArgs = editorArgs.splice(1);
|
|
|
|
|
|
|
|
let content = await Note.serializeForEdit(note);
|
|
|
|
|
|
|
|
let tempFilePath = Setting.value('tempDir') + '/' + Note.systemPath(note);
|
|
|
|
editorArgs.push(tempFilePath);
|
|
|
|
|
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
this.log(_('Starting to edit note. Close the editor to get back to the prompt.'));
|
|
|
|
|
|
|
|
await fs.writeFile(tempFilePath, content);
|
|
|
|
|
|
|
|
let watchTimeout = null;
|
|
|
|
watcher = fs.watch(tempFilePath, (eventType, filename) => {
|
|
|
|
// We need a timeout because for each change to the file, multiple events are generated.
|
|
|
|
|
|
|
|
if (watchTimeout) return;
|
|
|
|
|
|
|
|
watchTimeout = setTimeout(async () => {
|
|
|
|
let updatedNote = await fs.readFile(tempFilePath, 'utf8');
|
|
|
|
updatedNote = await Note.unserializeForEdit(updatedNote);
|
|
|
|
updatedNote.id = note.id;
|
|
|
|
await Note.save(updatedNote);
|
|
|
|
watchTimeout = null;
|
|
|
|
}, 200);
|
|
|
|
});
|
|
|
|
|
|
|
|
const childProcess = spawn(editorPath, editorArgs, { stdio: 'inherit' });
|
2017-07-13 23:26:45 +02:00
|
|
|
childProcess.on('exit', async (error, code) => {
|
|
|
|
await onFinishedEditing();
|
2017-07-10 22:03:46 +02:00
|
|
|
});
|
|
|
|
} catch(error) {
|
2017-07-13 23:26:45 +02:00
|
|
|
await onFinishedEditing();
|
2017-07-10 22:03:46 +02:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Command;
|