@hprint/plugins 0.0.1-alpha.0
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.
- package/dist/index.css +1 -0
- package/dist/index.js +478 -0
- package/dist/index.mjs +41731 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/objects/Arrow.d.ts +2 -0
- package/dist/src/objects/Arrow.d.ts.map +1 -0
- package/dist/src/objects/ThinTailArrow.d.ts +2 -0
- package/dist/src/objects/ThinTailArrow.d.ts.map +1 -0
- package/dist/src/plugins/AddBaseTypePlugin.d.ts +26 -0
- package/dist/src/plugins/AddBaseTypePlugin.d.ts.map +1 -0
- package/dist/src/plugins/AlignGuidLinePlugin.d.ts +16 -0
- package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -0
- package/dist/src/plugins/BarCodePlugin.d.ts +68 -0
- package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -0
- package/dist/src/plugins/CenterAlignPlugin.d.ts +29 -0
- package/dist/src/plugins/CenterAlignPlugin.d.ts.map +1 -0
- package/dist/src/plugins/ControlsPlugin.d.ts +11 -0
- package/dist/src/plugins/ControlsPlugin.d.ts.map +1 -0
- package/dist/src/plugins/ControlsRotatePlugin.d.ts +11 -0
- package/dist/src/plugins/ControlsRotatePlugin.d.ts.map +1 -0
- package/dist/src/plugins/CopyPlugin.d.ts +30 -0
- package/dist/src/plugins/CopyPlugin.d.ts.map +1 -0
- package/dist/src/plugins/CreateElementPlugin.d.ts +121 -0
- package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -0
- package/dist/src/plugins/DeleteHotKeyPlugin.d.ts +25 -0
- package/dist/src/plugins/DeleteHotKeyPlugin.d.ts.map +1 -0
- package/dist/src/plugins/DrawLinePlugin.d.ts +26 -0
- package/dist/src/plugins/DrawLinePlugin.d.ts.map +1 -0
- package/dist/src/plugins/DrawPolygonPlugin.d.ts +41 -0
- package/dist/src/plugins/DrawPolygonPlugin.d.ts.map +1 -0
- package/dist/src/plugins/DringPlugin.d.ts +33 -0
- package/dist/src/plugins/DringPlugin.d.ts.map +1 -0
- package/dist/src/plugins/FlipPlugin.d.ts +26 -0
- package/dist/src/plugins/FlipPlugin.d.ts.map +1 -0
- package/dist/src/plugins/FontPlugin.d.ts +33 -0
- package/dist/src/plugins/FontPlugin.d.ts.map +1 -0
- package/dist/src/plugins/FreeDrawPlugin.d.ts +23 -0
- package/dist/src/plugins/FreeDrawPlugin.d.ts.map +1 -0
- package/dist/src/plugins/GroupAlignPlugin.d.ts +24 -0
- package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -0
- package/dist/src/plugins/GroupPlugin.d.ts +24 -0
- package/dist/src/plugins/GroupPlugin.d.ts.map +1 -0
- package/dist/src/plugins/GroupTextEditorPlugin.d.ts +18 -0
- package/dist/src/plugins/GroupTextEditorPlugin.d.ts.map +1 -0
- package/dist/src/plugins/HistoryPlugin.d.ts +30 -0
- package/dist/src/plugins/HistoryPlugin.d.ts.map +1 -0
- package/dist/src/plugins/ImageStroke.d.ts +18 -0
- package/dist/src/plugins/ImageStroke.d.ts.map +1 -0
- package/dist/src/plugins/LayerPlugin.d.ts +31 -0
- package/dist/src/plugins/LayerPlugin.d.ts.map +1 -0
- package/dist/src/plugins/LockPlugin.d.ts +27 -0
- package/dist/src/plugins/LockPlugin.d.ts.map +1 -0
- package/dist/src/plugins/MaskPlugin.d.ts +38 -0
- package/dist/src/plugins/MaskPlugin.d.ts.map +1 -0
- package/dist/src/plugins/MaterialPlugin.d.ts +45 -0
- package/dist/src/plugins/MaterialPlugin.d.ts.map +1 -0
- package/dist/src/plugins/MiddleMousePlugin.d.ts +18 -0
- package/dist/src/plugins/MiddleMousePlugin.d.ts.map +1 -0
- package/dist/src/plugins/MoveHotKeyPlugin.d.ts +12 -0
- package/dist/src/plugins/MoveHotKeyPlugin.d.ts.map +1 -0
- package/dist/src/plugins/PathTextPlugin.d.ts +30 -0
- package/dist/src/plugins/PathTextPlugin.d.ts.map +1 -0
- package/dist/src/plugins/PolygonModifyPlugin.d.ts +28 -0
- package/dist/src/plugins/PolygonModifyPlugin.d.ts.map +1 -0
- package/dist/src/plugins/PrintPlugin.d.ts +39 -0
- package/dist/src/plugins/PrintPlugin.d.ts.map +1 -0
- package/dist/src/plugins/PsdPlugin.d.ts +17 -0
- package/dist/src/plugins/PsdPlugin.d.ts.map +1 -0
- package/dist/src/plugins/QrCodePlugin.d.ts +137 -0
- package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -0
- package/dist/src/plugins/ResizePlugin.d.ts +44 -0
- package/dist/src/plugins/ResizePlugin.d.ts.map +1 -0
- package/dist/src/plugins/RulerPlugin.d.ts +24 -0
- package/dist/src/plugins/RulerPlugin.d.ts.map +1 -0
- package/dist/src/plugins/SimpleClipImagePlugin.d.ts +18 -0
- package/dist/src/plugins/SimpleClipImagePlugin.d.ts.map +1 -0
- package/dist/src/plugins/UnitPlugin.d.ts +84 -0
- package/dist/src/plugins/UnitPlugin.d.ts.map +1 -0
- package/dist/src/plugins/WaterMarkPlugin.d.ts +40 -0
- package/dist/src/plugins/WaterMarkPlugin.d.ts.map +1 -0
- package/dist/src/plugins/WorkspacePlugin.d.ts +57 -0
- package/dist/src/plugins/WorkspacePlugin.d.ts.map +1 -0
- package/dist/src/types/eventType.d.ts +11 -0
- package/dist/src/types/eventType.d.ts.map +1 -0
- package/dist/src/utils/psd.d.ts +3 -0
- package/dist/src/utils/psd.d.ts.map +1 -0
- package/dist/src/utils/ruler/guideline.d.ts +4 -0
- package/dist/src/utils/ruler/guideline.d.ts.map +1 -0
- package/dist/src/utils/ruler/index.d.ts +5 -0
- package/dist/src/utils/ruler/index.d.ts.map +1 -0
- package/dist/src/utils/ruler/ruler.d.ts +147 -0
- package/dist/src/utils/ruler/ruler.d.ts.map +1 -0
- package/dist/src/utils/ruler/utils.d.ts +50 -0
- package/dist/src/utils/ruler/utils.d.ts.map +1 -0
- package/dist/src/utils/units.d.ts +22 -0
- package/dist/src/utils/units.d.ts.map +1 -0
- package/package.json +51 -0
- package/src/assets/edgecontrol.svg +17 -0
- package/src/assets/lock.svg +7 -0
- package/src/assets/middlecontrol.svg +17 -0
- package/src/assets/middlecontrolhoz.svg +17 -0
- package/src/assets/rotateicon.svg +20 -0
- package/src/assets/style/resizePlugin.css +27 -0
- package/src/index.ts +121 -0
- package/src/objects/Arrow.js +47 -0
- package/src/objects/ThinTailArrow.js +50 -0
- package/src/plugins/AddBaseTypePlugin.ts +107 -0
- package/src/plugins/AlignGuidLinePlugin.ts +1141 -0
- package/src/plugins/BarCodePlugin.ts +860 -0
- package/src/plugins/CenterAlignPlugin.ts +133 -0
- package/src/plugins/ControlsPlugin.ts +251 -0
- package/src/plugins/ControlsRotatePlugin.ts +111 -0
- package/src/plugins/CopyPlugin.ts +255 -0
- package/src/plugins/CreateElementPlugin.ts +548 -0
- package/src/plugins/DeleteHotKeyPlugin.ts +57 -0
- package/src/plugins/DrawLinePlugin.ts +162 -0
- package/src/plugins/DrawPolygonPlugin.ts +205 -0
- package/src/plugins/DringPlugin.ts +125 -0
- package/src/plugins/FlipPlugin.ts +59 -0
- package/src/plugins/FontPlugin.ts +165 -0
- package/src/plugins/FreeDrawPlugin.ts +49 -0
- package/src/plugins/GroupAlignPlugin.ts +365 -0
- package/src/plugins/GroupPlugin.ts +82 -0
- package/src/plugins/GroupTextEditorPlugin.ts +198 -0
- package/src/plugins/HistoryPlugin.ts +181 -0
- package/src/plugins/ImageStroke.ts +121 -0
- package/src/plugins/LayerPlugin.ts +108 -0
- package/src/plugins/LockPlugin.ts +240 -0
- package/src/plugins/MaskPlugin.ts +155 -0
- package/src/plugins/MaterialPlugin.ts +224 -0
- package/src/plugins/MiddleMousePlugin.ts +45 -0
- package/src/plugins/MoveHotKeyPlugin.ts +46 -0
- package/src/plugins/PathTextPlugin.ts +89 -0
- package/src/plugins/PolygonModifyPlugin.ts +224 -0
- package/src/plugins/PrintPlugin.ts +81 -0
- package/src/plugins/PsdPlugin.ts +52 -0
- package/src/plugins/QrCodePlugin.ts +393 -0
- package/src/plugins/ResizePlugin.ts +274 -0
- package/src/plugins/RulerPlugin.ts +78 -0
- package/src/plugins/SimpleClipImagePlugin.ts +244 -0
- package/src/plugins/UnitPlugin.ts +327 -0
- package/src/plugins/WaterMarkPlugin.ts +257 -0
- package/src/plugins/WorkspacePlugin.ts +307 -0
- package/src/types/eventType.ts +11 -0
- package/src/utils/psd.js +432 -0
- package/src/utils/ruler/guideline.ts +145 -0
- package/src/utils/ruler/index.ts +91 -0
- package/src/utils/ruler/ruler.ts +924 -0
- package/src/utils/ruler/utils.ts +162 -0
- package/src/utils/units.ts +133 -0
- package/tsconfig.json +10 -0
- package/vite.config.ts +29 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { v4 as uuid } from 'uuid';
|
|
2
|
+
import { fabric } from '@hprint/core';
|
|
3
|
+
import Arrow from '../objects/Arrow';
|
|
4
|
+
import ThinTailArrow from '../objects/ThinTailArrow';
|
|
5
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
6
|
+
|
|
7
|
+
type IPlugin = Pick<DrawLinePlugin, 'setLineType' | 'setMode'>;
|
|
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 DrawLinePlugin implements IPluginTempl {
|
|
15
|
+
static pluginName = 'DrawLinePlugin';
|
|
16
|
+
static apis = ['setLineType', 'setMode'];
|
|
17
|
+
isDrawingLineMode: boolean;
|
|
18
|
+
lineType: string;
|
|
19
|
+
lineToDraw: any;
|
|
20
|
+
pointer: any;
|
|
21
|
+
pointerPoints: any;
|
|
22
|
+
isDrawingLine: boolean;
|
|
23
|
+
constructor(
|
|
24
|
+
public canvas: fabric.Canvas,
|
|
25
|
+
public editor: IEditor
|
|
26
|
+
) {
|
|
27
|
+
this.isDrawingLine = false;
|
|
28
|
+
this.isDrawingLineMode = false;
|
|
29
|
+
this.lineType = '';
|
|
30
|
+
this.lineToDraw = null;
|
|
31
|
+
this.pointer = null;
|
|
32
|
+
this.pointerPoints = null;
|
|
33
|
+
this.init();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
init() {
|
|
37
|
+
const { canvas } = this;
|
|
38
|
+
canvas.on('mouse:down', (o) => {
|
|
39
|
+
if (!this.isDrawingLineMode) return;
|
|
40
|
+
canvas.discardActiveObject();
|
|
41
|
+
canvas.getObjects().forEach((obj) => {
|
|
42
|
+
obj.selectable = false;
|
|
43
|
+
obj.hasControls = false;
|
|
44
|
+
});
|
|
45
|
+
canvas.requestRenderAll();
|
|
46
|
+
this.isDrawingLine = true;
|
|
47
|
+
this.pointer = canvas.getPointer(o.e);
|
|
48
|
+
this.pointerPoints = [
|
|
49
|
+
this.pointer.x,
|
|
50
|
+
this.pointer.y,
|
|
51
|
+
this.pointer.x,
|
|
52
|
+
this.pointer.y,
|
|
53
|
+
];
|
|
54
|
+
let NodeHandler;
|
|
55
|
+
let opts: any = {
|
|
56
|
+
strokeWidth: 2,
|
|
57
|
+
stroke: '#000000',
|
|
58
|
+
id: uuid(),
|
|
59
|
+
};
|
|
60
|
+
switch (this.lineType) {
|
|
61
|
+
case 'line':
|
|
62
|
+
NodeHandler = fabric.Line;
|
|
63
|
+
break;
|
|
64
|
+
case 'arrow':
|
|
65
|
+
NodeHandler = Arrow;
|
|
66
|
+
break;
|
|
67
|
+
case 'thinTailArrow':
|
|
68
|
+
NodeHandler = ThinTailArrow;
|
|
69
|
+
opts = {
|
|
70
|
+
strokeWidth: 2,
|
|
71
|
+
stroke: '#ff0000',
|
|
72
|
+
fill: '#ff0000',
|
|
73
|
+
id: uuid(),
|
|
74
|
+
};
|
|
75
|
+
break;
|
|
76
|
+
default:
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
if (!NodeHandler) throw new Error('Draw failed: invalid lineType.');
|
|
80
|
+
|
|
81
|
+
this.lineToDraw = new NodeHandler(this.pointerPoints, opts);
|
|
82
|
+
|
|
83
|
+
this.lineToDraw.selectable = false;
|
|
84
|
+
this.lineToDraw.evented = false;
|
|
85
|
+
this.lineToDraw.strokeUniform = true;
|
|
86
|
+
canvas.add(this.lineToDraw);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
canvas.on('mouse:move', (o) => {
|
|
90
|
+
if (
|
|
91
|
+
!this.isDrawingLine ||
|
|
92
|
+
!['line', 'arrow', 'thinTailArrow'].includes(this.lineType)
|
|
93
|
+
)
|
|
94
|
+
return;
|
|
95
|
+
canvas.discardActiveObject();
|
|
96
|
+
const activeObject = canvas.getActiveObject();
|
|
97
|
+
if (activeObject) return;
|
|
98
|
+
this.pointer = canvas.getPointer(o.e);
|
|
99
|
+
|
|
100
|
+
if (o.e.shiftKey) {
|
|
101
|
+
// calc angle
|
|
102
|
+
const startX = this.pointerPoints[0];
|
|
103
|
+
const startY = this.pointerPoints[1];
|
|
104
|
+
const x2 = this.pointer.x - startX;
|
|
105
|
+
const y2 = this.pointer.y - startY;
|
|
106
|
+
const r = Math.sqrt(x2 * x2 + y2 * y2);
|
|
107
|
+
let angle = (Math.atan2(y2, x2) / Math.PI) * 180;
|
|
108
|
+
angle = ~~(((angle + 7.5) % 360) / 15) * 15;
|
|
109
|
+
|
|
110
|
+
const cosx = r * Math.cos((angle * Math.PI) / 180);
|
|
111
|
+
const sinx = r * Math.sin((angle * Math.PI) / 180);
|
|
112
|
+
|
|
113
|
+
this.lineToDraw.set({
|
|
114
|
+
x2: cosx + startX,
|
|
115
|
+
y2: sinx + startY,
|
|
116
|
+
});
|
|
117
|
+
} else {
|
|
118
|
+
this.lineToDraw.set({
|
|
119
|
+
x2: this.pointer.x,
|
|
120
|
+
y2: this.pointer.y,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
canvas.renderAll();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
canvas.on('mouse:up', () => {
|
|
128
|
+
if (!this.isDrawingLine) return;
|
|
129
|
+
this.lineToDraw.setCoords();
|
|
130
|
+
this.isDrawingLine = false;
|
|
131
|
+
canvas.discardActiveObject();
|
|
132
|
+
canvas.renderAll();
|
|
133
|
+
this.editor.saveState();
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
setLineType(params: any) {
|
|
138
|
+
this.lineType = params;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
setMode(params: any) {
|
|
142
|
+
this.isDrawingLineMode = params;
|
|
143
|
+
if (!this.isDrawingLineMode) {
|
|
144
|
+
this.endRest();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
endRest() {
|
|
149
|
+
this.canvas.getObjects().forEach((obj) => {
|
|
150
|
+
if (obj.id !== 'workspace') {
|
|
151
|
+
obj.selectable = true;
|
|
152
|
+
obj.hasControls = true;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
destroy() {
|
|
158
|
+
console.log('pluginDestroy');
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export default DrawLinePlugin;
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { fabric } from '@hprint/core';
|
|
2
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
3
|
+
import { SelectMode } from '../types/eventType';
|
|
4
|
+
|
|
5
|
+
type IPlugin = Pick<FlipPlugin, 'flip'>;
|
|
6
|
+
|
|
7
|
+
declare module '@hprint/core' {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
9
|
+
interface IEditor extends IPlugin {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// import event from '@/utils/event/notifier';
|
|
13
|
+
|
|
14
|
+
export default class FlipPlugin implements IPluginTempl {
|
|
15
|
+
static pluginName = 'FlipPlugin';
|
|
16
|
+
static apis = ['flip'];
|
|
17
|
+
constructor(
|
|
18
|
+
public canvas: fabric.Canvas,
|
|
19
|
+
public editor: IEditor
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
flip(type: 'X' | 'Y') {
|
|
23
|
+
const activeObject = this.canvas.getActiveObject();
|
|
24
|
+
if (activeObject) {
|
|
25
|
+
activeObject
|
|
26
|
+
.set(`flip${type}`, !activeObject[`flip${type}`])
|
|
27
|
+
.setCoords();
|
|
28
|
+
this.canvas.requestRenderAll();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
contextMenu() {
|
|
33
|
+
const selectedMode = this.editor.getSelectMode();
|
|
34
|
+
if (selectedMode === SelectMode.ONE) {
|
|
35
|
+
return [
|
|
36
|
+
{
|
|
37
|
+
text: '翻转',
|
|
38
|
+
hotkey: '❯',
|
|
39
|
+
subitems: [
|
|
40
|
+
{
|
|
41
|
+
text: '水平翻转',
|
|
42
|
+
hotkey: '|',
|
|
43
|
+
onclick: () => this.flip('X'),
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
text: '垂直翻转',
|
|
47
|
+
hotkey: '-',
|
|
48
|
+
onclick: () => this.flip('Y'),
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
destroy() {
|
|
57
|
+
console.log('pluginDestroy');
|
|
58
|
+
}
|
|
59
|
+
}
|