2020-11-07 17:59:37 +02:00
|
|
|
import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Folder from '@joplin/lib/models/Folder';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const declaration: CommandDeclaration = {
|
2020-07-03 23:32:39 +02:00
|
|
|
name: 'moveToFolder',
|
|
|
|
label: () => _('Move to notebook'),
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const runtime = (comp: any): CommandRuntime => {
|
2020-07-03 23:32:39 +02:00
|
|
|
return {
|
2020-11-12 21:13:28 +02:00
|
|
|
execute: async (context: CommandContext, noteIds: string[] = null) => {
|
2020-10-18 22:52:10 +02:00
|
|
|
noteIds = noteIds || context.state.selectedNoteIds;
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const folders: any[] = await Folder.sortFolderTree();
|
|
|
|
const startFolders: any[] = [];
|
2020-07-03 23:32:39 +02:00
|
|
|
const maxDepth = 15;
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const addOptions = (folders: any[], depth: number) => {
|
2020-07-03 23:32:39 +02:00
|
|
|
for (let i = 0; i < folders.length; i++) {
|
|
|
|
const folder = folders[i];
|
|
|
|
startFolders.push({ key: folder.id, value: folder.id, label: folder.title, indentDepth: depth });
|
|
|
|
if (folder.children) addOptions(folder.children, (depth + 1) < maxDepth ? depth + 1 : maxDepth);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
addOptions(folders, 0);
|
|
|
|
|
|
|
|
comp.setState({
|
|
|
|
promptOptions: {
|
|
|
|
label: _('Move to notebook:'),
|
|
|
|
inputType: 'dropdown',
|
|
|
|
value: '',
|
|
|
|
autocomplete: startFolders,
|
2020-11-12 21:13:28 +02:00
|
|
|
onClose: async (answer: any) => {
|
2021-08-27 17:58:42 +02:00
|
|
|
if (answer) {
|
2020-07-03 23:32:39 +02:00
|
|
|
for (let i = 0; i < noteIds.length; i++) {
|
|
|
|
await Note.moveToFolder(noteIds[i], answer.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
comp.setState({ promptOptions: null });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2023-07-16 18:42:42 +02:00
|
|
|
enabledCondition: 'someNotesSelected && !noteIsReadOnly',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
};
|