@hprint/plugins 0.0.2 → 0.0.3

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,278 +1,278 @@
1
- import { fabric } from '@hprint/core';
2
- import { throttle } from 'lodash-es';
3
- import '../assets/style/resizePlugin.css';
4
- import type { IEditor, IPluginTempl } from '@hprint/core';
5
-
6
- type IPosition = 'left' | 'right' | 'top' | 'bottom';
7
-
8
- class ResizePlugin implements IPluginTempl {
9
- static pluginName = 'ResizePlugin';
10
- static events = [];
11
- static apis = [];
12
- workspaceEl!: HTMLElement;
13
- // 最小画布尺寸
14
- minSize = { width: 1, height: 1 };
15
- // 控制条
16
- barOpts = {
17
- bWidth: 30, // 宽
18
- bHeight: 6, // 高
19
- bPadding: 10, // 离画布边缘的距离
20
- };
21
- hasCreatedBar = false;
22
- isDragging = false;
23
- dragEl: HTMLElement | null = null;
24
- startPoints = { x: 0, y: 0 };
25
- barOffset = { x: 0, y: 0 };
26
- wsOffset: Record<'left' | 'top' | 'width' | 'height', number> = {
27
- left: 0,
28
- top: 0,
29
- width: 0,
30
- height: 0,
31
- };
32
- constructor(
33
- public canvas: fabric.Canvas,
34
- public editor: IEditor
35
- ) {
36
- this._init();
37
- this._initResizeObserve();
38
- this._addListeners();
39
- }
40
-
41
- _init() {
42
- const workspaceEl = document.querySelector('#workspace') as HTMLElement;
43
- if (!workspaceEl) {
44
- throw new Error('element #workspace is missing, plz check!');
45
- }
46
- this.workspaceEl = workspaceEl;
47
- }
48
-
49
- // 初始化监听器
50
- _initResizeObserve() {
51
- const resizeObserver = new ResizeObserver(
52
- throttle(() => {
53
- this.renderBars();
54
- }, 50)
55
- );
56
- resizeObserver.observe(this.workspaceEl);
57
- }
58
-
59
- // 渲染控制条具体位置
60
- renderBars() {
61
- const viewportTransform = this.canvas.viewportTransform;
62
- const [scaleX, , , scaleY, offsetX, offsetY] = viewportTransform || [];
63
- const workspace = this.getWorkspase() as Required<fabric.Rect>;
64
- const wsWidth = workspace.width * scaleX;
65
- const wsHeight = workspace.height * scaleY;
66
- const wsLeft = workspace.left * scaleX;
67
- const wsTop = workspace.top * scaleY;
68
- const { bWidth, bHeight, bPadding } = this.barOpts;
69
- if (!viewportTransform) return;
70
- // 左控制条
71
- const leftBar = this._getBarFromType('left');
72
- leftBar.style.left = `${offsetX + wsLeft - bHeight - bPadding}px`;
73
- leftBar.style.top = `${offsetY + wsTop + wsHeight / 2 - bWidth / 2}px`;
74
- // 右控制条
75
- const rightBar = this._getBarFromType('right');
76
- rightBar.style.left = `${offsetX + wsLeft + wsWidth + bPadding}px`;
77
- rightBar.style.top = `${offsetY + wsTop + wsHeight / 2 - bWidth / 2}px`;
78
- // 上控制条
79
- const topBar = this._getBarFromType('top');
80
- topBar.style.left = `${offsetX + wsLeft + wsWidth / 2 - bWidth / 2}px`;
81
- topBar.style.top = `${offsetY + wsTop - bHeight - bPadding}px`;
82
- // 下控制条
83
- const bottomBar = this._getBarFromType('bottom');
84
- bottomBar.style.left = `${offsetX + wsLeft + wsWidth / 2 - bWidth / 2}px`;
85
- bottomBar.style.top = `${offsetY + wsTop + wsHeight + bPadding}px`;
86
- // 监听
87
- if (!this.hasCreatedBar) {
88
- this.hasCreatedBar = true;
89
- this._watchDrag();
90
- }
91
- }
92
-
93
- // 获取或创建控制条
94
- _getBarFromType(type: IPosition) {
95
- let bar = document.querySelector(`#resize-${type}-bar`) as HTMLElement;
96
- if (bar) return bar;
97
- bar = document.createElement('div');
98
- bar.id = `resize-${type}-bar`;
99
- bar.className = 'resize-bar';
100
- if (['left', 'right'].includes(type)) {
101
- bar.classList.add('horizontal');
102
- } else {
103
- bar.classList.add('vertical');
104
- }
105
- this.workspaceEl.appendChild(bar);
106
- return bar;
107
- }
108
-
109
- // 监听拖拽相关事件
110
- _watchDrag() {
111
- const barList = Array.from(
112
- document.getElementsByClassName('resize-bar')
113
- ) as HTMLElement[];
114
- barList.forEach((bar) => {
115
- bar.addEventListener('mousedown', (e: MouseEvent) => {
116
- this.isDragging = true;
117
- this.dragEl = bar;
118
- this.dragEl.classList.add('active');
119
- this.startPoints = {
120
- x: e.clientX,
121
- y: e.clientY,
122
- };
123
- this.barOffset = {
124
- x: bar.offsetLeft,
125
- y: bar.offsetTop,
126
- };
127
- const workspace = this.getWorkspase() as Required<fabric.Rect>;
128
- const { width, height, left, top } = workspace;
129
- this.wsOffset = { width, height, left, top };
130
- });
131
- });
132
- document.addEventListener('mousemove', this.eventHandler.onDragging);
133
- document.addEventListener('mouseup', () => {
134
- if (this.isDragging && this.dragEl) {
135
- this.isDragging = false;
136
- this.dragEl.classList.remove('active');
137
- this.dragEl = null;
138
- this.canvas.defaultCursor = 'default';
139
- }
140
- });
141
- }
142
-
143
- // 拖拽更新控制条及画布
144
- onDragging(e: MouseEvent) {
145
- if (this.isDragging && this.dragEl) {
146
- const workspace = this.getWorkspase() as Required<fabric.Rect>;
147
- const viewportTransform = this.canvas.viewportTransform;
148
- const [scaleX, , , scaleY] = viewportTransform || [];
149
- const deltaX = e.clientX - this.startPoints.x;
150
- const deltaY = e.clientY - this.startPoints.y;
151
- const deltaViewX = deltaX / scaleX;
152
- const deltaViewY = deltaY / scaleY;
153
- const type = this.dragEl.id.split('-')[1];
154
- let tempLength = 0;
155
- switch (type) {
156
- case 'left':
157
- tempLength = Math.round(
158
- this.wsOffset.width - deltaViewX * 2
159
- );
160
- if (tempLength >= this.minSize.width) {
161
- this.dragEl.style.left = `${this.barOffset.x + deltaX}px`;
162
- workspace.set(
163
- 'left',
164
- this.wsOffset.left + deltaViewX * 2
165
- );
166
- workspace.set('width', tempLength);
167
- this.editor.syncOriginSizeByUnit(tempLength, undefined);
168
- } else {
169
- workspace.set(
170
- 'left',
171
- this.wsOffset.left +
172
- this.wsOffset.width -
173
- this.minSize.width
174
- );
175
- workspace.set('width', this.minSize.width);
176
- this.editor.syncOriginSizeByUnit(this.minSize.width, undefined);
177
- }
178
- break;
179
- case 'right':
180
- tempLength = Math.round(
181
- this.wsOffset.width + deltaViewX * 2
182
- );
183
- if (tempLength >= this.minSize.width) {
184
- this.dragEl.style.left = `${this.barOffset.x + deltaX}px`;
185
- workspace.set('width', tempLength);
186
- this.editor.syncOriginSizeByUnit(tempLength, undefined);
187
- } else {
188
- workspace.set('width', this.minSize.width);
189
- this.editor.syncOriginSizeByUnit(this.minSize.width, undefined);
190
- }
191
- break;
192
- case 'top':
193
- tempLength = Math.round(
194
- this.wsOffset.height - deltaViewY * 2
195
- );
196
- if (tempLength >= this.minSize.height) {
197
- this.dragEl.style.top = `${this.barOffset.y + deltaY}px`;
198
- workspace.set(
199
- 'top',
200
- this.wsOffset.top + deltaViewY * 2
201
- );
202
- workspace.set('height', tempLength);
203
- this.editor.syncOriginSizeByUnit(undefined, tempLength);
204
- } else {
205
- workspace.set(
206
- 'top',
207
- this.wsOffset.top +
208
- this.wsOffset.height -
209
- this.minSize.height
210
- );
211
- workspace.set('height', this.minSize.height);
212
- this.editor.syncOriginSizeByUnit(undefined, this.minSize.height);
213
- }
214
- break;
215
- case 'bottom':
216
- tempLength = Math.round(
217
- this.wsOffset.height + deltaViewY * 2
218
- );
219
- if (tempLength >= this.minSize.height) {
220
- this.dragEl.style.top = `${this.barOffset.y + deltaY}px`;
221
- workspace.set('height', tempLength);
222
- this.editor.syncOriginSizeByUnit(undefined, tempLength);
223
- } else {
224
- workspace.set('height', this.minSize.height);
225
- this.editor.syncOriginSizeByUnit(undefined, this.minSize.height);
226
- }
227
- break;
228
- default:
229
- break;
230
- }
231
-
232
- this.editor.setCenterFromObject(workspace);
233
- workspace.clone((cloned: fabric.Rect) => {
234
- this.canvas.clipPath = cloned;
235
- this.canvas.requestRenderAll();
236
- });
237
- if (['left', 'right'].includes(type)) {
238
- this.canvas.defaultCursor = 'ew-resize';
239
- } else {
240
- this.canvas.defaultCursor = 'ns-resize';
241
- }
242
- let curUnitWidth = workspace.width;
243
- let curUnitHeight = workspace.height;
244
- if (this.editor.getUnit() !== 'px') {
245
- ({ width: curUnitWidth, height: curUnitHeight } = this.editor.getOriginSize());
246
- }
247
- this.editor.emit('sizeChange', {
248
- width: this.editor.getSizeByUnit(curUnitWidth),
249
- height: this.editor.getSizeByUnit(curUnitHeight),
250
- unit: this.editor.getUnit()
251
- });
252
- }
253
- }
254
-
255
- // 事件句柄缓存
256
- private eventHandler: Record<string, (...args: any) => void> = {
257
- render: throttle(this.renderBars.bind(this), 50),
258
- onDragging: throttle(this.onDragging.bind(this), 30),
259
- };
260
-
261
- // 监听画布渲染
262
- _addListeners() {
263
- this.canvas.on('after:render', this.eventHandler.render);
264
- }
265
-
266
- // 返回workspace对象
267
- getWorkspase() {
268
- return this.canvas
269
- .getObjects()
270
- .find((item) => item.id === 'workspace') as fabric.Rect;
271
- }
272
-
273
- destroy() {
274
- console.log('pluginDestroy');
275
- }
276
- }
277
-
278
- export default ResizePlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import { throttle } from 'lodash-es';
3
+ import '../assets/style/resizePlugin.css';
4
+ import type { IEditor, IPluginTempl } from '@hprint/core';
5
+
6
+ type IPosition = 'left' | 'right' | 'top' | 'bottom';
7
+
8
+ class ResizePlugin implements IPluginTempl {
9
+ static pluginName = 'ResizePlugin';
10
+ static events = [];
11
+ static apis = [];
12
+ workspaceEl!: HTMLElement;
13
+ // 最小画布尺寸
14
+ minSize = { width: 1, height: 1 };
15
+ // 控制条
16
+ barOpts = {
17
+ bWidth: 30, // 宽
18
+ bHeight: 6, // 高
19
+ bPadding: 10, // 离画布边缘的距离
20
+ };
21
+ hasCreatedBar = false;
22
+ isDragging = false;
23
+ dragEl: HTMLElement | null = null;
24
+ startPoints = { x: 0, y: 0 };
25
+ barOffset = { x: 0, y: 0 };
26
+ wsOffset: Record<'left' | 'top' | 'width' | 'height', number> = {
27
+ left: 0,
28
+ top: 0,
29
+ width: 0,
30
+ height: 0,
31
+ };
32
+ constructor(
33
+ public canvas: fabric.Canvas,
34
+ public editor: IEditor
35
+ ) {
36
+ this._init();
37
+ this._initResizeObserve();
38
+ this._addListeners();
39
+ }
40
+
41
+ _init() {
42
+ const workspaceEl = document.querySelector('#workspace') as HTMLElement;
43
+ if (!workspaceEl) {
44
+ throw new Error('element #workspace is missing, plz check!');
45
+ }
46
+ this.workspaceEl = workspaceEl;
47
+ }
48
+
49
+ // 初始化监听器
50
+ _initResizeObserve() {
51
+ const resizeObserver = new ResizeObserver(
52
+ throttle(() => {
53
+ this.renderBars();
54
+ }, 50)
55
+ );
56
+ resizeObserver.observe(this.workspaceEl);
57
+ }
58
+
59
+ // 渲染控制条具体位置
60
+ renderBars() {
61
+ const viewportTransform = this.canvas.viewportTransform;
62
+ const [scaleX, , , scaleY, offsetX, offsetY] = viewportTransform || [];
63
+ const workspace = this.getWorkspase() as Required<fabric.Rect>;
64
+ const wsWidth = workspace.width * scaleX;
65
+ const wsHeight = workspace.height * scaleY;
66
+ const wsLeft = workspace.left * scaleX;
67
+ const wsTop = workspace.top * scaleY;
68
+ const { bWidth, bHeight, bPadding } = this.barOpts;
69
+ if (!viewportTransform) return;
70
+ // 左控制条
71
+ const leftBar = this._getBarFromType('left');
72
+ leftBar.style.left = `${offsetX + wsLeft - bHeight - bPadding}px`;
73
+ leftBar.style.top = `${offsetY + wsTop + wsHeight / 2 - bWidth / 2}px`;
74
+ // 右控制条
75
+ const rightBar = this._getBarFromType('right');
76
+ rightBar.style.left = `${offsetX + wsLeft + wsWidth + bPadding}px`;
77
+ rightBar.style.top = `${offsetY + wsTop + wsHeight / 2 - bWidth / 2}px`;
78
+ // 上控制条
79
+ const topBar = this._getBarFromType('top');
80
+ topBar.style.left = `${offsetX + wsLeft + wsWidth / 2 - bWidth / 2}px`;
81
+ topBar.style.top = `${offsetY + wsTop - bHeight - bPadding}px`;
82
+ // 下控制条
83
+ const bottomBar = this._getBarFromType('bottom');
84
+ bottomBar.style.left = `${offsetX + wsLeft + wsWidth / 2 - bWidth / 2}px`;
85
+ bottomBar.style.top = `${offsetY + wsTop + wsHeight + bPadding}px`;
86
+ // 监听
87
+ if (!this.hasCreatedBar) {
88
+ this.hasCreatedBar = true;
89
+ this._watchDrag();
90
+ }
91
+ }
92
+
93
+ // 获取或创建控制条
94
+ _getBarFromType(type: IPosition) {
95
+ let bar = document.querySelector(`#resize-${type}-bar`) as HTMLElement;
96
+ if (bar) return bar;
97
+ bar = document.createElement('div');
98
+ bar.id = `resize-${type}-bar`;
99
+ bar.className = 'resize-bar';
100
+ if (['left', 'right'].includes(type)) {
101
+ bar.classList.add('horizontal');
102
+ } else {
103
+ bar.classList.add('vertical');
104
+ }
105
+ this.workspaceEl.appendChild(bar);
106
+ return bar;
107
+ }
108
+
109
+ // 监听拖拽相关事件
110
+ _watchDrag() {
111
+ const barList = Array.from(
112
+ document.getElementsByClassName('resize-bar')
113
+ ) as HTMLElement[];
114
+ barList.forEach((bar) => {
115
+ bar.addEventListener('mousedown', (e: MouseEvent) => {
116
+ this.isDragging = true;
117
+ this.dragEl = bar;
118
+ this.dragEl.classList.add('active');
119
+ this.startPoints = {
120
+ x: e.clientX,
121
+ y: e.clientY,
122
+ };
123
+ this.barOffset = {
124
+ x: bar.offsetLeft,
125
+ y: bar.offsetTop,
126
+ };
127
+ const workspace = this.getWorkspase() as Required<fabric.Rect>;
128
+ const { width, height, left, top } = workspace;
129
+ this.wsOffset = { width, height, left, top };
130
+ });
131
+ });
132
+ document.addEventListener('mousemove', this.eventHandler.onDragging);
133
+ document.addEventListener('mouseup', () => {
134
+ if (this.isDragging && this.dragEl) {
135
+ this.isDragging = false;
136
+ this.dragEl.classList.remove('active');
137
+ this.dragEl = null;
138
+ this.canvas.defaultCursor = 'default';
139
+ }
140
+ });
141
+ }
142
+
143
+ // 拖拽更新控制条及画布
144
+ onDragging(e: MouseEvent) {
145
+ if (this.isDragging && this.dragEl) {
146
+ const workspace = this.getWorkspase() as Required<fabric.Rect>;
147
+ const viewportTransform = this.canvas.viewportTransform;
148
+ const [scaleX, , , scaleY] = viewportTransform || [];
149
+ const deltaX = e.clientX - this.startPoints.x;
150
+ const deltaY = e.clientY - this.startPoints.y;
151
+ const deltaViewX = deltaX / scaleX;
152
+ const deltaViewY = deltaY / scaleY;
153
+ const type = this.dragEl.id.split('-')[1];
154
+ let tempLength = 0;
155
+ switch (type) {
156
+ case 'left':
157
+ tempLength = Math.round(
158
+ this.wsOffset.width - deltaViewX * 2
159
+ );
160
+ if (tempLength >= this.minSize.width) {
161
+ this.dragEl.style.left = `${this.barOffset.x + deltaX}px`;
162
+ workspace.set(
163
+ 'left',
164
+ this.wsOffset.left + deltaViewX * 2
165
+ );
166
+ workspace.set('width', tempLength);
167
+ this.editor.syncOriginSizeByUnit(tempLength, undefined);
168
+ } else {
169
+ workspace.set(
170
+ 'left',
171
+ this.wsOffset.left +
172
+ this.wsOffset.width -
173
+ this.minSize.width
174
+ );
175
+ workspace.set('width', this.minSize.width);
176
+ this.editor.syncOriginSizeByUnit(this.minSize.width, undefined);
177
+ }
178
+ break;
179
+ case 'right':
180
+ tempLength = Math.round(
181
+ this.wsOffset.width + deltaViewX * 2
182
+ );
183
+ if (tempLength >= this.minSize.width) {
184
+ this.dragEl.style.left = `${this.barOffset.x + deltaX}px`;
185
+ workspace.set('width', tempLength);
186
+ this.editor.syncOriginSizeByUnit(tempLength, undefined);
187
+ } else {
188
+ workspace.set('width', this.minSize.width);
189
+ this.editor.syncOriginSizeByUnit(this.minSize.width, undefined);
190
+ }
191
+ break;
192
+ case 'top':
193
+ tempLength = Math.round(
194
+ this.wsOffset.height - deltaViewY * 2
195
+ );
196
+ if (tempLength >= this.minSize.height) {
197
+ this.dragEl.style.top = `${this.barOffset.y + deltaY}px`;
198
+ workspace.set(
199
+ 'top',
200
+ this.wsOffset.top + deltaViewY * 2
201
+ );
202
+ workspace.set('height', tempLength);
203
+ this.editor.syncOriginSizeByUnit(undefined, tempLength);
204
+ } else {
205
+ workspace.set(
206
+ 'top',
207
+ this.wsOffset.top +
208
+ this.wsOffset.height -
209
+ this.minSize.height
210
+ );
211
+ workspace.set('height', this.minSize.height);
212
+ this.editor.syncOriginSizeByUnit(undefined, this.minSize.height);
213
+ }
214
+ break;
215
+ case 'bottom':
216
+ tempLength = Math.round(
217
+ this.wsOffset.height + deltaViewY * 2
218
+ );
219
+ if (tempLength >= this.minSize.height) {
220
+ this.dragEl.style.top = `${this.barOffset.y + deltaY}px`;
221
+ workspace.set('height', tempLength);
222
+ this.editor.syncOriginSizeByUnit(undefined, tempLength);
223
+ } else {
224
+ workspace.set('height', this.minSize.height);
225
+ this.editor.syncOriginSizeByUnit(undefined, this.minSize.height);
226
+ }
227
+ break;
228
+ default:
229
+ break;
230
+ }
231
+
232
+ this.editor.setCenterFromObject(workspace);
233
+ workspace.clone((cloned: fabric.Rect) => {
234
+ this.canvas.clipPath = cloned;
235
+ this.canvas.requestRenderAll();
236
+ });
237
+ if (['left', 'right'].includes(type)) {
238
+ this.canvas.defaultCursor = 'ew-resize';
239
+ } else {
240
+ this.canvas.defaultCursor = 'ns-resize';
241
+ }
242
+ let curUnitWidth = workspace.width;
243
+ let curUnitHeight = workspace.height;
244
+ if (this.editor.getUnit() !== 'px') {
245
+ ({ width: curUnitWidth, height: curUnitHeight } = this.editor.getOriginSize());
246
+ }
247
+ this.editor.emit('sizeChange', {
248
+ width: this.editor.getSizeByUnit(curUnitWidth),
249
+ height: this.editor.getSizeByUnit(curUnitHeight),
250
+ unit: this.editor.getUnit()
251
+ });
252
+ }
253
+ }
254
+
255
+ // 事件句柄缓存
256
+ private eventHandler: Record<string, (...args: any) => void> = {
257
+ render: throttle(this.renderBars.bind(this), 50),
258
+ onDragging: throttle(this.onDragging.bind(this), 30),
259
+ };
260
+
261
+ // 监听画布渲染
262
+ _addListeners() {
263
+ this.canvas.on('after:render', this.eventHandler.render);
264
+ }
265
+
266
+ // 返回workspace对象
267
+ getWorkspase() {
268
+ return this.canvas
269
+ .getObjects()
270
+ .find((item) => item.id === 'workspace') as fabric.Rect;
271
+ }
272
+
273
+ destroy() {
274
+ console.log('pluginDestroy');
275
+ }
276
+ }
277
+
278
+ export default ResizePlugin;