11import { shadowId } from './constant' ;
2- import { changeDragState , createInsertSortableShadow , insertDragElement } from './utils' ;
2+ import { changeDragState , createInsertSortableShadow , insertDragElement , judgeMouseIsInSortableArea ,
3+ exchangeShadowPosition , sameOriginExchangeElementPosition } from './utils' ;
34
45
56export default {
@@ -8,41 +9,58 @@ export default {
89 * @param el
910 * @description
1011 * 此命令用于将元素变为可放置的元素并且支持排序
11- * dragover:
12- * 1、说明此时进入可排序放置的区域
13- * 2、此时应该生成相应的可排序的shadow
14- * drop:
15- * 1、可放置区域里如果没有拖拽元素,直接放置
16- * 2、可放置区域里如果有其他的可拖拽元素,需要对比放置到正确的位置上
12+ * 功能分析
13+ * 1、非自身区域内拖动,生成shadow
14+ * 2、自身区域内拖动,不生成shadow
15+ * 实现分析(根据ng-devui)
16+ * shadow的生成规则
17+ * shadow的生成位置
18+ * 待思考问题
19+ * 1、整个拖拽过程中,是否有必要添加节流防抖?
1720 */
1821 mounted ( el : HTMLElement , binding : unknown ) : void {
22+ const self = el ;
1923 el . addEventListener ( 'dragover' , function ( event : DragEvent ) {
2024 event . preventDefault ( ) ;
21- const targetNode : any = event . target ;
25+ const dropArea = [ ... el . childNodes ] [ 1 ] ;
2226 const dragId = binding . instance . $root . identity ;
23- if ( ! binding . instance . $root . dropElement ) {
24- binding . instance . $root . dropElement = [ ...el . childNodes ] [ 1 ] ;
27+ if ( document . getElementById ( dragId ) ?. dataset . parent === 'sort-drop' ) {
28+ // 说明此时是同源操作(不需要生成shadow)
29+ // sameOriginExchangeElementPosition(event, [...dropArea.children], dragId, dropArea);
30+ return ;
2531 }
26- changeDragState ( document . getElementById ( binding . instance . $root . identity ) , binding . instance . $root . identity , 'true' , 'false' , 'true' , 'false' , 'true' , 'false' ) ;
27- const { dragover, shouldCreateShadow } = document . getElementById ( dragId ) . dataset ;
28- if ( dragover == 'true' ) {
29- if ( shouldCreateShadow == 'true' ) {
30- createInsertSortableShadow ( [ ...targetNode . children ] [ 1 ] , event , dragId ) ;
31- }
32+ // 需要判定是否存在阴影,否则会出现严重的抖动情况
33+ if ( ! document . getElementById ( 'shadow0611' ) && [ ...self . childNodes [ 1 ] . children ] . length === 0 ) {
34+ createInsertSortableShadow ( [ ...self . childNodes ] [ 1 ] , event , dragId ) ;
35+ } else if ( [ ...self . childNodes [ 1 ] . children ] . length >= 1 ) {
36+ // 说明此时想要进行换位操作
37+ // 需要得到此时shadow的位置,遇到shadow则跳过,否则当鼠标出现在shadow上时,会出现严重的抖动操作
38+ exchangeShadowPosition ( event , [ ...self . childNodes [ 1 ] . children ] , dragId , self . childNodes [ 1 ] ) ;
3239 }
33-
3440 } ) ;
3541 el . addEventListener ( 'drop' , function ( event : DragEvent ) {
3642 // 获取可放置区域
3743 const dropArea = [ ...el . childNodes ] [ 1 ] ;
3844 const dragId = binding . instance . $root . identity ;
39- dropArea . removeChild ( document . getElementById ( shadowId ) ) ;
40- if ( [ ...dropArea . childNodes ] . length == 0 ) {
41- dropArea . appendChild ( document . getElementById ( dragId ) ) ;
42- } else {
43- insertDragElement ( dropArea , dragId , event ) ;
45+ if ( document . getElementById ( dragId ) ?. dataset . parent === 'sort-drop' ) {
46+ // 说明是同源(不产生shadow,直接替换)
47+ sameOriginExchangeElementPosition ( event , [ ...dropArea . children ] , dragId , dropArea ) ;
48+ return ;
49+ }
50+ // 判断鼠标是否处于drop区域
51+ if ( document . getElementById ( shadowId ) ) {
52+ dropArea . replaceChild ( document . getElementById ( dragId ) , document . getElementById ( shadowId ) ) ;
53+ if ( document . getElementById ( dragId ) ) {
54+ document . getElementById ( dragId ) . dataset . parent = 'sort-drop' ;
55+ }
56+ }
57+ } ) ;
58+ // 主要用来移除shadow
59+ el . addEventListener ( 'dragleave' , function ( event : Event ) {
60+ const dropArea = [ ...el . childNodes ] [ 1 ] ;
61+ if ( document . getElementById ( shadowId ) && ! judgeMouseIsInSortableArea ( event , el ) ) {
62+ dropArea . removeChild ( document . getElementById ( shadowId ) ) ;
4463 }
45- changeDragState ( document . getElementById ( dragId ) , dragId , 'false' , 'false' , 'false' , 'true' , 'false' , 'false' ) ;
4664 } ) ;
4765 }
48- } ;
66+ } ;
0 commit comments