@hprint/plugins 0.0.6 → 0.0.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.
Files changed (47) hide show
  1. package/dist/index.js +15 -15
  2. package/dist/index.mjs +1864 -1862
  3. package/dist/src/plugins/BarCodePlugin.d.ts +2 -2
  4. package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -1
  5. package/dist/src/plugins/CopyPlugin.d.ts.map +1 -1
  6. package/dist/src/plugins/CreateElementPlugin.d.ts +2 -2
  7. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -1
  8. package/dist/src/plugins/QrCodePlugin.d.ts +2 -2
  9. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
  10. package/package.json +3 -3
  11. package/src/assets/style/resizePlugin.css +27 -27
  12. package/src/objects/Arrow.js +47 -47
  13. package/src/objects/ThinTailArrow.js +50 -50
  14. package/src/plugins/BarCodePlugin.ts +7 -7
  15. package/src/plugins/ControlsPlugin.ts +413 -413
  16. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  17. package/src/plugins/CopyPlugin.ts +261 -255
  18. package/src/plugins/CreateElementPlugin.ts +3 -1
  19. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  20. package/src/plugins/DrawLinePlugin.ts +162 -162
  21. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  22. package/src/plugins/DringPlugin.ts +125 -125
  23. package/src/plugins/FlipPlugin.ts +59 -59
  24. package/src/plugins/FontPlugin.ts +165 -165
  25. package/src/plugins/FreeDrawPlugin.ts +49 -49
  26. package/src/plugins/GroupPlugin.ts +82 -82
  27. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  28. package/src/plugins/HistoryPlugin.ts +181 -181
  29. package/src/plugins/ImageStroke.ts +121 -121
  30. package/src/plugins/LayerPlugin.ts +108 -108
  31. package/src/plugins/MaskPlugin.ts +155 -155
  32. package/src/plugins/MaterialPlugin.ts +224 -224
  33. package/src/plugins/MiddleMousePlugin.ts +45 -45
  34. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  35. package/src/plugins/PathTextPlugin.ts +89 -89
  36. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  37. package/src/plugins/PrintPlugin.ts +81 -81
  38. package/src/plugins/PsdPlugin.ts +52 -52
  39. package/src/plugins/QrCodePlugin.ts +6 -6
  40. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  41. package/src/types/eventType.ts +11 -11
  42. package/src/utils/psd.js +432 -432
  43. package/src/utils/ruler/guideline.ts +145 -145
  44. package/src/utils/ruler/index.ts +91 -91
  45. package/src/utils/ruler/utils.ts +162 -162
  46. package/tsconfig.json +10 -10
  47. package/vite.config.ts +29 -29
@@ -1,244 +1,244 @@
1
- import { fabric } from '@hprint/core';
2
- import { mathHelper } from '@hprint/shared';
3
- import { get, set } from 'lodash-es';
4
- import type { IEditor, IPluginTempl } from '@hprint/core';
5
-
6
- type IPlugin = Pick<SimpleClipImagePlugin, 'addClipPathToImage' | 'removeClip'>;
7
-
8
- declare module '@hprint/core' {
9
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
10
- interface IEditor extends IPlugin {}
11
- }
12
-
13
- const getBounds = (activeObject: fabric.Object) => {
14
- const { left = 0, top = 0 } = activeObject;
15
- return {
16
- width: activeObject.getScaledWidth(),
17
- height: activeObject.getScaledHeight(),
18
- left,
19
- top,
20
- };
21
- };
22
- const bindInfo = (shell: fabric.Object, activeObject: fabric.Object) => {
23
- bindFlagToObject(shell);
24
- bindFlagToObject(shell, 'targetId', get(activeObject, 'id'));
25
- bindFlagToObject(shell, 'targetType', get(activeObject, 'type'));
26
- };
27
- const bindFlagToObject = (
28
- activeObject: fabric.Object,
29
- key = 'clip',
30
- value: any = true
31
- ) => {
32
- set(activeObject, key, value);
33
- };
34
- const createRectClip = (activeObject: fabric.Object, inverted: boolean) => {
35
- const {
36
- width = 0,
37
- height = 0,
38
- left = 0,
39
- top = 0,
40
- } = getBounds(activeObject);
41
- const clipW = Math.round(width / 2);
42
- const clipH = Math.round(height / 2);
43
- const shell = new fabric.Rect({
44
- width: clipW,
45
- height: clipH,
46
- fill: 'rgba(0,0,0,0)',
47
- originX: 'center',
48
- originY: 'center',
49
- left: left + width / 2,
50
- top: top + height / 2,
51
- });
52
- bindInfo(shell, activeObject);
53
- const clipPath = new fabric.Rect({
54
- absolutePositioned: true,
55
- width: shell.width,
56
- height: shell.height,
57
- originX: 'center',
58
- originY: 'center',
59
- left: shell.left,
60
- top: shell.top,
61
- inverted: inverted,
62
- });
63
- return { clipPath, shell };
64
- };
65
- const createCircleClip = (activeObject: fabric.Object, inverted: boolean) => {
66
- const point = activeObject.getCenterPoint();
67
- const { width } = getBounds(activeObject);
68
- const shell = new fabric.Ellipse({
69
- fill: 'rgba(0,0,0,0)',
70
- originX: 'center',
71
- originY: 'center',
72
- left: point.x,
73
- top: point.y,
74
- rx: width / 4,
75
- ry: width / 4,
76
- });
77
- bindInfo(shell, activeObject);
78
- const clipPath = new fabric.Ellipse({
79
- absolutePositioned: true,
80
- originX: 'center',
81
- originY: 'center',
82
- left: shell.left,
83
- top: shell.top,
84
- inverted: inverted,
85
- rx: shell.rx,
86
- ry: shell.ry,
87
- });
88
- return { shell, clipPath };
89
- };
90
- const createTriClip = (activeObject: fabric.Object, inverted: boolean) => {
91
- const point = activeObject.getCenterPoint();
92
- const { width = 0, height = 0 } = getBounds(activeObject);
93
- const clipW = Math.round(width / 2);
94
- const clipH = Math.round(height / 2);
95
- const shell = new fabric.Triangle({
96
- fill: 'rgba(0,0,0,0)',
97
- originX: 'center',
98
- originY: 'center',
99
- left: point.x,
100
- top: point.y,
101
- width: clipW,
102
- height: clipH,
103
- });
104
- bindInfo(shell, activeObject);
105
- const clipPath = new fabric.Triangle({
106
- absolutePositioned: true,
107
- originX: 'center',
108
- originY: 'center',
109
- left: shell.left,
110
- top: shell.top,
111
- width: shell.width,
112
- height: shell.height,
113
- inverted: inverted,
114
- });
115
- return { shell, clipPath };
116
- };
117
- const createPolygonClip = (activeObject: fabric.Object, inverted: boolean) => {
118
- const point = activeObject.getCenterPoint();
119
- const points = mathHelper.getPolygonVertices(5, 200);
120
- const shell = new fabric.Polygon(points, {
121
- fill: 'rgba(0,0,0,0)',
122
- originY: 'center',
123
- originX: 'center',
124
- left: point.x,
125
- top: point.y,
126
- });
127
- bindInfo(shell, activeObject);
128
- const clipPath = new fabric.Polygon([...points], {
129
- absolutePositioned: true,
130
- originX: 'center',
131
- originY: 'center',
132
- left: shell.left,
133
- top: shell.top,
134
- inverted: inverted,
135
- });
136
- return { shell, clipPath };
137
- };
138
- export default class SimpleClipImagePlugin implements IPluginTempl {
139
- static pluginName = 'SimpleClipImagePlugin';
140
- // static events = ['sizeChange'];
141
- static apis = ['addClipPathToImage', 'removeClip'];
142
- constructor(
143
- public canvas: fabric.Canvas,
144
- public editor: IEditor
145
- ) {}
146
- addClipPathToImage(value: string) {
147
- const activeObject = this.canvas.getActiveObjects()[0];
148
- if (activeObject && activeObject.type === 'image') {
149
- let clip: { shell: fabric.Object; clipPath: fabric.Object } | null =
150
- null;
151
- const [name, inverted] = value.split('-');
152
- const isInverted = !!inverted;
153
- switch (name) {
154
- case 'polygon':
155
- clip = createPolygonClip(activeObject, isInverted);
156
- break;
157
- case 'rect':
158
- clip = createRectClip(activeObject, isInverted);
159
- break;
160
- case 'circle':
161
- clip = createCircleClip(activeObject, isInverted);
162
- break;
163
- case 'triangle':
164
- clip = createTriClip(activeObject, isInverted);
165
- break;
166
- }
167
- if (clip == null) return;
168
- const { shell, clipPath } = clip;
169
- shell.on('moving', () => {
170
- clipPath.setPositionByOrigin(
171
- shell.getCenterPoint(),
172
- 'center',
173
- 'center'
174
- );
175
- activeObject.set('dirty', true);
176
- });
177
- shell.on('rotating', () => {
178
- clipPath.set({ angle: shell.angle });
179
- activeObject.set('dirty', true);
180
- });
181
- shell.on('scaling', () => {
182
- clipPath.set({ scaleX: shell.scaleX, scaleY: shell.scaleY });
183
- clipPath.setPositionByOrigin(
184
- shell.getCenterPoint(),
185
- 'center',
186
- 'center'
187
- );
188
- activeObject.set('dirty', true);
189
- });
190
- shell.on('deselected', () => {
191
- if (
192
- clipPath instanceof fabric.Ellipse &&
193
- shell instanceof fabric.Ellipse
194
- ) {
195
- clipPath.set({ rx: shell.getRx(), ry: shell.getRy() });
196
- this.correctPosition(activeObject, shell, clipPath);
197
- } else if (shell instanceof fabric.Polygon) {
198
- this.correctPosition(activeObject, shell, clipPath);
199
- const { scaleX: cSx = 1, scaleY: cSy = 1 } = clipPath;
200
- const { scaleX: sSx = 1, scaleY: sSy = 1 } = shell;
201
- clipPath.set('scaleX', cSx * sSx);
202
- clipPath.set('scaleY', cSy * sSy);
203
- } else {
204
- this.correctPosition(activeObject, shell, clipPath);
205
- clipPath.set('width', shell.getScaledWidth());
206
- clipPath.set('height', shell.getScaledHeight());
207
- }
208
- activeObject.set('dirty', true);
209
- this.canvas.remove(shell);
210
- this.canvas.requestRenderAll();
211
- });
212
- activeObject.set({ clipPath: clipPath });
213
- this.canvas.add(shell);
214
- this.canvas.setActiveObject(shell);
215
- }
216
- }
217
- correctPosition(
218
- activeObject: fabric.Object,
219
- shell: fabric.Object,
220
- clipPath: fabric.Object
221
- ) {
222
- const position = activeObject.toLocalPoint(
223
- shell.getCenterPoint(),
224
- 'center',
225
- 'center'
226
- );
227
- const { scaleX = 1, scaleY = 1 } = activeObject;
228
- clipPath.set({
229
- absolutePositioned: false,
230
- left: position.x / scaleX,
231
- top: position.y / scaleY,
232
- scaleX: 1 / scaleX,
233
- scaleY: 1 / scaleY,
234
- });
235
- }
236
- removeClip() {
237
- const activeObject = this.canvas.getActiveObjects()[0];
238
- if (activeObject && activeObject.type === 'image') {
239
- activeObject.set({ clipPath: undefined });
240
- activeObject.set('dirty', true);
241
- this.canvas.requestRenderAll();
242
- }
243
- }
244
- }
1
+ import { fabric } from '@hprint/core';
2
+ import { mathHelper } from '@hprint/shared';
3
+ import { get, set } from 'lodash-es';
4
+ import type { IEditor, IPluginTempl } from '@hprint/core';
5
+
6
+ type IPlugin = Pick<SimpleClipImagePlugin, 'addClipPathToImage' | 'removeClip'>;
7
+
8
+ declare module '@hprint/core' {
9
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
10
+ interface IEditor extends IPlugin {}
11
+ }
12
+
13
+ const getBounds = (activeObject: fabric.Object) => {
14
+ const { left = 0, top = 0 } = activeObject;
15
+ return {
16
+ width: activeObject.getScaledWidth(),
17
+ height: activeObject.getScaledHeight(),
18
+ left,
19
+ top,
20
+ };
21
+ };
22
+ const bindInfo = (shell: fabric.Object, activeObject: fabric.Object) => {
23
+ bindFlagToObject(shell);
24
+ bindFlagToObject(shell, 'targetId', get(activeObject, 'id'));
25
+ bindFlagToObject(shell, 'targetType', get(activeObject, 'type'));
26
+ };
27
+ const bindFlagToObject = (
28
+ activeObject: fabric.Object,
29
+ key = 'clip',
30
+ value: any = true
31
+ ) => {
32
+ set(activeObject, key, value);
33
+ };
34
+ const createRectClip = (activeObject: fabric.Object, inverted: boolean) => {
35
+ const {
36
+ width = 0,
37
+ height = 0,
38
+ left = 0,
39
+ top = 0,
40
+ } = getBounds(activeObject);
41
+ const clipW = Math.round(width / 2);
42
+ const clipH = Math.round(height / 2);
43
+ const shell = new fabric.Rect({
44
+ width: clipW,
45
+ height: clipH,
46
+ fill: 'rgba(0,0,0,0)',
47
+ originX: 'center',
48
+ originY: 'center',
49
+ left: left + width / 2,
50
+ top: top + height / 2,
51
+ });
52
+ bindInfo(shell, activeObject);
53
+ const clipPath = new fabric.Rect({
54
+ absolutePositioned: true,
55
+ width: shell.width,
56
+ height: shell.height,
57
+ originX: 'center',
58
+ originY: 'center',
59
+ left: shell.left,
60
+ top: shell.top,
61
+ inverted: inverted,
62
+ });
63
+ return { clipPath, shell };
64
+ };
65
+ const createCircleClip = (activeObject: fabric.Object, inverted: boolean) => {
66
+ const point = activeObject.getCenterPoint();
67
+ const { width } = getBounds(activeObject);
68
+ const shell = new fabric.Ellipse({
69
+ fill: 'rgba(0,0,0,0)',
70
+ originX: 'center',
71
+ originY: 'center',
72
+ left: point.x,
73
+ top: point.y,
74
+ rx: width / 4,
75
+ ry: width / 4,
76
+ });
77
+ bindInfo(shell, activeObject);
78
+ const clipPath = new fabric.Ellipse({
79
+ absolutePositioned: true,
80
+ originX: 'center',
81
+ originY: 'center',
82
+ left: shell.left,
83
+ top: shell.top,
84
+ inverted: inverted,
85
+ rx: shell.rx,
86
+ ry: shell.ry,
87
+ });
88
+ return { shell, clipPath };
89
+ };
90
+ const createTriClip = (activeObject: fabric.Object, inverted: boolean) => {
91
+ const point = activeObject.getCenterPoint();
92
+ const { width = 0, height = 0 } = getBounds(activeObject);
93
+ const clipW = Math.round(width / 2);
94
+ const clipH = Math.round(height / 2);
95
+ const shell = new fabric.Triangle({
96
+ fill: 'rgba(0,0,0,0)',
97
+ originX: 'center',
98
+ originY: 'center',
99
+ left: point.x,
100
+ top: point.y,
101
+ width: clipW,
102
+ height: clipH,
103
+ });
104
+ bindInfo(shell, activeObject);
105
+ const clipPath = new fabric.Triangle({
106
+ absolutePositioned: true,
107
+ originX: 'center',
108
+ originY: 'center',
109
+ left: shell.left,
110
+ top: shell.top,
111
+ width: shell.width,
112
+ height: shell.height,
113
+ inverted: inverted,
114
+ });
115
+ return { shell, clipPath };
116
+ };
117
+ const createPolygonClip = (activeObject: fabric.Object, inverted: boolean) => {
118
+ const point = activeObject.getCenterPoint();
119
+ const points = mathHelper.getPolygonVertices(5, 200);
120
+ const shell = new fabric.Polygon(points, {
121
+ fill: 'rgba(0,0,0,0)',
122
+ originY: 'center',
123
+ originX: 'center',
124
+ left: point.x,
125
+ top: point.y,
126
+ });
127
+ bindInfo(shell, activeObject);
128
+ const clipPath = new fabric.Polygon([...points], {
129
+ absolutePositioned: true,
130
+ originX: 'center',
131
+ originY: 'center',
132
+ left: shell.left,
133
+ top: shell.top,
134
+ inverted: inverted,
135
+ });
136
+ return { shell, clipPath };
137
+ };
138
+ export default class SimpleClipImagePlugin implements IPluginTempl {
139
+ static pluginName = 'SimpleClipImagePlugin';
140
+ // static events = ['sizeChange'];
141
+ static apis = ['addClipPathToImage', 'removeClip'];
142
+ constructor(
143
+ public canvas: fabric.Canvas,
144
+ public editor: IEditor
145
+ ) {}
146
+ addClipPathToImage(value: string) {
147
+ const activeObject = this.canvas.getActiveObjects()[0];
148
+ if (activeObject && activeObject.type === 'image') {
149
+ let clip: { shell: fabric.Object; clipPath: fabric.Object } | null =
150
+ null;
151
+ const [name, inverted] = value.split('-');
152
+ const isInverted = !!inverted;
153
+ switch (name) {
154
+ case 'polygon':
155
+ clip = createPolygonClip(activeObject, isInverted);
156
+ break;
157
+ case 'rect':
158
+ clip = createRectClip(activeObject, isInverted);
159
+ break;
160
+ case 'circle':
161
+ clip = createCircleClip(activeObject, isInverted);
162
+ break;
163
+ case 'triangle':
164
+ clip = createTriClip(activeObject, isInverted);
165
+ break;
166
+ }
167
+ if (clip == null) return;
168
+ const { shell, clipPath } = clip;
169
+ shell.on('moving', () => {
170
+ clipPath.setPositionByOrigin(
171
+ shell.getCenterPoint(),
172
+ 'center',
173
+ 'center'
174
+ );
175
+ activeObject.set('dirty', true);
176
+ });
177
+ shell.on('rotating', () => {
178
+ clipPath.set({ angle: shell.angle });
179
+ activeObject.set('dirty', true);
180
+ });
181
+ shell.on('scaling', () => {
182
+ clipPath.set({ scaleX: shell.scaleX, scaleY: shell.scaleY });
183
+ clipPath.setPositionByOrigin(
184
+ shell.getCenterPoint(),
185
+ 'center',
186
+ 'center'
187
+ );
188
+ activeObject.set('dirty', true);
189
+ });
190
+ shell.on('deselected', () => {
191
+ if (
192
+ clipPath instanceof fabric.Ellipse &&
193
+ shell instanceof fabric.Ellipse
194
+ ) {
195
+ clipPath.set({ rx: shell.getRx(), ry: shell.getRy() });
196
+ this.correctPosition(activeObject, shell, clipPath);
197
+ } else if (shell instanceof fabric.Polygon) {
198
+ this.correctPosition(activeObject, shell, clipPath);
199
+ const { scaleX: cSx = 1, scaleY: cSy = 1 } = clipPath;
200
+ const { scaleX: sSx = 1, scaleY: sSy = 1 } = shell;
201
+ clipPath.set('scaleX', cSx * sSx);
202
+ clipPath.set('scaleY', cSy * sSy);
203
+ } else {
204
+ this.correctPosition(activeObject, shell, clipPath);
205
+ clipPath.set('width', shell.getScaledWidth());
206
+ clipPath.set('height', shell.getScaledHeight());
207
+ }
208
+ activeObject.set('dirty', true);
209
+ this.canvas.remove(shell);
210
+ this.canvas.requestRenderAll();
211
+ });
212
+ activeObject.set({ clipPath: clipPath });
213
+ this.canvas.add(shell);
214
+ this.canvas.setActiveObject(shell);
215
+ }
216
+ }
217
+ correctPosition(
218
+ activeObject: fabric.Object,
219
+ shell: fabric.Object,
220
+ clipPath: fabric.Object
221
+ ) {
222
+ const position = activeObject.toLocalPoint(
223
+ shell.getCenterPoint(),
224
+ 'center',
225
+ 'center'
226
+ );
227
+ const { scaleX = 1, scaleY = 1 } = activeObject;
228
+ clipPath.set({
229
+ absolutePositioned: false,
230
+ left: position.x / scaleX,
231
+ top: position.y / scaleY,
232
+ scaleX: 1 / scaleX,
233
+ scaleY: 1 / scaleY,
234
+ });
235
+ }
236
+ removeClip() {
237
+ const activeObject = this.canvas.getActiveObjects()[0];
238
+ if (activeObject && activeObject.type === 'image') {
239
+ activeObject.set({ clipPath: undefined });
240
+ activeObject.set('dirty', true);
241
+ this.canvas.requestRenderAll();
242
+ }
243
+ }
244
+ }
@@ -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
+ }