emacroh5lib 1.0.18 → 1.0.21

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.
@@ -1,3 +1,955 @@
1
- <template src="./vue-drag-resize.html"></template>
2
- <script src="./vue-drag-resize.js"></script>
3
- <style src="./vue-drag-resize.css"></style>
1
+ <template>
2
+ <div class="vdr" :style="positionStyle"
3
+ :class="`${(active || isActive) ? 'active' : 'inactive'} ${contentClass ? contentClass : ''}`"
4
+ @mousedown="bodyDown($event)" @touchstart="bodyDown($event)" @touchend="up($event)">
5
+ <div :style="sizeStyle" class="content-container" ref="container">
6
+ <slot></slot>
7
+ </div>
8
+ <div v-for="stick in sticks" class="vdr-stick" :class="['vdr-stick-' + stick, isResizable ? '' : 'not-resizable']"
9
+ @mousedown.stop.prevent="stickDown(stick, $event)" @touchstart.stop.prevent="stickDown(stick, $event)"
10
+ :style="vdrStick(stick)">
11
+ </div>
12
+ </div>
13
+ </template>
14
+
15
+ <script lang="ts">
16
+ import { Component, Prop, Vue, Watch } from "vue-property-decorator";
17
+
18
+ const styleMapping: any = {
19
+ y: {
20
+ t: 'top',
21
+ m: 'marginTop',
22
+ b: 'bottom',
23
+ },
24
+ x: {
25
+ l: 'left',
26
+ m: 'marginLeft',
27
+ r: 'right',
28
+ },
29
+ };
30
+
31
+ function addEvents(events: any) {
32
+ events.forEach((cb: any, eventName: string) => {
33
+ document.documentElement.addEventListener(eventName, cb);
34
+ });
35
+ }
36
+
37
+ function removeEvents(events: any) {
38
+ events.forEach((cb: any, eventName: any) => {
39
+ document.documentElement.removeEventListener(eventName, cb);
40
+ });
41
+ }
42
+
43
+ @Component({ name: 'DragResizeView' })
44
+ export default class DragResizeView extends Vue {
45
+
46
+ @Prop({
47
+ type: String,
48
+ required: false,
49
+ default: ''
50
+ }) name!: string;
51
+
52
+
53
+ @Prop({ type: Number, default: 8 }) stickSize!: number;
54
+ @Prop({ type: Number, default: 1 }) parentScaleX!: number;
55
+ @Prop({ type: Number, default: 1 }) parentScaleY!: number;
56
+ @Prop({ type: Boolean, default: false, }) isActive!: boolean;
57
+ @Prop({ type: Boolean, default: false, }) preventActiveBehavior!: boolean;
58
+ @Prop({ type: Boolean, default: true, }) isDraggable!: boolean;
59
+ @Prop({ type: Boolean, default: true, }) isResizable!: boolean;
60
+ @Prop({ type: Boolean, default: false, }) aspectRatio!: boolean;
61
+ @Prop({
62
+ type: Boolean, default: false,
63
+ }) parentLimitation!: boolean;
64
+ @Prop({
65
+ type: Boolean, default: false,
66
+ }) snapToGrid!: number;
67
+
68
+ @Prop({
69
+ type: Number,
70
+ default: 50,
71
+ validator(val) {
72
+ return val >= 0;
73
+ },
74
+ }) gridX!: number;
75
+
76
+ @Prop({
77
+ type: Number,
78
+ default: 50,
79
+ validator(val) {
80
+ return val >= 0;
81
+ },
82
+ }) gridY!: number;
83
+
84
+ @Prop({
85
+ type: Number,
86
+ default: 0,
87
+ validator(val) {
88
+ return val >= 0;
89
+ },
90
+ }) parentW!: number;
91
+
92
+ @Prop({
93
+ type: Number,
94
+ default: 0,
95
+ validator(val) {
96
+ return val >= 0;
97
+ },
98
+ }) parentH!: number;
99
+
100
+ @Prop({
101
+ type: [String, Number],
102
+ default: 200,
103
+ validator(val) {
104
+ return (typeof val === 'string') ? val === 'auto' : val >= 0;
105
+ },
106
+ }) w!: any;
107
+
108
+ @Prop({
109
+ type: [String, Number],
110
+ default: 200,
111
+ validator(val) {
112
+ return (typeof val === 'string') ? val === 'auto' : val >= 0;
113
+ },
114
+ }) h!: any;
115
+
116
+ @Prop({
117
+ type: Number,
118
+ default: 50,
119
+ validator(val) {
120
+ return val >= 0;
121
+ },
122
+ }) minw!: number;
123
+
124
+ @Prop({
125
+ type: Number,
126
+ default: 50,
127
+ validator(val) {
128
+ return val >= 0;
129
+ },
130
+ }) minh!: number;
131
+
132
+ @Prop({
133
+ type: Number,
134
+ default: 0,
135
+ validator(val) {
136
+ return typeof val === 'number';
137
+ },
138
+ }) x!: number;
139
+
140
+ @Prop({
141
+ type: Number,
142
+ default: 0,
143
+ validator(val) {
144
+ return typeof val === 'number';
145
+ },
146
+ }) y!: number;
147
+ @Prop({
148
+ type: [String, Number],
149
+ default: 'auto',
150
+ validator(val) {
151
+ return (typeof val === 'string') ? val === 'auto' : val >= 0;
152
+ },
153
+ }) z!: string | number;
154
+ @Prop({
155
+ type: String,
156
+ default: null,
157
+ }) dragHandle!: string;
158
+
159
+ @Prop({
160
+ type: String,
161
+ default: null,
162
+ }) dragCancel!: string;
163
+
164
+ @Prop({
165
+ type: Array,
166
+ default() {
167
+ return ['tl', 'tm', 'tr', 'mr', 'br', 'bm', 'bl', 'ml'];
168
+ },
169
+ }) sticks!: Array<string>;
170
+
171
+ @Prop({
172
+ type: String,
173
+ default: 'both',
174
+ validator(val) {
175
+ return ['x', 'y', 'both', 'none'].indexOf(val) !== -1;
176
+ },
177
+ }) axis!: string;
178
+
179
+ @Prop({
180
+ type: String,
181
+ required: false,
182
+ default: '',
183
+ }) contentClass!: boolean;
184
+
185
+ @Prop({
186
+ type: Boolean,
187
+ required: false,
188
+ default: false
189
+ }) isStorage!: boolean;
190
+
191
+
192
+
193
+ public emits = ['clicked', 'dragging', 'dragstop', 'resizing', 'resizestop', 'activated', 'deactivated']
194
+
195
+ public fixAspectRatio = null
196
+ public active = false
197
+ public zIndex: number = 0;
198
+ public parentWidth = 0;
199
+ public parentHeight = 0;
200
+ public left = 0;
201
+ public top = 0;
202
+ public right = 50;
203
+ public bottom = 50;
204
+ public minHeight = 10;
205
+ public stickDrag = false;
206
+ public bodyDrag = false;
207
+ public dimensionsBeforeMove: any;
208
+ public limits: any;
209
+ public currentStick: any;
210
+ public aspectFactor: any;
211
+ public domEvents: any;
212
+ public parentElement: any;
213
+ public _uid: any;
214
+
215
+ beforeCreate() {
216
+ this.stickDrag = false;
217
+ this.bodyDrag = false;
218
+ this.dimensionsBeforeMove = { pointerX: 0, pointerY: 0, x: 0, y: 0, w: 0, h: 0 };
219
+ this.limits = {
220
+ left: { min: null, max: null },
221
+ right: { min: null, max: null },
222
+ top: { min: null, max: null },
223
+ bottom: { min: null, max: null },
224
+ };
225
+
226
+ this.currentStick = null;
227
+ }
228
+
229
+
230
+ mounted() {
231
+
232
+ this.parentElement = this.$el.parentNode;
233
+ this.parentWidth = this.parentW ? this.parentW : this.parentElement.clientWidth;
234
+ this.parentHeight = this.parentH ? this.parentH : this.parentElement.clientHeight;
235
+
236
+
237
+ this.left = this.x;
238
+ this.top = this.y;
239
+ this.right = this.parentWidth - (this.w === 'auto' ? (this.$refs.container as HTMLElement).scrollWidth : this.w) - this.left;
240
+ this.bottom = this.parentHeight - (this.h === 'auto' ? (this.$refs.container as HTMLElement).scrollHeight : this.h) - this.top;
241
+
242
+ this.domEvents = new Map([
243
+ ['mousemove', this.move],
244
+ ['mouseup', this.up],
245
+ ['mouseleave', this.up],
246
+ ['mousedown', this.deselect],
247
+ ['touchmove', this.move],
248
+ ['touchend', this.up],
249
+ ['touchcancel', this.up],
250
+ ['touchstart', this.up],
251
+ ['keydown', this.keydown],
252
+ ]);
253
+
254
+ addEvents(this.domEvents);
255
+
256
+ if (this.dragHandle) {
257
+ [...this.$el.querySelectorAll(this.dragHandle)].forEach((dragHandle) => {
258
+ dragHandle.setAttribute('data-drag-handle', this._uid);
259
+ });
260
+ }
261
+
262
+ if (this.dragCancel) {
263
+ [...this.$el.querySelectorAll(this.dragCancel)].forEach((cancelHandle) => {
264
+ cancelHandle.setAttribute('data-drag-cancel', this._uid);
265
+ });
266
+ }
267
+
268
+ if (this.name.trim() !== '' && this.isStorage) {
269
+ let info = JSON.parse(localStorage.getItem("DragResizeView_Rect_" + this.name) as string)
270
+ if (info !== null
271
+ && info.left !== null && info.top !== null
272
+ && info.right !== null && info.bottom !== null) {
273
+ this.left = info.left
274
+ this.top = info.top
275
+ this.right = info.right
276
+ this.bottom = info.bottom
277
+ } else {
278
+ localStorage.removeItem("DragResizeView_Rect_" + this.name)
279
+ }
280
+ }
281
+ }
282
+
283
+ beforeDestroy() {
284
+ removeEvents(this.domEvents);
285
+ }
286
+
287
+ deselect() {
288
+ if (this.preventActiveBehavior) {
289
+ return;
290
+ }
291
+ this.active = false;
292
+ }
293
+
294
+ move(ev: any) {
295
+
296
+ if (!this.stickDrag && !this.bodyDrag) {
297
+ return;
298
+ }
299
+
300
+ ev.stopPropagation();
301
+
302
+ const pageX = typeof ev.pageX !== 'undefined' ? ev.pageX : ev.touches[0].pageX;
303
+ const pageY = typeof ev.pageY !== 'undefined' ? ev.pageY : ev.touches[0].pageY;
304
+
305
+ const { dimensionsBeforeMove } = this;
306
+
307
+ const delta = {
308
+ x: (dimensionsBeforeMove.pointerX - pageX) / this.parentScaleX,
309
+ y: (dimensionsBeforeMove.pointerY - pageY) / this.parentScaleY,
310
+ };
311
+
312
+ if (this.stickDrag) {
313
+ this.stickMove(delta);
314
+ }
315
+
316
+ if (this.bodyDrag) {
317
+ if (this.axis === 'x') {
318
+ delta.y = 0;
319
+ } else if (this.axis === 'y') {
320
+ delta.x = 0;
321
+ } else if (this.axis === 'none') {
322
+ return;
323
+ }
324
+ this.bodyMove(delta);
325
+ }
326
+ }
327
+
328
+ up(ev: any) {
329
+ if (this.stickDrag) {
330
+ this.stickUp();
331
+ } else if (this.bodyDrag) {
332
+ this.bodyUp();
333
+ }
334
+ }
335
+
336
+ keydown(ev: any) {
337
+
338
+ if (!this.stickDrag && !this.bodyDrag) {
339
+ return;
340
+ }
341
+ ev.stopPropagation();
342
+
343
+ const { dimensionsBeforeMove } = this;
344
+ let pageX = 0, pageY = 0;
345
+ if (ev.key === "a" || ev.key === "ArrowLeft") {
346
+ pageX = dimensionsBeforeMove.pointerX - 1;
347
+ pageY = dimensionsBeforeMove.pointerY;
348
+ } else if (ev.key === "d" || ev.key === "ArrowRight") {
349
+ pageX = dimensionsBeforeMove.pointerX + 1;
350
+ pageY = dimensionsBeforeMove.pointerY;
351
+ } else if (ev.key === "w" || ev.key === "ArrowUp") {
352
+ pageX = dimensionsBeforeMove.pointerX;
353
+ pageY = dimensionsBeforeMove.pointerY - 1;
354
+ } else if (ev.key === "s" || ev.key === "ArrowDown") {
355
+ pageX = dimensionsBeforeMove.pointerX;
356
+ pageY = dimensionsBeforeMove.pointerY + 1;
357
+ } else {
358
+ return
359
+ }
360
+
361
+ const delta = {
362
+ x: (dimensionsBeforeMove.pointerX - pageX) / this.parentScaleX,
363
+ y: (dimensionsBeforeMove.pointerY - pageY) / this.parentScaleY,
364
+ };
365
+
366
+ if (this.stickDrag) {
367
+ this.stickMove(delta);
368
+ }
369
+
370
+ if (this.axis === 'x') {
371
+ delta.y = 0;
372
+ } else if (this.axis === 'y') {
373
+ delta.x = 0;
374
+ } else if (this.axis === 'none') {
375
+ return;
376
+ }
377
+ this.bodyMove(delta);
378
+
379
+ this.saveDimensionsBeforeMove(delta.x, delta.y);
380
+ }
381
+
382
+
383
+ bodyDown(ev: any) {
384
+ const { target, button } = ev;
385
+
386
+ if (!this.preventActiveBehavior) {
387
+ this.active = true;
388
+ }
389
+
390
+ if (button && button !== 0) {
391
+ return;
392
+ }
393
+
394
+ this.$emit('clicked', ev);
395
+
396
+ if (!this.active) {
397
+ return;
398
+ }
399
+
400
+ if (this.dragHandle && target.getAttribute('data-drag-handle') !== this._uid.toString()) {
401
+ return;
402
+ }
403
+
404
+ if (this.dragCancel && target.getAttribute('data-drag-cancel') === this._uid.toString()) {
405
+ return;
406
+ }
407
+
408
+ if (typeof ev.stopPropagation !== 'undefined') {
409
+ ev.stopPropagation();
410
+ }
411
+
412
+ if (typeof ev.preventDefault !== 'undefined') {
413
+ ev.preventDefault();
414
+ }
415
+
416
+ if (this.isDraggable) {
417
+ this.bodyDrag = true;
418
+ }
419
+
420
+ const pointerX = typeof ev.pageX !== 'undefined' ? ev.pageX : ev.touches[0].pageX;
421
+ const pointerY = typeof ev.pageY !== 'undefined' ? ev.pageY : ev.touches[0].pageY;
422
+
423
+ this.saveDimensionsBeforeMove(pointerX, pointerY);
424
+
425
+ if (this.parentLimitation) {
426
+ this.limits = this.calcDragLimitation();
427
+ }
428
+ }
429
+
430
+ bodyMove(delta: any) {
431
+
432
+ const { dimensionsBeforeMove, parentWidth, parentHeight, gridX, gridY, width, height } = this;
433
+
434
+ let newTop = dimensionsBeforeMove.top - delta.y;
435
+ let newBottom = dimensionsBeforeMove.bottom + delta.y;
436
+ let newLeft = dimensionsBeforeMove.left - delta.x;
437
+ let newRight = dimensionsBeforeMove.right + delta.x;
438
+
439
+ if (this.snapToGrid) {
440
+ let alignTop = true;
441
+ let alignLeft = true;
442
+
443
+ let diffT = newTop - Math.floor(newTop / gridY) * gridY;
444
+ let diffB = (parentHeight - newBottom) - Math.floor((parentHeight - newBottom) / gridY) * gridY;
445
+ let diffL = newLeft - Math.floor(newLeft / gridX) * gridX;
446
+ let diffR = (parentWidth - newRight) - Math.floor((parentWidth - newRight) / gridX) * gridX;
447
+
448
+ if (diffT > (gridY / 2)) {
449
+ diffT -= gridY;
450
+ }
451
+ if (diffB > (gridY / 2)) {
452
+ diffB -= gridY;
453
+ }
454
+ if (diffL > (gridX / 2)) {
455
+ diffL -= gridX;
456
+ }
457
+ if (diffR > (gridX / 2)) {
458
+ diffR -= gridX;
459
+ }
460
+
461
+ if (Math.abs(diffB) < Math.abs(diffT)) {
462
+ alignTop = false;
463
+ }
464
+ if (Math.abs(diffR) < Math.abs(diffL)) {
465
+ alignLeft = false;
466
+ }
467
+
468
+ newTop -= (alignTop ? diffT : diffB);
469
+ newBottom = parentHeight - height - newTop;
470
+ newLeft -= (alignLeft ? diffL : diffR);
471
+ newRight = parentWidth - width - newLeft;
472
+ }
473
+
474
+ ({
475
+ newLeft: this.left,
476
+ newRight: this.right,
477
+ newTop: this.top,
478
+ newBottom: this.bottom,
479
+ } = this.rectCorrectionByLimit({ newLeft, newRight, newTop, newBottom }));
480
+
481
+ this.$emit('dragging', this.rect);
482
+
483
+ this.storageElementInfo();
484
+ }
485
+
486
+ bodyUp() {
487
+ this.bodyDrag = false;
488
+ this.$emit('dragging', this.rect);
489
+ this.$emit('dragstop', this.rect);
490
+
491
+ this.dimensionsBeforeMove = { pointerX: 0, pointerY: 0, x: 0, y: 0, w: 0, h: 0 };
492
+
493
+ this.limits = {
494
+ left: { min: null, max: null },
495
+ right: { min: null, max: null },
496
+ top: { min: null, max: null },
497
+ bottom: { min: null, max: null },
498
+ };
499
+ }
500
+
501
+ stickDown(stick: any, ev: any, force = false) {
502
+ if ((!this.isResizable || !this.active) && !force) {
503
+ return;
504
+ }
505
+
506
+ this.stickDrag = true;
507
+
508
+ const pointerX = typeof ev.pageX !== 'undefined' ? ev.pageX : ev.touches[0].pageX;
509
+ const pointerY = typeof ev.pageY !== 'undefined' ? ev.pageY : ev.touches[0].pageY;
510
+
511
+ this.saveDimensionsBeforeMove(pointerX, pointerY);
512
+
513
+ this.currentStick = stick;
514
+
515
+ this.limits = this.calcResizeLimits();
516
+ }
517
+
518
+ saveDimensionsBeforeMove(pointerX: number, pointerY: number): void {
519
+ this.dimensionsBeforeMove.pointerX = pointerX;
520
+ this.dimensionsBeforeMove.pointerY = pointerY;
521
+
522
+ this.dimensionsBeforeMove.left = this.left;
523
+ this.dimensionsBeforeMove.right = this.right;
524
+ this.dimensionsBeforeMove.top = this.top;
525
+ this.dimensionsBeforeMove.bottom = this.bottom;
526
+
527
+ this.dimensionsBeforeMove.width = this.width;
528
+ this.dimensionsBeforeMove.height = this.height;
529
+
530
+ this.aspectFactor = this.width / this.height;
531
+ }
532
+
533
+ stickMove(delta: any) {
534
+ const {
535
+ currentStick,
536
+ dimensionsBeforeMove,
537
+ gridY,
538
+ gridX,
539
+ snapToGrid,
540
+ parentHeight,
541
+ parentWidth,
542
+ } = this;
543
+
544
+ let newTop = dimensionsBeforeMove.top;
545
+ let newBottom = dimensionsBeforeMove.bottom;
546
+ let newLeft = dimensionsBeforeMove.left;
547
+ let newRight = dimensionsBeforeMove.right;
548
+ switch (currentStick[0]) {
549
+ case 'b':
550
+ newBottom = dimensionsBeforeMove.bottom + delta.y;
551
+
552
+ if (snapToGrid) {
553
+ newBottom = parentHeight - Math.round((parentHeight - newBottom) / gridY) * gridY;
554
+ }
555
+
556
+ break;
557
+
558
+ case 't':
559
+ newTop = dimensionsBeforeMove.top - delta.y;
560
+
561
+ if (snapToGrid) {
562
+ newTop = Math.round(newTop / gridY) * gridY;
563
+ }
564
+
565
+ break;
566
+ default:
567
+ break;
568
+ }
569
+
570
+ switch (currentStick[1]) {
571
+ case 'r':
572
+ newRight = dimensionsBeforeMove.right + delta.x;
573
+
574
+ if (snapToGrid) {
575
+ newRight = parentWidth - Math.round((parentWidth - newRight) / gridX) * gridX;
576
+ }
577
+
578
+ break;
579
+
580
+ case 'l':
581
+ newLeft = dimensionsBeforeMove.left - delta.x;
582
+
583
+ if (snapToGrid) {
584
+ newLeft = Math.round(newLeft / gridX) * gridX;
585
+ }
586
+
587
+ break;
588
+ default:
589
+ break;
590
+ }
591
+
592
+ ({
593
+ newLeft,
594
+ newRight,
595
+ newTop,
596
+ newBottom,
597
+ } = this.rectCorrectionByLimit({ newLeft, newRight, newTop, newBottom }));
598
+
599
+ if (this.aspectRatio) {
600
+ ({
601
+ newLeft,
602
+ newRight,
603
+ newTop,
604
+ newBottom,
605
+ } = this.rectCorrectionByAspectRatio({ newLeft, newRight, newTop, newBottom }));
606
+ }
607
+
608
+ this.left = newLeft;
609
+ this.right = newRight;
610
+ this.top = newTop;
611
+ this.bottom = newBottom;
612
+
613
+ this.$emit('resizing', this.rect);
614
+
615
+ this.storageElementInfo()
616
+ }
617
+
618
+ stickUp() {
619
+ this.stickDrag = false;
620
+ this.dimensionsBeforeMove = {
621
+ pointerX: 0,
622
+ pointerY: 0,
623
+ x: 0,
624
+ y: 0,
625
+ w: 0,
626
+ h: 0,
627
+ };
628
+ this.limits = {
629
+ left: { min: null, max: null },
630
+ right: { min: null, max: null },
631
+ top: { min: null, max: null },
632
+ bottom: { min: null, max: null },
633
+ };
634
+
635
+ this.$emit('resizing', this.rect);
636
+ this.$emit('resizestop', this.rect);
637
+ }
638
+
639
+ calcDragLimitation() {
640
+ const { parentWidth, parentHeight } = this;
641
+
642
+ return {
643
+ left: { min: 0, max: parentWidth - this.width },
644
+ right: { min: 0, max: parentWidth - this.width },
645
+ top: { min: 0, max: parentHeight - this.height },
646
+ bottom: { min: 0, max: parentHeight - this.height },
647
+ };
648
+ }
649
+
650
+ calcResizeLimits() {
651
+ const { aspectFactor, width, height, bottom, top, left, right } = this;
652
+ let { minh: minHeight, minw: minWidth } = this;
653
+
654
+ const parentLim = this.parentLimitation ? 0 : null;
655
+
656
+ if (this.aspectRatio) {
657
+ if (minWidth / minHeight > aspectFactor) {
658
+ minHeight = minWidth / aspectFactor;
659
+ } else {
660
+ minWidth = aspectFactor * minHeight;
661
+ }
662
+ }
663
+
664
+ const limits = {
665
+ left: { min: parentLim, max: left + (width - minWidth) },
666
+ right: { min: parentLim, max: right + (width - minWidth) },
667
+ top: { min: parentLim, max: top + (height - minHeight) },
668
+ bottom: { min: parentLim, max: bottom + (height - minHeight) },
669
+ };
670
+
671
+ if (this.aspectRatio) {
672
+ const aspectLimits = {
673
+ left: {
674
+ min: left - (Math.min(top, bottom) * aspectFactor) * 2,
675
+ max: left + ((((height - minHeight) / 2) * aspectFactor) * 2),
676
+ },
677
+ right: {
678
+ min: right - (Math.min(top, bottom) * aspectFactor) * 2,
679
+ max: right + ((((height - minHeight) / 2) * aspectFactor) * 2),
680
+ },
681
+ top: {
682
+ min: top - (Math.min(left, right) / aspectFactor) * 2,
683
+ max: top + ((((width - minWidth) / 2) / aspectFactor) * 2),
684
+ },
685
+ bottom: {
686
+ min: bottom - (Math.min(left, right) / aspectFactor) * 2,
687
+ max: bottom + ((((width - minWidth) / 2) / aspectFactor) * 2),
688
+ },
689
+ };
690
+
691
+ if (this.currentStick[0] === 'm') {
692
+ limits.left = {
693
+ min: Math.max(limits.left.min as number, aspectLimits.left.min),
694
+ max: Math.min(limits.left.max, aspectLimits.left.max),
695
+ };
696
+ limits.right = {
697
+ min: Math.max(limits.right.min as number, aspectLimits.right.min),
698
+ max: Math.min(limits.right.max, aspectLimits.right.max),
699
+ };
700
+
701
+ } else if (this.currentStick[1] === 'm') {
702
+ limits.top = {
703
+ min: Math.max(limits.top.min as number, aspectLimits.top.min),
704
+ max: Math.min(limits.top.max, aspectLimits.top.max),
705
+ };
706
+ limits.bottom = {
707
+ min: Math.max(limits.bottom.min as number, aspectLimits.bottom.min),
708
+ max: Math.min(limits.bottom.max, aspectLimits.bottom.max),
709
+ };
710
+ }
711
+ }
712
+
713
+ return limits;
714
+ }
715
+
716
+ sideCorrectionByLimit(limit: any, current: any) {
717
+ let value = current;
718
+
719
+ if (limit.min !== null && current < limit.min) {
720
+ value = limit.min;
721
+ } else if (limit.max !== null && limit.max < current) {
722
+ value = limit.max;
723
+ }
724
+
725
+ return value;
726
+ }
727
+
728
+ rectCorrectionByLimit(rect: any) {
729
+ const { limits } = this;
730
+ let { newRight, newLeft, newBottom, newTop } = rect;
731
+
732
+ newLeft = this.sideCorrectionByLimit(limits.left, newLeft);
733
+ newRight = this.sideCorrectionByLimit(limits.right, newRight);
734
+ newTop = this.sideCorrectionByLimit(limits.top, newTop);
735
+ newBottom = this.sideCorrectionByLimit(limits.bottom, newBottom);
736
+
737
+ return {
738
+ newLeft,
739
+ newRight,
740
+ newTop,
741
+ newBottom,
742
+ };
743
+ }
744
+
745
+ rectCorrectionByAspectRatio(rect: any) {
746
+ let { newLeft, newRight, newTop, newBottom } = rect;
747
+ const { parentWidth, parentHeight, currentStick, aspectFactor, dimensionsBeforeMove } = this;
748
+
749
+ let newWidth = parentWidth - newLeft - newRight;
750
+ let newHeight = parentHeight - newTop - newBottom;
751
+
752
+ if (currentStick[1] === 'm') {
753
+ const deltaHeight = newHeight - dimensionsBeforeMove.height;
754
+
755
+ newLeft -= (deltaHeight * aspectFactor) / 2;
756
+ newRight -= (deltaHeight * aspectFactor) / 2;
757
+ } else if (currentStick[0] === 'm') {
758
+ const deltaWidth = newWidth - dimensionsBeforeMove.width;
759
+
760
+ newTop -= (deltaWidth / aspectFactor) / 2;
761
+ newBottom -= (deltaWidth / aspectFactor) / 2;
762
+ } else if (newWidth / newHeight > aspectFactor) {
763
+ newWidth = aspectFactor * newHeight;
764
+
765
+ if (currentStick[1] === 'l') {
766
+ newLeft = parentWidth - newRight - newWidth;
767
+ } else {
768
+ newRight = parentWidth - newLeft - newWidth;
769
+ }
770
+ } else {
771
+ newHeight = newWidth / aspectFactor;
772
+
773
+ if (currentStick[0] === 't') {
774
+ newTop = parentHeight - newBottom - newHeight;
775
+ } else {
776
+ newBottom = parentHeight - newTop - newHeight;
777
+ }
778
+ }
779
+
780
+ return { newLeft, newRight, newTop, newBottom };
781
+ }
782
+
783
+ storageElementInfo() {
784
+
785
+ if (!this.isStorage)
786
+ return
787
+
788
+ if (this.name.trim() === "") {
789
+ throw "unspecified name"
790
+ }
791
+
792
+ if (this.rect.left === null || this.rect.top === null || this.right === null || this.bottom === null) {
793
+ return
794
+ }
795
+
796
+ const info = { "left": this.rect.left, "top": this.rect.top, "right": this.right, "bottom": this.bottom }
797
+ localStorage.setItem("DragResizeView_Rect_" + this.name, JSON.stringify(info))
798
+ }
799
+
800
+ get positionStyle() {
801
+ return {
802
+ top: this.top + 'px',
803
+ left: this.left + 'px',
804
+ zIndex: this.zIndex,
805
+ };
806
+ }
807
+
808
+ get sizeStyle() {
809
+ return {
810
+ width: this.w == 'auto' ? 'auto' : this.width + 'px',
811
+ height: this.h == 'auto' ? 'auto' : this.height + 'px'
812
+ };
813
+ }
814
+
815
+
816
+ get vdrStick() {
817
+ return (stick: any) => {
818
+ const stickStyle: any = {
819
+ width: `${this.stickSize / this.parentScaleX}px`,
820
+ height: `${this.stickSize / this.parentScaleY}px`,
821
+ };
822
+ stickStyle[styleMapping.y[stick[0]]] = `${this.stickSize / this.parentScaleX / -2}px`;
823
+ stickStyle[styleMapping.x[stick[1]]] = `${this.stickSize / this.parentScaleX / -2}px`;
824
+ return stickStyle;
825
+ };
826
+ }
827
+
828
+ get width() {
829
+ return this.parentWidth - this.left - this.right;
830
+ }
831
+
832
+ get height() {
833
+ return this.parentHeight - this.top - this.bottom;
834
+ }
835
+
836
+ get rect() {
837
+ return {
838
+ left: Math.round(this.left),
839
+ top: Math.round(this.top),
840
+ width: Math.round(this.width),
841
+ height: Math.round(this.height),
842
+ };
843
+ }
844
+
845
+ @Watch('active', { immediate: false, deep: false })
846
+ onActive(isActive: boolean) {
847
+ if (isActive) {
848
+ this.$emit('activated');
849
+ } else {
850
+ this.$emit('deactivated');
851
+ }
852
+ }
853
+
854
+ @Watch('isActive', { immediate: true, deep: false })
855
+ onIsActive(val: boolean) {
856
+ this.active = val;
857
+ }
858
+
859
+ @Watch('z', { immediate: true, deep: false })
860
+ onZ(val: number | string) {
861
+ if (val >= 0 || val === 'auto') {
862
+ this.zIndex = val as number;
863
+ }
864
+ }
865
+
866
+ @Watch('x', { immediate: false, deep: false })
867
+ onX(newVal: number, oldVal: number) {
868
+ if (this.stickDrag || this.bodyDrag || (newVal === this.left)) {
869
+ return;
870
+ }
871
+
872
+ const delta = oldVal - newVal;
873
+
874
+ this.bodyDown({ pageX: this.left, pageY: this.top });
875
+ this.bodyMove({ x: delta, y: 0 });
876
+
877
+ this.$nextTick(() => {
878
+ this.bodyUp();
879
+ });
880
+ }
881
+
882
+ @Watch('y', { immediate: false, deep: false })
883
+ onY(newVal: number, oldVal: number) {
884
+ if (this.stickDrag || this.bodyDrag || (newVal === this.top)) {
885
+ return;
886
+ }
887
+
888
+ const delta = oldVal - newVal;
889
+
890
+ this.bodyDown({ pageX: this.left, pageY: this.top });
891
+ this.bodyMove({ x: 0, y: delta });
892
+
893
+ this.$nextTick(() => {
894
+ this.bodyUp();
895
+ });
896
+ }
897
+
898
+ @Watch('w', { immediate: false, deep: false })
899
+ onW(newVal: number, oldVal: number) {
900
+ if (this.stickDrag || this.bodyDrag || (newVal === this.width)) {
901
+ return;
902
+ }
903
+
904
+ const stick = 'mr';
905
+ const delta = oldVal - newVal;
906
+
907
+ this.stickDown(stick, { pageX: this.right, pageY: this.top + (this.height / 2) }, true);
908
+ this.stickMove({ x: delta, y: 0 });
909
+
910
+ this.$nextTick(() => {
911
+ this.stickUp();
912
+ });
913
+ }
914
+
915
+ @Watch('h', { immediate: false, deep: false })
916
+ onH(newVal: number, oldVal: number) {
917
+ if (this.stickDrag || this.bodyDrag || (newVal === this.height)) {
918
+ return;
919
+ }
920
+
921
+ const stick = 'bm';
922
+ const delta = oldVal - newVal;
923
+
924
+ this.stickDown(stick, { pageX: this.left + (this.width / 2), pageY: this.bottom }, true);
925
+ this.stickMove({ x: 0, y: delta });
926
+
927
+ this.$nextTick(() => {
928
+ this.stickUp();
929
+ });
930
+ }
931
+
932
+ @Watch('parentW', { immediate: false, deep: false })
933
+ onParentW(val: number) {
934
+ this.right = val - this.width - this.left;
935
+ this.parentWidth = val;
936
+ }
937
+
938
+
939
+ @Watch('parentH', { immediate: false, deep: false })
940
+ onParentH(val: number) {
941
+ this.bottom = val - this.height - this.top;
942
+ this.parentHeight = val;
943
+ }
944
+
945
+ }
946
+
947
+ // 待实现功能:
948
+ // 1. 移动时与其他组件对齐
949
+ // 2. 通过键盘上下左右移动
950
+
951
+ </script>
952
+
953
+ <style lang="less" scoped>
954
+ @import "index.less";
955
+ </style>