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

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-07-26 23:07:27 +02:00
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { BaseModel } from 'lib/base-model.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
import { time } from 'lib/time-utils.js';
class Command extends BaseCommand {
usage() {
2017-08-04 18:02:43 +02:00
return 'todo <todo-command> <note-pattern>';
2017-07-26 23:07:27 +02:00
}
description() {
2017-08-04 18:02:43 +02:00
return _('<todo-command> can either be "toggle" or "clear". Use "toggle" to toggle the given todo between completed and uncompleted state (If the target is a regular note it will be converted to a todo). Use "clear" to convert the todo back to a regular note.');
2017-07-26 23:07:27 +02:00
}
async action(args) {
2017-08-04 18:02:43 +02:00
const action = args['todo-command'];
const pattern = args['note-pattern'];
2017-07-26 23:07:27 +02:00
const notes = await app().loadItems(BaseModel.TYPE_NOTE, pattern);
if (!notes.length) throw new Error(_('Cannot find "%s".', pattern));
for (let i = 0; i < notes.length; i++) {
const note = notes[i];
let toSave = {
id: note.id,
};
if (action == 'toggle') {
2017-07-31 20:47:06 +02:00
if (!note.is_todo) {
toSave = Note.toggleIsTodo(note);
} else {
toSave.todo_completed = note.todo_completed ? 0 : time.unixMs();
}
2017-07-26 23:07:27 +02:00
} else if (action == 'clear') {
toSave.is_todo = 0;
}
await Note.save(toSave);
}
}
}
module.exports = Command;