@cocreate/resize 1.12.0 → 1.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/demo/dashboard.html +451 -452
- package/package.json +1 -1
- package/src/index.js +674 -432
package/src/index.js
CHANGED
|
@@ -2,456 +2,698 @@
|
|
|
2
2
|
import observer from "@cocreate/observer";
|
|
3
3
|
import "./index.css";
|
|
4
4
|
|
|
5
|
-
const EVENTS = [
|
|
6
|
-
|
|
5
|
+
const EVENTS = [
|
|
6
|
+
"mousemove touchmove",
|
|
7
|
+
"mousedown touchstart",
|
|
8
|
+
"mouseup touchend"
|
|
9
|
+
];
|
|
10
|
+
const DIRECTIONS = ["left", "right", "top", "bottom"];
|
|
7
11
|
|
|
8
12
|
const styleEl = document.createElement("style");
|
|
9
|
-
styleEl.setAttribute(
|
|
13
|
+
styleEl.setAttribute("component", "resize");
|
|
10
14
|
document.head.appendChild(styleEl);
|
|
11
15
|
const styleSheet = styleEl.sheet;
|
|
12
16
|
|
|
13
17
|
const coCreateResize = {
|
|
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
|
-
|
|
18
|
+
selector: "", //'.resize',
|
|
19
|
+
resizers: [],
|
|
20
|
+
resizeWidgets: [],
|
|
21
|
+
|
|
22
|
+
init: function (handleObj) {
|
|
23
|
+
for (var handleKey in handleObj)
|
|
24
|
+
if (handleObj.hasOwnProperty(handleKey) && handleKey == "selector")
|
|
25
|
+
this.selector = handleObj[handleKey];
|
|
26
|
+
|
|
27
|
+
this.resizers = document.querySelectorAll(this.selector);
|
|
28
|
+
var _this = this;
|
|
29
|
+
this.resizers.forEach(function (resize, idx) {
|
|
30
|
+
let resizeWidget = new CoCreateResize(resize, handleObj);
|
|
31
|
+
_this.resizeWidgets[idx] = resizeWidget;
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
initElement: function (target) {
|
|
36
|
+
let resizeWidget = new CoCreateResize(target, {
|
|
37
|
+
dragLeft: "[resize='left']",
|
|
38
|
+
dragRight: "[resize='right']",
|
|
39
|
+
dragTop: "[resize='top']",
|
|
40
|
+
dragBottom: "[resize='bottom']"
|
|
41
|
+
});
|
|
42
|
+
this.resizeWidgets[0] = resizeWidget;
|
|
43
|
+
}
|
|
40
44
|
};
|
|
41
45
|
|
|
42
46
|
function CoCreateResize(resizer, options) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
this.resizeWidget = resizer;
|
|
48
|
+
this.cornerSize = 10;
|
|
49
|
+
this.init(options);
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
CoCreateResize.prototype = {
|
|
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
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
53
|
+
init: function (handleObj) {
|
|
54
|
+
if (this.resizeWidget) {
|
|
55
|
+
this.initDrags = [];
|
|
56
|
+
this.checkCorners = [];
|
|
57
|
+
this.doDrags = [];
|
|
58
|
+
this.Drags = [];
|
|
59
|
+
|
|
60
|
+
//this is for grid
|
|
61
|
+
this.isGrid = this.detectGrid();
|
|
62
|
+
this.gridWidth = 0;
|
|
63
|
+
this.gridHeight = 0;
|
|
64
|
+
this.limitSpan = 0;
|
|
65
|
+
this.widthSpan = 0;
|
|
66
|
+
this.heightSpan = 0;
|
|
67
|
+
this.originClassAttribute = this.resizeWidget.getAttribute("class");
|
|
68
|
+
|
|
69
|
+
DIRECTIONS.map((d) => {
|
|
70
|
+
this.Drags[d] = this.resizeWidget.querySelector(
|
|
71
|
+
handleObj["drag" + d.charAt(0).toUpperCase() + d.slice(1)]
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
this.bindListeners();
|
|
76
|
+
this.initResize();
|
|
77
|
+
|
|
78
|
+
//this is just for grid system
|
|
79
|
+
this.getGridProperty();
|
|
80
|
+
window.addEventListener("resize", this.checkGridColumns.bind(this));
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
getGridProperty: function () {
|
|
85
|
+
if (!this.resizeWidget.parentNode) return;
|
|
86
|
+
let compStyles = window.getComputedStyle(this.resizeWidget.parentNode);
|
|
87
|
+
let gridColumns = compStyles.gridTemplateColumns;
|
|
88
|
+
let height = compStyles.gridAutoRows;
|
|
89
|
+
|
|
90
|
+
if (gridColumns !== "none") {
|
|
91
|
+
let width = gridColumns.split(" ");
|
|
92
|
+
this.limitSpan = width.length;
|
|
93
|
+
this.gridColumnGap = Number.parseInt(compStyles.gridColumnGap);
|
|
94
|
+
this.gridRowGap = Number.parseInt(compStyles.gridRowGap);
|
|
95
|
+
this.gridWidth = Number.parseFloat(width[0]);
|
|
96
|
+
this.gridHeight = Number.parseInt(height);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
checkGridColumns: function () {
|
|
101
|
+
if (
|
|
102
|
+
typeof this.resizeWidget !== "undefined" &&
|
|
103
|
+
this.resizeWidget.parentNode
|
|
104
|
+
) {
|
|
105
|
+
let compStylesOfParent = window.getComputedStyle(
|
|
106
|
+
this.resizeWidget.parentNode
|
|
107
|
+
);
|
|
108
|
+
let spans = 0;
|
|
109
|
+
let gridColumns = compStylesOfParent.gridTemplateColumns.split(" ");
|
|
110
|
+
let temp = gridColumns[0];
|
|
111
|
+
gridColumns.map((v) => {
|
|
112
|
+
if (v == temp) spans++;
|
|
113
|
+
});
|
|
114
|
+
this.limitSpan = spans;
|
|
115
|
+
if (this.resizeWidget.style["grid-column-end"]) {
|
|
116
|
+
let curClassAttributes = this.resizeWidget
|
|
117
|
+
.getAttribute("class")
|
|
118
|
+
.split(" ");
|
|
119
|
+
let prevColumnState =
|
|
120
|
+
curClassAttributes[curClassAttributes.length - 1].split(
|
|
121
|
+
"_"
|
|
122
|
+
);
|
|
123
|
+
let prevSpan = Number.parseInt(prevColumnState[1]);
|
|
124
|
+
let curColumnState =
|
|
125
|
+
this.resizeWidget.style["grid-column-end"].split(" ");
|
|
126
|
+
let curSpan = Number.parseInt(curColumnState[1]);
|
|
127
|
+
|
|
128
|
+
let span = 0;
|
|
129
|
+
if (this.limitSpan >= prevSpan) span = prevSpan;
|
|
130
|
+
else if (prevSpan > this.limitSpan) span = this.limitSpan;
|
|
131
|
+
else span = Math.min(curSpan, this.limitSpan);
|
|
132
|
+
|
|
133
|
+
this.resizeWidget.style["grid-column-end"] = "span " + span;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
initResize: function () {
|
|
139
|
+
DIRECTIONS.map((d) => {
|
|
140
|
+
if (this.Drags[d])
|
|
141
|
+
this.addListenerMulti(
|
|
142
|
+
this.Drags[d],
|
|
143
|
+
EVENTS[0],
|
|
144
|
+
this.checkCorners[d]
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
checkDragImplementation: function (e, from, to, offset, fcur, scur) {
|
|
150
|
+
if (e.touches) e = e.touches[0];
|
|
151
|
+
let other;
|
|
152
|
+
if (to == "top") other = "bottom";
|
|
153
|
+
else if (to == "bottom") other = "top";
|
|
154
|
+
else if (to == "left") other = "right";
|
|
155
|
+
else if (to == "right") other = "left";
|
|
156
|
+
this.removeListenerMulti(
|
|
157
|
+
this.Drags[from],
|
|
158
|
+
EVENTS[1],
|
|
159
|
+
this.initDrags[from]
|
|
160
|
+
);
|
|
161
|
+
this.removeListenerMulti(
|
|
162
|
+
this.Drags[from],
|
|
163
|
+
EVENTS[1],
|
|
164
|
+
this.initDrags[to]
|
|
165
|
+
);
|
|
166
|
+
this.removeListenerMulti(
|
|
167
|
+
this.Drags[from],
|
|
168
|
+
EVENTS[1],
|
|
169
|
+
this.initDrags[other]
|
|
170
|
+
);
|
|
171
|
+
this.addListenerMulti(
|
|
172
|
+
this.Drags[from],
|
|
173
|
+
EVENTS[1],
|
|
174
|
+
this.initDrags[from]
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
if (offset < this.cornerSize && this.Drags[to]) {
|
|
178
|
+
this.Drags[from].style.cursor = fcur;
|
|
179
|
+
this.addListenerMulti(
|
|
180
|
+
this.Drags[from],
|
|
181
|
+
EVENTS[1],
|
|
182
|
+
this.initDrags[to]
|
|
183
|
+
);
|
|
184
|
+
} else {
|
|
185
|
+
this.Drags[from].style.cursor = scur;
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
initDrag: function (e, idx) {
|
|
190
|
+
e.preventDefault();
|
|
191
|
+
|
|
192
|
+
if (!this.resizeWidget.hasAttribute("resizing")) {
|
|
193
|
+
this.resizeWidget.setAttribute("resizing", "");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
let selector = document.defaultView.getComputedStyle(this.resizeWidget);
|
|
197
|
+
styleSheet.insertRule("iframe {pointer-events: none}", 0);
|
|
198
|
+
|
|
199
|
+
if (idx == "top" || idx == "bottom") {
|
|
200
|
+
this.startTop = parseInt(selector.top, 10);
|
|
201
|
+
this.startHeight = parseInt(selector.height, 10);
|
|
202
|
+
|
|
203
|
+
if (e.touches) this.startY = e.touches[0].clientY;
|
|
204
|
+
else this.startY = e.clientY;
|
|
205
|
+
} else {
|
|
206
|
+
this.startLeft = parseInt(selector.left, 10);
|
|
207
|
+
this.startRight = parseInt(selector.right, 10);
|
|
208
|
+
this.startWidth = parseInt(selector.width, 10);
|
|
209
|
+
|
|
210
|
+
if (e.touches) this.startX = e.touches[0].clientX;
|
|
211
|
+
else this.startX = e.clientX;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
this.addListenerMulti(
|
|
215
|
+
document.documentElement,
|
|
216
|
+
EVENTS[0],
|
|
217
|
+
this.doDrags[idx]
|
|
218
|
+
);
|
|
219
|
+
this.addListenerMulti(
|
|
220
|
+
document.documentElement,
|
|
221
|
+
EVENTS[2],
|
|
222
|
+
this.stopDrag
|
|
223
|
+
);
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
initTopDrag: function (e) {
|
|
227
|
+
this.initDrag(e, "top");
|
|
228
|
+
},
|
|
229
|
+
initBottomDrag: function (e) {
|
|
230
|
+
this.initDrag(e, "bottom");
|
|
231
|
+
},
|
|
232
|
+
initLeftDrag: function (e) {
|
|
233
|
+
this.initDrag(e, "left");
|
|
234
|
+
},
|
|
235
|
+
initRightDrag: function (e) {
|
|
236
|
+
this.initDrag(e, "right");
|
|
237
|
+
},
|
|
238
|
+
|
|
239
|
+
//this is just for grid system
|
|
240
|
+
setRowEnd: function (height) {
|
|
241
|
+
if (this.gridHeight) {
|
|
242
|
+
this.heightSpan = Math.ceil(
|
|
243
|
+
(height - (this.heightSpan - 1) * this.gridRowGap) /
|
|
244
|
+
this.gridHeight
|
|
245
|
+
);
|
|
246
|
+
this.resizeWidget.style["grid-row-end"] = "span " + this.heightSpan;
|
|
247
|
+
this.collaborate({ rowEnd: this.heightSpan });
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
doTopDrag: function (e) {
|
|
252
|
+
let top, height;
|
|
253
|
+
|
|
254
|
+
if (e.touches) e = e.touches[0];
|
|
255
|
+
top = this.startTop + e.clientY - this.startY;
|
|
256
|
+
height = this.startHeight - e.clientY + this.startY;
|
|
257
|
+
|
|
258
|
+
if (height < 10) return;
|
|
259
|
+
if (!this.isGrid) {
|
|
260
|
+
this.resizeWidget.style.top = top + "px";
|
|
261
|
+
this.collaborate({ top, height });
|
|
262
|
+
}
|
|
263
|
+
this.resizeWidget.style.height = height + "px";
|
|
264
|
+
this.setRowEnd(height);
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
doBottomDrag: function (e) {
|
|
268
|
+
let height;
|
|
269
|
+
|
|
270
|
+
if (e.touches) e = e.touches[0];
|
|
271
|
+
|
|
272
|
+
height = this.startHeight + e.clientY - this.startY;
|
|
273
|
+
|
|
274
|
+
if (height < 10) return;
|
|
275
|
+
if (!this.isGrid) this.collaborate({ height });
|
|
276
|
+
|
|
277
|
+
this.resizeWidget.style.height = height + "px";
|
|
278
|
+
this.setRowEnd(height);
|
|
279
|
+
},
|
|
280
|
+
|
|
281
|
+
//this is just for grid system
|
|
282
|
+
setColumnEnd: function (width) {
|
|
283
|
+
if (this.gridWidth) {
|
|
284
|
+
let spans = Math.ceil(
|
|
285
|
+
(width - (this.widthSpan - 1) * this.gridColumnGap) /
|
|
286
|
+
this.gridWidth
|
|
287
|
+
);
|
|
288
|
+
this.widthSpan = spans > this.limitSpan ? this.limitSpan : spans;
|
|
289
|
+
this.resizeWidget.style["grid-column-end"] =
|
|
290
|
+
"span " + this.widthSpan;
|
|
291
|
+
this.collaborate({ columnEnd: this.widthSpan });
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
doLeftDrag: function (e) {
|
|
296
|
+
let left, right, width;
|
|
297
|
+
if (e.touches) e = e.touches[0];
|
|
298
|
+
left = this.startLeft + e.clientX - this.startX;
|
|
299
|
+
right = this.startRight;
|
|
300
|
+
width = this.startWidth - e.clientX + this.startX;
|
|
301
|
+
|
|
302
|
+
if (width < 10) return;
|
|
303
|
+
if (!this.isGrid) {
|
|
304
|
+
this.resizeWidget.style.left = "";
|
|
305
|
+
this.resizeWidget.style.right = right + "px";
|
|
306
|
+
this.collaborate({ left, width });
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
this.resizeWidget.style.width = width + "px";
|
|
310
|
+
this.setColumnEnd(width);
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
doRightDrag: function (e) {
|
|
314
|
+
let width;
|
|
315
|
+
if (e.touches) e = e.touches[0];
|
|
316
|
+
width = this.startWidth + e.clientX - this.startX;
|
|
317
|
+
if (width < 10) return;
|
|
318
|
+
|
|
319
|
+
if (!this.isGrid) this.collaborate({ width });
|
|
320
|
+
|
|
321
|
+
this.resizeWidget.style.width = width + "px";
|
|
322
|
+
this.setColumnEnd(width);
|
|
323
|
+
},
|
|
324
|
+
|
|
325
|
+
detectGrid: function () {
|
|
326
|
+
let isGrid = false;
|
|
327
|
+
if (this.resizeWidget.parentNode) {
|
|
328
|
+
let compStyles = window.getComputedStyle(
|
|
329
|
+
this.resizeWidget.parentNode
|
|
330
|
+
);
|
|
331
|
+
isGrid = compStyles.display === "grid";
|
|
332
|
+
}
|
|
333
|
+
this.isGrid = isGrid;
|
|
334
|
+
return isGrid;
|
|
335
|
+
},
|
|
336
|
+
|
|
337
|
+
stopDrag: function (e) {
|
|
338
|
+
styleSheet.deleteRule(0);
|
|
339
|
+
if (this.resizeWidget.hasAttribute("resizing")) {
|
|
340
|
+
this.resizeWidget.removeAttribute("resizing", "");
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
//this is just for grid system
|
|
344
|
+
if (this.detectGrid()) {
|
|
345
|
+
this.resizeWidget.style.width = null;
|
|
346
|
+
this.resizeWidget.style.height = null;
|
|
347
|
+
this.resizeWidget.style.left = null;
|
|
348
|
+
this.resizeWidget.style.top = null;
|
|
349
|
+
|
|
350
|
+
if (this.widthSpan)
|
|
351
|
+
this.resizeWidget.setAttribute(
|
|
352
|
+
"class",
|
|
353
|
+
this.originClassAttribute +
|
|
354
|
+
" grid-column-end:span_" +
|
|
355
|
+
this.widthSpan
|
|
356
|
+
);
|
|
357
|
+
if (this.heightSpan)
|
|
358
|
+
this.resizeWidget.setAttribute(
|
|
359
|
+
"class",
|
|
360
|
+
this.originClassAttribute +
|
|
361
|
+
" grid-row-end:span_" +
|
|
362
|
+
this.heightSpan
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
DIRECTIONS.map((d) => {
|
|
367
|
+
this.removeListenerMulti(
|
|
368
|
+
document.documentElement,
|
|
369
|
+
EVENTS[0],
|
|
370
|
+
this.doDrags[d]
|
|
371
|
+
);
|
|
372
|
+
});
|
|
373
|
+
this.removeListenerMulti(
|
|
374
|
+
document.documentElement,
|
|
375
|
+
EVENTS[2],
|
|
376
|
+
this.stopDrag
|
|
377
|
+
);
|
|
378
|
+
},
|
|
379
|
+
checkLeftCorners: function (e) {
|
|
380
|
+
let offset =
|
|
381
|
+
e.clientY -
|
|
382
|
+
this.getDistance(this.Drags["left"], true) +
|
|
383
|
+
document.documentElement.scrollTop;
|
|
384
|
+
if (offset > 10) {
|
|
385
|
+
offset =
|
|
386
|
+
this.getDistance(this.Drags["left"], true) +
|
|
387
|
+
this.Drags["left"].clientHeight -
|
|
388
|
+
e.clientY -
|
|
389
|
+
document.documentElement.scrollTop;
|
|
390
|
+
this.checkDragImplementation(
|
|
391
|
+
e,
|
|
392
|
+
"left",
|
|
393
|
+
"bottom",
|
|
394
|
+
offset,
|
|
395
|
+
"ne-resize",
|
|
396
|
+
"e-resize"
|
|
397
|
+
);
|
|
398
|
+
} else {
|
|
399
|
+
this.checkDragImplementation(
|
|
400
|
+
e,
|
|
401
|
+
"left",
|
|
402
|
+
"top",
|
|
403
|
+
offset,
|
|
404
|
+
"se-resize",
|
|
405
|
+
"e-resize"
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
},
|
|
409
|
+
checkTopCorners: function (e) {
|
|
410
|
+
let offset =
|
|
411
|
+
e.clientX -
|
|
412
|
+
this.getDistance(this.Drags["top"], false) +
|
|
413
|
+
document.documentElement.scrollTop;
|
|
414
|
+
if (offset > 10) {
|
|
415
|
+
offset =
|
|
416
|
+
this.getDistance(this.Drags["top"], false) +
|
|
417
|
+
this.Drags["top"].clientWidth -
|
|
418
|
+
e.clientX -
|
|
419
|
+
document.documentElement.scrollLeft;
|
|
420
|
+
this.checkDragImplementation(
|
|
421
|
+
e,
|
|
422
|
+
"top",
|
|
423
|
+
"right",
|
|
424
|
+
offset,
|
|
425
|
+
"ne-resize",
|
|
426
|
+
"s-resize"
|
|
427
|
+
);
|
|
428
|
+
} else {
|
|
429
|
+
this.checkDragImplementation(
|
|
430
|
+
e,
|
|
431
|
+
"top",
|
|
432
|
+
"left",
|
|
433
|
+
offset,
|
|
434
|
+
"se-resize",
|
|
435
|
+
"s-resize"
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
checkBottomCorners: function (e) {
|
|
440
|
+
let offset =
|
|
441
|
+
e.clientX -
|
|
442
|
+
this.getDistance(this.Drags["bottom"], false) +
|
|
443
|
+
document.documentElement.scrollTop;
|
|
444
|
+
if (offset > 10) {
|
|
445
|
+
offset =
|
|
446
|
+
this.getDistance(this.Drags["bottom"], false) +
|
|
447
|
+
this.Drags["bottom"].clientWidth -
|
|
448
|
+
e.clientX -
|
|
449
|
+
document.documentElement.scrollLeft;
|
|
450
|
+
this.checkDragImplementation(
|
|
451
|
+
e,
|
|
452
|
+
"bottom",
|
|
453
|
+
"right",
|
|
454
|
+
offset,
|
|
455
|
+
"se-resize",
|
|
456
|
+
"s-resize"
|
|
457
|
+
);
|
|
458
|
+
} else {
|
|
459
|
+
this.checkDragImplementation(
|
|
460
|
+
e,
|
|
461
|
+
"bottom",
|
|
462
|
+
"left",
|
|
463
|
+
offset,
|
|
464
|
+
"ne-resize",
|
|
465
|
+
"s-resize"
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
},
|
|
469
|
+
checkRightCorners: function (e) {
|
|
470
|
+
let offset =
|
|
471
|
+
e.clientY -
|
|
472
|
+
this.getDistance(this.Drags["right"], true) +
|
|
473
|
+
document.documentElement.scrollTop;
|
|
474
|
+
if (offset > 10) {
|
|
475
|
+
offset =
|
|
476
|
+
this.getDistance(this.Drags["right"], true) +
|
|
477
|
+
this.Drags["right"].clientHeight -
|
|
478
|
+
e.clientY -
|
|
479
|
+
document.documentElement.scrollTop;
|
|
480
|
+
this.checkDragImplementation(
|
|
481
|
+
e,
|
|
482
|
+
"right",
|
|
483
|
+
"bottom",
|
|
484
|
+
offset,
|
|
485
|
+
"se-resize",
|
|
486
|
+
"e-resize"
|
|
487
|
+
);
|
|
488
|
+
} else {
|
|
489
|
+
this.checkDragImplementation(
|
|
490
|
+
e,
|
|
491
|
+
"right",
|
|
492
|
+
"top",
|
|
493
|
+
offset,
|
|
494
|
+
"ne-resize",
|
|
495
|
+
"e-resize"
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
|
|
500
|
+
bindListeners: function () {
|
|
501
|
+
this.doDrags["left"] = this.doLeftDrag.bind(this);
|
|
502
|
+
this.doDrags["top"] = this.doTopDrag.bind(this);
|
|
503
|
+
this.doDrags["right"] = this.doRightDrag.bind(this);
|
|
504
|
+
this.doDrags["bottom"] = this.doBottomDrag.bind(this);
|
|
505
|
+
|
|
506
|
+
this.stopDrag = this.stopDrag.bind(this);
|
|
507
|
+
|
|
508
|
+
this.checkCorners["left"] = this.checkLeftCorners.bind(this);
|
|
509
|
+
this.checkCorners["top"] = this.checkTopCorners.bind(this);
|
|
510
|
+
this.checkCorners["bottom"] = this.checkBottomCorners.bind(this);
|
|
511
|
+
this.checkCorners["right"] = this.checkRightCorners.bind(this);
|
|
512
|
+
|
|
513
|
+
this.initDrags["top"] = this.initTopDrag.bind(this);
|
|
514
|
+
this.initDrags["left"] = this.initLeftDrag.bind(this);
|
|
515
|
+
this.initDrags["bottom"] = this.initBottomDrag.bind(this);
|
|
516
|
+
this.initDrags["right"] = this.initRightDrag.bind(this);
|
|
517
|
+
},
|
|
518
|
+
|
|
519
|
+
getDistance: function (elem, flag) {
|
|
520
|
+
var location = 0;
|
|
521
|
+
if (!elem) return;
|
|
522
|
+
if (elem.offsetParent) {
|
|
523
|
+
do {
|
|
524
|
+
location += flag ? elem.offsetTop : elem.offsetLeft;
|
|
525
|
+
elem = elem.offsetParent;
|
|
526
|
+
} while (elem);
|
|
527
|
+
}
|
|
528
|
+
return location >= 0 ? location : 0;
|
|
529
|
+
},
|
|
530
|
+
|
|
531
|
+
// Bind multiiple events to a listener
|
|
532
|
+
addListenerMulti: function (element, eventNames, listener) {
|
|
533
|
+
var events = eventNames.split(" ");
|
|
534
|
+
for (var i = 0, iLen = events.length; i < iLen; i++) {
|
|
535
|
+
element.addEventListener(events[i], listener, { passive: false });
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
|
|
539
|
+
// Remove multiiple events from a listener
|
|
540
|
+
removeListenerMulti: function (element, eventNames, listener) {
|
|
541
|
+
var events = eventNames.split(" ");
|
|
542
|
+
for (var i = 0, iLen = events.length; i < iLen; i++) {
|
|
543
|
+
element.removeEventListener(events[i], listener, {
|
|
544
|
+
passive: false
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
|
|
549
|
+
collaborate: function ({ left, top, width, height, rowEnd, columnEnd }) {
|
|
550
|
+
if (window.CoCreate && window.CoCreate.text) {
|
|
551
|
+
let domTextEditor = this.resizeWidget.closest("[contenteditable]");
|
|
552
|
+
if (domTextEditor) {
|
|
553
|
+
if (left)
|
|
554
|
+
CoCreate.text.setStyle({
|
|
555
|
+
domTextEditor,
|
|
556
|
+
target: this.resizeWidget,
|
|
557
|
+
property: "left",
|
|
558
|
+
value: left + "px"
|
|
559
|
+
});
|
|
560
|
+
if (top)
|
|
561
|
+
CoCreate.text.setStyle({
|
|
562
|
+
domTextEditor,
|
|
563
|
+
target: this.resizeWidget,
|
|
564
|
+
property: "top",
|
|
565
|
+
value: top + "px"
|
|
566
|
+
});
|
|
567
|
+
if (width)
|
|
568
|
+
CoCreate.text.setStyle({
|
|
569
|
+
domTextEditor,
|
|
570
|
+
target: this.resizeWidget,
|
|
571
|
+
property: "width",
|
|
572
|
+
value: width + "px"
|
|
573
|
+
});
|
|
574
|
+
if (height)
|
|
575
|
+
CoCreate.text.setStyle({
|
|
576
|
+
domTextEditor,
|
|
577
|
+
target: this.resizeWidget,
|
|
578
|
+
property: "height",
|
|
579
|
+
value: height + "px"
|
|
580
|
+
});
|
|
581
|
+
if (rowEnd)
|
|
582
|
+
CoCreate.text.setStyle({
|
|
583
|
+
domTextEditor,
|
|
584
|
+
target: this.resizeWidget,
|
|
585
|
+
property: "grid-row-end",
|
|
586
|
+
value: "span " + rowEnd
|
|
587
|
+
});
|
|
588
|
+
if (columnEnd)
|
|
589
|
+
CoCreate.text.setStyle({
|
|
590
|
+
domTextEditor,
|
|
591
|
+
target: this.resizeWidget,
|
|
592
|
+
property: "grid-column-end",
|
|
593
|
+
value: "span " + columnEnd
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
438
598
|
};
|
|
439
599
|
|
|
600
|
+
// Function to auto-resize the element based on its content
|
|
601
|
+
function autoResizeElement(element) {
|
|
602
|
+
// Reset height to auto to calculate scrollHeight correctly
|
|
603
|
+
element.style.height = "auto";
|
|
604
|
+
|
|
605
|
+
let scrollHeight;
|
|
606
|
+
if (element.contentDocument) {
|
|
607
|
+
scrollHeight = element.contentDocument.body.scrollHeight;
|
|
608
|
+
} else {
|
|
609
|
+
// For other elements, get scrollHeight directly
|
|
610
|
+
scrollHeight = element.scrollHeight;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// Set the height of the element to the calculated scrollHeight
|
|
614
|
+
element.style.height = scrollHeight + "px";
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Observe the resize of the element
|
|
618
|
+
function observeElementResize(element) {
|
|
619
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
620
|
+
autoResizeElement(element);
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
resizeObserver.observe(element);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// Listen for input events on the entire document
|
|
627
|
+
function initAutoHeight(element) {
|
|
628
|
+
if (
|
|
629
|
+
element &&
|
|
630
|
+
!(element instanceof HTMLCollection) &&
|
|
631
|
+
!(element instanceof NodeList) &&
|
|
632
|
+
!Array.isArray(element)
|
|
633
|
+
)
|
|
634
|
+
element = [element];
|
|
635
|
+
else if (!element) {
|
|
636
|
+
element = document.querySelectorAll('[height="auto"]');
|
|
637
|
+
|
|
638
|
+
// Add event listener for iframes on load
|
|
639
|
+
document.addEventListener(
|
|
640
|
+
"input",
|
|
641
|
+
function (event) {
|
|
642
|
+
// Check if the target element has height="auto"
|
|
643
|
+
if (event.target.getAttribute("height") === "auto") {
|
|
644
|
+
autoResizeElement(event.target);
|
|
645
|
+
}
|
|
646
|
+
},
|
|
647
|
+
false
|
|
648
|
+
);
|
|
649
|
+
|
|
650
|
+
if (element.contentDocument) {
|
|
651
|
+
// Create a MutationObserver for changes within the iframe's body
|
|
652
|
+
const observer = new MutationObserver(autoResizeElement);
|
|
653
|
+
observer.observe(iframeDocument.body, {
|
|
654
|
+
childList: true,
|
|
655
|
+
subtree: true
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
// Handle iframe document changes (e.g., if src or srcdoc is modified)
|
|
659
|
+
element.addEventListener("load", () => {
|
|
660
|
+
observer.disconnect(); // Disconnect the previous observer
|
|
661
|
+
observeIframeContent(); // Reinitialize the observer on new document
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
for (let i = 0; i < element.length; i++) {
|
|
667
|
+
autoResizeElement(element[i]);
|
|
668
|
+
observeElementResize(element[i]); // Observe resize changes
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
initAutoHeight();
|
|
673
|
+
|
|
440
674
|
observer.init({
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
675
|
+
name: "CoCreateResize",
|
|
676
|
+
observe: ["addedNodes"],
|
|
677
|
+
selector: "[resizable]:not([resizable='false'])",
|
|
678
|
+
callback: function (mutation) {
|
|
679
|
+
coCreateResize.initElement(mutation.target);
|
|
680
|
+
}
|
|
681
|
+
});
|
|
446
682
|
|
|
447
|
-
|
|
683
|
+
observer.init({
|
|
684
|
+
name: "CoCreateResize",
|
|
685
|
+
observe: ["addedNodes"],
|
|
686
|
+
selector: "[height='auto']",
|
|
687
|
+
callback: function (mutation) {
|
|
688
|
+
initAutoHeight(mutation.target);
|
|
689
|
+
}
|
|
448
690
|
});
|
|
449
691
|
|
|
450
692
|
coCreateResize.init({
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
693
|
+
selector: "[resizable]:not([resizable='false'])",
|
|
694
|
+
dragLeft: "[resize='left']",
|
|
695
|
+
dragRight: "[resize='right']",
|
|
696
|
+
dragTop: "[resize='top']",
|
|
697
|
+
dragBottom: "[resize='bottom']"
|
|
456
698
|
});
|
|
457
699
|
export default coCreateResize;
|