@tmagic/stage 1.0.0-beta.7 → 1.0.0-beta.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.
@@ -0,0 +1,32 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import Guides from '@scena/guides';
4
+ export default class Rule extends EventEmitter {
5
+ hGuides: Guides;
6
+ vGuides: Guides;
7
+ horizontalGuidelines: number[];
8
+ verticalGuidelines: number[];
9
+ private container;
10
+ private containerResizeObserver;
11
+ constructor(container: HTMLDivElement);
12
+ /**
13
+ * 是否显示标尺
14
+ * @param show 是否显示
15
+ */
16
+ showGuides(show?: boolean): void;
17
+ /**
18
+ * 清空所有参考线
19
+ */
20
+ clearGuides(): void;
21
+ /**
22
+ * 是否显示标尺
23
+ * @param show 是否显示
24
+ */
25
+ showRule(show?: boolean): void;
26
+ scrollRule(scrollTop: number): void;
27
+ destroy(): void;
28
+ private getGuidesStyle;
29
+ private createGuides;
30
+ private hGuidesChangeGuidesHandler;
31
+ private vGuidesChangeGuidesHandler;
32
+ }
@@ -12,12 +12,18 @@ export default class StageCore extends EventEmitter {
12
12
  dr: StageDragResize;
13
13
  config: StageCoreConfig;
14
14
  zoom: number;
15
+ private canSelect;
15
16
  constructor(config: StageCoreConfig);
17
+ setElementFromPoint(event: MouseEvent): Promise<void>;
16
18
  /**
17
19
  * 选中组件
18
20
  * @param idOrEl 组件Dom节点的id属性,或者Dom节点
19
21
  */
20
- select(idOrEl: Id | HTMLElement): Promise<void>;
22
+ select(idOrEl: Id | HTMLElement, event?: MouseEvent): Promise<void>;
23
+ /**
24
+ * 更新选中的节点
25
+ * @param data 更新的数据
26
+ */
21
27
  update(data: UpdateData): void;
22
28
  sortNode(data: SortEventData): void;
23
29
  add(data: UpdateData): void;
@@ -36,9 +42,4 @@ export default class StageCore extends EventEmitter {
36
42
  * 销毁实例
37
43
  */
38
44
  destroy(): void;
39
- private maskScrollHandler;
40
- private hGuidesChangeGuidesHandler;
41
- private vGuidesChangeGuidesHandler;
42
- private setMaskLayout;
43
- private beforeSelect;
44
45
  }
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
3
  import Moveable from 'moveable';
4
+ import { GuidesType } from './const';
4
5
  import StageCore from './StageCore';
5
6
  import type { SortEventData, StageDragResizeConfig } from './types';
6
7
  /**
@@ -10,41 +11,43 @@ export default class StageDragResize extends EventEmitter {
10
11
  core: StageCore;
11
12
  container: HTMLElement;
12
13
  target?: HTMLElement;
14
+ dragEl?: HTMLElement;
13
15
  moveable?: Moveable;
14
16
  horizontalGuidelines: number[];
15
17
  verticalGuidelines: number[];
18
+ private moveableOptions;
16
19
  private dragStatus;
17
- private elObserver?;
18
20
  private ghostEl;
19
21
  private mode;
20
22
  constructor(config: StageDragResizeConfig);
21
23
  /**
22
24
  * 将选中框渲染并覆盖到选中的组件Dom节点上方
25
+ * 当选中的节点是不是absolute时,会创建一个新的节点出来作为拖拽目标
23
26
  * @param el 选中组件的Dom节点元素
27
+ * @param event 鼠标事件
24
28
  */
25
- select(el: HTMLElement): Promise<void>;
29
+ select(el: HTMLElement, event?: MouseEvent): Promise<void>;
26
30
  /**
27
31
  * 初始化选中框并渲染出来
28
- * @param param0
29
32
  */
30
33
  refresh(): Promise<void>;
31
- setVGuidelines(verticalGuidelines: number[]): Promise<void>;
32
- setHGuidelines(horizontalGuidelines: number[]): Promise<void>;
33
- updateMoveableTarget(target?: HTMLElement): void;
34
+ setGuidelines(type: GuidesType, guidelines: number[]): void;
35
+ clearGuides(): void;
34
36
  /**
35
37
  * 销毁实例
36
38
  */
37
39
  destroy(): void;
40
+ private initMoveable;
38
41
  private bindResizeEvent;
39
42
  private bindDragEvent;
40
43
  private getSnapElements;
41
44
  private sort;
42
- private drag;
45
+ private update;
43
46
  private generateGhostEl;
44
47
  private destroyGhostEl;
48
+ private generateDragEl;
49
+ private destroyDragEl;
45
50
  private getOptions;
46
- private initObserver;
47
- private syncRect;
48
51
  private calcValueByFontsize;
49
52
  }
50
53
  /**
@@ -1,32 +1,28 @@
1
- /// <reference types="node" />
2
- import { EventEmitter } from 'events';
3
- import Guides from '@scena/guides';
1
+ import { Mode } from './const';
2
+ import Rule from './Rule';
4
3
  import type StageCore from './StageCore';
5
4
  import type { StageMaskConfig } from './types';
6
5
  /**
7
6
  * 蒙层
8
7
  * @description 用于拦截页面的点击动作,避免点击时触发组件自身动作;在编辑器中点击组件应当是选中组件;
9
8
  */
10
- export default class StageMask extends EventEmitter {
9
+ export default class StageMask extends Rule {
11
10
  content: HTMLDivElement;
12
11
  wrapper: HTMLDivElement;
13
12
  core: StageCore;
14
13
  page: HTMLElement | null;
15
- hGuides: Guides;
16
- vGuides: Guides;
17
- private target;
18
- private resizeObserver;
19
- private parentResizeObserver;
20
- private canSelect;
14
+ pageScrollParent: HTMLElement | null;
15
+ scrollTop: number;
16
+ scrollLeft: number;
17
+ width: number;
18
+ height: number;
19
+ wrapperHeight: number;
20
+ wrapperWidth: number;
21
+ private mode;
22
+ private pageResizeObserver;
23
+ private wrapperResizeObserver;
21
24
  constructor(config: StageMaskConfig);
22
- /**
23
- * 设置成固定定位模式
24
- */
25
- setFixed(): void;
26
- /**
27
- * 设置成绝对定位模式
28
- */
29
- setAbsolute(): void;
25
+ setMode(mode: Mode): void;
30
26
  /**
31
27
  * 监听页面大小变化
32
28
  * @description 同步页面与mask的大小
@@ -38,24 +34,13 @@ export default class StageMask extends EventEmitter {
38
34
  * @param el 将蒙层挂载到该Dom节点上
39
35
  */
40
36
  mount(el: HTMLDivElement): void;
37
+ setLayout(el: HTMLElement): void;
41
38
  /**
42
39
  * 销毁实例
43
40
  */
44
41
  destroy(): void;
45
- /**
46
- * 是否显示标尺
47
- * @param show 是否显示
48
- */
49
- showGuides(show?: boolean): void;
50
- /**
51
- * 是否显示标尺
52
- * @param show 是否显示
53
- */
54
- showRule(show?: boolean): void;
55
- /**
56
- * 清空所有参考线
57
- */
58
- clearGuides(): void;
42
+ private scroll;
43
+ private scrollTo;
59
44
  /**
60
45
  * 设置蒙层高度
61
46
  * @param height 高度
@@ -72,9 +57,5 @@ export default class StageMask extends EventEmitter {
72
57
  */
73
58
  private mouseDownHandler;
74
59
  private mouseUpHandler;
75
- private mouseMoveHandler;
76
- private contextmenuHandler;
77
- private select;
78
- private getGuidesStyle;
79
- private createGuides;
60
+ private mouseWheelHandler;
80
61
  }
@@ -25,4 +25,5 @@ export default class StageRender extends EventEmitter {
25
25
  * 销毁实例
26
26
  */
27
27
  destroy(): void;
28
+ private loadHandler;
28
29
  }
@@ -1,2 +1,19 @@
1
1
  export declare const GHOST_EL_ID_PREFIX = "ghost_el_";
2
2
  export declare const DEFAULT_ZOOM = 1;
3
+ export declare enum GuidesType {
4
+ HORIZONTAL = "horizontal",
5
+ VERTICAL = "vertical"
6
+ }
7
+ export declare enum ZIndex {
8
+ MASK = "99999"
9
+ }
10
+ export declare enum MouseButton {
11
+ LEFT = 0,
12
+ MIDDLE = 1,
13
+ RIGHT = 2
14
+ }
15
+ export declare enum Mode {
16
+ ABSOLUTE = "absolute",
17
+ FIXED = "fixed",
18
+ SORTABLE = "sortable"
19
+ }
@@ -1,5 +1,6 @@
1
1
  import { MoveableOptions } from 'react-moveable/declaration/types';
2
2
  import { Id, MApp, MNode } from '@tmagic/schema';
3
+ import { GuidesType } from './const';
3
4
  import StageCore from './StageCore';
4
5
  export declare type CanSelect = (el: HTMLElement, stop: () => boolean) => boolean | Promise<boolean>;
5
6
  export declare type StageCoreConfig = {
@@ -31,6 +32,10 @@ export interface Offset {
31
32
  left: number;
32
33
  top: number;
33
34
  }
35
+ export interface GuidesEventData {
36
+ type: GuidesType;
37
+ guides: number[];
38
+ }
34
39
  export interface UpdateEventData {
35
40
  el: HTMLElement;
36
41
  ghostEl: HTMLElement;
@@ -73,12 +78,3 @@ export interface Magic {
73
78
  export interface RuntimeWindow extends Window {
74
79
  magic: Magic;
75
80
  }
76
- export declare enum ZIndex {
77
- MASK = "99999",
78
- GHOST_EL = "99998"
79
- }
80
- export declare enum MouseButton {
81
- LEFT = 0,
82
- MIDDLE = 1,
83
- RIGHT = 2
84
- }
@@ -1,9 +1,5 @@
1
+ import { Mode } from './const';
1
2
  import type { Offset } from './types';
2
- export declare enum Mode {
3
- ABSOLUTE = "absolute",
4
- FIXED = "fixed",
5
- SORTABLE = "sortable"
6
- }
7
3
  export declare const getOffset: (el: HTMLElement) => Offset;
8
4
  export declare const getAbsolutePosition: (el: HTMLElement, { top, left }: Offset) => {
9
5
  left: number;
@@ -15,4 +11,10 @@ export declare const isAbsolute: (el?: HTMLElement | undefined) => boolean;
15
11
  export declare const isRelative: (el?: HTMLElement | undefined) => boolean;
16
12
  export declare const isStatic: (el?: HTMLElement | undefined) => boolean;
17
13
  export declare const isFixed: (el?: HTMLElement | undefined) => boolean;
14
+ export declare const isFixedParent: (el: HTMLElement) => boolean;
18
15
  export declare const getMode: (el: HTMLElement) => Mode;
16
+ export declare const getScrollParent: (element: HTMLElement, includeHidden?: boolean) => HTMLElement | null;
17
+ export declare const createDiv: ({ className, cssText }: {
18
+ className: string;
19
+ cssText: string;
20
+ }) => HTMLDivElement;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0-beta.7",
2
+ "version": "1.0.0-beta.8",
3
3
  "name": "@tmagic/stage",
4
4
  "sideEffects": false,
5
5
  "main": "dist/tmagic-stage.umd.js",
@@ -23,11 +23,11 @@
23
23
  },
24
24
  "dependencies": {
25
25
  "@scena/guides": "^0.17.0",
26
- "@tmagic/schema": "^1.0.0-beta.6",
27
- "@tmagic/utils": "^1.0.0-beta.6",
26
+ "@tmagic/schema": "^1.0.0-beta.8",
27
+ "@tmagic/utils": "^1.0.0-beta.8",
28
28
  "events": "^3.3.0",
29
29
  "lodash-es": "^4.17.21",
30
- "moveable": "^0.25.3"
30
+ "moveable": "^0.28.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/events": "^3.0.0",
@@ -37,5 +37,6 @@
37
37
  "typescript": "^4.3.4",
38
38
  "vite": "^2.3.7",
39
39
  "vite-plugin-dts": "^0.9.6"
40
- }
40
+ },
41
+ "gitHead": "ff8e32901775ecc145a82f906d6af6119652d61e"
41
42
  }
package/src/Rule.ts ADDED
@@ -0,0 +1,150 @@
1
+ import EventEmitter from 'events';
2
+
3
+ import Guides, { GuidesEvents } from '@scena/guides';
4
+
5
+ import { GuidesType } from './const';
6
+
7
+ export default class Rule extends EventEmitter {
8
+ public hGuides: Guides;
9
+ public vGuides: Guides;
10
+ public horizontalGuidelines: number[] = [];
11
+ public verticalGuidelines: number[] = [];
12
+
13
+ private container: HTMLDivElement;
14
+ private containerResizeObserver: ResizeObserver;
15
+
16
+ constructor(container: HTMLDivElement) {
17
+ super();
18
+
19
+ this.container = container;
20
+ this.hGuides = this.createGuides(GuidesType.HORIZONTAL);
21
+ this.vGuides = this.createGuides(GuidesType.VERTICAL);
22
+
23
+ this.hGuides.on('changeGuides', this.hGuidesChangeGuidesHandler);
24
+ this.vGuides.on('changeGuides', this.vGuidesChangeGuidesHandler);
25
+
26
+ this.containerResizeObserver = new ResizeObserver(() => {
27
+ this.vGuides.resize();
28
+ this.hGuides.resize();
29
+ });
30
+ this.containerResizeObserver.observe(this.container);
31
+ }
32
+
33
+ /**
34
+ * 是否显示标尺
35
+ * @param show 是否显示
36
+ */
37
+ public showGuides(show = true) {
38
+ this.hGuides.setState({
39
+ showGuides: show,
40
+ });
41
+
42
+ this.vGuides.setState({
43
+ showGuides: show,
44
+ });
45
+ }
46
+
47
+ /**
48
+ * 清空所有参考线
49
+ */
50
+ public clearGuides() {
51
+ this.horizontalGuidelines = [];
52
+ this.verticalGuidelines = [];
53
+
54
+ this.vGuides.setState({
55
+ defaultGuides: [],
56
+ });
57
+
58
+ this.hGuides.setState({
59
+ defaultGuides: [],
60
+ });
61
+
62
+ this.emit('changeGuides', {
63
+ type: GuidesType.VERTICAL,
64
+ guides: [],
65
+ });
66
+
67
+ this.emit('changeGuides', {
68
+ type: GuidesType.HORIZONTAL,
69
+ guides: [],
70
+ });
71
+ }
72
+
73
+ /**
74
+ * 是否显示标尺
75
+ * @param show 是否显示
76
+ */
77
+ public showRule(show = true) {
78
+ // 当尺子隐藏时发现大小变化,显示后会变形,所以这里做重新初始化处理
79
+ if (show) {
80
+ this.hGuides.destroy();
81
+ this.hGuides = this.createGuides(GuidesType.HORIZONTAL, this.horizontalGuidelines);
82
+
83
+ this.vGuides.destroy();
84
+ this.vGuides = this.createGuides(GuidesType.VERTICAL, this.verticalGuidelines);
85
+ } else {
86
+ this.hGuides.setState({
87
+ rulerStyle: {
88
+ visibility: 'hidden',
89
+ },
90
+ });
91
+
92
+ this.vGuides.setState({
93
+ rulerStyle: {
94
+ visibility: 'hidden',
95
+ },
96
+ });
97
+ }
98
+ }
99
+
100
+ scrollRule(scrollTop: number) {
101
+ this.hGuides.scrollGuides(scrollTop);
102
+ this.hGuides.scroll(0);
103
+
104
+ this.vGuides.scrollGuides(0);
105
+ this.vGuides.scroll(scrollTop);
106
+ }
107
+
108
+ public destroy(): void {
109
+ this.hGuides.off('changeGuides', this.hGuidesChangeGuidesHandler);
110
+ this.vGuides.off('changeGuides', this.vGuidesChangeGuidesHandler);
111
+ this.containerResizeObserver.disconnect();
112
+ this.removeAllListeners();
113
+ }
114
+
115
+ private getGuidesStyle = (type: GuidesType) => ({
116
+ position: 'fixed',
117
+ zIndex: 1,
118
+ left: type === GuidesType.HORIZONTAL ? 0 : '-30px',
119
+ top: type === GuidesType.HORIZONTAL ? '-30px' : 0,
120
+ width: type === GuidesType.HORIZONTAL ? '100%' : '30px',
121
+ height: type === GuidesType.HORIZONTAL ? '30px' : '100%',
122
+ });
123
+
124
+ private createGuides = (type: GuidesType, defaultGuides: number[] = []): Guides =>
125
+ new Guides(this.container, {
126
+ type,
127
+ defaultGuides,
128
+ displayDragPos: true,
129
+ backgroundColor: '#fff',
130
+ lineColor: '#000',
131
+ textColor: '#000',
132
+ style: this.getGuidesStyle(type),
133
+ });
134
+
135
+ private hGuidesChangeGuidesHandler = (e: GuidesEvents['changeGuides']) => {
136
+ this.horizontalGuidelines = e.guides;
137
+ this.emit('changeGuides', {
138
+ type: GuidesType.HORIZONTAL,
139
+ guides: this.horizontalGuidelines,
140
+ });
141
+ };
142
+
143
+ private vGuidesChangeGuidesHandler = (e: GuidesEvents['changeGuides']) => {
144
+ this.verticalGuidelines = e.guides;
145
+ this.emit('changeGuides', {
146
+ type: GuidesType.VERTICAL,
147
+ guides: this.verticalGuidelines,
148
+ });
149
+ };
150
+ }