|
|
@@ -1,7 +1,8 @@
|
|
|
import { MovableArea, MovableView, View, ScrollView } from '@tarojs/components'
|
|
|
import './MoveOrderList.scss'
|
|
|
-import { useState } from 'react';
|
|
|
+import { useEffect, useRef, useState } from 'react';
|
|
|
import { ColorType } from '@/context/themes/color';
|
|
|
+import Taro from '@tarojs/taro';
|
|
|
|
|
|
export default function Component(props: { array: any, itemHeight: number }) {
|
|
|
const [list, setList] = useState(props.array)
|
|
|
@@ -12,13 +13,26 @@ export default function Component(props: { array: any, itemHeight: number }) {
|
|
|
const [startPageY, setStartPageY] = useState(0)
|
|
|
const [dragIndex, setDragIndex] = useState(-1)
|
|
|
const [scrollThreshold, setScrollThreshold] = useState(0.5)
|
|
|
- const [upperThreshold, setUpperThreshold] = useState(100)
|
|
|
- const [lowThreshold, setLowThreshold] = useState(100)
|
|
|
+ const upperThreshold = 100
|
|
|
+ const lowThreshold = 100
|
|
|
const [duration, setDuration] = useState(1000)
|
|
|
const [canScroll, setCanScroll] = useState(true)
|
|
|
- const [y,setY] = useState(0)
|
|
|
- const [changedIndex,setChangedIndex] = useState(-1)
|
|
|
- const [hiddenContent,setHiddenContent] = useState(false)
|
|
|
+ const [changedIndex, setChangedIndex] = useState(-1)
|
|
|
+ const [hiddenContent, setHiddenContent] = useState(false)
|
|
|
+
|
|
|
+ const [scrollTop, setScrollTop] = useState(0)
|
|
|
+ const [contentY, setContentY] = useState(0)
|
|
|
+ const [contentHeight, setContentHeight] = useState(0)
|
|
|
+ const ref = useRef(null)
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ const query = Taro.createSelectorQuery();
|
|
|
+ query.select('#myScrollView').boundingClientRect();
|
|
|
+ query.exec((res) => {
|
|
|
+ setContentY(res[0].top)
|
|
|
+ setContentHeight(res[0].height)
|
|
|
+ })
|
|
|
+ }, [])
|
|
|
|
|
|
function longPress(e, index) {
|
|
|
setChangedIndex(index)
|
|
|
@@ -28,43 +42,29 @@ export default function Component(props: { array: any, itemHeight: number }) {
|
|
|
setDragElement(list[index])
|
|
|
setMovaleViewY(e.target.offsetTop)
|
|
|
setCanScroll(false)
|
|
|
-
|
|
|
- setY(e.touches[0].pageY-index*props.itemHeight)
|
|
|
-
|
|
|
- console.log(e.touches[0].pageY)
|
|
|
- console.log(e.touches[0].pageY-index*props.itemHeight)
|
|
|
}
|
|
|
|
|
|
function touchMove(e) {
|
|
|
if (dragElement) {
|
|
|
let clientY = e.touches[0].clientY;
|
|
|
- //this._pageScroll(clientY);
|
|
|
+ pageScroll(clientY);
|
|
|
let pageY = e.touches[0].pageY;
|
|
|
let targetMoveDistance = pageY - startPageY
|
|
|
let movaleViewY2 = startOffsetY + targetMoveDistance
|
|
|
|
|
|
let targetIndex = computeFutureIndex(targetMoveDistance, dragIndex)
|
|
|
- if (targetIndex!==false && targetIndex!=changedIndex){
|
|
|
- var temps = swapListItems(list,targetIndex,changedIndex)
|
|
|
+ if (targetIndex !== false && targetIndex != changedIndex) {
|
|
|
+ var temps = swapListItems(list, targetIndex, changedIndex)
|
|
|
setList(temps)
|
|
|
setChangedIndex(targetIndex)
|
|
|
|
|
|
}
|
|
|
- // if (targetIndex !==false){
|
|
|
- // var temps = swapListItems(list,targetIndex,dragIndex)
|
|
|
- // setList(temps)
|
|
|
- // setDragIndex(targetIndex)
|
|
|
- // // setStartPageY(targetIndex*props.itemHeight+y)
|
|
|
- // }
|
|
|
- // console.log(targetIndex)
|
|
|
|
|
|
- // const futrueIndex = computeFutureIndex(targetMoveDistance, dragIndex)
|
|
|
- // if (futrueIndex !== false) {
|
|
|
- // var temps = list
|
|
|
- // temps.splice(futrueIndex, 0, temps.splice(dragIndex, 1)[0])
|
|
|
-
|
|
|
- // setList(temps)
|
|
|
- // }
|
|
|
+ if (targetIndex === false && targetIndex != changedIndex) {
|
|
|
+ var temps = swapListItems(list, dragIndex, changedIndex)
|
|
|
+ setList(temps)
|
|
|
+ setChangedIndex(dragIndex)
|
|
|
+ }
|
|
|
|
|
|
setMovaleViewY(movaleViewY2)
|
|
|
setLastTarget(targetIndex as any)
|
|
|
@@ -72,6 +72,32 @@ export default function Component(props: { array: any, itemHeight: number }) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ function pageScroll(clientY) {
|
|
|
+
|
|
|
+ // console.log(clientY,contentY,contentHeight)
|
|
|
+ if ((clientY - contentY) + upperThreshold >= contentHeight) {
|
|
|
+ console.log('ccc',(clientY - contentY) + props.itemHeight);
|
|
|
+ // var scroll = ref.current
|
|
|
+ // debugger
|
|
|
+ // (ref.current as any).scrollTo({scrollTop:100})
|
|
|
+ // setScrollTop((clientY - contentY) + props.itemHeight)
|
|
|
+ // setScrollTop(2200)
|
|
|
+ // setCanScroll(true)
|
|
|
+ Taro.pageScrollTo({
|
|
|
+ selector:'#myScrollView',
|
|
|
+ scrollTop:(clientY - contentY) + props.itemHeight,
|
|
|
+ duration:200
|
|
|
+ })
|
|
|
+ }
|
|
|
+ else if ((clientY - contentY) - lowThreshold <= 0) {
|
|
|
+ setScrollTop((clientY - contentY) - props.itemHeight)
|
|
|
+ // setCanScroll(true)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // setCanScroll(true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
function swapListItems<T>(list: T[], index1: number, index2: number): T[] {
|
|
|
const newList = [...list];
|
|
|
const temp = newList[index1];
|
|
|
@@ -84,21 +110,20 @@ export default function Component(props: { array: any, itemHeight: number }) {
|
|
|
|
|
|
function touchEnd(e) {
|
|
|
if (dragElement) {
|
|
|
- let pageY = e.changedTouches[0].pageY
|
|
|
- let targetMoveDistance = pageY - startPageY;
|
|
|
- /** 初始点击项索引 */
|
|
|
- let dragElementIndex = dragIndex;
|
|
|
- const futrueIndex = computeFutureIndex(targetMoveDistance, dragElementIndex)
|
|
|
- if (futrueIndex !== false) {
|
|
|
- var temps = list
|
|
|
- temps.splice(futrueIndex, 0, temps.splice(dragIndex, 1)[0])
|
|
|
+ // let pageY = e.changedTouches[0].pageY
|
|
|
+ // let targetMoveDistance = pageY - startPageY;
|
|
|
+ // let dragElementIndex = dragIndex;
|
|
|
+ // const futrueIndex = computeFutureIndex(targetMoveDistance, dragElementIndex)
|
|
|
+ // if (futrueIndex !== false) {
|
|
|
+ // var temps = list
|
|
|
+ // temps.splice(futrueIndex, 0, temps.splice(dragIndex, 1)[0])
|
|
|
|
|
|
- setList(temps)
|
|
|
- }
|
|
|
+ // setList(temps)
|
|
|
+ // }
|
|
|
setDragElement(null)
|
|
|
setLastTarget(null)
|
|
|
setDragIndex(-1)
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
setChangedIndex(-1)
|
|
|
setCanScroll(true)
|
|
|
@@ -147,7 +172,7 @@ export default function Component(props: { array: any, itemHeight: number }) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return <ScrollView scrollY={canScroll} style={{ height: 400 }}>
|
|
|
+ return <View id="myScrollView" ref={ref} style={{ height: 400,overflow:'scroll' }} catchMove={!canScroll}>
|
|
|
<MovableArea style={{ height: list.length * props.itemHeight }}>
|
|
|
{list.map((item, index) => {
|
|
|
var className = 'demo_item'
|
|
|
@@ -160,7 +185,7 @@ export default function Component(props: { array: any, itemHeight: number }) {
|
|
|
|
|
|
// console.log(className)
|
|
|
}
|
|
|
- return <View style={{ opacity: changedIndex == index && hiddenContent ? 0 : 1,height:props.itemHeight}}
|
|
|
+ return <View style={{ opacity: changedIndex == index && hiddenContent ? 0 : 1, height: props.itemHeight }}
|
|
|
className={className}
|
|
|
onLongPress={(e) => longPress(e, index)}
|
|
|
onTouchMove={(e) => touchMove(e)}
|
|
|
@@ -173,10 +198,10 @@ export default function Component(props: { array: any, itemHeight: number }) {
|
|
|
animation={false}
|
|
|
y={movaleViewY}
|
|
|
>
|
|
|
- <View className='demo_item' style={{backgroundColor:ColorType.fast}}>
|
|
|
+ <View className='demo_item' style={{ backgroundColor: ColorType.fast }}>
|
|
|
{dragElement}
|
|
|
</View>
|
|
|
</MovableView>
|
|
|
</MovableArea>
|
|
|
- </ScrollView>
|
|
|
+ </View>
|
|
|
}
|