mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Handle editing note
This commit is contained in:
parent
e323a86563
commit
9d630ab0ca
@ -200,7 +200,16 @@ commands.push({
|
||||
usage: 'edit <title>',
|
||||
description: 'Edit note.',
|
||||
action: async function(args, end) {
|
||||
try {
|
||||
|
||||
let watcher = null;
|
||||
const onFinishedEditing = () => {
|
||||
if (watcher) watcher.close();
|
||||
vorpal.show();
|
||||
this.log(_('Done editing.'));
|
||||
end();
|
||||
}
|
||||
|
||||
try {
|
||||
let title = args['title'];
|
||||
|
||||
if (!currentFolder) throw new Error(_('No active notebook.'));
|
||||
@ -210,44 +219,45 @@ commands.push({
|
||||
|
||||
let editorPath = getTextEditorPath();
|
||||
let editorArgs = editorPath.split(' ');
|
||||
|
||||
editorPath = editorArgs[0];
|
||||
editorArgs = [editorArgs[1]];
|
||||
editorArgs = editorArgs.splice(1);
|
||||
|
||||
let content = await Note.serializeForEdit(note);
|
||||
|
||||
const temp = require('temp');
|
||||
let tempFilePath = Setting.value('profileDir') + '/tmp/' + Note.systemPath(note);
|
||||
editorArgs.push(tempFilePath);
|
||||
|
||||
const spawn = require('child_process').spawn;
|
||||
|
||||
this.log(_('Starting to edit note...'));
|
||||
this.log(_('Starting to edit note. Close the editor to get back to the prompt.'));
|
||||
|
||||
vorpal.hide();
|
||||
|
||||
temp.track();
|
||||
await fs.writeFile(tempFilePath, content);
|
||||
|
||||
temp.open(Setting.value('appName'), async (error, info) => {
|
||||
if (error) throw error;
|
||||
let watchTimeout = null;
|
||||
watcher = fs.watch(tempFilePath, (eventType, filename) => {
|
||||
// We need a timeout because for each change to the file, multiple events are generated.
|
||||
|
||||
await fs.writeFile(info.path, content);
|
||||
if (watchTimeout) return;
|
||||
|
||||
fs.watch(info.path, (eventType, filename) => {
|
||||
console.info('cHANGE...');
|
||||
});
|
||||
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);
|
||||
});
|
||||
|
||||
// https://github.com/dthree/vorpal/issues/190
|
||||
|
||||
editorArgs.push(info.path);
|
||||
|
||||
const childProcess = spawn(editorPath, editorArgs, { stdio: 'inherit' });
|
||||
|
||||
childProcess.on('exit', (error, code) => {
|
||||
this.log(_('Done editing note.'));
|
||||
vorpal.show();
|
||||
end();
|
||||
});
|
||||
const childProcess = spawn(editorPath, editorArgs, { stdio: 'inherit' });
|
||||
childProcess.on('exit', (error, code) => {
|
||||
onFinishedEditing();
|
||||
});
|
||||
} catch(error) {
|
||||
this.log(error);
|
||||
end();
|
||||
onFinishedEditing();
|
||||
}
|
||||
},
|
||||
autocomplete: autocompleteItems,
|
||||
@ -853,8 +863,6 @@ function getTextEditorPath() {
|
||||
}
|
||||
|
||||
process.stdin.on('keypress', (_, key) => {
|
||||
console.info(_, key);
|
||||
|
||||
if (key && key.name === 'return') {
|
||||
updatePrompt();
|
||||
}
|
||||
@ -900,12 +908,15 @@ async function main() {
|
||||
|
||||
const profileDir = initArgs.profileDir ? initArgs.profileDir : os.homedir() + '/.config/' + Setting.value('appName');
|
||||
const resourceDir = profileDir + '/resources';
|
||||
const tempDir = profileDir + '/tmp';
|
||||
|
||||
Setting.setConstant('profileDir', profileDir);
|
||||
Setting.setConstant('resourceDir', resourceDir);
|
||||
Setting.setConstant('tempDir', tempDir);
|
||||
|
||||
await fs.mkdirp(profileDir, 0o755);
|
||||
await fs.mkdirp(resourceDir, 0o755);
|
||||
await fs.mkdirp(tempDir, 0o755);
|
||||
|
||||
logger.addTarget('file', { path: profileDir + '/log.txt' });
|
||||
logger.setLevel(logLevel);
|
||||
|
@ -31,7 +31,6 @@
|
||||
"sqlite3": "^3.1.8",
|
||||
"string-to-stream": "^1.1.0",
|
||||
"tcp-port-used": "^0.1.2",
|
||||
"temp": "^0.8.3",
|
||||
"uuid": "^3.0.1",
|
||||
"vorpal": "^1.12.0"
|
||||
},
|
||||
|
@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
CLIENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
bash $CLIENT_DIR/build.sh && NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes2 --stack-trace-enabled --redraw-disabled "$@"
|
||||
bash $CLIENT_DIR/build.sh && NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes2 --stack-trace-enabled --log-level debug --redraw-disabled "$@"
|
||||
#bash $CLIENT_DIR/build.sh && NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes import-enex --fuzzy-matching /home/laurent/Desktop/afaire.enex afaire
|
||||
#bash $CLIENT_DIR/build.sh && NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes import-enex --fuzzy-matching /home/laurent/Desktop/Laurent.enex laurent
|
@ -35,11 +35,6 @@ class BaseItem extends BaseModel {
|
||||
let d = BaseItem.syncItemDefinitions_[i];
|
||||
if (Number(item) == d.type) return this.getClass(d.className);
|
||||
}
|
||||
// if (Number(item) === BaseModel.TYPE_NOTE) return this.getClass('Note');
|
||||
// if (Number(item) === BaseModel.TYPE_FOLDER) return this.getClass('Folder');
|
||||
// if (Number(item) === BaseModel.TYPE_RESOURCE) return this.getClass('Resource');
|
||||
// if (Number(item) === BaseModel.TYPE_TAG) return this.getClass('Tag');
|
||||
// if (Number(item) === BaseModel.TYPE_NOTE_TAG) return this.getClass('NoteTag');
|
||||
throw new Error('Unknown type: ' + item);
|
||||
}
|
||||
}
|
||||
@ -144,7 +139,7 @@ class BaseItem extends BaseModel {
|
||||
|
||||
if ('body' in item) {
|
||||
output.push(item.body);
|
||||
output.push('');
|
||||
if (shownKeys.length) output.push('');
|
||||
}
|
||||
|
||||
for (let i = 0; i < shownKeys.length; i++) {
|
||||
@ -169,6 +164,7 @@ class BaseItem extends BaseModel {
|
||||
let output = {};
|
||||
let state = 'readingProps';
|
||||
let body = [];
|
||||
|
||||
for (let i = lines.length - 1; i >= 0; i--) {
|
||||
let line = lines[i];
|
||||
|
||||
|
@ -24,6 +24,11 @@ class Note extends BaseItem {
|
||||
return super.serialize(note, 'note', []);
|
||||
}
|
||||
|
||||
static async unserializeForEdit(content) {
|
||||
content += "\n\ntype_: " + BaseModel.TYPE_NOTE;
|
||||
return super.unserialize(content);
|
||||
}
|
||||
|
||||
static modelType() {
|
||||
return BaseModel.TYPE_NOTE;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user