-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathResizeMirror.ts
166 lines (144 loc) · 3.97 KB
/
ResizeMirror.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import AbstractPlugin from 'shared/AbstractPlugin';
import {requestNextAnimationFrame, AutoBind} from 'shared/utils';
import {FixMeAny} from 'shared/types';
import {MirrorCreatedEvent} from '../../Draggable/Plugins/Mirror/MirrorEvent';
import {
DragOverEvent,
DragOverContainerEvent,
isDragOverEvent,
} from '../../Draggable/DragEvent';
/**
* ResizeMirror default options
* @property {Object} defaultOptions
* @type {Object}
*/
export const defaultOptions = {};
/**
* The ResizeMirror plugin resizes the mirror element to the dimensions of the draggable element that the mirror is hovering over
* @class ResizeMirror
* @module ResizeMirror
* @extends AbstractPlugin
*/
export default class ResizeMirror extends AbstractPlugin {
private lastWidth: number;
private lastHeight: number;
private mirror: HTMLElement | null;
/**
* ResizeMirror constructor.
* @constructs ResizeMirror
* @param {Draggable} draggable - Draggable instance
*/
constructor(draggable: FixMeAny) {
super(draggable);
/**
* ResizeMirror remembers the last width when resizing the mirror
* to avoid additional writes to the DOM
* @property {number} lastWidth
*/
this.lastWidth = 0;
/**
* ResizeMirror remembers the last height when resizing the mirror
* to avoid additional writes to the DOM
* @property {number} lastHeight
*/
this.lastHeight = 0;
/**
* Keeps track of the mirror element
* @property {HTMLElement} mirror
*/
this.mirror = null;
}
/**
* Attaches plugins event listeners
*/
attach() {
this.draggable
.on('mirror:created', this.onMirrorCreated)
.on('drag:over', this.onDragOver)
.on('drag:over:container', this.onDragOver);
}
/**
* Detaches plugins event listeners
*/
detach() {
this.draggable
.off('mirror:created', this.onMirrorCreated)
.off('mirror:destroy', this.onMirrorDestroy)
.off('drag:over', this.onDragOver)
.off('drag:over:container', this.onDragOver);
}
/**
* Returns options passed through draggable
* @return {Object}
*/
getOptions() {
return this.draggable.options.resizeMirror || {};
}
/**
* Mirror created handler
* @param {MirrorCreatedEvent} mirrorEvent
* @private
*/
@AutoBind
private onMirrorCreated({mirror}: MirrorCreatedEvent) {
this.mirror = mirror;
}
/**
* Mirror destroy handler
* @param {MirrorDestroyEvent} mirrorEvent
* @private
*/
@AutoBind
private onMirrorDestroy() {
this.mirror = null;
}
/**
* Drag over handler
* @param {DragOverEvent | DragOverContainer} dragEvent
* @private
*/
@AutoBind
private onDragOver(dragEvent: DragOverEvent | DragOverContainerEvent) {
this.resize(dragEvent);
}
/**
* Resize function for
* @param {DragOverEvent | DragOverContainer} dragEvent
* @private
*/
private resize(dragEvent: DragOverEvent | DragOverContainerEvent) {
requestAnimationFrame(() => {
let over: HTMLElement | null = null;
const {overContainer} = dragEvent;
if (this.mirror == null || this.mirror.parentNode == null) {
return;
}
if (this.mirror.parentNode !== overContainer) {
overContainer.appendChild(this.mirror);
}
if (isDragOverEvent(dragEvent)) {
over = dragEvent.over;
}
const overElement =
over ||
this.draggable.getDraggableElementsForContainer(overContainer)[0];
if (!overElement) {
return;
}
requestNextAnimationFrame(() => {
const overRect = overElement.getBoundingClientRect();
if (
this.mirror == null ||
(this.lastHeight === overRect.height &&
this.lastWidth === overRect.width)
) {
return;
}
this.mirror.style.width = `${overRect.width}px`;
this.mirror.style.height = `${overRect.height}px`;
this.lastWidth = overRect.width;
this.lastHeight = overRect.height;
});
});
}
}