@hprint/plugins 0.0.7 → 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 (37) hide show
  1. package/dist/index.js +1 -1
  2. package/dist/index.mjs +1 -1
  3. package/package.json +3 -3
  4. package/src/assets/style/resizePlugin.css +27 -27
  5. package/src/objects/Arrow.js +47 -47
  6. package/src/objects/ThinTailArrow.js +50 -50
  7. package/src/plugins/ControlsPlugin.ts +413 -413
  8. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  9. package/src/plugins/CopyPlugin.ts +261 -261
  10. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  11. package/src/plugins/DrawLinePlugin.ts +162 -162
  12. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  13. package/src/plugins/DringPlugin.ts +125 -125
  14. package/src/plugins/FlipPlugin.ts +59 -59
  15. package/src/plugins/FontPlugin.ts +165 -165
  16. package/src/plugins/FreeDrawPlugin.ts +49 -49
  17. package/src/plugins/GroupPlugin.ts +82 -82
  18. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  19. package/src/plugins/HistoryPlugin.ts +181 -181
  20. package/src/plugins/ImageStroke.ts +121 -121
  21. package/src/plugins/LayerPlugin.ts +108 -108
  22. package/src/plugins/MaskPlugin.ts +155 -155
  23. package/src/plugins/MaterialPlugin.ts +224 -224
  24. package/src/plugins/MiddleMousePlugin.ts +45 -45
  25. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  26. package/src/plugins/PathTextPlugin.ts +89 -89
  27. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  28. package/src/plugins/PrintPlugin.ts +81 -81
  29. package/src/plugins/PsdPlugin.ts +52 -52
  30. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  31. package/src/types/eventType.ts +11 -11
  32. package/src/utils/psd.js +432 -432
  33. package/src/utils/ruler/guideline.ts +145 -145
  34. package/src/utils/ruler/index.ts +91 -91
  35. package/src/utils/ruler/utils.ts +162 -162
  36. package/tsconfig.json +10 -10
  37. package/vite.config.ts +29 -29
@@ -1,108 +1,108 @@
1
- import { fabric } from '@hprint/core';
2
- import type { IEditor, IPluginTempl } from '@hprint/core';
3
-
4
- type IPlugin = Pick<LayerPlugin, 'up' | 'down' | 'toFront' | 'toBack'>;
5
-
6
- declare module '@hprint/core' {
7
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
8
- interface IEditor extends IPlugin {}
9
- }
10
-
11
- class LayerPlugin implements IPluginTempl {
12
- static pluginName = 'LayerPlugin';
13
- static apis = ['up', 'down', 'toFront', 'toBack'];
14
- constructor(
15
- public canvas: fabric.Canvas,
16
- public editor: IEditor
17
- ) {}
18
-
19
- _getWorkspace() {
20
- return this.canvas.getObjects().find((item) => item.id === 'workspace');
21
- }
22
-
23
- _workspaceSendToBack() {
24
- const workspace = this._getWorkspace();
25
- workspace && workspace.sendToBack();
26
- }
27
-
28
- up() {
29
- const actives = this.canvas.getActiveObjects();
30
- if (actives && actives.length === 1) {
31
- const activeObject = this.canvas.getActiveObjects()[0];
32
- activeObject && activeObject.bringForward();
33
- this.canvas.renderAll();
34
- this._workspaceSendToBack();
35
- }
36
- }
37
-
38
- down() {
39
- const actives = this.canvas.getActiveObjects();
40
- if (actives && actives.length === 1) {
41
- const activeObject = this.canvas.getActiveObjects()[0];
42
- activeObject && activeObject.sendBackwards();
43
- this.canvas.renderAll();
44
- this._workspaceSendToBack();
45
- }
46
- }
47
-
48
- toFront() {
49
- const actives = this.canvas.getActiveObjects();
50
- if (actives && actives.length === 1) {
51
- const activeObject = this.canvas.getActiveObjects()[0];
52
- activeObject && activeObject.bringToFront();
53
- this.canvas.renderAll();
54
- this._workspaceSendToBack();
55
- }
56
- }
57
-
58
- toBack() {
59
- const actives = this.canvas.getActiveObjects();
60
- if (actives && actives.length === 1) {
61
- const activeObject = this.canvas.getActiveObjects()[0];
62
- activeObject && activeObject.sendToBack();
63
- this.canvas.renderAll();
64
- this._workspaceSendToBack();
65
- }
66
- }
67
-
68
- contextMenu() {
69
- const activeObject = this.canvas.getActiveObject();
70
- if (activeObject) {
71
- return [
72
- {
73
- text: '图层管理',
74
- hotkey: '❯',
75
- subitems: [
76
- {
77
- text: '上一个',
78
- hotkey: '',
79
- onclick: () => this.up(),
80
- },
81
- {
82
- text: '下一个',
83
- hotkey: '',
84
- onclick: () => this.down(),
85
- },
86
- {
87
- text: '置顶',
88
- hotkey: '',
89
- onclick: () => this.toFront(),
90
- },
91
- {
92
- text: '置底',
93
- hotkey: '',
94
- onclick: () => this.toBack(),
95
- },
96
- ],
97
- },
98
- ];
99
- // return [{ text: '复制', hotkey: 'Ctrl+V', disabled: false, onclick: () => this.clone() }];
100
- }
101
- }
102
-
103
- destroy() {
104
- console.log('pluginDestroy');
105
- }
106
- }
107
-
108
- export default LayerPlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import type { IEditor, IPluginTempl } from '@hprint/core';
3
+
4
+ type IPlugin = Pick<LayerPlugin, 'up' | 'down' | 'toFront' | 'toBack'>;
5
+
6
+ declare module '@hprint/core' {
7
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
8
+ interface IEditor extends IPlugin {}
9
+ }
10
+
11
+ class LayerPlugin implements IPluginTempl {
12
+ static pluginName = 'LayerPlugin';
13
+ static apis = ['up', 'down', 'toFront', 'toBack'];
14
+ constructor(
15
+ public canvas: fabric.Canvas,
16
+ public editor: IEditor
17
+ ) {}
18
+
19
+ _getWorkspace() {
20
+ return this.canvas.getObjects().find((item) => item.id === 'workspace');
21
+ }
22
+
23
+ _workspaceSendToBack() {
24
+ const workspace = this._getWorkspace();
25
+ workspace && workspace.sendToBack();
26
+ }
27
+
28
+ up() {
29
+ const actives = this.canvas.getActiveObjects();
30
+ if (actives && actives.length === 1) {
31
+ const activeObject = this.canvas.getActiveObjects()[0];
32
+ activeObject && activeObject.bringForward();
33
+ this.canvas.renderAll();
34
+ this._workspaceSendToBack();
35
+ }
36
+ }
37
+
38
+ down() {
39
+ const actives = this.canvas.getActiveObjects();
40
+ if (actives && actives.length === 1) {
41
+ const activeObject = this.canvas.getActiveObjects()[0];
42
+ activeObject && activeObject.sendBackwards();
43
+ this.canvas.renderAll();
44
+ this._workspaceSendToBack();
45
+ }
46
+ }
47
+
48
+ toFront() {
49
+ const actives = this.canvas.getActiveObjects();
50
+ if (actives && actives.length === 1) {
51
+ const activeObject = this.canvas.getActiveObjects()[0];
52
+ activeObject && activeObject.bringToFront();
53
+ this.canvas.renderAll();
54
+ this._workspaceSendToBack();
55
+ }
56
+ }
57
+
58
+ toBack() {
59
+ const actives = this.canvas.getActiveObjects();
60
+ if (actives && actives.length === 1) {
61
+ const activeObject = this.canvas.getActiveObjects()[0];
62
+ activeObject && activeObject.sendToBack();
63
+ this.canvas.renderAll();
64
+ this._workspaceSendToBack();
65
+ }
66
+ }
67
+
68
+ contextMenu() {
69
+ const activeObject = this.canvas.getActiveObject();
70
+ if (activeObject) {
71
+ return [
72
+ {
73
+ text: '图层管理',
74
+ hotkey: '❯',
75
+ subitems: [
76
+ {
77
+ text: '上一个',
78
+ hotkey: '',
79
+ onclick: () => this.up(),
80
+ },
81
+ {
82
+ text: '下一个',
83
+ hotkey: '',
84
+ onclick: () => this.down(),
85
+ },
86
+ {
87
+ text: '置顶',
88
+ hotkey: '',
89
+ onclick: () => this.toFront(),
90
+ },
91
+ {
92
+ text: '置底',
93
+ hotkey: '',
94
+ onclick: () => this.toBack(),
95
+ },
96
+ ],
97
+ },
98
+ ];
99
+ // return [{ text: '复制', hotkey: 'Ctrl+V', disabled: false, onclick: () => this.clone() }];
100
+ }
101
+ }
102
+
103
+ destroy() {
104
+ console.log('pluginDestroy');
105
+ }
106
+ }
107
+
108
+ export default LayerPlugin;
@@ -1,155 +1,155 @@
1
- import { fabric } from '@hprint/core';
2
- import type { IEditor, IPluginTempl } from '@hprint/core';
3
-
4
- type IPlugin = Pick<
5
- MaskPlugin,
6
- 'setCoverMask' | 'workspaceMaskToggle' | 'getworkspaceMaskStatus'
7
- >;
8
-
9
- declare module '@hprint/core' {
10
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
11
- interface IEditor extends IPlugin {}
12
- }
13
-
14
- class MaskPlugin implements IPluginTempl {
15
- static pluginName = 'MaskPlugin';
16
- static apis = [
17
- 'setCoverMask',
18
- 'workspaceMaskToggle',
19
- 'getworkspaceMaskStatus',
20
- ];
21
- coverMask: null | fabric.Rect = null;
22
- workspace: null | fabric.Rect = null;
23
- workspaceEl!: HTMLElement;
24
- hackFlag = false;
25
- constructor(
26
- public canvas: fabric.Canvas,
27
- public editor: IEditor
28
- ) {
29
- this.init();
30
- }
31
-
32
- private init() {
33
- const workspaceEl = document.querySelector('#workspace') as HTMLElement;
34
- if (!workspaceEl) {
35
- throw new Error('element #workspace is missing, plz check!');
36
- }
37
- this.workspaceEl = workspaceEl;
38
- }
39
-
40
- /**
41
- * @desc 蒙版开关
42
- * @param val Boolean false
43
- */
44
- workspaceMaskToggle() {
45
- const workspaceMask = this.getWorkspaceMask();
46
- if (!workspaceMask) {
47
- this.initMask();
48
- } else {
49
- // 如果有 则删除
50
- workspaceMask && this.canvas.remove(workspaceMask);
51
- this.workspace?.clone((cloned: fabric.Rect) => {
52
- this.canvas.clipPath = cloned;
53
- this.coverMask = null;
54
- this.canvas.requestRenderAll();
55
- });
56
- this.editor.off('loadJson', this.initMask);
57
- }
58
- }
59
- /**
60
- * @desc 获取蒙版开关
61
- */
62
- getworkspaceMaskStatus() {
63
- return this.coverMask !== null;
64
- }
65
-
66
- /**
67
- * @desc 获取蒙版
68
- * @returns Object
69
- */
70
- getWorkspaceMask() {
71
- return this.canvas
72
- .getObjects()
73
- .find((item) => item.id === 'coverMask') as fabric.Rect;
74
- }
75
-
76
- // 返回workspace对象
77
- getWorkspase() {
78
- return this.canvas
79
- .getObjects()
80
- .find((item) => item.id === 'workspace') as fabric.Rect;
81
- }
82
-
83
- setCoverMask(hack = false) {
84
- if (!this.coverMask || !this.workspace) {
85
- return;
86
- }
87
- const center = this.canvas.getCenter();
88
- const zoom = this.canvas.getZoom();
89
- let zoomToPointNumber = zoom;
90
- if (hack) {
91
- // 比较hack的方法,判断为fabric内部的数据更新问题
92
- zoomToPointNumber += 0.0000001 * (this.hackFlag ? 1 : -1);
93
- this.hackFlag = !this.hackFlag;
94
- }
95
-
96
- this.canvas.zoomToPoint(
97
- new fabric.Point(center.left, center.top),
98
- zoomToPointNumber
99
- );
100
- if (zoom) {
101
- const { workspaceEl } = this;
102
- const width = workspaceEl.offsetWidth;
103
- const height = workspaceEl.offsetHeight;
104
- const cWidth = width / zoom;
105
- const cHeight = height / zoom;
106
- this.coverMask.width = cWidth;
107
- this.coverMask.height = cHeight;
108
- this.coverMask.left =
109
- (this.workspace.left || 0) +
110
- (this.workspace.width! - cWidth) / 2;
111
- this.coverMask.top =
112
- (this.workspace.top || 0) +
113
- (this.workspace.height! - cHeight) / 2;
114
- this.workspace.clone((clone: fabric.Rect) => {
115
- clone.left = -clone.width! / 2;
116
- clone.top = -clone.height! / 2;
117
- clone.inverted = true;
118
- this.coverMask!.clipPath = clone;
119
- this.canvas.requestRenderAll();
120
- });
121
- }
122
- }
123
-
124
- initMask(needBindLoadJSON = true) {
125
- this.workspace = this.getWorkspase();
126
- if (!this.workspace) {
127
- throw new Error('MaskPlugin must be used after WorkspacePlugin!');
128
- }
129
- const coverMask = new fabric.Rect({
130
- fill: 'rgba(0,0,0,0.5)',
131
- id: 'coverMask',
132
- strokeWidth: 0,
133
- });
134
- coverMask.set('selectable', false);
135
- coverMask.set('hasControls', false);
136
- coverMask.set('evented', false);
137
- coverMask.hoverCursor = 'default';
138
- this.canvas.on('object:added', () => {
139
- coverMask.bringToFront();
140
- });
141
- this.canvas.clipPath = undefined;
142
- this.canvas.add(coverMask);
143
- this.coverMask = coverMask;
144
- this.setCoverMask();
145
- // 适配模板和psd的loadjson,在加载完成后再入mask
146
- needBindLoadJSON &&
147
- this.editor.on('loadJson', () => this.initMask(false));
148
- }
149
-
150
- destroy() {
151
- console.log('pluginDestroy');
152
- }
153
- }
154
-
155
- export default MaskPlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import type { IEditor, IPluginTempl } from '@hprint/core';
3
+
4
+ type IPlugin = Pick<
5
+ MaskPlugin,
6
+ 'setCoverMask' | 'workspaceMaskToggle' | 'getworkspaceMaskStatus'
7
+ >;
8
+
9
+ declare module '@hprint/core' {
10
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
11
+ interface IEditor extends IPlugin {}
12
+ }
13
+
14
+ class MaskPlugin implements IPluginTempl {
15
+ static pluginName = 'MaskPlugin';
16
+ static apis = [
17
+ 'setCoverMask',
18
+ 'workspaceMaskToggle',
19
+ 'getworkspaceMaskStatus',
20
+ ];
21
+ coverMask: null | fabric.Rect = null;
22
+ workspace: null | fabric.Rect = null;
23
+ workspaceEl!: HTMLElement;
24
+ hackFlag = false;
25
+ constructor(
26
+ public canvas: fabric.Canvas,
27
+ public editor: IEditor
28
+ ) {
29
+ this.init();
30
+ }
31
+
32
+ private init() {
33
+ const workspaceEl = document.querySelector('#workspace') as HTMLElement;
34
+ if (!workspaceEl) {
35
+ throw new Error('element #workspace is missing, plz check!');
36
+ }
37
+ this.workspaceEl = workspaceEl;
38
+ }
39
+
40
+ /**
41
+ * @desc 蒙版开关
42
+ * @param val Boolean false
43
+ */
44
+ workspaceMaskToggle() {
45
+ const workspaceMask = this.getWorkspaceMask();
46
+ if (!workspaceMask) {
47
+ this.initMask();
48
+ } else {
49
+ // 如果有 则删除
50
+ workspaceMask && this.canvas.remove(workspaceMask);
51
+ this.workspace?.clone((cloned: fabric.Rect) => {
52
+ this.canvas.clipPath = cloned;
53
+ this.coverMask = null;
54
+ this.canvas.requestRenderAll();
55
+ });
56
+ this.editor.off('loadJson', this.initMask);
57
+ }
58
+ }
59
+ /**
60
+ * @desc 获取蒙版开关
61
+ */
62
+ getworkspaceMaskStatus() {
63
+ return this.coverMask !== null;
64
+ }
65
+
66
+ /**
67
+ * @desc 获取蒙版
68
+ * @returns Object
69
+ */
70
+ getWorkspaceMask() {
71
+ return this.canvas
72
+ .getObjects()
73
+ .find((item) => item.id === 'coverMask') as fabric.Rect;
74
+ }
75
+
76
+ // 返回workspace对象
77
+ getWorkspase() {
78
+ return this.canvas
79
+ .getObjects()
80
+ .find((item) => item.id === 'workspace') as fabric.Rect;
81
+ }
82
+
83
+ setCoverMask(hack = false) {
84
+ if (!this.coverMask || !this.workspace) {
85
+ return;
86
+ }
87
+ const center = this.canvas.getCenter();
88
+ const zoom = this.canvas.getZoom();
89
+ let zoomToPointNumber = zoom;
90
+ if (hack) {
91
+ // 比较hack的方法,判断为fabric内部的数据更新问题
92
+ zoomToPointNumber += 0.0000001 * (this.hackFlag ? 1 : -1);
93
+ this.hackFlag = !this.hackFlag;
94
+ }
95
+
96
+ this.canvas.zoomToPoint(
97
+ new fabric.Point(center.left, center.top),
98
+ zoomToPointNumber
99
+ );
100
+ if (zoom) {
101
+ const { workspaceEl } = this;
102
+ const width = workspaceEl.offsetWidth;
103
+ const height = workspaceEl.offsetHeight;
104
+ const cWidth = width / zoom;
105
+ const cHeight = height / zoom;
106
+ this.coverMask.width = cWidth;
107
+ this.coverMask.height = cHeight;
108
+ this.coverMask.left =
109
+ (this.workspace.left || 0) +
110
+ (this.workspace.width! - cWidth) / 2;
111
+ this.coverMask.top =
112
+ (this.workspace.top || 0) +
113
+ (this.workspace.height! - cHeight) / 2;
114
+ this.workspace.clone((clone: fabric.Rect) => {
115
+ clone.left = -clone.width! / 2;
116
+ clone.top = -clone.height! / 2;
117
+ clone.inverted = true;
118
+ this.coverMask!.clipPath = clone;
119
+ this.canvas.requestRenderAll();
120
+ });
121
+ }
122
+ }
123
+
124
+ initMask(needBindLoadJSON = true) {
125
+ this.workspace = this.getWorkspase();
126
+ if (!this.workspace) {
127
+ throw new Error('MaskPlugin must be used after WorkspacePlugin!');
128
+ }
129
+ const coverMask = new fabric.Rect({
130
+ fill: 'rgba(0,0,0,0.5)',
131
+ id: 'coverMask',
132
+ strokeWidth: 0,
133
+ });
134
+ coverMask.set('selectable', false);
135
+ coverMask.set('hasControls', false);
136
+ coverMask.set('evented', false);
137
+ coverMask.hoverCursor = 'default';
138
+ this.canvas.on('object:added', () => {
139
+ coverMask.bringToFront();
140
+ });
141
+ this.canvas.clipPath = undefined;
142
+ this.canvas.add(coverMask);
143
+ this.coverMask = coverMask;
144
+ this.setCoverMask();
145
+ // 适配模板和psd的loadjson,在加载完成后再入mask
146
+ needBindLoadJSON &&
147
+ this.editor.on('loadJson', () => this.initMask(false));
148
+ }
149
+
150
+ destroy() {
151
+ console.log('pluginDestroy');
152
+ }
153
+ }
154
+
155
+ export default MaskPlugin;