Debian Codegraph @

packages/interaction/src/dnd/ElementMirror.ts

raw
1 import { removeElement, applyStyle, whenTransitionDone, Rect } from '@fullcalendar/core/internal'
2
3 /*
4 An effect in which an element follows the movement of a pointer across the screen.
5 The moving element is a clone of some other element.
6 Must call start + handleMove + stop.
7 */
8 export class ElementMirror {
9 isVisible: boolean = false // must be explicitly enabled
10 origScreenX?: number
11 origScreenY?: number
12 deltaX?: number
13 deltaY?: number
14 sourceEl: HTMLElement | null = null
15 mirrorEl: HTMLElement | null = null
16 sourceElRect: Rect | null = null // screen coords relative to viewport
17
18 // options that can be set directly by caller
19 parentNode: HTMLElement = document.body // HIGHLY SUGGESTED to set this to sidestep ShadowDOM issues
20 zIndex: number = 9999
21 revertDuration: number = 0
22
23 start(sourceEl: HTMLElement, pageX: number, pageY: number) {
24 this.sourceEl = sourceEl
25 this.sourceElRect = this.sourceEl.getBoundingClientRect()
26 this.origScreenX = pageX - window.scrollX
27 this.origScreenY = pageY - window.scrollY
28 this.deltaX = 0
29 this.deltaY = 0
30 this.updateElPosition()
31 }
32
33 handleMove(pageX: number, pageY: number) {
34 this.deltaX = (pageX - window.scrollX) - this.origScreenX!
35 this.deltaY = (pageY - window.scrollY) - this.origScreenY!
36 this.updateElPosition()
37 }
38
39 // can be called before start
40 setIsVisible(bool: boolean) {
41 if (bool) {
42 if (!this.isVisible) {
43 if (this.mirrorEl) {
44 this.mirrorEl.style.display = ''
45 }
46
47 this.isVisible = bool // needs to happen before updateElPosition
48 this.updateElPosition() // because was not updating the position while invisible
49 }
50 } else if (this.isVisible) {
51 if (this.mirrorEl) {
52 this.mirrorEl.style.display = 'none'
53 }
54
55 this.isVisible = bool
56 }
57 }
58
59 // always async
60 stop(needsRevertAnimation: boolean, callback: () => void) {
61 let done = () => {
62 this.cleanup()
63 callback()
64 }
65
66 if (
67 needsRevertAnimation &&
68 this.mirrorEl &&
69 this.isVisible &&
70 this.revertDuration && // if 0, transition won't work
71 (this.deltaX || this.deltaY) // if same coords, transition won't work
72 ) {
73 this.doRevertAnimation(done, this.revertDuration)
74 } else {
75 setTimeout(done, 0)
76 }
77 }
78
79 doRevertAnimation(callback: () => void, revertDuration: number) {
80 let mirrorEl = this.mirrorEl!
81 let finalSourceElRect = this.sourceEl!.getBoundingClientRect() // because autoscrolling might have happened
82
83 mirrorEl.style.transition =
84 'top ' + revertDuration + 'ms,' +
85 'left ' + revertDuration + 'ms'
86
87 applyStyle(mirrorEl, {
88 left: finalSourceElRect.left,
89 top: finalSourceElRect.top,
90 })
91
92 whenTransitionDone(mirrorEl, () => {
93 mirrorEl.style.transition = ''
94 callback()
95 })
96 }
97
98 cleanup() {
99 if (this.mirrorEl) {
100 removeElement(this.mirrorEl)
101 this.mirrorEl = null
102 }
103
104 this.sourceEl = null
105 }
106
107 updateElPosition() {
108 if (this.sourceEl && this.isVisible) {
109 applyStyle(this.getMirrorEl(), {
110 left: this.sourceElRect!.left + this.deltaX!,
111 top: this.sourceElRect!.top + this.deltaY!,
112 })
113 }
114 }
115
116 getMirrorEl(): HTMLElement {
117 let sourceElRect = this.sourceElRect!
118 let mirrorEl = this.mirrorEl
119
120 if (!mirrorEl) {
121 mirrorEl = this.mirrorEl = this.sourceEl!.cloneNode(true) as HTMLElement // cloneChildren=true
122
123 // we don't want long taps or any mouse interaction causing selection/menus.
124 // would use preventSelection(), but that prevents selectstart, causing problems.
125 mirrorEl.style.userSelect = 'none'
126 mirrorEl.style.webkitUserSelect = 'none'
127 mirrorEl.style.pointerEvents = 'none'
128
129 mirrorEl.classList.add('fc-event-dragging')
130
131 applyStyle(mirrorEl, {
132 position: 'fixed',
133 zIndex: this.zIndex,
134 visibility: '', // in case original element was hidden by the drag effect
135 boxSizing: 'border-box', // for easy width/height
136 width: sourceElRect.right - sourceElRect.left, // explicit height in case there was a 'right' value
137 height: sourceElRect.bottom - sourceElRect.top, // explicit width in case there was a 'bottom' value
138 right: 'auto', // erase and set width instead
139 bottom: 'auto', // erase and set height instead
140 margin: 0,
141 })
142
143 this.parentNode.appendChild(mirrorEl)
144 }
145
146 return mirrorEl
147 }
148 }