@tmagic/stage 1.0.0-rc.5 → 1.0.0-rc.8

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0-rc.5",
2
+ "version": "1.0.0-rc.8",
3
3
  "name": "@tmagic/stage",
4
4
  "sideEffects": [
5
5
  "dist/*"
@@ -27,9 +27,9 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@scena/guides": "^0.17.0",
30
- "@tmagic/core": "^1.0.0-rc.5",
31
- "@tmagic/schema": "^1.0.0-rc.5",
32
- "@tmagic/utils": "^1.0.0-rc.5",
30
+ "@tmagic/core": "1.0.0-rc.8",
31
+ "@tmagic/schema": "1.0.0-rc.8",
32
+ "@tmagic/utils": "1.0.0-rc.8",
33
33
  "events": "^3.3.0",
34
34
  "lodash-es": "^4.17.21",
35
35
  "moveable": "^0.29.4",
@@ -43,6 +43,5 @@
43
43
  "typescript": "^4.3.4",
44
44
  "vite": "^2.3.7",
45
45
  "vite-plugin-dts": "^0.9.6"
46
- },
47
- "gitHead": "d147b361d7e6123cda99c6ba68a2ac7e074be37f"
46
+ }
48
47
  }
@@ -48,7 +48,7 @@ export default class StageDragResize extends EventEmitter {
48
48
  /** 目标节点 */
49
49
  public target?: HTMLElement;
50
50
  /** 目标节点在蒙层中的占位节点 */
51
- public dragEl: HTMLElement;
51
+ public dragEl: HTMLDivElement;
52
52
  /** Moveable拖拽类实例 */
53
53
  public moveable?: Moveable;
54
54
  /** 水平参考线 */
@@ -87,10 +87,9 @@ export default class StageDragResize extends EventEmitter {
87
87
  const oldTarget = this.target;
88
88
  this.target = el;
89
89
 
90
- this.init(el);
91
-
92
90
  // 从不能拖动到能拖动的节点之间切换,要重新创建moveable,不然dragStart不生效
93
91
  if (!this.moveable || this.target !== oldTarget) {
92
+ this.init(el);
94
93
  this.moveableHelper = MoveableHelper.create({
95
94
  useBeforeRender: true,
96
95
  useRender: false,
@@ -203,49 +202,41 @@ export default class StageDragResize extends EventEmitter {
203
202
 
204
203
  this.bindResizeEvent();
205
204
  this.bindDragEvent();
205
+ this.bindRotateEvent();
206
+ this.bindScaleEvent();
206
207
  }
207
208
 
208
209
  private bindResizeEvent(): void {
209
210
  if (!this.moveable) throw new Error('moveable 为初始化');
210
211
 
211
212
  const frame = {
212
- translate: [0, 0],
213
+ left: 0,
214
+ top: 0,
213
215
  };
214
216
 
215
217
  this.moveable
216
218
  .on('resizeStart', (e) => {
217
- if (e.dragStart) {
218
- const rect = this.moveable!.getRect();
219
- const offset = getAbsolutePosition(e.target as HTMLElement, rect);
220
- e.dragStart.set([offset.left, offset.top]);
221
- }
219
+ if (!this.target) return;
220
+
221
+ this.dragStatus = ActionStatus.START;
222
+ this.moveableHelper?.onResizeStart(e);
223
+
224
+ frame.top = this.target.offsetTop;
225
+ frame.left = this.target.offsetLeft;
222
226
  })
223
- .on('resize', ({ width, height, drag }) => {
227
+ .on('resize', (e) => {
228
+ const { width, height, drag } = e;
224
229
  if (!this.moveable || !this.target || !this.dragEl) return;
225
230
 
226
231
  const { beforeTranslate } = drag;
227
- frame.translate = beforeTranslate;
228
232
  this.dragStatus = ActionStatus.ING;
229
233
 
234
+ this.moveableHelper?.onResize(e);
235
+
230
236
  this.target.style.width = `${width}px`;
231
237
  this.target.style.height = `${height}px`;
232
- this.dragEl.style.width = `${width}px`;
233
- this.dragEl.style.height = `${height}px`;
234
-
235
- // 流式布局
236
- if (this.mode === Mode.SORTABLE) {
237
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
238
- this.target.style.top = `0px`;
239
- return;
240
- }
241
-
242
- this.dragEl.style.left = `${beforeTranslate[0]}px`;
243
- this.dragEl.style.top = `${beforeTranslate[1]}px`;
244
-
245
- const offset = getAbsolutePosition(this.target, { left: beforeTranslate[0], top: beforeTranslate[1] });
246
-
247
- this.target.style.left = `${offset.left}px`;
248
- this.target.style.top = `${offset.top}px`;
238
+ this.target.style.left = `${frame.left + beforeTranslate[0]}px`;
239
+ this.target.style.top = `${frame.top + beforeTranslate[1]}px`;
249
240
  })
250
241
  .on('resizeEnd', () => {
251
242
  this.dragStatus = ActionStatus.END;
@@ -311,6 +302,60 @@ export default class StageDragResize extends EventEmitter {
311
302
  });
312
303
  }
313
304
 
305
+ private bindRotateEvent(): void {
306
+ if (!this.moveable) throw new Error('moveable 为初始化');
307
+
308
+ this.moveable
309
+ .on('rotateStart', (e) => {
310
+ this.dragStatus = ActionStatus.START;
311
+ this.moveableHelper?.onRotateStart(e);
312
+ })
313
+ .on('rotate', (e) => {
314
+ if (!this.target || !this.dragEl) return;
315
+ this.dragStatus = ActionStatus.ING;
316
+ this.moveableHelper?.onRotate(e);
317
+ const frame = this.moveableHelper?.getFrame(e.target);
318
+ this.target.style.transform = frame?.toCSSObject().transform || '';
319
+ })
320
+ .on('rotateEnd', (e) => {
321
+ this.dragStatus = ActionStatus.END;
322
+ const frame = this.moveableHelper?.getFrame(e.target);
323
+ this.emit('update', {
324
+ el: this.target,
325
+ style: {
326
+ transform: frame?.get('transform'),
327
+ },
328
+ });
329
+ });
330
+ }
331
+
332
+ private bindScaleEvent(): void {
333
+ if (!this.moveable) throw new Error('moveable 为初始化');
334
+
335
+ this.moveable
336
+ .on('scaleStart', (e) => {
337
+ this.dragStatus = ActionStatus.START;
338
+ this.moveableHelper?.onScaleStart(e);
339
+ })
340
+ .on('scale', (e) => {
341
+ if (!this.target || !this.dragEl) return;
342
+ this.dragStatus = ActionStatus.ING;
343
+ this.moveableHelper?.onScale(e);
344
+ const frame = this.moveableHelper?.getFrame(e.target);
345
+ this.target.style.transform = frame?.toCSSObject().transform || '';
346
+ })
347
+ .on('scaleEnd', (e) => {
348
+ this.dragStatus = ActionStatus.END;
349
+ const frame = this.moveableHelper?.getFrame(e.target);
350
+ this.emit('update', {
351
+ el: this.target,
352
+ style: {
353
+ transform: frame?.get('transform'),
354
+ },
355
+ });
356
+ });
357
+ }
358
+
314
359
  private sort(): void {
315
360
  if (!this.target || !this.ghostEl) throw new Error('未知错误');
316
361
  const { top } = this.ghostEl.getBoundingClientRect();
@@ -331,14 +376,15 @@ export default class StageDragResize extends EventEmitter {
331
376
  }
332
377
 
333
378
  private update(isResize = false): void {
334
- const rect = this.moveable!.getRect();
379
+ if (!this.target) return;
380
+
335
381
  const offset =
336
- this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : getAbsolutePosition(this.target as HTMLElement, rect);
382
+ this.mode === Mode.SORTABLE ? { left: 0, top: 0 } : { left: this.target.offsetLeft, top: this.target.offsetTop };
337
383
 
338
384
  const left = this.calcValueByFontsize(offset.left);
339
385
  const top = this.calcValueByFontsize(offset.top);
340
- const width = this.calcValueByFontsize(rect.width);
341
- const height = this.calcValueByFontsize(rect.height);
386
+ const width = this.calcValueByFontsize(this.target.clientWidth);
387
+ const height = this.calcValueByFontsize(this.target.clientHeight);
342
388
 
343
389
  this.emit('update', {
344
390
  el: this.target,
@@ -369,19 +415,24 @@ export default class StageDragResize extends EventEmitter {
369
415
  }
370
416
 
371
417
  private updateDragEl(el: HTMLElement) {
372
- const { width, height } = el.getBoundingClientRect();
373
418
  const offset = getOffset(el);
419
+ const { transform } = getComputedStyle(el);
374
420
 
375
421
  this.dragEl.style.cssText = `
376
422
  position: absolute;
423
+ transform: ${transform};
377
424
  left: ${offset.left}px;
378
425
  top: ${offset.top}px;
379
- width: ${width}px;
380
- height: ${height}px;
426
+ width: ${el.clientWidth}px;
427
+ height: ${el.clientHeight}px;
381
428
  z-index: ${ZIndex.DRAG_EL};
382
429
  `;
383
430
 
384
431
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
432
+
433
+ if (typeof this.core.config.updateDragEl === 'function') {
434
+ this.core.config.updateDragEl(this.dragEl, el);
435
+ }
385
436
  }
386
437
 
387
438
  private destroyDragEl(): void {
@@ -415,6 +466,8 @@ export default class StageDragResize extends EventEmitter {
415
466
  dragArea: false,
416
467
  draggable: true,
417
468
  resizable: true,
469
+ scalable: false,
470
+ rotatable: false,
418
471
  snappable: isAbsolute || isFixed,
419
472
  snapGap: isAbsolute || isFixed,
420
473
  snapThreshold: 5,
@@ -41,6 +41,7 @@ export default class StageHighlight extends EventEmitter {
41
41
  parent: this.core.mask.content,
42
42
  mask: this.core.mask,
43
43
  dr: this.core.dr,
44
+ core: this.core,
44
45
  });
45
46
  }
46
47
 
@@ -18,7 +18,6 @@
18
18
 
19
19
  import { EventEmitter } from 'events';
20
20
 
21
- import { SELECTED_CLASS, ZIndex } from './const';
22
21
  import StageCore from './StageCore';
23
22
  import type { Runtime, RuntimeWindow, StageRenderConfig } from './types';
24
23
  import { getHost, isSameDomain } from './util';
@@ -122,16 +121,5 @@ export default class StageRender extends EventEmitter {
122
121
  this.iframe?.contentDocument?.body?.appendChild(el);
123
122
  }
124
123
  }
125
-
126
- if (this.contentWindow) {
127
- const style = this.contentWindow.document.createElement('style');
128
- style.id = 'tmagic-stage-render';
129
- style.innerHTML = `
130
- .${SELECTED_CLASS}, .${SELECTED_CLASS}-parent {
131
- z-index: ${ZIndex.SELECTED_EL};
132
- }
133
- `;
134
- this.contentWindow.document.head.appendChild(style);
135
- }
136
124
  };
137
125
  }
@@ -20,6 +20,7 @@
20
20
  import { EventEmitter } from 'events';
21
21
 
22
22
  import { Mode } from './const';
23
+ import StageCore from './StageCore';
23
24
  import StageDragResize from './StageDragResize';
24
25
  import StageMask from './StageMask';
25
26
  import type { Offset, TargetCalibrateConfig } from './types';
@@ -32,7 +33,8 @@ export default class TargetCalibrate extends EventEmitter {
32
33
  public parent: HTMLElement;
33
34
  public mask: StageMask;
34
35
  public dr: StageDragResize;
35
- public operationEl: HTMLElement;
36
+ public core: StageCore;
37
+ public operationEl: HTMLDivElement;
36
38
 
37
39
  constructor(config: TargetCalibrateConfig) {
38
40
  super();
@@ -40,23 +42,30 @@ export default class TargetCalibrate extends EventEmitter {
40
42
  this.parent = config.parent;
41
43
  this.mask = config.mask;
42
44
  this.dr = config.dr;
45
+ this.core = config.core;
43
46
 
44
47
  this.operationEl = globalThis.document.createElement('div');
45
48
  this.parent.append(this.operationEl);
46
49
  }
47
50
 
48
51
  public update(el: HTMLElement, prefix: String): HTMLElement {
49
- const { width, height } = el.getBoundingClientRect();
50
52
  const { left, top } = this.getOffset(el);
53
+ const { transform } = getComputedStyle(el);
51
54
  this.operationEl.style.cssText = `
52
55
  position: absolute;
56
+ transform: ${transform};
53
57
  left: ${left}px;
54
58
  top: ${top}px;
55
- width: ${width}px;
56
- height: ${height}px;
59
+ width: ${el.clientWidth}px;
60
+ height: ${el.clientHeight}px;
57
61
  `;
58
62
 
59
63
  this.operationEl.id = `${prefix}${el.id}`;
64
+
65
+ if (typeof this.core.config.updateDragEl === 'function') {
66
+ this.core.config.updateDragEl(this.operationEl, el);
67
+ }
68
+
60
69
  return this.operationEl;
61
70
  }
62
71
 
@@ -65,35 +74,10 @@ export default class TargetCalibrate extends EventEmitter {
65
74
  }
66
75
 
67
76
  private getOffset(el: HTMLElement): Offset {
68
- const { transform } = getComputedStyle(el);
69
77
  const { offsetParent } = el;
70
78
 
71
- let left = el.offsetLeft;
72
- let top = el.offsetTop;
73
-
74
- if (transform.indexOf('matrix') > -1) {
75
- let a = 1;
76
- let b = 1;
77
- let c = 1;
78
- let d = 1;
79
- let e = 0;
80
- let f = 0;
81
- transform.replace(
82
- /matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/,
83
- ($0: string, $1: string, $2: string, $3: string, $4: string, $5: string, $6: string): string => {
84
- a = +$1;
85
- b = +$2;
86
- c = +$3;
87
- d = +$4;
88
- e = +$5;
89
- f = +$6;
90
- return transform;
91
- },
92
- );
93
-
94
- left = a * left + c * top + e;
95
- top = b * left + d * top + f;
96
- }
79
+ const left = el.offsetLeft;
80
+ const top = el.offsetTop;
97
81
 
98
82
  if (offsetParent) {
99
83
  const parentOffset = this.getOffset(offsetParent as HTMLElement);
package/src/types.ts CHANGED
@@ -16,7 +16,7 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { MoveableOptions } from 'react-moveable/declaration/types';
19
+ import { MoveableOptions } from 'moveable';
20
20
 
21
21
  import Core from '@tmagic/core';
22
22
  import { Id, MApp, MNode } from '@tmagic/schema';
@@ -39,6 +39,7 @@ export type StageCoreConfig = {
39
39
  runtimeUrl?: string;
40
40
  render?: (renderer: StageCore) => Promise<HTMLElement> | HTMLElement;
41
41
  autoScrollIntoView?: boolean;
42
+ updateDragEl?: (el: HTMLDivElement, target: HTMLElement) => void;
42
43
  };
43
44
 
44
45
  export interface StageRenderConfig {
@@ -58,6 +59,7 @@ export type Rect = {
58
59
  width: number;
59
60
  height: number;
60
61
  } & Offset;
62
+
61
63
  export interface Offset {
62
64
  left: number;
63
65
  top: number;
@@ -72,10 +74,14 @@ export interface UpdateEventData {
72
74
  el: HTMLElement;
73
75
  ghostEl: HTMLElement;
74
76
  style: {
75
- width: number;
76
- height: number;
77
+ width?: number;
78
+ height?: number;
77
79
  left?: number;
78
80
  top?: number;
81
+ transform?: {
82
+ rotate?: string;
83
+ scale?: string;
84
+ };
79
85
  };
80
86
  }
81
87
 
@@ -128,4 +134,5 @@ export interface TargetCalibrateConfig {
128
134
  parent: HTMLElement;
129
135
  mask: StageMask;
130
136
  dr: StageDragResize;
137
+ core: StageCore;
131
138
  }
package/src/util.ts CHANGED
@@ -30,35 +30,10 @@ const getParents = (el: Element, relative: Element) => {
30
30
  };
31
31
 
32
32
  export const getOffset = (el: HTMLElement): Offset => {
33
- const { transform } = getComputedStyle(el);
34
33
  const { offsetParent } = el;
35
34
 
36
- let left = el.offsetLeft;
37
- let top = el.offsetTop;
38
-
39
- if (transform.indexOf('matrix') > -1) {
40
- let a = 1;
41
- let b = 1;
42
- let c = 1;
43
- let d = 1;
44
- let e = 0;
45
- let f = 0;
46
- transform.replace(
47
- /matrix\((.+), (.+), (.+), (.+), (.+), (.+)\)/,
48
- ($0: string, $1: string, $2: string, $3: string, $4: string, $5: string, $6: string): string => {
49
- a = +$1;
50
- b = +$2;
51
- c = +$3;
52
- d = +$4;
53
- e = +$5;
54
- f = +$6;
55
- return transform;
56
- },
57
- );
58
-
59
- left = a * left + c * top + e;
60
- top = b * left + d * top + f;
61
- }
35
+ const left = el.offsetLeft;
36
+ const top = el.offsetTop;
62
37
 
63
38
  if (offsetParent) {
64
39
  const parentOffset = getOffset(offsetParent as HTMLElement);