@hprint/plugins 0.0.1-alpha.3 → 0.0.1

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.
Files changed (50) hide show
  1. package/dist/index.js +17 -17
  2. package/dist/index.mjs +1282 -1277
  3. package/dist/src/plugins/CreateElementPlugin.d.ts +4 -0
  4. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -1
  5. package/dist/src/utils/ruler/ruler.d.ts +1 -0
  6. package/dist/src/utils/ruler/ruler.d.ts.map +1 -1
  7. package/package.json +3 -3
  8. package/src/assets/style/resizePlugin.css +27 -27
  9. package/src/objects/Arrow.js +47 -47
  10. package/src/objects/ThinTailArrow.js +50 -50
  11. package/src/plugins/AlignGuidLinePlugin.ts +1152 -1152
  12. package/src/plugins/ControlsPlugin.ts +251 -251
  13. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  14. package/src/plugins/CopyPlugin.ts +255 -255
  15. package/src/plugins/CreateElementPlugin.ts +23 -2
  16. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  17. package/src/plugins/DrawLinePlugin.ts +162 -162
  18. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  19. package/src/plugins/DringPlugin.ts +125 -125
  20. package/src/plugins/FlipPlugin.ts +59 -59
  21. package/src/plugins/FontPlugin.ts +165 -165
  22. package/src/plugins/FreeDrawPlugin.ts +49 -49
  23. package/src/plugins/GroupAlignPlugin.ts +365 -365
  24. package/src/plugins/GroupPlugin.ts +82 -82
  25. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  26. package/src/plugins/HistoryPlugin.ts +181 -181
  27. package/src/plugins/ImageStroke.ts +121 -121
  28. package/src/plugins/LayerPlugin.ts +108 -108
  29. package/src/plugins/LockPlugin.ts +242 -242
  30. package/src/plugins/MaskPlugin.ts +155 -155
  31. package/src/plugins/MaterialPlugin.ts +224 -224
  32. package/src/plugins/MiddleMousePlugin.ts +45 -45
  33. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  34. package/src/plugins/PathTextPlugin.ts +89 -89
  35. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  36. package/src/plugins/PrintPlugin.ts +81 -81
  37. package/src/plugins/PsdPlugin.ts +52 -52
  38. package/src/plugins/ResizePlugin.ts +278 -278
  39. package/src/plugins/RulerPlugin.ts +78 -78
  40. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  41. package/src/plugins/UnitPlugin.ts +326 -326
  42. package/src/plugins/WaterMarkPlugin.ts +257 -257
  43. package/src/types/eventType.ts +11 -11
  44. package/src/utils/psd.js +432 -432
  45. package/src/utils/ruler/guideline.ts +145 -145
  46. package/src/utils/ruler/index.ts +91 -91
  47. package/src/utils/ruler/ruler.ts +936 -924
  48. package/src/utils/ruler/utils.ts +162 -162
  49. package/tsconfig.json +10 -10
  50. package/vite.config.ts +29 -29
@@ -1,257 +1,257 @@
1
- import { cloneDeep } from 'lodash-es';
2
- import { fabric } from '@hprint/core';
3
- import type { IEditor, IPluginTempl } from '@hprint/core';
4
-
5
- type IPlugin = Pick<
6
- WaterMarkPlugin,
7
- 'drawWaterMark' | 'clearWaterMMatk' | 'updateDrawStatus'
8
- >;
9
-
10
- declare module '@hprint/core' {
11
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
12
- interface IEditor extends IPlugin { }
13
- }
14
-
15
- enum POSITION {
16
- lt = 'Left_Top',
17
- lb = 'Left_Right',
18
- rt = 'Right_Top',
19
- rb = 'Right_Bottom',
20
- full = 'Full',
21
- }
22
-
23
- type IPosition =
24
- | POSITION.lt
25
- | POSITION.lb
26
- | POSITION.rt
27
- | POSITION.rb
28
- | POSITION.full; // lt 左上 lr 左上 rt 右上 rb 右下 full 平铺 后续可扩展其他功能
29
- type IDrawOps = {
30
- text: string;
31
- size: number;
32
- fontFamily: string;
33
- color: string;
34
- isRotate: boolean;
35
- position: IPosition;
36
- };
37
-
38
- const defaultOptions: IDrawOps = {
39
- text: '',
40
- size: 24,
41
- isRotate: false, // 是否倾斜
42
- fontFamily: '汉体', // 可考虑自定义字体
43
- color: '#ccc', // 可考虑自定义颜色
44
- position: POSITION.lt,
45
- };
46
-
47
- class WaterMarkPlugin implements IPluginTempl {
48
- static pluginName = 'WaterMarkPlugin';
49
- static apis = ['drawWaterMark', 'clearWaterMMatk', 'updateDrawStatus'];
50
- private hadDraw = false;
51
- private drawOps: IDrawOps = defaultOptions;
52
- constructor(
53
- public canvas: fabric.Canvas,
54
- public editor: IEditor
55
- ) {
56
- this.init();
57
- }
58
-
59
- private createCanvas(width: number, height: number) {
60
- const waterCanvas: HTMLCanvasElement = document.createElement('canvas');
61
- waterCanvas.width = width;
62
- waterCanvas.height = height;
63
- waterCanvas.style.position = 'fixed';
64
- waterCanvas.style.opacity = '0';
65
- waterCanvas.style.zIndex = '-1';
66
- return waterCanvas;
67
- }
68
-
69
- // 待优化
70
- private drawing: Record<IPosition, (...arg: any[]) => void> = {
71
- [POSITION.lt]: (
72
- width: number,
73
- height: number,
74
- cb: (imgString: string) => void
75
- ) => {
76
- let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
77
- width,
78
- height
79
- );
80
- const w = waterCanvas.width || width;
81
- let ctx: CanvasRenderingContext2D | null =
82
- waterCanvas.getContext('2d')!;
83
- ctx.fillStyle = this.drawOps.color;
84
- ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
85
- ctx.fillText(this.drawOps.text, 10, this.drawOps.size + 10, w - 20);
86
- cb && cb(waterCanvas.toDataURL());
87
- waterCanvas = null;
88
- ctx = null;
89
- },
90
- [POSITION.rt]: (
91
- width: number,
92
- height: number,
93
- cb: (imgString: string) => void
94
- ) => {
95
- let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
96
- width,
97
- height
98
- );
99
- let ctx: CanvasRenderingContext2D | null =
100
- waterCanvas.getContext('2d')!;
101
- const w = waterCanvas.width || width;
102
- ctx.fillStyle = this.drawOps.color;
103
- ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
104
- ctx.fillText(
105
- this.drawOps.text,
106
- w - ctx.measureText(this.drawOps.text).width - 20,
107
- this.drawOps.size + 10,
108
- w - 20
109
- );
110
- cb && cb(waterCanvas.toDataURL());
111
- waterCanvas = null;
112
- ctx = null;
113
- },
114
- [POSITION.lb]: (
115
- width: number,
116
- height: number,
117
- cb: (imgString: string) => void
118
- ) => {
119
- let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
120
- width,
121
- height
122
- );
123
- let ctx: CanvasRenderingContext2D | null =
124
- waterCanvas.getContext('2d')!;
125
- const w = waterCanvas.width || width;
126
- const h = waterCanvas.height || height;
127
- ctx.fillStyle = this.drawOps.color;
128
- ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
129
- ctx.fillText(this.drawOps.text, 10, h - this.drawOps.size, w - 20);
130
- cb && cb(waterCanvas.toDataURL());
131
- waterCanvas = null;
132
- ctx = null;
133
- },
134
- [POSITION.rb]: (
135
- width: number,
136
- height: number,
137
- cb: (imgString: string) => void
138
- ) => {
139
- let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
140
- width,
141
- height
142
- );
143
- let ctx: CanvasRenderingContext2D | null =
144
- waterCanvas.getContext('2d')!;
145
- const w = waterCanvas.width || width;
146
- ctx.fillStyle = this.drawOps.color;
147
- ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
148
- ctx.fillText(
149
- this.drawOps.text,
150
- w - ctx.measureText(this.drawOps.text).width - 20,
151
- height - this.drawOps.size,
152
- width - 20
153
- );
154
- cb && cb(waterCanvas.toDataURL());
155
- waterCanvas = null;
156
- ctx = null;
157
- },
158
- [POSITION.full]: (
159
- width: number,
160
- height: number,
161
- cb: (imgString: string) => void
162
- ) => {
163
- const angle = -30; // 按逆时针30度算
164
- const R = (angle * Math.PI) / 180;
165
- const font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
166
- let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
167
- width,
168
- height
169
- );
170
- let ctx: CanvasRenderingContext2D | null =
171
- waterCanvas.getContext('2d')!;
172
- ctx.font = font;
173
- const textW = ctx.measureText(this.drawOps.text).width + 40;
174
- let patternCanvas: HTMLCanvasElement | null = this.createCanvas(
175
- this.drawOps.isRotate
176
- ? textW * Math.abs(Math.cos(R)) + this.drawOps.size
177
- : textW,
178
- this.drawOps.isRotate
179
- ? textW * Math.abs(Math.sin(R)) + this.drawOps.size
180
- : this.drawOps.size + 20
181
- );
182
- document.body.appendChild(patternCanvas);
183
- let ctxWater: CanvasRenderingContext2D | null =
184
- patternCanvas.getContext('2d')!;
185
- ctxWater.textAlign = 'left';
186
- ctxWater.textBaseline = 'top';
187
- ctxWater.font = font;
188
- ctxWater.fillStyle = `${this.drawOps.color}`;
189
- if (this.drawOps.isRotate) {
190
- ctxWater.translate(0, textW * Math.abs(Math.sin(R)));
191
- ctxWater.rotate(R);
192
- ctxWater.fillText(this.drawOps.text, 0, 0);
193
- } else {
194
- ctxWater.fillText(this.drawOps.text, 10, 10);
195
- }
196
- ctx.fillStyle = ctx.createPattern(patternCanvas, 'repeat')!;
197
- ctx.fillRect(0, 0, width, height);
198
- cb && cb(waterCanvas.toDataURL());
199
- waterCanvas = null;
200
- patternCanvas = null;
201
- ctx = null;
202
- ctxWater = null;
203
- },
204
- };
205
-
206
- drawWaterMark(ops: IDrawOps) {
207
- this.drawOps = Object.assign(cloneDeep(this.drawOps), ops);
208
- if (!this.drawOps.text) return;
209
- const workspace = this.canvas
210
- .getObjects()
211
- .find((item: any) => item.id === 'workspace');
212
- const { width, height, left, top }: any = workspace;
213
- this.drawing[this.drawOps?.position](
214
- width,
215
- height,
216
- (imgString: string) => {
217
- this.canvas.overlayImage = undefined;
218
- this.hadDraw = true;
219
- this.canvas.setOverlayImage(
220
- imgString,
221
- this.canvas.renderAll.bind(this.canvas),
222
- {
223
- left: left || 0,
224
- top: top || 0,
225
- originX: 'left',
226
- originY: 'top',
227
- }
228
- );
229
- }
230
- );
231
- }
232
-
233
- // 更新handDrow 导入json时无法知道是否绘制
234
- updateDrawStatus(status: boolean) {
235
- this.hadDraw = status;
236
- }
237
-
238
- clearWaterMMatk() {
239
- if (!this.hadDraw) return;
240
- this.canvas.overlayImage = undefined;
241
- this.canvas.renderAll();
242
- this.hadDraw = false;
243
- this.drawOps = defaultOptions;
244
- }
245
-
246
- init() {
247
- // TODO 这里接受的参数应该时当前单位对应的尺寸信息,可能会有问题
248
- this.editor.on('sizeChange', this.drawWaterMark.bind(this));
249
- }
250
-
251
- destroy() {
252
- // TODO 这里接受的参数应该时当前单位对应的尺寸信息,可能会有问题
253
- this.editor.off('sizeChange', this.drawWaterMark);
254
- }
255
- }
256
-
257
- export default WaterMarkPlugin;
1
+ import { cloneDeep } from 'lodash-es';
2
+ import { fabric } from '@hprint/core';
3
+ import type { IEditor, IPluginTempl } from '@hprint/core';
4
+
5
+ type IPlugin = Pick<
6
+ WaterMarkPlugin,
7
+ 'drawWaterMark' | 'clearWaterMMatk' | 'updateDrawStatus'
8
+ >;
9
+
10
+ declare module '@hprint/core' {
11
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
12
+ interface IEditor extends IPlugin { }
13
+ }
14
+
15
+ enum POSITION {
16
+ lt = 'Left_Top',
17
+ lb = 'Left_Right',
18
+ rt = 'Right_Top',
19
+ rb = 'Right_Bottom',
20
+ full = 'Full',
21
+ }
22
+
23
+ type IPosition =
24
+ | POSITION.lt
25
+ | POSITION.lb
26
+ | POSITION.rt
27
+ | POSITION.rb
28
+ | POSITION.full; // lt 左上 lr 左上 rt 右上 rb 右下 full 平铺 后续可扩展其他功能
29
+ type IDrawOps = {
30
+ text: string;
31
+ size: number;
32
+ fontFamily: string;
33
+ color: string;
34
+ isRotate: boolean;
35
+ position: IPosition;
36
+ };
37
+
38
+ const defaultOptions: IDrawOps = {
39
+ text: '',
40
+ size: 24,
41
+ isRotate: false, // 是否倾斜
42
+ fontFamily: '汉体', // 可考虑自定义字体
43
+ color: '#ccc', // 可考虑自定义颜色
44
+ position: POSITION.lt,
45
+ };
46
+
47
+ class WaterMarkPlugin implements IPluginTempl {
48
+ static pluginName = 'WaterMarkPlugin';
49
+ static apis = ['drawWaterMark', 'clearWaterMMatk', 'updateDrawStatus'];
50
+ private hadDraw = false;
51
+ private drawOps: IDrawOps = defaultOptions;
52
+ constructor(
53
+ public canvas: fabric.Canvas,
54
+ public editor: IEditor
55
+ ) {
56
+ this.init();
57
+ }
58
+
59
+ private createCanvas(width: number, height: number) {
60
+ const waterCanvas: HTMLCanvasElement = document.createElement('canvas');
61
+ waterCanvas.width = width;
62
+ waterCanvas.height = height;
63
+ waterCanvas.style.position = 'fixed';
64
+ waterCanvas.style.opacity = '0';
65
+ waterCanvas.style.zIndex = '-1';
66
+ return waterCanvas;
67
+ }
68
+
69
+ // 待优化
70
+ private drawing: Record<IPosition, (...arg: any[]) => void> = {
71
+ [POSITION.lt]: (
72
+ width: number,
73
+ height: number,
74
+ cb: (imgString: string) => void
75
+ ) => {
76
+ let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
77
+ width,
78
+ height
79
+ );
80
+ const w = waterCanvas.width || width;
81
+ let ctx: CanvasRenderingContext2D | null =
82
+ waterCanvas.getContext('2d')!;
83
+ ctx.fillStyle = this.drawOps.color;
84
+ ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
85
+ ctx.fillText(this.drawOps.text, 10, this.drawOps.size + 10, w - 20);
86
+ cb && cb(waterCanvas.toDataURL());
87
+ waterCanvas = null;
88
+ ctx = null;
89
+ },
90
+ [POSITION.rt]: (
91
+ width: number,
92
+ height: number,
93
+ cb: (imgString: string) => void
94
+ ) => {
95
+ let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
96
+ width,
97
+ height
98
+ );
99
+ let ctx: CanvasRenderingContext2D | null =
100
+ waterCanvas.getContext('2d')!;
101
+ const w = waterCanvas.width || width;
102
+ ctx.fillStyle = this.drawOps.color;
103
+ ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
104
+ ctx.fillText(
105
+ this.drawOps.text,
106
+ w - ctx.measureText(this.drawOps.text).width - 20,
107
+ this.drawOps.size + 10,
108
+ w - 20
109
+ );
110
+ cb && cb(waterCanvas.toDataURL());
111
+ waterCanvas = null;
112
+ ctx = null;
113
+ },
114
+ [POSITION.lb]: (
115
+ width: number,
116
+ height: number,
117
+ cb: (imgString: string) => void
118
+ ) => {
119
+ let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
120
+ width,
121
+ height
122
+ );
123
+ let ctx: CanvasRenderingContext2D | null =
124
+ waterCanvas.getContext('2d')!;
125
+ const w = waterCanvas.width || width;
126
+ const h = waterCanvas.height || height;
127
+ ctx.fillStyle = this.drawOps.color;
128
+ ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
129
+ ctx.fillText(this.drawOps.text, 10, h - this.drawOps.size, w - 20);
130
+ cb && cb(waterCanvas.toDataURL());
131
+ waterCanvas = null;
132
+ ctx = null;
133
+ },
134
+ [POSITION.rb]: (
135
+ width: number,
136
+ height: number,
137
+ cb: (imgString: string) => void
138
+ ) => {
139
+ let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
140
+ width,
141
+ height
142
+ );
143
+ let ctx: CanvasRenderingContext2D | null =
144
+ waterCanvas.getContext('2d')!;
145
+ const w = waterCanvas.width || width;
146
+ ctx.fillStyle = this.drawOps.color;
147
+ ctx.font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
148
+ ctx.fillText(
149
+ this.drawOps.text,
150
+ w - ctx.measureText(this.drawOps.text).width - 20,
151
+ height - this.drawOps.size,
152
+ width - 20
153
+ );
154
+ cb && cb(waterCanvas.toDataURL());
155
+ waterCanvas = null;
156
+ ctx = null;
157
+ },
158
+ [POSITION.full]: (
159
+ width: number,
160
+ height: number,
161
+ cb: (imgString: string) => void
162
+ ) => {
163
+ const angle = -30; // 按逆时针30度算
164
+ const R = (angle * Math.PI) / 180;
165
+ const font = `${this.drawOps.size}px ${this.drawOps.fontFamily}`;
166
+ let waterCanvas: HTMLCanvasElement | null = this.createCanvas(
167
+ width,
168
+ height
169
+ );
170
+ let ctx: CanvasRenderingContext2D | null =
171
+ waterCanvas.getContext('2d')!;
172
+ ctx.font = font;
173
+ const textW = ctx.measureText(this.drawOps.text).width + 40;
174
+ let patternCanvas: HTMLCanvasElement | null = this.createCanvas(
175
+ this.drawOps.isRotate
176
+ ? textW * Math.abs(Math.cos(R)) + this.drawOps.size
177
+ : textW,
178
+ this.drawOps.isRotate
179
+ ? textW * Math.abs(Math.sin(R)) + this.drawOps.size
180
+ : this.drawOps.size + 20
181
+ );
182
+ document.body.appendChild(patternCanvas);
183
+ let ctxWater: CanvasRenderingContext2D | null =
184
+ patternCanvas.getContext('2d')!;
185
+ ctxWater.textAlign = 'left';
186
+ ctxWater.textBaseline = 'top';
187
+ ctxWater.font = font;
188
+ ctxWater.fillStyle = `${this.drawOps.color}`;
189
+ if (this.drawOps.isRotate) {
190
+ ctxWater.translate(0, textW * Math.abs(Math.sin(R)));
191
+ ctxWater.rotate(R);
192
+ ctxWater.fillText(this.drawOps.text, 0, 0);
193
+ } else {
194
+ ctxWater.fillText(this.drawOps.text, 10, 10);
195
+ }
196
+ ctx.fillStyle = ctx.createPattern(patternCanvas, 'repeat')!;
197
+ ctx.fillRect(0, 0, width, height);
198
+ cb && cb(waterCanvas.toDataURL());
199
+ waterCanvas = null;
200
+ patternCanvas = null;
201
+ ctx = null;
202
+ ctxWater = null;
203
+ },
204
+ };
205
+
206
+ drawWaterMark(ops: IDrawOps) {
207
+ this.drawOps = Object.assign(cloneDeep(this.drawOps), ops);
208
+ if (!this.drawOps.text) return;
209
+ const workspace = this.canvas
210
+ .getObjects()
211
+ .find((item: any) => item.id === 'workspace');
212
+ const { width, height, left, top }: any = workspace;
213
+ this.drawing[this.drawOps?.position](
214
+ width,
215
+ height,
216
+ (imgString: string) => {
217
+ this.canvas.overlayImage = undefined;
218
+ this.hadDraw = true;
219
+ this.canvas.setOverlayImage(
220
+ imgString,
221
+ this.canvas.renderAll.bind(this.canvas),
222
+ {
223
+ left: left || 0,
224
+ top: top || 0,
225
+ originX: 'left',
226
+ originY: 'top',
227
+ }
228
+ );
229
+ }
230
+ );
231
+ }
232
+
233
+ // 更新handDrow 导入json时无法知道是否绘制
234
+ updateDrawStatus(status: boolean) {
235
+ this.hadDraw = status;
236
+ }
237
+
238
+ clearWaterMMatk() {
239
+ if (!this.hadDraw) return;
240
+ this.canvas.overlayImage = undefined;
241
+ this.canvas.renderAll();
242
+ this.hadDraw = false;
243
+ this.drawOps = defaultOptions;
244
+ }
245
+
246
+ init() {
247
+ // TODO 这里接受的参数应该时当前单位对应的尺寸信息,可能会有问题
248
+ this.editor.on('sizeChange', this.drawWaterMark.bind(this));
249
+ }
250
+
251
+ destroy() {
252
+ // TODO 这里接受的参数应该时当前单位对应的尺寸信息,可能会有问题
253
+ this.editor.off('sizeChange', this.drawWaterMark);
254
+ }
255
+ }
256
+
257
+ export default WaterMarkPlugin;
@@ -1,11 +1,11 @@
1
- // 选择事件(用于广播)
2
- export enum SelectEvent {
3
- ONE = 'selectOne',
4
- MULTI = 'selectMultiple',
5
- CANCEL = 'selectCancel',
6
- }
7
- export enum SelectMode {
8
- EMPTY = '',
9
- ONE = 'one',
10
- MULTI = 'multiple',
11
- }
1
+ // 选择事件(用于广播)
2
+ export enum SelectEvent {
3
+ ONE = 'selectOne',
4
+ MULTI = 'selectMultiple',
5
+ CANCEL = 'selectCancel',
6
+ }
7
+ export enum SelectMode {
8
+ EMPTY = '',
9
+ ONE = 'one',
10
+ MULTI = 'multiple',
11
+ }