2022-11-01 17:28:14 +02:00
const BaseCommand = require ( './base-command' ) . default ;
2017-11-03 02:09:34 +02:00
const { app } = require ( './app.js' ) ;
2020-11-07 17:59:37 +02:00
const { _ } = require ( '@joplin/lib/locale' ) ;
const BaseModel = require ( '@joplin/lib/BaseModel' ) . default ;
2021-01-22 19:41:11 +02:00
const Folder = require ( '@joplin/lib/models/Folder' ) . default ;
const Note = require ( '@joplin/lib/models/Note' ) . default ;
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
usage ( ) {
2022-09-05 13:37:51 +02:00
return 'mv <item> [notebook]' ;
2017-07-10 22:03:46 +02:00
}
description ( ) {
2022-10-30 20:37:58 +02:00
return _ ( 'Moves the given <item> to [notebook]' ) ;
2017-07-10 22:03:46 +02:00
}
async action ( args ) {
2022-09-05 13:37:51 +02:00
const pattern = args [ 'item' ] ;
2017-08-04 18:02:43 +02:00
const destination = args [ 'notebook' ] ;
2022-09-05 13:37:51 +02:00
let folder = null ;
2019-07-30 09:35:42 +02:00
2022-09-05 13:37:51 +02:00
if ( destination !== 'root' ) {
folder = await app ( ) . loadItem ( BaseModel . TYPE _FOLDER , destination ) ;
if ( ! folder ) throw new Error ( _ ( 'Cannot find "%s".' , destination ) ) ;
}
2017-07-10 22:03:46 +02:00
2022-09-05 13:37:51 +02:00
const destinationDuplicates = await Folder . search ( { titlePattern : destination , limit : 2 } ) ;
if ( destinationDuplicates . length > 1 ) {
2023-01-11 20:37:22 +02:00
throw new Error ( _ ( 'Ambiguous notebook "%s". Please use short notebook id instead - press "ti" to see the short notebook id' , destination ) ) ;
2022-09-05 13:37:51 +02:00
}
2017-07-10 22:03:46 +02:00
2022-09-05 13:37:51 +02:00
const itemFolder = await app ( ) . loadItem ( BaseModel . TYPE _FOLDER , pattern ) ;
if ( itemFolder ) {
const sourceDuplicates = await Folder . search ( { titlePattern : pattern , limit : 2 } ) ;
if ( sourceDuplicates . length > 1 ) {
throw new Error ( _ ( 'Ambiguous notebook "%s". Please use notebook id instead - press "ti" to see the short notebook id or use $b for current selected notebook' , pattern ) ) ;
}
if ( destination === 'root' ) {
await Folder . moveToFolder ( itemFolder . id , '' ) ;
} else {
await Folder . moveToFolder ( itemFolder . id , folder . id ) ;
}
} else {
const notes = await app ( ) . loadItems ( BaseModel . TYPE _NOTE , pattern ) ;
if ( notes . length === 0 ) throw new Error ( _ ( 'Cannot find "%s".' , pattern ) ) ;
for ( let i = 0 ; i < notes . length ; i ++ ) {
await Note . moveToFolder ( notes [ i ] . id , folder . id ) ;
}
2017-07-10 22:03:46 +02:00
}
}
}
2019-07-30 09:35:42 +02:00
module . exports = Command ;