1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-03 08:35:29 +02:00
joplin/CliClient/app/command-edit.js

115 lines
3.6 KiB
JavaScript
Raw Normal View History

const fs = require('fs-extra');
const { BaseCommand } = require('./base-command.js');
const { uuid } = require('lib/uuid.js');
const { app } = require('./app.js');
const { _ } = require('lib/locale.js');
const { Folder } = require('lib/models/folder.js');
const { Note } = require('lib/models/note.js');
const { Setting } = require('lib/models/setting.js');
const { BaseModel } = require('lib/base-model.js');
const { cliUtils } = require('./cli-utils.js');
const { time } = require('lib/time-utils.js');
2017-07-10 22:03:46 +02:00
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
}
async action(args) {
let watcher = null;
2017-10-14 20:03:23 +02:00
let tempFilePath = null;
2017-07-10 22:03:46 +02:00
2017-07-13 23:26:45 +02:00
const onFinishedEditing = async () => {
2017-10-14 20:03:23 +02:00
if (tempFilePath) fs.removeSync(tempFilePath);
2017-07-10 22:03:46 +02:00
}
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-10-14 20:03:23 +02:00
// -------------------------------------------------------------------------
// Load note or create it if it doesn't exist
// -------------------------------------------------------------------------
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.'));
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-10-08 19:50:43 +02:00
const ok = await this.prompt(_('Note does not exist: "%s". Create it?', title));
if (!ok) return;
2017-10-14 20:03:23 +02:00
note = await Note.save({ title: title, parent_id: app().currentFolder().id });
note = await Note.load(note.id);
2017-07-13 23:26:45 +02:00
}
2017-07-10 22:03:46 +02:00
2017-10-14 20:03:23 +02:00
// -------------------------------------------------------------------------
// Create the file to be edited and prepare the editor program arguments
// -------------------------------------------------------------------------
2017-07-10 22:03:46 +02:00
let editorPath = textEditorPath();
let editorArgs = editorPath.split(' ');
editorPath = editorArgs[0];
editorArgs = editorArgs.splice(1);
2017-10-14 20:03:23 +02:00
const originalContent = await Note.serializeForEdit(note);
2017-07-10 22:03:46 +02:00
2017-10-14 20:03:23 +02:00
tempFilePath = Setting.value('tempDir') + '/' + uuid.create() + '.md';
2017-07-10 22:03:46 +02:00
editorArgs.push(tempFilePath);
2017-10-14 20:03:23 +02:00
await fs.writeFile(tempFilePath, originalContent);
// -------------------------------------------------------------------------
// Start editing the file
// -------------------------------------------------------------------------
this.logger().info('Disabling fullscreen...');
2017-07-10 22:03:46 +02:00
2017-10-24 21:52:26 +02:00
app().gui().showModalOverlay(_('Starting to edit note. Close the editor to get back to the prompt.'));
await app().gui().forceRender();
2017-10-14 23:44:50 +02:00
const termState = app().gui().term().saveState();
2017-07-10 22:03:46 +02:00
2017-10-14 20:03:23 +02:00
const spawnSync = require('child_process').spawnSync;
spawnSync(editorPath, editorArgs, { stdio: 'inherit' });
2017-07-10 22:03:46 +02:00
2017-10-14 23:44:50 +02:00
app().gui().term().restoreState(termState);
2017-10-24 21:52:26 +02:00
app().gui().hideModalOverlay();
2017-10-14 23:44:50 +02:00
app().gui().forceRender();
2017-07-10 22:03:46 +02:00
2017-10-14 20:03:23 +02:00
// -------------------------------------------------------------------------
// Save the note and clean up
// -------------------------------------------------------------------------
2017-07-10 22:03:46 +02:00
2017-10-14 20:03:23 +02:00
const updatedContent = await fs.readFile(tempFilePath, 'utf8');
if (updatedContent !== originalContent) {
let updatedNote = await Note.unserializeForEdit(updatedContent);
updatedNote.id = note.id;
await Note.save(updatedNote);
2017-10-17 23:56:22 +02:00
this.stdout(_('Note has been saved.'));
2017-10-14 20:03:23 +02:00
}
2017-10-14 23:44:50 +02:00
this.dispatch({
2017-11-08 23:22:24 +02:00
type: 'NOTE_SELECT',
id: note.id,
2017-10-14 23:44:50 +02:00
});
2017-10-14 20:03:23 +02:00
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;