|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import type TreeItem from "./TreeItem"; |
| 3 | +import { ContextValue } from "./TreeProvider"; |
| 4 | +import Command from "./models/command"; |
| 5 | +import { CommandFolder } from "./models/command_folder"; |
| 6 | +import { ExecCommands } from "./models/exec_commands"; |
| 7 | +import { StateType } from "./models/etters"; |
| 8 | + |
| 9 | +export default class DragAndDropController implements vscode.TreeDragAndDropController<TreeItem> { |
| 10 | + dragMimeTypes = ["application/vnd.code.tree.save-commands-view"]; |
| 11 | + dropMimeTypes = ["application/vnd.code.tree.save-commands-view"]; |
| 12 | + |
| 13 | + constructor(private context: vscode.ExtensionContext) { } |
| 14 | + |
| 15 | + public async handleDrag( |
| 16 | + source: readonly TreeItem[], |
| 17 | + dataTransfer: vscode.DataTransfer, |
| 18 | + token: vscode.CancellationToken, |
| 19 | + ): Promise<void> { |
| 20 | + if (source.length > 0) { |
| 21 | + dataTransfer.set( |
| 22 | + "application/vnd.code.tree.save-commands-view", |
| 23 | + new vscode.DataTransferItem(source[0]), |
| 24 | + ); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public async handleDrop( |
| 29 | + target: TreeItem | undefined, |
| 30 | + dataTransfer: vscode.DataTransfer, |
| 31 | + token: vscode.CancellationToken, |
| 32 | + ): Promise<void> { |
| 33 | + const transferItem = dataTransfer.get("application/vnd.code.tree.save-commands-view"); |
| 34 | + if (!transferItem) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const sourceItem: TreeItem = transferItem.value; |
| 39 | + if (!sourceItem || !sourceItem.id) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Prevent moving between Global and Workspace for now to keep it simple and safe |
| 44 | + const targetStateType = target?.stateType ?? sourceItem.stateType; |
| 45 | + if (sourceItem.stateType !== targetStateType) { |
| 46 | + vscode.window.showWarningMessage("Moving items between Global and Workspace is not supported yet."); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Determine new parentFolderId |
| 51 | + let newParentFolderId: string | null = null; |
| 52 | + if (target) { |
| 53 | + if (target.contextValue === ContextValue.folder) { |
| 54 | + newParentFolderId = target.id ?? null; |
| 55 | + } else if (target.contextValue === ContextValue.command) { |
| 56 | + newParentFolderId = target.parentFolderId ?? null; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Prevent moving a folder into itself |
| 61 | + if (sourceItem.contextValue === ContextValue.folder && sourceItem.id === newParentFolderId) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + if (sourceItem.contextValue === ContextValue.command) { |
| 67 | + await this.moveCommand(sourceItem, newParentFolderId, target); |
| 68 | + } else if (sourceItem.contextValue === ContextValue.folder) { |
| 69 | + await this.moveFolder(sourceItem, newParentFolderId, target); |
| 70 | + } |
| 71 | + |
| 72 | + vscode.commands.executeCommand(ExecCommands.refreshView); |
| 73 | + } catch (error) { |
| 74 | + vscode.window.showErrorMessage(`Error moving item: ${error}`); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private async moveCommand( |
| 79 | + sourceItem: TreeItem, |
| 80 | + newParentFolderId: string | null, |
| 81 | + target: TreeItem | undefined, |
| 82 | + ) { |
| 83 | + const { etter } = Command.getEtterFromTreeContext(sourceItem); |
| 84 | + const commands = etter.getValue(this.context); |
| 85 | + const sourceIndex = commands.findIndex((c) => c.id === sourceItem.id); |
| 86 | + |
| 87 | + if (sourceIndex === -1) return; |
| 88 | + |
| 89 | + const [command] = commands.splice(sourceIndex, 1); |
| 90 | + command.parentFolderId = newParentFolderId; |
| 91 | + |
| 92 | + // If dropped on another command or folder, we can try to reorder |
| 93 | + if (target?.id && target.contextValue !== ContextValue.root) { |
| 94 | + const targetIndex = commands.findIndex((c) => c.id === target.id); |
| 95 | + if (targetIndex !== -1) { |
| 96 | + commands.splice(targetIndex, 0, command); |
| 97 | + } else { |
| 98 | + commands.push(command); |
| 99 | + } |
| 100 | + } else { |
| 101 | + commands.push(command); |
| 102 | + } |
| 103 | + |
| 104 | + // Update sortOrder for all commands to match array index |
| 105 | + for (let i = 0; i < commands.length; i++) { |
| 106 | + commands[i].sortOrder = i; |
| 107 | + } |
| 108 | + |
| 109 | + await etter.setValue(this.context, commands); |
| 110 | + } |
| 111 | + |
| 112 | + private async moveFolder( |
| 113 | + sourceItem: TreeItem, |
| 114 | + newParentFolderId: string | null, |
| 115 | + target: TreeItem | undefined, |
| 116 | + ) { |
| 117 | + const { etter } = CommandFolder.getEtterFromTreeContext(sourceItem); |
| 118 | + const folders = etter.getValue(this.context); |
| 119 | + const sourceIndex = folders.findIndex((f) => f.id === sourceItem.id); |
| 120 | + |
| 121 | + if (sourceIndex === -1) return; |
| 122 | + |
| 123 | + const [folder] = folders.splice(sourceIndex, 1); |
| 124 | + |
| 125 | + // Prevent nesting if it would cause a cycle (simple check: don't move into a descendant) |
| 126 | + // For now, let's just allow top level or direct parent update |
| 127 | + folder.parentFolderId = newParentFolderId; |
| 128 | + |
| 129 | + if (target?.id && target.contextValue !== ContextValue.root) { |
| 130 | + const targetIndex = folders.findIndex((f) => f.id === target.id); |
| 131 | + if (targetIndex !== -1) { |
| 132 | + folders.splice(targetIndex, 0, folder); |
| 133 | + } else { |
| 134 | + folders.push(folder); |
| 135 | + } |
| 136 | + } else { |
| 137 | + folders.push(folder); |
| 138 | + } |
| 139 | + |
| 140 | + // Update sortOrder for all folders to match array index |
| 141 | + for (let i = 0; i < folders.length; i++) { |
| 142 | + folders[i].sortOrder = i; |
| 143 | + } |
| 144 | + |
| 145 | + await etter.setValue(this.context, folders); |
| 146 | + } |
| 147 | +} |
0 commit comments