2020-11-07 17:59:37 +02:00
|
|
|
import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
2024-09-14 23:18:07 +02:00
|
|
|
import Folder, { FolderEntityWithChildren } from '@joplin/lib/models/Folder';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
2024-09-14 23:18:07 +02:00
|
|
|
import BaseItem from '@joplin/lib/models/BaseItem';
|
|
|
|
import { ModelType } from '@joplin/lib/BaseModel';
|
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
|
|
|
|
const logger = Logger.create('commands/moveToFolder');
|
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'),
|
|
|
|
};
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
export const runtime = (comp: any): CommandRuntime => {
|
2020-07-03 23:32:39 +02:00
|
|
|
return {
|
2024-09-14 23:18:07 +02:00
|
|
|
execute: async (context: CommandContext, itemIds: string[] = null) => {
|
|
|
|
itemIds = itemIds || context.state.selectedNoteIds;
|
2020-10-18 22:52:10 +02:00
|
|
|
|
2024-09-14 23:18:07 +02:00
|
|
|
let allAreFolders = true;
|
|
|
|
const itemIdToType = new Map<string, ModelType>();
|
|
|
|
for (const id of itemIds) {
|
|
|
|
const item = await BaseItem.loadItemById(id);
|
|
|
|
itemIdToType.set(id, item.type_);
|
|
|
|
|
|
|
|
if (item.type_ !== ModelType.Folder) {
|
|
|
|
allAreFolders = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const folders = await Folder.sortFolderTree();
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const startFolders: any[] = [];
|
2020-07-03 23:32:39 +02:00
|
|
|
const maxDepth = 15;
|
|
|
|
|
2024-09-14 23:18:07 +02:00
|
|
|
// It's okay for folders (but not notes) to have no parent folder:
|
|
|
|
if (allAreFolders) {
|
|
|
|
startFolders.push({
|
|
|
|
key: '',
|
|
|
|
value: '',
|
|
|
|
label: _('None'),
|
|
|
|
indentDepth: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const addOptions = (folders: FolderEntityWithChildren[], depth: number) => {
|
2020-07-03 23:32:39 +02:00
|
|
|
for (let i = 0; i < folders.length; i++) {
|
|
|
|
const folder = folders[i];
|
2024-09-14 23:18:07 +02:00
|
|
|
|
|
|
|
// Disallow making a folder a subfolder of itself.
|
|
|
|
if (itemIdToType.has(folder.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
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,
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
onClose: async (answer: any) => {
|
2021-08-27 17:58:42 +02:00
|
|
|
if (answer) {
|
2024-09-14 23:18:07 +02:00
|
|
|
try {
|
|
|
|
const targetFolderId = answer.value;
|
|
|
|
for (const id of itemIds) {
|
|
|
|
if (id === targetFolderId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const itemType = itemIdToType.get(id);
|
|
|
|
if (itemType === ModelType.Note) {
|
|
|
|
await Note.moveToFolder(id, targetFolderId);
|
|
|
|
} else if (itemType === ModelType.Folder) {
|
|
|
|
await Folder.moveToFolder(id, targetFolderId);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Cannot move item with type ${itemType}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Error moving items', error);
|
|
|
|
void shim.showMessageBox(`Error: ${error}`);
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
comp.setState({ promptOptions: null });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2023-07-16 18:42:42 +02:00
|
|
|
enabledCondition: 'someNotesSelected && !noteIsReadOnly',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
};
|