@hprint/plugins 0.0.1-alpha.1 → 0.0.1-alpha.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.
Files changed (64) hide show
  1. package/dist/index.js +122 -117
  2. package/dist/index.mjs +41257 -21115
  3. package/dist/src/plugins/AlignGuidLinePlugin.d.ts +7 -2
  4. package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -1
  5. package/dist/src/plugins/BarCodePlugin.d.ts +4 -0
  6. package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -1
  7. package/dist/src/plugins/CreateElementPlugin.d.ts +3 -9
  8. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -1
  9. package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -1
  10. package/dist/src/plugins/LockPlugin.d.ts.map +1 -1
  11. package/dist/src/plugins/QrCodePlugin.d.ts +19 -97
  12. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
  13. package/dist/src/plugins/ResizePlugin.d.ts.map +1 -1
  14. package/dist/src/plugins/UnitPlugin.d.ts.map +1 -1
  15. package/dist/src/plugins/WorkspacePlugin.d.ts.map +1 -1
  16. package/dist/src/utils/units.d.ts.map +1 -1
  17. package/package.json +5 -4
  18. package/src/assets/style/resizePlugin.css +27 -27
  19. package/src/objects/Arrow.js +47 -47
  20. package/src/objects/ThinTailArrow.js +50 -50
  21. package/src/plugins/AlignGuidLinePlugin.ts +1152 -1141
  22. package/src/plugins/BarCodePlugin.ts +33 -27
  23. package/src/plugins/ControlsPlugin.ts +251 -251
  24. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  25. package/src/plugins/CopyPlugin.ts +255 -255
  26. package/src/plugins/CreateElementPlugin.ts +14 -10
  27. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  28. package/src/plugins/DrawLinePlugin.ts +162 -162
  29. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  30. package/src/plugins/DringPlugin.ts +125 -125
  31. package/src/plugins/FlipPlugin.ts +59 -59
  32. package/src/plugins/FontPlugin.ts +165 -165
  33. package/src/plugins/FreeDrawPlugin.ts +49 -49
  34. package/src/plugins/GroupAlignPlugin.ts +365 -365
  35. package/src/plugins/GroupPlugin.ts +82 -82
  36. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  37. package/src/plugins/HistoryPlugin.ts +181 -181
  38. package/src/plugins/ImageStroke.ts +121 -121
  39. package/src/plugins/LayerPlugin.ts +108 -108
  40. package/src/plugins/LockPlugin.ts +242 -240
  41. package/src/plugins/MaskPlugin.ts +155 -155
  42. package/src/plugins/MaterialPlugin.ts +224 -224
  43. package/src/plugins/MiddleMousePlugin.ts +45 -45
  44. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  45. package/src/plugins/PathTextPlugin.ts +89 -89
  46. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  47. package/src/plugins/PrintPlugin.ts +81 -81
  48. package/src/plugins/PsdPlugin.ts +52 -52
  49. package/src/plugins/QrCodePlugin.ts +322 -393
  50. package/src/plugins/ResizePlugin.ts +278 -274
  51. package/src/plugins/RulerPlugin.ts +78 -78
  52. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  53. package/src/plugins/UnitPlugin.ts +326 -327
  54. package/src/plugins/WaterMarkPlugin.ts +257 -257
  55. package/src/plugins/WorkspacePlugin.ts +10 -6
  56. package/src/types/eventType.ts +11 -11
  57. package/src/utils/psd.js +432 -432
  58. package/src/utils/ruler/guideline.ts +145 -145
  59. package/src/utils/ruler/index.ts +91 -91
  60. package/src/utils/ruler/ruler.ts +924 -924
  61. package/src/utils/ruler/utils.ts +162 -162
  62. package/src/utils/units.ts +4 -2
  63. package/tsconfig.json +10 -10
  64. package/vite.config.ts +29 -29
@@ -1,205 +1,205 @@
1
- import { fabric } from '@hprint/core';
2
- import { v4 as uuid } from 'uuid';
3
- import { utils } from '@hprint/shared';
4
- import type { IEditor, IPluginTempl } from '@hprint/core';
5
-
6
- type IPlugin = Pick<
7
- DrawPolygonPlugin,
8
- 'beginDrawPolygon' | 'endDrawPolygon' | 'discardPolygon'
9
- >;
10
-
11
- declare module '@hprint/core' {
12
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
13
- interface IEditor extends IPlugin {}
14
- }
15
-
16
- type LineCoords = [fabric.Point, fabric.Point];
17
- type OffListener = (ev: fabric.IEvent) => void;
18
- type OnEnd = (...args: any[]) => void;
19
- class DrawPolygonPlugin implements IPluginTempl {
20
- isDrawingPolygon = false;
21
- points: fabric.Point[] = [];
22
- lines: fabric.Line[] = [];
23
- anchors: fabric.Circle[] = [];
24
- tempPoint: fabric.Point | undefined;
25
- tempLine: fabric.Line | undefined;
26
- lastPoint: fabric.Point | undefined;
27
- onEnd?: OnEnd;
28
- // 最后一点和第一点的距离为<=delta即闭合
29
- delta = 5;
30
- static pluginName = 'DrawPolygonPlugin';
31
- static apis = ['beginDrawPolygon', 'endDrawPolygon', 'discardPolygon'];
32
- constructor(
33
- public canvas: fabric.Canvas,
34
- public editor: IEditor
35
- ) {}
36
-
37
- _isUsingHistory() {
38
- return (
39
- this.canvas.offHistory &&
40
- typeof this.canvas.offHistory === 'function'
41
- );
42
- }
43
- _bindEvent() {
44
- window.addEventListener('keydown', this._escListener);
45
- this.canvas.on('mouse:down', this._downHandler);
46
- this.canvas.on('mouse:move', this._moveHandler);
47
- }
48
- _escListener = (evt: KeyboardEvent) => {
49
- if (evt.key === 'Escape' || evt['keyCode'] === 27) {
50
- this._confirmBuildPolygon();
51
- }
52
- };
53
- _downHandler = (ev: fabric.IEvent<MouseEvent>): void => {
54
- if (!this.isDrawingPolygon) return;
55
- const absPointer = ev.absolutePointer!;
56
- const confirmPoint = new fabric.Point(absPointer.x, absPointer.y);
57
- const anchor = this._mackAnchor(absPointer);
58
- this.anchors.push(anchor);
59
- if (this.tempLine == null) {
60
- const tempPoint = new fabric.Point(absPointer.x, absPointer.y);
61
- this.tempLine = this._makeLine([tempPoint, tempPoint]);
62
- this.canvas.add(this.tempLine);
63
- } else {
64
- ev.e.shiftKey &&
65
- confirmPoint.setXY(this.tempLine.x2!, this.tempLine.y2!);
66
- anchor.set({ left: confirmPoint.x, top: confirmPoint.y });
67
- this.tempLine.set({
68
- x1: confirmPoint.x,
69
- y1: confirmPoint.y,
70
- x2: confirmPoint.x,
71
- y2: confirmPoint.y,
72
- });
73
- }
74
- if (this.lastPoint) {
75
- const line = this._makeLine([this.lastPoint, confirmPoint]);
76
- this.lines.push(line);
77
- this.canvas.add(line);
78
- if (
79
- this.points[0].distanceFrom(confirmPoint) /
80
- this.canvas.getZoom() <=
81
- this.delta
82
- ) {
83
- this._confirmBuildPolygon();
84
- return;
85
- }
86
- }
87
- this.canvas.add(anchor);
88
- this.lastPoint = confirmPoint;
89
- this.points.push(confirmPoint);
90
- this._ensureAnchorsForward();
91
- };
92
- _moveHandler = (ev: fabric.IEvent<MouseEvent>): void => {
93
- if (!this.isDrawingPolygon || !this.tempLine) return;
94
- const absPoint = ev.absolutePointer!;
95
- if (ev.e.shiftKey && this.lastPoint) {
96
- const point = utils.shiftAngle(this.lastPoint, absPoint);
97
- this.tempLine.set({
98
- x2: point.x,
99
- y2: point.y,
100
- });
101
- } else {
102
- this.tempLine.set({
103
- x2: absPoint.x,
104
- y2: absPoint.y,
105
- });
106
- }
107
- this.canvas.renderAll();
108
- };
109
- _ensureAnchorsForward() {
110
- this.anchors.forEach((item) => {
111
- item.bringForward();
112
- });
113
- }
114
- _unbindEvent() {
115
- window.removeEventListener('keydown', this._escListener);
116
- this.canvas.off('mouse:down', this._downHandler as OffListener);
117
- this.canvas.off('mouse:move', this._moveHandler as OffListener);
118
- }
119
- _createPolygon(points: fabric.Point[]) {
120
- return new fabric.Polygon(points, {
121
- fill: '#ccccccff',
122
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
123
- // @ts-ignore
124
- id: uuid(),
125
- });
126
- }
127
- _makeLine(coors: LineCoords) {
128
- const [p1, p2] = coors;
129
- return new fabric.Line([p1.x, p1.y, p2.x, p2.y], {
130
- fill: '#000',
131
- stroke: '#000',
132
- strokeWidth: 1,
133
- selectable: false,
134
- evented: false,
135
- });
136
- }
137
- _mackAnchor(position: fabric.Point) {
138
- return new fabric.Circle({
139
- radius: 5,
140
- left: position.x,
141
- top: position.y,
142
- fill: 'rgb(178, 53, 84)',
143
- scaleX: 1 / this.canvas.getZoom(),
144
- scaleY: 1 / this.canvas.getZoom(),
145
- strokeWidth: 1 / this.canvas.getZoom(),
146
- originX: 'center',
147
- originY: 'center',
148
- evented: false,
149
- selectable: false,
150
- });
151
- }
152
- _confirmBuildPolygon() {
153
- const points = this.points;
154
- this.discardPolygon();
155
- if (this._isUsingHistory()) {
156
- this.canvas.historyProcessing = false;
157
- }
158
- if (points.length >= 3) {
159
- const poly = this._createPolygon(points);
160
- this.canvas.add(poly);
161
- }
162
- }
163
- _prepare() {
164
- this.canvas.discardActiveObject();
165
- this.canvas.getObjects().forEach((obj) => {
166
- obj.selectable = false;
167
- obj.hasControls = false;
168
- });
169
- }
170
- beginDrawPolygon(onEnd?: OnEnd) {
171
- this._prepare();
172
- this.onEnd = onEnd;
173
- if (this._isUsingHistory()) {
174
- this.canvas.historyProcessing = true;
175
- }
176
- this.canvas.requestRenderAll();
177
- this.isDrawingPolygon = true;
178
- this._bindEvent();
179
- }
180
- endDrawPolygon() {
181
- this.canvas.discardActiveObject();
182
- this.isDrawingPolygon = false;
183
- this.lastPoint = undefined;
184
- this.tempPoint = undefined;
185
- this._unbindEvent();
186
- this.onEnd && this.onEnd();
187
- this.onEnd = undefined;
188
- }
189
- discardPolygon() {
190
- this.lines.forEach((item) => {
191
- this.canvas.remove(item);
192
- });
193
- this.anchors.forEach((item) => {
194
- this.canvas.remove(item);
195
- });
196
- this.tempLine && this.canvas.remove(this.tempLine);
197
- this.tempLine = undefined;
198
- this.anchors = [];
199
- this.lines = [];
200
- this.points = [];
201
- this.endDrawPolygon();
202
- }
203
- }
204
-
205
- export default DrawPolygonPlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import { v4 as uuid } from 'uuid';
3
+ import { utils } from '@hprint/shared';
4
+ import type { IEditor, IPluginTempl } from '@hprint/core';
5
+
6
+ type IPlugin = Pick<
7
+ DrawPolygonPlugin,
8
+ 'beginDrawPolygon' | 'endDrawPolygon' | 'discardPolygon'
9
+ >;
10
+
11
+ declare module '@hprint/core' {
12
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
13
+ interface IEditor extends IPlugin {}
14
+ }
15
+
16
+ type LineCoords = [fabric.Point, fabric.Point];
17
+ type OffListener = (ev: fabric.IEvent) => void;
18
+ type OnEnd = (...args: any[]) => void;
19
+ class DrawPolygonPlugin implements IPluginTempl {
20
+ isDrawingPolygon = false;
21
+ points: fabric.Point[] = [];
22
+ lines: fabric.Line[] = [];
23
+ anchors: fabric.Circle[] = [];
24
+ tempPoint: fabric.Point | undefined;
25
+ tempLine: fabric.Line | undefined;
26
+ lastPoint: fabric.Point | undefined;
27
+ onEnd?: OnEnd;
28
+ // 最后一点和第一点的距离为<=delta即闭合
29
+ delta = 5;
30
+ static pluginName = 'DrawPolygonPlugin';
31
+ static apis = ['beginDrawPolygon', 'endDrawPolygon', 'discardPolygon'];
32
+ constructor(
33
+ public canvas: fabric.Canvas,
34
+ public editor: IEditor
35
+ ) {}
36
+
37
+ _isUsingHistory() {
38
+ return (
39
+ this.canvas.offHistory &&
40
+ typeof this.canvas.offHistory === 'function'
41
+ );
42
+ }
43
+ _bindEvent() {
44
+ window.addEventListener('keydown', this._escListener);
45
+ this.canvas.on('mouse:down', this._downHandler);
46
+ this.canvas.on('mouse:move', this._moveHandler);
47
+ }
48
+ _escListener = (evt: KeyboardEvent) => {
49
+ if (evt.key === 'Escape' || evt['keyCode'] === 27) {
50
+ this._confirmBuildPolygon();
51
+ }
52
+ };
53
+ _downHandler = (ev: fabric.IEvent<MouseEvent>): void => {
54
+ if (!this.isDrawingPolygon) return;
55
+ const absPointer = ev.absolutePointer!;
56
+ const confirmPoint = new fabric.Point(absPointer.x, absPointer.y);
57
+ const anchor = this._mackAnchor(absPointer);
58
+ this.anchors.push(anchor);
59
+ if (this.tempLine == null) {
60
+ const tempPoint = new fabric.Point(absPointer.x, absPointer.y);
61
+ this.tempLine = this._makeLine([tempPoint, tempPoint]);
62
+ this.canvas.add(this.tempLine);
63
+ } else {
64
+ ev.e.shiftKey &&
65
+ confirmPoint.setXY(this.tempLine.x2!, this.tempLine.y2!);
66
+ anchor.set({ left: confirmPoint.x, top: confirmPoint.y });
67
+ this.tempLine.set({
68
+ x1: confirmPoint.x,
69
+ y1: confirmPoint.y,
70
+ x2: confirmPoint.x,
71
+ y2: confirmPoint.y,
72
+ });
73
+ }
74
+ if (this.lastPoint) {
75
+ const line = this._makeLine([this.lastPoint, confirmPoint]);
76
+ this.lines.push(line);
77
+ this.canvas.add(line);
78
+ if (
79
+ this.points[0].distanceFrom(confirmPoint) /
80
+ this.canvas.getZoom() <=
81
+ this.delta
82
+ ) {
83
+ this._confirmBuildPolygon();
84
+ return;
85
+ }
86
+ }
87
+ this.canvas.add(anchor);
88
+ this.lastPoint = confirmPoint;
89
+ this.points.push(confirmPoint);
90
+ this._ensureAnchorsForward();
91
+ };
92
+ _moveHandler = (ev: fabric.IEvent<MouseEvent>): void => {
93
+ if (!this.isDrawingPolygon || !this.tempLine) return;
94
+ const absPoint = ev.absolutePointer!;
95
+ if (ev.e.shiftKey && this.lastPoint) {
96
+ const point = utils.shiftAngle(this.lastPoint, absPoint);
97
+ this.tempLine.set({
98
+ x2: point.x,
99
+ y2: point.y,
100
+ });
101
+ } else {
102
+ this.tempLine.set({
103
+ x2: absPoint.x,
104
+ y2: absPoint.y,
105
+ });
106
+ }
107
+ this.canvas.renderAll();
108
+ };
109
+ _ensureAnchorsForward() {
110
+ this.anchors.forEach((item) => {
111
+ item.bringForward();
112
+ });
113
+ }
114
+ _unbindEvent() {
115
+ window.removeEventListener('keydown', this._escListener);
116
+ this.canvas.off('mouse:down', this._downHandler as OffListener);
117
+ this.canvas.off('mouse:move', this._moveHandler as OffListener);
118
+ }
119
+ _createPolygon(points: fabric.Point[]) {
120
+ return new fabric.Polygon(points, {
121
+ fill: '#ccccccff',
122
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
123
+ // @ts-ignore
124
+ id: uuid(),
125
+ });
126
+ }
127
+ _makeLine(coors: LineCoords) {
128
+ const [p1, p2] = coors;
129
+ return new fabric.Line([p1.x, p1.y, p2.x, p2.y], {
130
+ fill: '#000',
131
+ stroke: '#000',
132
+ strokeWidth: 1,
133
+ selectable: false,
134
+ evented: false,
135
+ });
136
+ }
137
+ _mackAnchor(position: fabric.Point) {
138
+ return new fabric.Circle({
139
+ radius: 5,
140
+ left: position.x,
141
+ top: position.y,
142
+ fill: 'rgb(178, 53, 84)',
143
+ scaleX: 1 / this.canvas.getZoom(),
144
+ scaleY: 1 / this.canvas.getZoom(),
145
+ strokeWidth: 1 / this.canvas.getZoom(),
146
+ originX: 'center',
147
+ originY: 'center',
148
+ evented: false,
149
+ selectable: false,
150
+ });
151
+ }
152
+ _confirmBuildPolygon() {
153
+ const points = this.points;
154
+ this.discardPolygon();
155
+ if (this._isUsingHistory()) {
156
+ this.canvas.historyProcessing = false;
157
+ }
158
+ if (points.length >= 3) {
159
+ const poly = this._createPolygon(points);
160
+ this.canvas.add(poly);
161
+ }
162
+ }
163
+ _prepare() {
164
+ this.canvas.discardActiveObject();
165
+ this.canvas.getObjects().forEach((obj) => {
166
+ obj.selectable = false;
167
+ obj.hasControls = false;
168
+ });
169
+ }
170
+ beginDrawPolygon(onEnd?: OnEnd) {
171
+ this._prepare();
172
+ this.onEnd = onEnd;
173
+ if (this._isUsingHistory()) {
174
+ this.canvas.historyProcessing = true;
175
+ }
176
+ this.canvas.requestRenderAll();
177
+ this.isDrawingPolygon = true;
178
+ this._bindEvent();
179
+ }
180
+ endDrawPolygon() {
181
+ this.canvas.discardActiveObject();
182
+ this.isDrawingPolygon = false;
183
+ this.lastPoint = undefined;
184
+ this.tempPoint = undefined;
185
+ this._unbindEvent();
186
+ this.onEnd && this.onEnd();
187
+ this.onEnd = undefined;
188
+ }
189
+ discardPolygon() {
190
+ this.lines.forEach((item) => {
191
+ this.canvas.remove(item);
192
+ });
193
+ this.anchors.forEach((item) => {
194
+ this.canvas.remove(item);
195
+ });
196
+ this.tempLine && this.canvas.remove(this.tempLine);
197
+ this.tempLine = undefined;
198
+ this.anchors = [];
199
+ this.lines = [];
200
+ this.points = [];
201
+ this.endDrawPolygon();
202
+ }
203
+ }
204
+
205
+ export default DrawPolygonPlugin;
@@ -1,125 +1,125 @@
1
- import { IEditor, IPluginTempl } from '@hprint/core';
2
-
3
- type IPlugin = Pick<DringPlugin, 'startDring' | 'endDring'>;
4
-
5
- declare module '@hprint/core' {
6
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
7
- interface IEditor extends IPlugin {}
8
- }
9
-
10
- export class DringPlugin implements IPluginTempl {
11
- defautOption = {};
12
- static pluginName = 'DringPlugin';
13
- static events = ['startDring', 'endDring'];
14
- static apis = ['startDring', 'endDring'];
15
- hotkeys: string[] = ['space'];
16
- dragMode = false;
17
- constructor(
18
- public canvas: fabric.Canvas,
19
- public editor: IEditor
20
- ) {
21
- this.dragMode = false;
22
- this.init();
23
- }
24
- init() {
25
- this._initDring();
26
- }
27
-
28
- startDring() {
29
- this.dragMode = true;
30
- this.canvas.setCursor('grab');
31
- this.editor.emit('startDring');
32
- this.canvas.renderAll();
33
- }
34
- endDring() {
35
- this.dragMode = false;
36
- this.canvas.setCursor('default');
37
- this.canvas.isDragging = false;
38
- this.editor.emit('endDring');
39
- this.canvas.renderAll();
40
- }
41
-
42
- // 拖拽模式;
43
- _initDring() {
44
- const This = this;
45
- this.canvas.on('mouse:down', function (this: ExtCanvas, opt) {
46
- const evt = opt.e;
47
- // evt.button === 1 为鼠标中键的判断
48
- if (evt.altKey || This.dragMode || evt.button === 1) {
49
- This.canvas.setCursor('grabbing');
50
- This.canvas.discardActiveObject();
51
- This._setDring();
52
- this.selection = false;
53
- this.isDragging = true;
54
- this.lastPosX = evt.clientX;
55
- this.lastPosY = evt.clientY;
56
- this.requestRenderAll();
57
- }
58
- });
59
-
60
- this.canvas.on('mouse:move', function (this: ExtCanvas, opt) {
61
- This.dragMode && This.canvas.setCursor('grab');
62
- if (this.isDragging) {
63
- This.canvas.discardActiveObject();
64
- This.canvas.setCursor('grabbing');
65
- const { e } = opt;
66
- if (!this.viewportTransform) return;
67
- const vpt = this.viewportTransform;
68
- vpt[4] += e.clientX - this.lastPosX;
69
- vpt[5] += e.clientY - this.lastPosY;
70
- this.lastPosX = e.clientX;
71
- this.lastPosY = e.clientY;
72
- this.requestRenderAll();
73
- }
74
- });
75
-
76
- this.canvas.on('mouse:up', function (this: ExtCanvas) {
77
- if (!this.viewportTransform) return;
78
- this.setViewportTransform(this.viewportTransform);
79
- this.isDragging = false;
80
- this.selection = true;
81
- this.getObjects().forEach((obj) => {
82
- if (obj.id !== 'workspace' && obj.hasControls) {
83
- obj.selectable = true;
84
- }
85
- });
86
- This.dragMode && This.canvas.setCursor('grab');
87
- this.requestRenderAll();
88
- });
89
- }
90
-
91
- _setDring() {
92
- this.canvas.selection = false;
93
- this.canvas.getObjects().forEach((obj) => {
94
- obj.selectable = false;
95
- });
96
- this.canvas.requestRenderAll();
97
- }
98
-
99
- destroy() {
100
- console.log('pluginDestroy');
101
- }
102
-
103
- // 快捷键扩展回调
104
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
- hotkeyEvent(eventName: string, e: KeyboardEvent) {
106
- if (e.code === 'Space' && e.type === 'keydown') {
107
- if (!this.dragMode) {
108
- this.startDring();
109
- }
110
- }
111
- if (e.code === 'Space' && e.type === 'keyup') {
112
- this.endDring();
113
- }
114
- }
115
- }
116
-
117
- declare global {
118
- export type ExtCanvas = fabric.Canvas & {
119
- isDragging: boolean;
120
- lastPosX: number;
121
- lastPosY: number;
122
- };
123
- }
124
-
125
- export default DringPlugin;
1
+ import { IEditor, IPluginTempl } from '@hprint/core';
2
+
3
+ type IPlugin = Pick<DringPlugin, 'startDring' | 'endDring'>;
4
+
5
+ declare module '@hprint/core' {
6
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
7
+ interface IEditor extends IPlugin {}
8
+ }
9
+
10
+ export class DringPlugin implements IPluginTempl {
11
+ defautOption = {};
12
+ static pluginName = 'DringPlugin';
13
+ static events = ['startDring', 'endDring'];
14
+ static apis = ['startDring', 'endDring'];
15
+ hotkeys: string[] = ['space'];
16
+ dragMode = false;
17
+ constructor(
18
+ public canvas: fabric.Canvas,
19
+ public editor: IEditor
20
+ ) {
21
+ this.dragMode = false;
22
+ this.init();
23
+ }
24
+ init() {
25
+ this._initDring();
26
+ }
27
+
28
+ startDring() {
29
+ this.dragMode = true;
30
+ this.canvas.setCursor('grab');
31
+ this.editor.emit('startDring');
32
+ this.canvas.renderAll();
33
+ }
34
+ endDring() {
35
+ this.dragMode = false;
36
+ this.canvas.setCursor('default');
37
+ this.canvas.isDragging = false;
38
+ this.editor.emit('endDring');
39
+ this.canvas.renderAll();
40
+ }
41
+
42
+ // 拖拽模式;
43
+ _initDring() {
44
+ const This = this;
45
+ this.canvas.on('mouse:down', function (this: ExtCanvas, opt) {
46
+ const evt = opt.e;
47
+ // evt.button === 1 为鼠标中键的判断
48
+ if (evt.altKey || This.dragMode || evt.button === 1) {
49
+ This.canvas.setCursor('grabbing');
50
+ This.canvas.discardActiveObject();
51
+ This._setDring();
52
+ this.selection = false;
53
+ this.isDragging = true;
54
+ this.lastPosX = evt.clientX;
55
+ this.lastPosY = evt.clientY;
56
+ this.requestRenderAll();
57
+ }
58
+ });
59
+
60
+ this.canvas.on('mouse:move', function (this: ExtCanvas, opt) {
61
+ This.dragMode && This.canvas.setCursor('grab');
62
+ if (this.isDragging) {
63
+ This.canvas.discardActiveObject();
64
+ This.canvas.setCursor('grabbing');
65
+ const { e } = opt;
66
+ if (!this.viewportTransform) return;
67
+ const vpt = this.viewportTransform;
68
+ vpt[4] += e.clientX - this.lastPosX;
69
+ vpt[5] += e.clientY - this.lastPosY;
70
+ this.lastPosX = e.clientX;
71
+ this.lastPosY = e.clientY;
72
+ this.requestRenderAll();
73
+ }
74
+ });
75
+
76
+ this.canvas.on('mouse:up', function (this: ExtCanvas) {
77
+ if (!this.viewportTransform) return;
78
+ this.setViewportTransform(this.viewportTransform);
79
+ this.isDragging = false;
80
+ this.selection = true;
81
+ this.getObjects().forEach((obj) => {
82
+ if (obj.id !== 'workspace' && obj.hasControls) {
83
+ obj.selectable = true;
84
+ }
85
+ });
86
+ This.dragMode && This.canvas.setCursor('grab');
87
+ this.requestRenderAll();
88
+ });
89
+ }
90
+
91
+ _setDring() {
92
+ this.canvas.selection = false;
93
+ this.canvas.getObjects().forEach((obj) => {
94
+ obj.selectable = false;
95
+ });
96
+ this.canvas.requestRenderAll();
97
+ }
98
+
99
+ destroy() {
100
+ console.log('pluginDestroy');
101
+ }
102
+
103
+ // 快捷键扩展回调
104
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
+ hotkeyEvent(eventName: string, e: KeyboardEvent) {
106
+ if (e.code === 'Space' && e.type === 'keydown') {
107
+ if (!this.dragMode) {
108
+ this.startDring();
109
+ }
110
+ }
111
+ if (e.code === 'Space' && e.type === 'keyup') {
112
+ this.endDring();
113
+ }
114
+ }
115
+ }
116
+
117
+ declare global {
118
+ export type ExtCanvas = fabric.Canvas & {
119
+ isDragging: boolean;
120
+ lastPosX: number;
121
+ lastPosY: number;
122
+ };
123
+ }
124
+
125
+ export default DringPlugin;