@tmagic/stage 1.0.0-beta.13 → 1.0.0-beta.16

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.
@@ -2,12 +2,14 @@
2
2
  import { EventEmitter } from 'events';
3
3
  import Moveable from 'moveable';
4
4
  import StageCore from './StageCore';
5
+ import TargetCalibrate from './TargetCalibrate';
5
6
  import type { StageHighlightConfig } from './types';
6
7
  export default class StageHighlight extends EventEmitter {
7
8
  core: StageCore;
8
9
  container: HTMLElement;
9
10
  target?: HTMLElement;
10
11
  moveable?: Moveable;
12
+ calibrationTarget: TargetCalibrate;
11
13
  constructor(config: StageHighlightConfig);
12
14
  /**
13
15
  * 高亮鼠标悬停的组件
@@ -0,0 +1,18 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ import StageDragResize from './StageDragResize';
4
+ import StageMask from './StageMask';
5
+ import type { TargetCalibrateConfig } from './types';
6
+ /**
7
+ * 将选中的节点修正定位后,添加一个操作节点到蒙层上
8
+ */
9
+ export default class TargetCalibrate extends EventEmitter {
10
+ parent: HTMLElement;
11
+ mask: StageMask;
12
+ dr: StageDragResize;
13
+ operationEl: HTMLElement;
14
+ constructor(config: TargetCalibrateConfig);
15
+ update(el: HTMLElement, prefix: String): HTMLElement;
16
+ destroy(): void;
17
+ private getOffset;
18
+ }
@@ -1,5 +1,6 @@
1
1
  export declare const GHOST_EL_ID_PREFIX = "ghost_el_";
2
2
  export declare const DRAG_EL_ID_PREFIX = "drag_el_";
3
+ export declare const HIGHLIGHT_EL_ID_PREFIX = "highlight_el_";
3
4
  export declare const DEFAULT_ZOOM = 1;
4
5
  export declare enum GuidesType {
5
6
  HORIZONTAL = "horizontal",
@@ -2,6 +2,8 @@ import { MoveableOptions } from 'react-moveable/declaration/types';
2
2
  import { Id, MApp, MNode } from '@tmagic/schema';
3
3
  import { GuidesType } from './const';
4
4
  import StageCore from './StageCore';
5
+ import StageDragResize from './StageDragResize';
6
+ import StageMask from './StageMask';
5
7
  export declare type CanSelect = (el: HTMLElement, event: MouseEvent, stop: () => boolean) => boolean | Promise<boolean>;
6
8
  export declare type StageCoreConfig = {
7
9
  /** 需要对齐的dom节点的CSS选择器字符串 */
@@ -82,3 +84,8 @@ export interface StageHighlightConfig {
82
84
  core: StageCore;
83
85
  container: HTMLElement;
84
86
  }
87
+ export interface TargetCalibrateConfig {
88
+ parent: HTMLElement;
89
+ mask: StageMask;
90
+ dr: StageDragResize;
91
+ }
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
- "version": "1.0.0-beta.13",
2
+ "version": "1.0.0-beta.16",
3
3
  "name": "@tmagic/stage",
4
- "sideEffects": false,
4
+ "sideEffects": [
5
+ "dist/*"
6
+ ],
5
7
  "main": "dist/tmagic-stage.umd.js",
6
8
  "module": "dist/tmagic-stage.es.js",
7
9
  "types": "dist/types/src/index.d.ts",
@@ -9,7 +11,8 @@
9
11
  ".": {
10
12
  "import": "./dist/tmagic-stage.es.js",
11
13
  "require": "./dist/tmagic-stage.umd.js"
12
- }
14
+ },
15
+ "./*": "./*"
13
16
  },
14
17
  "license": "Apache-2.0",
15
18
  "scripts": {
@@ -24,11 +27,11 @@
24
27
  },
25
28
  "dependencies": {
26
29
  "@scena/guides": "^0.17.0",
27
- "@tmagic/schema": "^1.0.0-beta.13",
28
- "@tmagic/utils": "^1.0.0-beta.13",
30
+ "@tmagic/schema": "^1.0.0-beta.16",
31
+ "@tmagic/utils": "^1.0.0-beta.16",
29
32
  "events": "^3.3.0",
30
33
  "lodash-es": "^4.17.21",
31
- "moveable": "^0.28.0",
34
+ "moveable": "^0.29.4",
32
35
  "moveable-helper": "^0.4.0"
33
36
  },
34
37
  "devDependencies": {
@@ -40,5 +43,5 @@
40
43
  "vite": "^2.3.7",
41
44
  "vite-plugin-dts": "^0.9.6"
42
45
  },
43
- "gitHead": "ca2b203614cbdaff580e5cb6f35630f50b827451"
46
+ "gitHead": "2aebfb1b8ceb5c8e26dfb59b643ed9f4f4b7bd15"
44
47
  }
@@ -46,11 +46,11 @@ export default class StageDragResize extends EventEmitter {
46
46
  public horizontalGuidelines: number[] = [];
47
47
  public verticalGuidelines: number[] = [];
48
48
  public elementGuidelines: HTMLElement[] = [];
49
+ public mode: Mode = Mode.ABSOLUTE;
49
50
 
50
51
  private moveableOptions: MoveableOptions = {};
51
52
  private dragStatus: ActionStatus = ActionStatus.END;
52
53
  private ghostEl: HTMLElement | undefined;
53
- private mode: Mode = Mode.ABSOLUTE;
54
54
  private moveableHelper?: MoveableHelper;
55
55
 
56
56
  constructor(config: StageDragResizeConfig) {
@@ -354,6 +354,7 @@ export default class StageDragResize extends EventEmitter {
354
354
  top: ${offset.top}px;
355
355
  width: ${width}px;
356
356
  height: ${height}px;
357
+ z-index: 9;
357
358
  `;
358
359
 
359
360
  this.dragEl.id = `${DRAG_EL_ID_PREFIX}${el.id}`;
@@ -21,19 +21,27 @@ import { EventEmitter } from 'events';
21
21
 
22
22
  import Moveable from 'moveable';
23
23
 
24
+ import { HIGHLIGHT_EL_ID_PREFIX } from './const';
24
25
  import StageCore from './StageCore';
26
+ import TargetCalibrate from './TargetCalibrate';
25
27
  import type { StageHighlightConfig } from './types';
26
28
  export default class StageHighlight extends EventEmitter {
27
29
  public core: StageCore;
28
30
  public container: HTMLElement;
29
31
  public target?: HTMLElement;
30
32
  public moveable?: Moveable;
33
+ public calibrationTarget: TargetCalibrate;
31
34
 
32
35
  constructor(config: StageHighlightConfig) {
33
36
  super();
34
37
 
35
38
  this.core = config.core;
36
39
  this.container = config.container;
40
+ this.calibrationTarget = new TargetCalibrate({
41
+ parent: this.core.mask.content,
42
+ mask: this.core.mask,
43
+ dr: this.core.dr,
44
+ });
37
45
  }
38
46
 
39
47
  /**
@@ -44,10 +52,12 @@ export default class StageHighlight extends EventEmitter {
44
52
  if (!el || el === this.target) return;
45
53
  this.target = el;
46
54
  this.moveable?.destroy();
55
+
47
56
  this.moveable = new Moveable(this.container, {
48
- target: this.target,
49
- scrollable: true,
57
+ target: this.calibrationTarget.update(el, HIGHLIGHT_EL_ID_PREFIX),
50
58
  origin: false,
59
+ rootContainer: this.core.container,
60
+ zoom: 1,
51
61
  });
52
62
  }
53
63
 
@@ -66,5 +76,6 @@ export default class StageHighlight extends EventEmitter {
66
76
  */
67
77
  public destroy(): void {
68
78
  this.moveable?.destroy();
79
+ this.calibrationTarget.destroy();
69
80
  }
70
81
  }
@@ -0,0 +1,135 @@
1
+ /*
2
+ * Tencent is pleased to support the open source community by making TMagicEditor available.
3
+ *
4
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ /* eslint-disable no-param-reassign */
20
+ import { EventEmitter } from 'events';
21
+
22
+ import { Mode } from './const';
23
+ import StageDragResize from './StageDragResize';
24
+ import StageMask from './StageMask';
25
+ import type { Offset, TargetCalibrateConfig } from './types';
26
+ import { getMode } from './util';
27
+
28
+ /**
29
+ * 将选中的节点修正定位后,添加一个操作节点到蒙层上
30
+ */
31
+ export default class TargetCalibrate extends EventEmitter {
32
+ public parent: HTMLElement;
33
+ public mask: StageMask;
34
+ public dr: StageDragResize;
35
+ public operationEl: HTMLElement;
36
+
37
+ constructor(config: TargetCalibrateConfig) {
38
+ super();
39
+
40
+ this.parent = config.parent;
41
+ this.mask = config.mask;
42
+ this.dr = config.dr;
43
+
44
+ this.operationEl = globalThis.document.createElement('div');
45
+ this.parent.append(this.operationEl);
46
+ }
47
+
48
+ public update(el: HTMLElement, prefix: String): HTMLElement {
49
+ const { width, height } = el.getBoundingClientRect();
50
+ const { left, top } = this.getOffset(el);
51
+ this.operationEl.style.cssText = `
52
+ position: absolute;
53
+ left: ${left}px;
54
+ top: ${top}px;
55
+ width: ${width}px;
56
+ height: ${height}px;
57
+ `;
58
+
59
+ this.operationEl.id = `${prefix}${el.id}`;
60
+ return this.operationEl;
61
+ }
62
+
63
+ public destroy(): void {
64
+ this.operationEl?.remove();
65
+ }
66
+
67
+ private getOffset(el: HTMLElement): Offset {
68
+ const { transform } = getComputedStyle(el);
69
+ const { offsetParent } = el;
70
+
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
+ }
97
+
98
+ if (offsetParent) {
99
+ const parentOffset = this.getOffset(offsetParent as HTMLElement);
100
+ return {
101
+ left: left + parentOffset.left,
102
+ top: top + parentOffset.top,
103
+ };
104
+ }
105
+
106
+ // 选中固定定位元素后editor-mask高度被置为视窗大小
107
+ if (this.dr.mode === Mode.FIXED) {
108
+ // 弹窗的情况
109
+ if (getMode(el) === Mode.FIXED) {
110
+ return {
111
+ left,
112
+ top,
113
+ };
114
+ }
115
+
116
+ return {
117
+ left: left - this.mask.scrollLeft,
118
+ top: top - this.mask.scrollTop,
119
+ };
120
+ }
121
+
122
+ // 无父元素的固定定位需按滚动值计算
123
+ if (getMode(el) === Mode.FIXED) {
124
+ return {
125
+ left: left + this.mask.scrollLeft,
126
+ top: top + this.mask.scrollTop,
127
+ };
128
+ }
129
+
130
+ return {
131
+ left,
132
+ top,
133
+ };
134
+ }
135
+ }
package/src/const.ts CHANGED
@@ -21,6 +21,8 @@ export const GHOST_EL_ID_PREFIX = 'ghost_el_';
21
21
 
22
22
  export const DRAG_EL_ID_PREFIX = 'drag_el_';
23
23
 
24
+ export const HIGHLIGHT_EL_ID_PREFIX = 'highlight_el_';
25
+
24
26
  // 默认放到缩小倍数
25
27
  export const DEFAULT_ZOOM = 1;
26
28
 
package/src/types.ts CHANGED
@@ -22,6 +22,8 @@ import { Id, MApp, MNode } from '@tmagic/schema';
22
22
 
23
23
  import { GuidesType } from './const';
24
24
  import StageCore from './StageCore';
25
+ import StageDragResize from './StageDragResize';
26
+ import StageMask from './StageMask';
25
27
 
26
28
  export type CanSelect = (el: HTMLElement, event: MouseEvent, stop: () => boolean) => boolean | Promise<boolean>;
27
29
 
@@ -54,7 +56,6 @@ export type Rect = {
54
56
  width: number;
55
57
  height: number;
56
58
  } & Offset;
57
-
58
59
  export interface Offset {
59
60
  left: number;
60
61
  top: number;
@@ -119,3 +120,9 @@ export interface StageHighlightConfig {
119
120
  core: StageCore;
120
121
  container: HTMLElement;
121
122
  }
123
+
124
+ export interface TargetCalibrateConfig {
125
+ parent: HTMLElement;
126
+ mask: StageMask;
127
+ dr: StageDragResize;
128
+ }