-
Notifications
You must be signed in to change notification settings - Fork 207
feat(Upload): support draggable API #2141
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
Open
Wesley-0808
wants to merge
19
commits into
develop
Choose a base branch
from
wesley/feat/upload-draggable
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f756a2c
feat: api
Wesley-0808 4784ba7
chore: useUpload hooks
Wesley-0808 0d5e96d
chore: demo
Wesley-0808 c65d02b
chore: common
Wesley-0808 9bc191e
feat: draggable
Wesley-0808 9cb65e5
fix: test
Wesley-0808 63e5199
chore: update snapshot
github-actions[bot] e1bc279
chore: type and lint
Wesley-0808 870f2ac
Merge branch 'develop' into wesley/feat/upload-draggable
Wesley-0808 021d364
chore: hooks
Wesley-0808 18f65ed
Merge branch 'develop' into wesley/feat/upload-draggable
Wesley-0808 395f233
chore: update common
Wesley-0808 ee98224
chore: revert docs
Wesley-0808 d9a4b05
chore: click
Wesley-0808 59df22a
chore: type
Wesley-0808 b75dc85
fix: cr
Wesley-0808 7dd245a
chore: merge develop
github-actions[bot] f772ca3
chore: update snapshot
github-actions[bot] fa013eb
Merge branch 'develop' into wesley/feat/upload-draggable
Wesley-0808 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
Submodule _common
updated
4 files
| +0 −4 | docs/mobile/api/cascader.en-US.md | |
| +0 −4 | docs/mobile/api/cascader.md | |
| +4 −4 | style/mobile/components/tabs/_index.less | |
| +4 −0 | style/mobile/components/upload/_index.less |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import { ComputedRef, ref, Ref, onMounted, onBeforeUnmount } from 'vue'; | ||
| import type { TdUploadProps, UploadFile } from '../type'; | ||
|
|
||
| interface UploadFileWithUid extends UploadFile { | ||
| __uid?: string; | ||
| } | ||
|
|
||
| export default function useDrag( | ||
| props: TdUploadProps, | ||
| setUploadValue: (value: UploadFile[], ...args: any[]) => void, | ||
| uploadClass: ComputedRef<string>, | ||
| listRef: Ref<HTMLElement | undefined>, | ||
| ) { | ||
| const dragIndex = ref<number>(-1); | ||
| let pendingTargetIndex = -1; | ||
|
|
||
| // 缓存网格布局信息 | ||
| let baseData = { | ||
| itemWidth: 0, | ||
| itemHeight: 0, | ||
| columns: 0, | ||
| wrapLeft: 0, | ||
| wrapTop: 0, | ||
| }; | ||
|
|
||
| // 生成文件唯一ID,用于拖拽排序识别 | ||
| const getFileId = (file: UploadFileWithUid) => { | ||
| if (file.__uid) return file.__uid; | ||
| const uid = `u_${Math.random().toString(36).slice(2, 9)}`; | ||
| file.__uid = uid; | ||
| return uid; | ||
| }; | ||
|
|
||
| const updateDragBaseData = () => { | ||
| const listEl = listRef.value; | ||
| const itemEl = listEl?.querySelector(`.${uploadClass.value}__item`); | ||
| if (listEl && itemEl) { | ||
| const rect = listEl.getBoundingClientRect(); | ||
| const itemWidth = (itemEl as HTMLElement).offsetWidth; | ||
| const itemHeight = (itemEl as HTMLElement).offsetHeight; | ||
| const style = window.getComputedStyle(listEl); | ||
| const gapX = parseInt(style.columnGap || style.gap, 10) || 8; | ||
| const gapY = parseInt(style.rowGap || style.gap, 10) || 16; | ||
| const paddingLeft = parseInt(style.paddingLeft, 10) || 0; | ||
| const paddingTop = parseInt(style.paddingTop, 10) || 0; | ||
|
|
||
| baseData = { | ||
| itemWidth: itemWidth + gapX, | ||
| itemHeight: itemHeight + gapY, | ||
| columns: | ||
| Math.round( | ||
| (rect.width - paddingLeft - (parseInt(style.paddingRight, 10) || 0) + gapX) / (itemWidth + gapX), | ||
| ) || 4, | ||
| wrapLeft: rect.left + paddingLeft, | ||
| wrapTop: rect.top + paddingTop, | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| let resizeObserver: ResizeObserver | null = null; | ||
|
|
||
| onMounted(() => { | ||
| updateDragBaseData(); | ||
| if (listRef.value && window.ResizeObserver) { | ||
| resizeObserver = new ResizeObserver(() => { | ||
| updateDragBaseData(); | ||
| }); | ||
| resizeObserver.observe(listRef.value); | ||
| } | ||
| }); | ||
|
|
||
| onBeforeUnmount(() => { | ||
| resizeObserver?.disconnect(); | ||
| resizeObserver = null; | ||
| }); | ||
|
|
||
| const performSort = (index: number, displayFiles: UploadFile[], e: TouchEvent) => { | ||
| if (index === -1 || index === dragIndex.value) { | ||
| pendingTargetIndex = -1; | ||
| return; | ||
| } | ||
| if (index === pendingTargetIndex) return; | ||
|
|
||
| pendingTargetIndex = index; | ||
| const sourceIdx = dragIndex.value; | ||
| const newFiles = [...displayFiles]; | ||
|
|
||
| if (sourceIdx === -1 || sourceIdx === index) return; | ||
|
|
||
| const [item] = newFiles.splice(sourceIdx, 1); | ||
| newFiles.splice(index, 0, item); | ||
|
|
||
| setUploadValue(newFiles, { e, trigger: 'sort', index, file: item, files: newFiles }); | ||
| if (dragIndex.value !== -1) { | ||
| dragIndex.value = index; | ||
| } | ||
| }; | ||
|
|
||
| // 根据网格坐标计算目标索引 | ||
| const onTouchmove = (e: TouchEvent, displayFiles: UploadFile[]) => { | ||
| if (!props.draggable || dragIndex.value === -1) return; | ||
| if (e.cancelable) e.preventDefault(); | ||
|
|
||
| const touch = e.touches[0]; | ||
| const { itemWidth, itemHeight, columns, wrapLeft, wrapTop } = baseData; | ||
|
|
||
| const x = touch.clientX - wrapLeft; | ||
| const y = touch.clientY - wrapTop; | ||
|
|
||
| let curX = Math.floor(x / itemWidth); | ||
| const curY = Math.floor(y / itemHeight); | ||
|
|
||
| // 将 X 限制在列范围内,确保在行边界内滑动 | ||
| curX = Math.max(0, Math.min(columns - 1, curX)); | ||
|
|
||
| let targetIndex = curX + columns * curY; | ||
| targetIndex = Math.max(0, Math.min(displayFiles.length - 1, targetIndex)); | ||
|
|
||
| if (targetIndex !== dragIndex.value) { | ||
| const targetX = targetIndex % columns; | ||
| const targetY = Math.floor(targetIndex / columns); | ||
| const sourceX = dragIndex.value % columns; | ||
| const sourceY = Math.floor(dragIndex.value / columns); | ||
|
|
||
| const xProgress = x / itemWidth - targetX; | ||
| const yProgress = y / itemHeight - targetY; | ||
|
|
||
| let shouldSort = false; | ||
| if (targetY !== sourceY) { | ||
| shouldSort = targetY > sourceY ? yProgress > 0.5 : yProgress < 0.5; | ||
| } else { | ||
| shouldSort = targetX > sourceX ? xProgress > 0.5 : xProgress < 0.5; | ||
| } | ||
|
|
||
| if (shouldSort) { | ||
| performSort(targetIndex, displayFiles, e); | ||
| } else if (pendingTargetIndex !== -1) { | ||
| pendingTargetIndex = -1; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const resetDrag = () => { | ||
| dragIndex.value = -1; | ||
| pendingTargetIndex = -1; | ||
| }; | ||
|
|
||
| return { | ||
| onTouchstart: (e: TouchEvent, index: number) => { | ||
| if (!props.draggable) return; | ||
| dragIndex.value = index; | ||
| }, | ||
| onTouchmove, | ||
| onTouchend: resetDrag, | ||
| dragIndex, | ||
| getFileId, | ||
| }; | ||
| } |
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.
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.