1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-26 12:02:59 +02:00

mv folder in cli

This commit is contained in:
Laurent Cozic 2017-07-17 19:19:01 +00:00
parent b124aabe2b
commit 63772629cf
5 changed files with 47 additions and 20 deletions

View File

@ -66,6 +66,15 @@ class Application {
this.updatePrompt(); this.updatePrompt();
} }
async guessTypeAndLoadItem(pattern, options = null) {
let type = BaseModel.TYPE_NOTE;
if (pattern.indexOf('/') === 0) {
type = BaseModel.TYPE_FOLDER;
pattern = pattern.substr(1);
}
return this.loadItem(type, pattern, options);
}
async loadItem(type, pattern, options = null) { async loadItem(type, pattern, options = null) {
let output = await this.loadItems(type, pattern, options); let output = await this.loadItems(type, pattern, options);
return output.length ? output[0] : null; return output.length ? output[0] : null;

View File

@ -18,10 +18,7 @@ class Command extends BaseCommand {
} }
async action(args) { async action(args) {
let folder = await Folder.save({ title: args['notebook'] }, { let folder = await Folder.save({ title: args['notebook'] }, { userSideValidation: true });
duplicateCheck: true,
reservedTitleCheck: true,
});
app().switchCurrentFolder(folder); app().switchCurrentFolder(folder);
} }

View File

@ -9,11 +9,11 @@ import { autocompleteItems } from './autocomplete.js';
class Command extends BaseCommand { class Command extends BaseCommand {
usage() { usage() {
return 'mv <pattern> <notebook>'; return 'mv <pattern> <destination>';
} }
description() { description() {
return 'Moves the notes matching <pattern> to <notebook>.'; return 'Moves the notes matching <pattern> to <destination>. If <pattern> is a note, it will be moved to the notebook <destination>. If <pattern> is a notebook, it will be renamed to <destination>.';
} }
autocomplete() { autocomplete() {
@ -22,15 +22,25 @@ class Command extends BaseCommand {
async action(args) { async action(args) {
const pattern = args['pattern']; const pattern = args['pattern'];
const destination = args['destination'];
const folder = await Folder.loadByField('title', args['notebook']); const item = await app().guessTypeAndLoadItem(pattern);
if (!folder) throw new Error(_('No notebook "%s"', args['notebook']));
const notes = await app().loadItems(BaseModel.TYPE_NOTE, pattern); if (!item) throw new Error(_('No item matches pattern "%s"', pattern));
if (!notes.length) throw new Error(_('No note matches this pattern: "%s"', pattern));
for (let i = 0; i < notes.length; i++) { if (item.type_ == BaseModel.TYPE_FOLDER) {
await Note.moveToFolder(notes[i].id, folder.id); await Folder.save({ id: item.id, title: destination }, { userSideValidation: true });
await app().refreshCurrentFolder();
} else { // TYPE_NOTE
const folder = await Folder.loadByField('title', destination);
if (!folder) throw new Error(_('No notebook "%s"', destination));
const notes = await app().loadItems(BaseModel.TYPE_NOTE, pattern);
if (!notes.length) throw new Error(_('No note matches this pattern: "%s"', pattern));
for (let i = 0; i < notes.length; i++) {
await Note.moveToFolder(notes[i].id, folder.id);
}
} }
} }

View File

@ -66,10 +66,7 @@ class FolderScreenComponent extends BaseScreenComponent {
let folder = Object.assign({}, this.state.folder); let folder = Object.assign({}, this.state.folder);
try { try {
folder = await Folder.save(folder, { folder = await Folder.save(folder, { userSideValidation: true });
duplicateCheck: true,
reservedTitleCheck: true,
});
reg.scheduleSync(); reg.scheduleSync();
} catch (error) { } catch (error) {

View File

@ -114,15 +114,29 @@ class Folder extends BaseItem {
// These "duplicateCheck" and "reservedTitleCheck" should only be done when a user is // These "duplicateCheck" and "reservedTitleCheck" should only be done when a user is
// manually creating a folder. They shouldn't be done for example when the folders // manually creating a folder. They shouldn't be done for example when the folders
// are being synced to avoid any strange side-effect. Technically it's possible to // are being synced to avoid any strange side-effects. Technically it's possible to
// have folders and notes with duplicate titles (or no title), or with reserved words, // have folders and notes with duplicate titles (or no title), or with reserved words.
static async save(o, options = null) { static async save(o, options = null) {
if (options && options.duplicateCheck === true && o.title) { if (!options) options = {};
if (options.userSideValidation === true) {
if (!('duplicateCheck' in options)) options.duplicateCheck = true;
if (!('reservedTitleCheck' in options)) options.reservedTitleCheck = true;
if (!('stripLeftSlashes' in options)) options.stripLeftSlashes = true;
}
if (options.stripLeftSlashes === true && o.title) {
while (o.title.length && (o.title[0] == '/' || o.title[0] == "\\")) {
o.title = o.title.substr(1);
}
}
if (options.duplicateCheck === true && o.title) {
let existingFolder = await Folder.loadByTitle(o.title); let existingFolder = await Folder.loadByTitle(o.title);
if (existingFolder && existingFolder.id != o.id) throw new Error(_('A notebook with this title already exists: "%s"', o.title)); if (existingFolder && existingFolder.id != o.id) throw new Error(_('A notebook with this title already exists: "%s"', o.title));
} }
if (options && options.reservedTitleCheck === true && o.title) { if (options.reservedTitleCheck === true && o.title) {
if (o.title == Folder.conflictFolderTitle()) throw new Error(_('Notebooks cannot be named "%s", which is a reserved title.', o.title)); if (o.title == Folder.conflictFolderTitle()) throw new Error(_('Notebooks cannot be named "%s", which is a reserved title.', o.title));
} }