1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/CliClient/app/command-tag.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

const { BaseCommand } = require('./base-command.js');
const { app } = require('./app.js');
const { _ } = require('lib/locale.js');
2017-12-14 20:12:14 +02:00
const Tag = require('lib/models/Tag.js');
const BaseModel = require('lib/BaseModel.js');
const { time } = require('lib/time-utils.js');
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
usage() {
2017-08-04 18:02:43 +02:00
return 'tag <tag-command> [tag] [note]';
2017-07-10 22:03:46 +02:00
}
description() {
return _('<tag-command> can be "add", "remove" or "list" to assign or remove [tag] from [note], or to list the notes associated with [tag]. The command `tag list` can be used to list all the tags (use -l for long option).');
2017-07-10 22:03:46 +02:00
}
options() {
return [
['-l, --long', _('Use long list format. Format is ID, NOTE_COUNT (for notebook), DATE, TODO_CHECKED (for to-dos), TITLE')],
];
}
2017-07-10 22:03:46 +02:00
async action(args) {
let tag = null;
let options = args.options;
2017-07-10 22:03:46 +02:00
if (args.tag) tag = await app().loadItem(BaseModel.TYPE_TAG, args.tag);
2017-07-11 20:17:23 +02:00
let notes = [];
if (args.note) {
notes = await app().loadItems(BaseModel.TYPE_NOTE, args.note);
}
2017-07-10 22:03:46 +02:00
2017-08-04 18:02:43 +02:00
const command = args['tag-command'];
2017-07-10 22:03:46 +02:00
2017-08-04 18:02:43 +02:00
if (command == 'remove' && !tag) throw new Error(_('Cannot find "%s".', args.tag));
if (command == 'add') {
2017-07-18 20:21:03 +02:00
if (!notes.length) throw new Error(_('Cannot find "%s".', args.note));
2017-11-12 01:13:14 +02:00
if (!tag) tag = await Tag.save({ title: args.tag }, { userSideValidation: true });
2017-07-11 20:17:23 +02:00
for (let i = 0; i < notes.length; i++) {
await Tag.addNote(tag.id, notes[i].id);
}
2017-08-04 18:02:43 +02:00
} else if (command == 'remove') {
2017-07-18 20:21:03 +02:00
if (!tag) throw new Error(_('Cannot find "%s".', args.tag));
if (!notes.length) throw new Error(_('Cannot find "%s".', args.note));
2017-07-11 20:17:23 +02:00
for (let i = 0; i < notes.length; i++) {
await Tag.removeNote(tag.id, notes[i].id);
}
2017-08-04 18:02:43 +02:00
} else if (command == 'list') {
2017-07-10 22:03:46 +02:00
if (tag) {
let notes = await Tag.notes(tag.id);
notes.map((note) => {
let line = '';
if (options.long) {
line += BaseModel.shortId(note.id);
line += ' ';
line += time.formatMsToLocal(note.user_updated_time);
line += ' ';
}
if (note.is_todo) {
line += '[';
if (note.todo_completed) {
line += 'X';
} else {
line += ' ';
}
line += '] ';
} else {
line += ' ';
}
line += note.title;
this.stdout(line);
});
2017-07-10 22:03:46 +02:00
} else {
let tags = await Tag.all();
2017-10-07 18:30:27 +02:00
tags.map((tag) => { this.stdout(tag.title); });
2017-07-10 22:03:46 +02:00
}
} else {
2017-08-04 18:02:43 +02:00
throw new Error(_('Invalid command: "%s"', command));
2017-07-10 22:03:46 +02:00
}
}
}
module.exports = Command;