1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-cli/app/command-edit.ts

114 lines
3.6 KiB
TypeScript
Raw Normal View History

import * as fs from 'fs-extra';
import BaseCommand from './base-command';
import { splitCommandString } from '@joplin/utils';
import uuid from '@joplin/lib/uuid';
const { app } = require('./app.js');
import { _ } from '@joplin/lib/locale';
import Note from '@joplin/lib/models/Note';
import Setting from '@joplin/lib/models/Setting';
import BaseModel from '@joplin/lib/BaseModel';
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
public override usage() {
2017-08-04 19:51:01 +02:00
return 'edit <note>';
2017-07-10 22:03:46 +02:00
}
public override description() {
2017-07-18 20:21:03 +02:00
return _('Edit note.');
2017-07-10 22:03:46 +02:00
}
public override async action(args: any) {
let tempFilePath: string|null = 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>`'));
};
2017-07-10 22:03:46 +02:00
try {
2017-10-14 20:03:23 +02:00
// -------------------------------------------------------------------------
// Load note or create it if it doesn't exist
// -------------------------------------------------------------------------
const 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-12-26 12:38:53 +02:00
this.encryptionCheck(note);
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 = splitCommandString(editorPath);
2017-07-10 22:03:46 +02:00
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
2019-09-19 23:51:18 +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
app().gui().showModalOverlay(_('Starting to edit note. Close the editor to get back to the prompt.'));
await app().gui().forceRender();
const termState = app().gui().termSaveState();
const spawnSync = require('child_process').spawnSync;
const result = spawnSync(editorPath, editorArgs, { stdio: 'inherit' });
if (result.error) this.stdout(_('Error opening note in editor: %s', result.error.message));
2017-07-10 22:03:46 +02:00
app().gui().termRestoreState(termState);
app().gui().hideModalOverlay();
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) {
const updatedNote = await Note.unserializeForEdit(updatedContent);
2017-10-14 20:03:23 +02:00
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();
} 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;