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

96 lines
2.8 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", "list", or "notetags" to assign or remove [tag] from [note], to list notes associated with [tag], or to list tags associated with [note]. 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;
const 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));
if (!tag) tag = await Tag.saveNested({}, 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) {
const 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 {
const tags = await Tag.all();
tags.map(tag => {
this.stdout(Tag.getCachedFullTitle(tag.id));
});
2017-07-10 22:03:46 +02:00
}
} else if (command == 'notetags') {
if (args.tag) {
2019-10-30 11:54:32 +02:00
const note = await app().loadItem(BaseModel.TYPE_NOTE, args.tag);
if (!note) throw new Error(_('Cannot find "%s".', args.tag));
const tags = await Tag.tagsByNoteId(note.id);
tags.map(tag => {
this.stdout(tag.title);
});
} else {
throw new Error(_('Cannot find "%s".', ''));
}
2017-07-10 22:03:46 +02:00
} else {
throw new Error(_('Invalid command: "%s"', command));
2017-07-10 22:03:46 +02:00
}
}
}
module.exports = Command;