-
Notifications
You must be signed in to change notification settings - Fork 290
feat: tree节点拖拽功能 #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: tree节点拖拽功能 #184
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
packages/devui-vue/devui/tree/src/composables/use-draggable.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import { reactive, ref, watch } from 'vue' | ||
import type { Ref } from 'vue' | ||
import { TreeItem, IDropType, Nullable } from '../tree-types' | ||
import { cloneDeep } from 'lodash-es' | ||
|
||
const ACTIVE_NODE = 'devui-tree-node__content--value-wrapper' | ||
interface DragState { | ||
dropType?: 'prev' | 'next' | 'inner' | ||
draggingNode?: Nullable<HTMLElement> | ||
} | ||
|
||
export default function useDraggable( | ||
draggable: boolean, | ||
dropType: IDropType, | ||
node: Ref<Nullable<HTMLElement>>, | ||
renderData: Ref<TreeItem[]>, | ||
data: Ref<TreeItem[]> | ||
): any { | ||
const dragState = reactive<DragState>({ | ||
dropType: null, | ||
draggingNode: null, | ||
}) | ||
const treeIdMapValue = ref({}) | ||
watch( | ||
() => renderData.value, | ||
() => { | ||
treeIdMapValue.value = renderData.value.reduce((acc, cur) => ({ ...acc, [cur.id]: cur }), {}) | ||
}, | ||
{ deep: true, immediate: true } | ||
) | ||
|
||
const removeDraggingStyle = (target: Nullable<HTMLElement>) => { | ||
target | ||
.querySelector(`.${ACTIVE_NODE}`) | ||
?.classList.remove(...['prev', 'next', 'inner'].map((item) => `devui-drop-${item}`)) | ||
} | ||
|
||
const checkIsParent = (childNodeId: number | string, parentNodeId: number | string) => { | ||
const realParentId = treeIdMapValue.value[childNodeId].parentId | ||
if (realParentId === parentNodeId) { | ||
return true | ||
} else if (realParentId !== undefined) { | ||
return checkIsParent(realParentId, parentNodeId) | ||
} else { | ||
return false | ||
} | ||
} | ||
const handlerDropData = (dragNodeId: string | number, dropNodeId: string | number, dropType?: string) => { | ||
const cloneData = cloneDeep(data.value) | ||
let nowDragNode | ||
let nowDropNode | ||
const findDragAndDropNode = (curr: TreeItem[]) => { | ||
if (!Array.isArray(curr)) return | ||
curr.every((item, index) => { | ||
if (nowDragNode && nowDropNode) { | ||
return false | ||
} | ||
if (item.id === dragNodeId) { | ||
nowDragNode = { target: curr, index, item } | ||
} else if (item.id === dropNodeId) { | ||
nowDropNode = { target: curr, index, item } | ||
} | ||
if (!nowDragNode || !nowDropNode) { | ||
findDragAndDropNode(item.children) | ||
} | ||
return true | ||
}) | ||
} | ||
findDragAndDropNode(cloneData) | ||
if (nowDragNode && nowDropNode && dropType) { | ||
const cloneDrapNode = cloneDeep(nowDragNode.target[nowDragNode.index]) | ||
if (dropType === 'prev') { | ||
nowDropNode.target.splice(nowDropNode.index, 0, cloneDrapNode) | ||
} else if (dropType === 'next') { | ||
nowDropNode.target.splice(nowDropNode.index + 1, 0, cloneDrapNode) | ||
} else if (dropType === 'inner') { | ||
const children = nowDropNode.target[nowDropNode.index].children | ||
if (Array.isArray(children)) { | ||
children.unshift(cloneDrapNode) | ||
} else { | ||
nowDropNode.target[nowDropNode.index].children = [cloneDrapNode] | ||
} | ||
} | ||
const targetIndex = nowDragNode.target.indexOf(nowDragNode.item) | ||
if (targetIndex !== -1) { | ||
nowDragNode.target.splice(targetIndex, 1) | ||
} | ||
|
||
} | ||
|
||
return cloneData | ||
} | ||
const onDragstart = (event: DragEvent, treeNode: TreeItem) => { | ||
dragState.draggingNode = <Nullable<HTMLElement>>event.target | ||
const data = { | ||
type: 'tree-node', | ||
nodeId: treeNode.id | ||
} | ||
event.dataTransfer.setData('Text', JSON.stringify(data)) | ||
} | ||
const onDragover = (event: DragEvent) => { | ||
if (draggable) { | ||
event.preventDefault() | ||
event.dataTransfer.dropEffect = 'move' | ||
if (!node) { | ||
return | ||
} | ||
const dropPrev = dropType.dropPrev | ||
const dropNext = dropType.dropNext | ||
const dropInner = dropType.dropInner | ||
|
||
let innerDropType | ||
|
||
const prevPercent = dropPrev ? (dropInner ? 0.25 : dropNext ? 0.45 : 1) : -1 | ||
const nextPercent = dropNext ? (dropInner ? 0.75 : dropPrev ? 0.55 : 0) : 1 | ||
const currentTarget = <Nullable<HTMLElement>>event.currentTarget | ||
const targetPosition = currentTarget.getBoundingClientRect() | ||
const distance = event.clientY - targetPosition.top | ||
|
||
if (distance < targetPosition.height * prevPercent) { | ||
innerDropType = 'prev' | ||
} else if (distance > targetPosition.height * nextPercent) { | ||
innerDropType = 'next' | ||
} else if (dropInner) { | ||
innerDropType = 'inner' | ||
} else { | ||
innerDropType = undefined | ||
} | ||
removeDraggingStyle(currentTarget) | ||
if (innerDropType && innerDropType !== 'none') { | ||
currentTarget.querySelector(`.${ACTIVE_NODE}`)?.classList.add(`devui-drop-${innerDropType}`) | ||
} | ||
dragState.dropType = innerDropType | ||
} | ||
} | ||
const onDragleave = (event: DragEvent) => { | ||
removeDraggingStyle(<Nullable<HTMLElement>>event.currentTarget) | ||
} | ||
const onDrop = (event: DragEvent, dropNode: TreeItem) => { | ||
removeDraggingStyle(<Nullable<HTMLElement>>event.currentTarget) | ||
if (!draggable) { | ||
return | ||
} | ||
event.preventDefault() | ||
const transferDataStr = event.dataTransfer.getData('Text') | ||
if (transferDataStr) { | ||
try { | ||
const transferData = JSON.parse(transferDataStr) | ||
if (typeof transferData === 'object' && transferData.type === 'tree-node') { | ||
const dragNodeId = transferData.nodeId | ||
const isParent = checkIsParent(dropNode.id, dragNodeId) | ||
if (dragNodeId === dropNode.id || isParent) { | ||
return | ||
} | ||
let result | ||
if (dragState.dropType) { | ||
result = handlerDropData(dragNodeId, dropNode.id, dragState.dropType) | ||
} | ||
data.value = result | ||
} | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
} | ||
|
||
return { | ||
onDragstart, | ||
onDragover, | ||
onDragleave, | ||
onDrop, | ||
dragState | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.