@tmagic/stage 1.0.0-rc.9 → 1.0.2

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.9",
2
+ "version": "1.0.2",
3
3
  "name": "@tmagic/stage",
4
4
  "sideEffects": [
5
5
  "dist/*"
@@ -27,12 +27,12 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@scena/guides": "^0.17.0",
30
- "@tmagic/core": "1.0.0-rc.9",
31
- "@tmagic/schema": "1.0.0-rc.9",
32
- "@tmagic/utils": "1.0.0-rc.9",
30
+ "@tmagic/core": "1.0.2",
31
+ "@tmagic/schema": "1.0.2",
32
+ "@tmagic/utils": "1.0.2",
33
33
  "events": "^3.3.0",
34
34
  "lodash-es": "^4.17.21",
35
- "moveable": "^0.29.4",
35
+ "moveable": "^0.30.0",
36
36
  "moveable-helper": "^0.4.0"
37
37
  },
38
38
  "devDependencies": {
package/src/Rule.ts CHANGED
@@ -2,14 +2,13 @@ import EventEmitter from 'events';
2
2
 
3
3
  import Guides, { GuidesEvents } from '@scena/guides';
4
4
 
5
- import { GuidesType, H_GUIDE_LINE_STORAGE_KEY, V_GUIDE_LINE_STORAGE_KEY } from './const';
6
- import { getGuideLineFromCache } from './util';
5
+ import { GuidesType } from './const';
7
6
 
8
7
  export default class Rule extends EventEmitter {
9
8
  public hGuides: Guides;
10
9
  public vGuides: Guides;
11
- public horizontalGuidelines: number[] = getGuideLineFromCache(GuidesType.HORIZONTAL);
12
- public verticalGuidelines: number[] = getGuideLineFromCache(GuidesType.VERTICAL);
10
+ public horizontalGuidelines: number[] = [];
11
+ public verticalGuidelines: number[] = [];
13
12
 
14
13
  private container: HTMLDivElement;
15
14
  private containerResizeObserver: ResizeObserver;
@@ -28,6 +27,7 @@ export default class Rule extends EventEmitter {
28
27
  this.vGuides.resize();
29
28
  this.hGuides.resize();
30
29
  });
30
+
31
31
  this.containerResizeObserver.observe(this.container);
32
32
  }
33
33
 
@@ -45,33 +45,34 @@ export default class Rule extends EventEmitter {
45
45
  });
46
46
  }
47
47
 
48
- /**
49
- * 清空所有参考线
50
- */
51
- public clearGuides() {
52
- this.horizontalGuidelines = [];
53
- this.verticalGuidelines = [];
48
+ public setGuides([hLines, vLines]: [number[], number[]]) {
49
+ this.horizontalGuidelines = hLines;
50
+ this.verticalGuidelines = vLines;
54
51
 
55
- this.vGuides.setState({
56
- defaultGuides: [],
52
+ this.hGuides.setState({
53
+ defaultGuides: hLines,
57
54
  });
58
55
 
59
- this.hGuides.setState({
60
- defaultGuides: [],
56
+ this.vGuides.setState({
57
+ defaultGuides: vLines,
61
58
  });
62
59
 
63
60
  this.emit('changeGuides', {
64
- type: GuidesType.VERTICAL,
65
- guides: [],
61
+ type: GuidesType.HORIZONTAL,
62
+ guides: hLines,
66
63
  });
67
64
 
68
65
  this.emit('changeGuides', {
69
- type: GuidesType.HORIZONTAL,
70
- guides: [],
66
+ type: GuidesType.VERTICAL,
67
+ guides: vLines,
71
68
  });
69
+ }
72
70
 
73
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, '[]');
74
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, '[]');
71
+ /**
72
+ * 清空所有参考线
73
+ */
74
+ public clearGuides() {
75
+ this.setGuides([[], []]);
75
76
  }
76
77
 
77
78
  /**
@@ -101,7 +102,7 @@ export default class Rule extends EventEmitter {
101
102
  }
102
103
  }
103
104
 
104
- scrollRule(scrollTop: number) {
105
+ public scrollRule(scrollTop: number) {
105
106
  this.hGuides.scrollGuides(scrollTop);
106
107
  this.hGuides.scroll(0);
107
108
 
@@ -142,8 +143,6 @@ export default class Rule extends EventEmitter {
142
143
  type: GuidesType.HORIZONTAL,
143
144
  guides: this.horizontalGuidelines,
144
145
  });
145
-
146
- globalThis.localStorage.setItem(H_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
147
146
  };
148
147
 
149
148
  private vGuidesChangeGuidesHandler = (e: GuidesEvents['changeGuides']) => {
@@ -152,7 +151,5 @@ export default class Rule extends EventEmitter {
152
151
  type: GuidesType.VERTICAL,
153
152
  guides: this.verticalGuidelines,
154
153
  });
155
-
156
- globalThis.localStorage.setItem(V_GUIDE_LINE_STORAGE_KEY, JSON.stringify(e.guides));
157
154
  };
158
155
  }
package/src/StageCore.ts CHANGED
@@ -230,11 +230,11 @@ export default class StageCore extends EventEmitter {
230
230
  * 挂载Dom节点
231
231
  * @param el 将stage挂载到该Dom节点上
232
232
  */
233
- public mount(el: HTMLDivElement): void {
233
+ public async mount(el: HTMLDivElement) {
234
234
  this.container = el;
235
235
  const { mask, renderer } = this;
236
236
 
237
- renderer.mount(el);
237
+ await renderer.mount(el);
238
238
  mask.mount(el);
239
239
 
240
240
  this.emit('mounted');
@@ -26,7 +26,7 @@ import MoveableHelper from 'moveable-helper';
26
26
  import { DRAG_EL_ID_PREFIX, GHOST_EL_ID_PREFIX, GuidesType, Mode, ZIndex } from './const';
27
27
  import StageCore from './StageCore';
28
28
  import type { SortEventData, StageDragResizeConfig } from './types';
29
- import { getAbsolutePosition, getGuideLineFromCache, getMode, getOffset } from './util';
29
+ import { getAbsolutePosition, getMode, getOffset } from './util';
30
30
 
31
31
  /** 拖动状态 */
32
32
  enum ActionStatus {
@@ -52,9 +52,9 @@ export default class StageDragResize extends EventEmitter {
52
52
  /** Moveable拖拽类实例 */
53
53
  public moveable?: Moveable;
54
54
  /** 水平参考线 */
55
- public horizontalGuidelines: number[] = getGuideLineFromCache(GuidesType.HORIZONTAL);
55
+ public horizontalGuidelines: number[] = [];
56
56
  /** 垂直参考线 */
57
- public verticalGuidelines: number[] = getGuideLineFromCache(GuidesType.VERTICAL);
57
+ public verticalGuidelines: number[] = [];
58
58
  /** 对齐元素集合 */
59
59
  public elementGuidelines: HTMLElement[] = [];
60
60
  /** 布局方式:流式布局、绝对定位、固定定位 */
@@ -111,7 +111,7 @@ export default class StageDragResize extends EventEmitter {
111
111
  */
112
112
  public updateMoveable(el = this.target): void {
113
113
  if (!this.moveable) throw new Error('未初始化moveable');
114
- if (!el) throw new Error('为选中任何节点');
114
+ if (!el) throw new Error('未选中任何节点');
115
115
 
116
116
  this.target = el;
117
117
 
@@ -132,7 +132,9 @@ export default class StageDragResize extends EventEmitter {
132
132
  this.moveableOptions.verticalGuidelines = guidelines;
133
133
  }
134
134
 
135
- this.updateMoveable();
135
+ if (this.moveable) {
136
+ this.updateMoveable();
137
+ }
136
138
  }
137
139
 
138
140
  public clearGuides() {
@@ -207,7 +209,7 @@ export default class StageDragResize extends EventEmitter {
207
209
  }
208
210
 
209
211
  private bindResizeEvent(): void {
210
- if (!this.moveable) throw new Error('moveable 为初始化');
212
+ if (!this.moveable) throw new Error('moveable 未初始化');
211
213
 
212
214
  const frame = {
213
215
  left: 0,
@@ -251,7 +253,7 @@ export default class StageDragResize extends EventEmitter {
251
253
  }
252
254
 
253
255
  private bindDragEvent(): void {
254
- if (!this.moveable) throw new Error('moveable 为初始化');
256
+ if (!this.moveable) throw new Error('moveable 未初始化');
255
257
 
256
258
  const frame = {
257
259
  left: 0,
@@ -307,7 +309,7 @@ export default class StageDragResize extends EventEmitter {
307
309
  }
308
310
 
309
311
  private bindRotateEvent(): void {
310
- if (!this.moveable) throw new Error('moveable 为初始化');
312
+ if (!this.moveable) throw new Error('moveable 未初始化');
311
313
 
312
314
  this.moveable
313
315
  .on('rotateStart', (e) => {
@@ -334,7 +336,7 @@ export default class StageDragResize extends EventEmitter {
334
336
  }
335
337
 
336
338
  private bindScaleEvent(): void {
337
- if (!this.moveable) throw new Error('moveable 为初始化');
339
+ if (!this.moveable) throw new Error('moveable 未初始化');
338
340
 
339
341
  this.moveable
340
342
  .on('scaleStart', (e) => {
@@ -499,7 +501,8 @@ export default class StageDragResize extends EventEmitter {
499
501
 
500
502
  bounds: {
501
503
  top: 0,
502
- left: 0,
504
+ // 设置0的话无法移动到left为0,所以只能设置为-1
505
+ left: -1,
503
506
  right: this.container.clientWidth,
504
507
  bottom: this.container.clientHeight,
505
508
  ...(moveableOptions.bounds || {}),
@@ -81,10 +81,6 @@ export default class StageRender extends EventEmitter {
81
81
  }
82
82
 
83
83
  el.appendChild<HTMLIFrameElement>(this.iframe);
84
-
85
- this.contentWindow = this.iframe?.contentWindow as RuntimeWindow;
86
-
87
- this.contentWindow.magic = this.getMagicApi();
88
84
  } else {
89
85
  throw Error('mount 失败');
90
86
  }
@@ -113,7 +109,9 @@ export default class StageRender extends EventEmitter {
113
109
  }
114
110
 
115
111
  private loadHandler = async () => {
116
- this.emit('onload');
112
+ this.contentWindow = this.iframe?.contentWindow as RuntimeWindow;
113
+
114
+ this.contentWindow.magic = this.getMagicApi();
117
115
 
118
116
  if (this.render) {
119
117
  const el = await this.render(this.core);
@@ -121,5 +119,14 @@ export default class StageRender extends EventEmitter {
121
119
  this.iframe?.contentDocument?.body?.appendChild(el);
122
120
  }
123
121
  }
122
+
123
+ this.emit('onload');
124
+
125
+ this.contentWindow.postMessage(
126
+ {
127
+ tmagicRuntimeReady: true,
128
+ },
129
+ '*',
130
+ );
124
131
  };
125
132
  }
package/src/const.ts CHANGED
@@ -22,7 +22,7 @@ export const GHOST_EL_ID_PREFIX = 'ghost_el_';
22
22
  /** 拖动的时候需要在蒙层中创建一个占位节点,该节点的id前缀 */
23
23
  export const DRAG_EL_ID_PREFIX = 'drag_el_';
24
24
 
25
- /** 高亮事需要在蒙层中创建一个占位节点,该节点的id前缀 */
25
+ /** 高亮时需要在蒙层中创建一个占位节点,该节点的id前缀 */
26
26
  export const HIGHLIGHT_EL_ID_PREFIX = 'highlight_el_';
27
27
 
28
28
  /** 默认放到缩小倍数 */
@@ -68,6 +68,3 @@ export enum Mode {
68
68
 
69
69
  /** 选中节点的class name */
70
70
  export const SELECTED_CLASS = 'tmagic-stage-selected-area';
71
-
72
- export const H_GUIDE_LINE_STORAGE_KEY = '$MagicStageHorizontalGuidelinesData';
73
- export const V_GUIDE_LINE_STORAGE_KEY = '$MagicStageVerticalGuidelinesData';
package/src/index.ts CHANGED
@@ -23,4 +23,5 @@ export { default as StageRender } from './StageRender';
23
23
  export { default as StageMask } from './StageMask';
24
24
  export { default as StageDragResize } from './StageDragResize';
25
25
  export * from './types';
26
+ export * from './const';
26
27
  export default StageCore;
package/src/types.ts CHANGED
@@ -105,7 +105,7 @@ export interface Runtime {
105
105
  getApp?: () => Core;
106
106
  beforeSelect?: (el: HTMLElement) => Promise<boolean> | boolean;
107
107
  getSnapElements?: (el?: HTMLElement) => HTMLElement[];
108
- updateRootConfig: (config: MApp) => void;
108
+ updateRootConfig?: (config: MApp) => void;
109
109
  updatePageId?: (id: Id) => void;
110
110
  select?: (id: Id) => Promise<HTMLElement> | HTMLElement;
111
111
  add?: (data: UpdateData) => void;
package/src/util.ts CHANGED
@@ -16,7 +16,7 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- import { GuidesType, H_GUIDE_LINE_STORAGE_KEY, Mode, SELECTED_CLASS, V_GUIDE_LINE_STORAGE_KEY } from './const';
19
+ import { Mode, SELECTED_CLASS } from './const';
20
20
  import type { Offset } from './types';
21
21
 
22
22
  const getParents = (el: Element, relative: Element) => {
@@ -73,31 +73,19 @@ export const isSameDomain = (targetUrl = '', source = globalThis.location.host)
73
73
  return getHost(targetUrl) === source;
74
74
  };
75
75
 
76
- export const isAbsolute = (el?: HTMLElement): boolean => {
77
- if (!el) return false;
78
- return getComputedStyle(el).position === 'absolute';
79
- };
76
+ export const isAbsolute = (style: CSSStyleDeclaration): boolean => style.position === 'absolute';
80
77
 
81
- export const isRelative = (el?: HTMLElement): boolean => {
82
- if (!el) return false;
83
- return getComputedStyle(el).position === 'relative';
84
- };
78
+ export const isRelative = (style: CSSStyleDeclaration): boolean => style.position === 'relative';
85
79
 
86
- export const isStatic = (el?: HTMLElement): boolean => {
87
- if (!el) return false;
88
- return getComputedStyle(el).position === 'static';
89
- };
80
+ export const isStatic = (style: CSSStyleDeclaration): boolean => style.position === 'static';
90
81
 
91
- export const isFixed = (el?: HTMLElement): boolean => {
92
- if (!el) return false;
93
- return getComputedStyle(el).position === 'fixed';
94
- };
82
+ export const isFixed = (style: CSSStyleDeclaration): boolean => style.position === 'fixed';
95
83
 
96
84
  export const isFixedParent = (el: HTMLElement) => {
97
85
  let fixed = false;
98
86
  let dom = el;
99
87
  while (dom) {
100
- fixed = isFixed(dom);
88
+ fixed = isFixed(getComputedStyle(dom));
101
89
  if (fixed) {
102
90
  break;
103
91
  }
@@ -112,7 +100,8 @@ export const isFixedParent = (el: HTMLElement) => {
112
100
 
113
101
  export const getMode = (el: HTMLElement): Mode => {
114
102
  if (isFixedParent(el)) return Mode.FIXED;
115
- if (isStatic(el) || isRelative(el)) return Mode.SORTABLE;
103
+ const style = getComputedStyle(el);
104
+ if (isStatic(style) || isRelative(style)) return Mode.SORTABLE;
116
105
  return Mode.ABSOLUTE;
117
106
  };
118
107
 
@@ -120,13 +109,14 @@ export const getScrollParent = (element: HTMLElement, includeHidden = false): HT
120
109
  let style = getComputedStyle(element);
121
110
  const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
122
111
 
123
- if (isFixed(element)) return null;
112
+ if (isFixed(style)) return null;
113
+
124
114
  for (let parent = element; parent.parentElement; ) {
125
115
  parent = parent.parentElement;
126
116
  style = getComputedStyle(parent);
127
- if (isAbsolute(element) && isStatic(element)) {
128
- continue;
129
- }
117
+
118
+ if (isAbsolute(style) && isStatic(style)) continue;
119
+
130
120
  if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) return parent;
131
121
  }
132
122
 
@@ -159,23 +149,3 @@ export const addSelectedClassName = (el: Element, doc: Document) => {
159
149
  item.classList.add(`${SELECTED_CLASS}-parents`);
160
150
  });
161
151
  };
162
-
163
- export const getGuideLineFromCache = (type: GuidesType): number[] => {
164
- const key = {
165
- [GuidesType.HORIZONTAL]: H_GUIDE_LINE_STORAGE_KEY,
166
- [GuidesType.VERTICAL]: V_GUIDE_LINE_STORAGE_KEY,
167
- }[type];
168
-
169
- if (!key) return [];
170
-
171
- const guideLineCacheData = globalThis.localStorage.getItem(key);
172
- if (guideLineCacheData) {
173
- try {
174
- return JSON.parse(guideLineCacheData) || [];
175
- } catch (e) {
176
- console.error(e);
177
- }
178
- }
179
-
180
- return [];
181
- };