|
56 | 56 | :class=" |
57 | 57 | showShared && hasPermission(EditionConst.IS_EE, 'OR') ? 'tree-height-shared' : 'tree-height' |
58 | 58 | " |
| 59 | + @node-drop="handleDrop" |
59 | 60 | @handleNodeClick="handleNodeClick" |
60 | 61 | :current-node-key="currentNodeKey" |
61 | 62 | > |
@@ -231,6 +232,149 @@ const MoreFilledPermission = (node: any) => { |
231 | 232 |
|
232 | 233 | const emit = defineEmits(['handleNodeClick', 'refreshTree']) |
233 | 234 |
|
| 235 | +const handleDrop = (draggingNode: any, dropNode: any, dropType: string) => { |
| 236 | + const dragData = draggingNode.data |
| 237 | + const dropData = dropNode.data |
| 238 | + console.log(draggingNode, dropNode, dropType) |
| 239 | +
|
| 240 | + const oldParentId = dragData.parent_id |
| 241 | + let newParentId: string |
| 242 | + if (dropType === 'inner') { |
| 243 | + newParentId = dropData.id |
| 244 | + } else if (dropType === 'prev' || dropType === 'next') { |
| 245 | + newParentId = dropData.parent_id |
| 246 | + } else { |
| 247 | + newParentId = dropData.parent_id |
| 248 | + } |
| 249 | +
|
| 250 | + const isCrossNode: boolean = oldParentId !== newParentId |
| 251 | +
|
| 252 | + if (isCrossNode) { |
| 253 | + const obj = { |
| 254 | + ...dragData, |
| 255 | + parent_id: newParentId, |
| 256 | + } |
| 257 | + folderApi |
| 258 | + .putFolder(dragData.id, props.source, obj, loading) |
| 259 | + .then(() => { |
| 260 | + emit('refreshTree') |
| 261 | +
|
| 262 | + MsgSuccess(t('common.saveSuccess')) |
| 263 | + }) |
| 264 | + .catch(() => { |
| 265 | + emit('refreshTree') |
| 266 | + }) |
| 267 | + } else { |
| 268 | + // 同级拖拽,直接放置 |
| 269 | + sortAfterDrop(dragData, dropData, dropType, newParentId) |
| 270 | + } |
| 271 | +} |
| 272 | +
|
| 273 | +const savePositions = debounce(doSave, 300) |
| 274 | +function sortAfterDrop( |
| 275 | + draggingNodeData: any, |
| 276 | + dropNodeData: any, |
| 277 | + dropType: string, |
| 278 | + newParentId: string, |
| 279 | +) { |
| 280 | + const sortMethod = localStorage.getItem(FOLDER_SORT_TYPE) |
| 281 | + currentSort.value = sortMethod as SortType |
| 282 | +
|
| 283 | + if (sortMethod === SORT_TYPES.CUSTOM) { |
| 284 | + const positions = getPositions(newParentId) |
| 285 | + let prevPos: number |
| 286 | + let nextPos: number |
| 287 | + if (dropType === 'inner') { |
| 288 | + const childrenPositions: number[] = Object.values(positions) |
| 289 | + if (childrenPositions.length === 0) { |
| 290 | + positions[draggingNodeData.id] = encode(1, 0) |
| 291 | + savePositions(newParentId, positions) |
| 292 | + return |
| 293 | + } |
| 294 | + // 放到最后 |
| 295 | + const maxPos = Math.max(...childrenPositions) |
| 296 | + positions[draggingNodeData.id] = maxPos + encode(1, 0) |
| 297 | + savePositions(newParentId, positions) |
| 298 | + } else if (dropType === 'before') { |
| 299 | + const { dropPos, sortedNodes, dropIndex } = getSortContext(positions, dropNodeData.id) |
| 300 | + const prevNode: any[] = sortedNodes[dropIndex - 1] |
| 301 | +
|
| 302 | + prevPos = prevNode ? prevNode[1] : 0 |
| 303 | + nextPos = dropPos |
| 304 | +
|
| 305 | + const newPos = mid(prevPos, nextPos) |
| 306 | +
|
| 307 | + if (newPos === null) { |
| 308 | + // rebalance |
| 309 | + rebalanceAndInsert(newParentId, draggingNodeData.id, dropNodeData.id, 'before') |
| 310 | + return |
| 311 | + } |
| 312 | + positions[draggingNodeData.id] = newPos |
| 313 | + savePositions(newParentId, positions) |
| 314 | + } else if (dropType === 'after') { |
| 315 | + const { dropPos, sortedNodes, dropIndex } = getSortContext(positions, dropNodeData.id) |
| 316 | + const nextNode: any[] = sortedNodes[dropIndex + 1] |
| 317 | +
|
| 318 | + prevPos = dropPos |
| 319 | + nextPos = nextNode ? nextNode[1] : Infinity |
| 320 | +
|
| 321 | + if (nextPos === Infinity) { |
| 322 | + positions[draggingNodeData.id] = prevPos + encode(1, 0) |
| 323 | + } else { |
| 324 | + const newPos = mid(prevPos, nextPos) |
| 325 | +
|
| 326 | + if (newPos === null) { |
| 327 | + rebalanceAndInsert(newParentId, draggingNodeData.id, dropNodeData.id, 'after') |
| 328 | + return |
| 329 | + } |
| 330 | + positions[draggingNodeData.id] = newPos |
| 331 | + } |
| 332 | +
|
| 333 | + savePositions(newParentId, positions) |
| 334 | + } |
| 335 | + } else { |
| 336 | + emit('refreshTree') |
| 337 | + } |
| 338 | +} |
| 339 | +
|
| 340 | +function rebalanceAndInsert( |
| 341 | + parentId: string, |
| 342 | + dragNodeId: string, |
| 343 | + dropNodeId: string, |
| 344 | + position: 'before' | 'after', |
| 345 | +) { |
| 346 | + const positions = getPositions(parentId) |
| 347 | + const sortedIds = Object.entries(positions) |
| 348 | + .sort((a: any[], b: any[]) => a[1] - b[1]) |
| 349 | + .map(([id]) => id) |
| 350 | +
|
| 351 | + const dragIndex = sortedIds.indexOf(dragNodeId) |
| 352 | + if (dragIndex > -1) { |
| 353 | + sortedIds.splice(dragIndex, 1) |
| 354 | + } |
| 355 | + const dropIndex = sortedIds.indexOf(dropNodeId) |
| 356 | + if (position === 'before') { |
| 357 | + sortedIds.splice(dropIndex, 0, dragNodeId) |
| 358 | + } else { |
| 359 | + sortedIds.splice(dropIndex + 1, 0, dragNodeId) |
| 360 | + } |
| 361 | +
|
| 362 | + const tempPositions: Record<string, number> = {} |
| 363 | + sortedIds.forEach((id, index) => { |
| 364 | + tempPositions[id] = index |
| 365 | + }) |
| 366 | +
|
| 367 | + const newPositions = rebalance(tempPositions) |
| 368 | + savePositionsInit(parentId, newPositions) |
| 369 | + // rebalance finish |
| 370 | +} |
| 371 | +function getSortContext(positions: Record<string, number>, nodeId: string) { |
| 372 | + const dropPos = positions[nodeId] |
| 373 | + const sortedNodes = Object.entries(positions).sort((a: any[], b: any[]) => a[1] - b[1]) |
| 374 | + const dropIndex = sortedNodes.findIndex(([id]) => id === nodeId) |
| 375 | +
|
| 376 | + return { dropPos, sortedNodes, dropIndex } |
| 377 | +} |
234 | 378 | const treeRef = ref() |
235 | 379 | const filterText = ref('') |
236 | 380 | const hoverNodeId = ref<string | undefined>('') |
|
0 commit comments