@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,89 @@
|
|
|
1
|
+
import { fabric } from '@hprint/core';
|
|
2
|
+
import { v4 as uuid } from 'uuid';
|
|
3
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
4
|
+
|
|
5
|
+
type IPlugin = Pick<PathTextPlugin, 'startTextPathDraw' | 'endTextPathDraw'>;
|
|
6
|
+
|
|
7
|
+
declare module '@hprint/core' {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
9
|
+
interface IEditor extends IPlugin {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type DrawOptions = {
|
|
13
|
+
decimate: number;
|
|
14
|
+
width: number;
|
|
15
|
+
defaultText: string;
|
|
16
|
+
color: string;
|
|
17
|
+
lineColor: string;
|
|
18
|
+
defaultFontSize: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default class PathTextPlugin implements IPluginTempl {
|
|
22
|
+
static pluginName = 'PathTextPlugin';
|
|
23
|
+
static apis = ['startTextPathDraw', 'endTextPathDraw'];
|
|
24
|
+
private options?: DrawOptions;
|
|
25
|
+
constructor(
|
|
26
|
+
public canvas: fabric.Canvas,
|
|
27
|
+
public editor: IEditor
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
_beforeHandler = (opt: any) => {
|
|
31
|
+
if (this.options == null) return;
|
|
32
|
+
const path = opt.path as any;
|
|
33
|
+
const getPathSegmentsInfo = (fabric.util as any).getPathSegmentsInfo;
|
|
34
|
+
path.segmentsInfo = getPathSegmentsInfo(path.path);
|
|
35
|
+
path.set({ stroke: this.options.lineColor });
|
|
36
|
+
const text = this.options.defaultText;
|
|
37
|
+
const fontSize = this.options.defaultFontSize;
|
|
38
|
+
const textObject = new fabric.IText(text, {
|
|
39
|
+
shadow: '',
|
|
40
|
+
fontFamily: 'arial',
|
|
41
|
+
fontSize: fontSize,
|
|
42
|
+
top: path.top,
|
|
43
|
+
left: path.left,
|
|
44
|
+
fill: this.options.color,
|
|
45
|
+
path: path,
|
|
46
|
+
id: uuid(),
|
|
47
|
+
// 路径文字元素禁止在画布上直接编辑
|
|
48
|
+
editable: false,
|
|
49
|
+
});
|
|
50
|
+
this.canvas.add(textObject);
|
|
51
|
+
};
|
|
52
|
+
_createdHandler = (opt: any) => {
|
|
53
|
+
this.canvas.remove(opt.path);
|
|
54
|
+
};
|
|
55
|
+
_bindEvent() {
|
|
56
|
+
this.canvas.on('before:path:created', this._beforeHandler);
|
|
57
|
+
this.canvas.on('path:created', this._createdHandler);
|
|
58
|
+
}
|
|
59
|
+
_unbindEvent() {
|
|
60
|
+
this.canvas.off('before:path:created', this._beforeHandler);
|
|
61
|
+
this.canvas.off('path:created', this._createdHandler);
|
|
62
|
+
}
|
|
63
|
+
startTextPathDraw(options: Partial<DrawOptions> = {}) {
|
|
64
|
+
const defaultOptions = {
|
|
65
|
+
decimate: 8,
|
|
66
|
+
width: 2,
|
|
67
|
+
defaultText: '诸事顺遂 万事大吉',
|
|
68
|
+
color: '#000000',
|
|
69
|
+
lineColor: '#000000',
|
|
70
|
+
defaultFontSize: 20,
|
|
71
|
+
};
|
|
72
|
+
this.options = {
|
|
73
|
+
...defaultOptions,
|
|
74
|
+
...options,
|
|
75
|
+
};
|
|
76
|
+
this.canvas.isDrawingMode = true;
|
|
77
|
+
const brush = (this.canvas.freeDrawingBrush = new fabric.PencilBrush(
|
|
78
|
+
this.canvas
|
|
79
|
+
));
|
|
80
|
+
brush.decimate = this.options.decimate;
|
|
81
|
+
brush.width = this.options.width;
|
|
82
|
+
brush.color = this.options.color;
|
|
83
|
+
this._bindEvent();
|
|
84
|
+
}
|
|
85
|
+
endTextPathDraw() {
|
|
86
|
+
this.canvas.isDrawingMode = false;
|
|
87
|
+
this._unbindEvent();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { fabric } from '@hprint/core';
|
|
2
|
+
import { utils } from '@hprint/shared';
|
|
3
|
+
import edgeImg from '../assets/edgecontrol.svg?url';
|
|
4
|
+
import { noop } from 'lodash-es';
|
|
5
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
6
|
+
|
|
7
|
+
type IPlugin = Pick<
|
|
8
|
+
PolygonModifyPlugin,
|
|
9
|
+
'toggleEdit' | 'activeEdit' | 'inActiveEdit'
|
|
10
|
+
>;
|
|
11
|
+
|
|
12
|
+
declare module '@hprint/core' {
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
14
|
+
interface IEditor extends IPlugin {}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type Options = {
|
|
18
|
+
fill: string;
|
|
19
|
+
style: fabric.IObjectOptions['cornerStyle'];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
interface PointIndexPolygon extends fabric.Polygon {
|
|
23
|
+
pointIndex: number;
|
|
24
|
+
__corner: string;
|
|
25
|
+
_setPositionDimensions: (...args: any[]) => any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PointIndexControl extends fabric.Control {
|
|
29
|
+
pointIndex: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const actionHandler: fabric.Control['actionHandler'] = function (
|
|
33
|
+
eventData: MouseEvent,
|
|
34
|
+
transform: fabric.Transform,
|
|
35
|
+
x: number,
|
|
36
|
+
y: number
|
|
37
|
+
) {
|
|
38
|
+
const polygon = transform.target as PointIndexPolygon,
|
|
39
|
+
currentControl = polygon.controls[
|
|
40
|
+
polygon.__corner
|
|
41
|
+
] as PointIndexControl,
|
|
42
|
+
mouseLocalPosition = polygon.toLocalPoint(
|
|
43
|
+
new fabric.Point(x, y),
|
|
44
|
+
'center',
|
|
45
|
+
'center'
|
|
46
|
+
),
|
|
47
|
+
polygonBaseSize = getObjectSizeWithStroke(polygon),
|
|
48
|
+
size = polygon._getTransformedDimensions(0, 0);
|
|
49
|
+
if (polygon.points == null) return false;
|
|
50
|
+
polygon.points[currentControl.pointIndex] = new fabric.Point(
|
|
51
|
+
(mouseLocalPosition.x * polygonBaseSize.x) / size.x +
|
|
52
|
+
polygon.pathOffset.x,
|
|
53
|
+
(mouseLocalPosition.y * polygonBaseSize.y) / size.y +
|
|
54
|
+
polygon.pathOffset.y
|
|
55
|
+
);
|
|
56
|
+
return true;
|
|
57
|
+
};
|
|
58
|
+
const anchorWrapper = function (
|
|
59
|
+
anchorIndex: number,
|
|
60
|
+
fn: fabric.Control['actionHandler']
|
|
61
|
+
) {
|
|
62
|
+
return function (
|
|
63
|
+
eventData: MouseEvent,
|
|
64
|
+
transform: fabric.Transform,
|
|
65
|
+
x: number,
|
|
66
|
+
y: number
|
|
67
|
+
) {
|
|
68
|
+
const fabricObject = transform.target as PointIndexPolygon;
|
|
69
|
+
if (fabricObject.points == null) return false;
|
|
70
|
+
const absolutePoint = fabric.util.transformPoint(
|
|
71
|
+
new fabric.Point(
|
|
72
|
+
fabricObject.points[anchorIndex].x -
|
|
73
|
+
fabricObject.pathOffset.x,
|
|
74
|
+
fabricObject.points[anchorIndex].y -
|
|
75
|
+
fabricObject.pathOffset.y
|
|
76
|
+
),
|
|
77
|
+
fabricObject.calcTransformMatrix()
|
|
78
|
+
),
|
|
79
|
+
actionPerformed = fn(eventData, transform, x, y),
|
|
80
|
+
// newDim = fabricObject._setPositionDimensions({}),
|
|
81
|
+
polygonBaseSize = getObjectSizeWithStroke(fabricObject),
|
|
82
|
+
newX =
|
|
83
|
+
(fabricObject.points[anchorIndex].x -
|
|
84
|
+
fabricObject.pathOffset.x) /
|
|
85
|
+
polygonBaseSize.x,
|
|
86
|
+
newY =
|
|
87
|
+
(fabricObject.points[anchorIndex].y -
|
|
88
|
+
fabricObject.pathOffset.y) /
|
|
89
|
+
polygonBaseSize.y;
|
|
90
|
+
const originX = (newX + 0.5) as any;
|
|
91
|
+
const originY = (newY + 0.5) as any;
|
|
92
|
+
fabricObject.setPositionByOrigin(absolutePoint, originX, originY);
|
|
93
|
+
return actionPerformed;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
const getObjectSizeWithStroke = function (object: fabric.Object) {
|
|
97
|
+
const stroke = new fabric.Point(
|
|
98
|
+
object.strokeUniform ? 1 / object.scaleX! : 1,
|
|
99
|
+
object.strokeUniform ? 1 / object.scaleY! : 1
|
|
100
|
+
).multiply(object.strokeWidth!);
|
|
101
|
+
return new fabric.Point(
|
|
102
|
+
object.width! + stroke.x,
|
|
103
|
+
object.height! + stroke.y
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
const polygonPositionHandler = function (
|
|
107
|
+
this: PointIndexControl,
|
|
108
|
+
dim: any,
|
|
109
|
+
finalMatrix: any,
|
|
110
|
+
fabricObject: any
|
|
111
|
+
) {
|
|
112
|
+
const x =
|
|
113
|
+
fabricObject.points[this.pointIndex].x - fabricObject.pathOffset.x,
|
|
114
|
+
y = fabricObject.points[this.pointIndex].y - fabricObject.pathOffset.y;
|
|
115
|
+
// 求出在世界坐标系的位置
|
|
116
|
+
return fabric.util.transformPoint(
|
|
117
|
+
new fabric.Point(x, y), // 物体坐标系下的位置
|
|
118
|
+
fabric.util.multiplyTransformMatrices(
|
|
119
|
+
fabricObject.canvas.viewportTransform,
|
|
120
|
+
fabricObject.calcTransformMatrix()
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
};
|
|
124
|
+
function renderIconEdge(
|
|
125
|
+
ctx: CanvasRenderingContext2D,
|
|
126
|
+
left: number,
|
|
127
|
+
top: number,
|
|
128
|
+
styleOverride: any,
|
|
129
|
+
fabricObject: fabric.Object,
|
|
130
|
+
img: HTMLImageElement
|
|
131
|
+
) {
|
|
132
|
+
utils.drawImg(
|
|
133
|
+
ctx,
|
|
134
|
+
left,
|
|
135
|
+
top,
|
|
136
|
+
img,
|
|
137
|
+
25,
|
|
138
|
+
25,
|
|
139
|
+
fabric.util.degreesToRadians(fabricObject.angle || 0)
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
class PolygonModifyPlugin implements IPluginTempl {
|
|
144
|
+
public isEdit: boolean;
|
|
145
|
+
private img: HTMLImageElement;
|
|
146
|
+
static pluginName = 'PolygonModifyPlugin';
|
|
147
|
+
static events = [];
|
|
148
|
+
static apis = ['toggleEdit', 'activeEdit', 'inActiveEdit'];
|
|
149
|
+
|
|
150
|
+
constructor(
|
|
151
|
+
public canvas: fabric.Canvas,
|
|
152
|
+
public editor: IEditor
|
|
153
|
+
) {
|
|
154
|
+
this.isEdit = false;
|
|
155
|
+
const img = document.createElement('img');
|
|
156
|
+
img.src = edgeImg;
|
|
157
|
+
this.img = img;
|
|
158
|
+
this.init();
|
|
159
|
+
}
|
|
160
|
+
init() {
|
|
161
|
+
console.info('[PolygonModifyPlugin]: init');
|
|
162
|
+
}
|
|
163
|
+
_onDeselected: () => any = noop;
|
|
164
|
+
_ensureEvent(poly: fabric.Object) {
|
|
165
|
+
poly.off('deselected', this._onDeselected);
|
|
166
|
+
}
|
|
167
|
+
toggleEdit() {
|
|
168
|
+
this.isEdit ? this.inActiveEdit() : this.activeEdit();
|
|
169
|
+
}
|
|
170
|
+
activeEdit() {
|
|
171
|
+
this.isEdit = true;
|
|
172
|
+
const poly = this.canvas.getActiveObject() as fabric.Polygon;
|
|
173
|
+
if (poly && poly.type === 'polygon') {
|
|
174
|
+
this._ensureEvent(poly);
|
|
175
|
+
if (poly.points == null) return;
|
|
176
|
+
const lastControl = poly.points.length - 1;
|
|
177
|
+
const This = this;
|
|
178
|
+
poly.controls = poly.points.reduce<
|
|
179
|
+
Record<string, PointIndexControl>
|
|
180
|
+
>(function (acc, point, index) {
|
|
181
|
+
acc['p' + index] = <PointIndexControl>new fabric.Control({
|
|
182
|
+
positionHandler: polygonPositionHandler,
|
|
183
|
+
actionHandler: anchorWrapper(
|
|
184
|
+
index > 0 ? index - 1 : lastControl,
|
|
185
|
+
actionHandler
|
|
186
|
+
),
|
|
187
|
+
actionName: 'modifyPolygon',
|
|
188
|
+
render: (...args) => renderIconEdge(...args, This.img),
|
|
189
|
+
});
|
|
190
|
+
Object.defineProperty(acc['p' + index], 'pointIndex', {
|
|
191
|
+
value: index,
|
|
192
|
+
});
|
|
193
|
+
return acc;
|
|
194
|
+
}, {});
|
|
195
|
+
poly.set({
|
|
196
|
+
objectCaching: false,
|
|
197
|
+
});
|
|
198
|
+
poly.hasBorders = !this.isEdit;
|
|
199
|
+
this.canvas.requestRenderAll();
|
|
200
|
+
this._onDeselected = () => this.inActiveEdit(poly);
|
|
201
|
+
poly.on('deselected', this._onDeselected);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
inActiveEdit(poly?: fabric.Polygon) {
|
|
205
|
+
this.isEdit = false;
|
|
206
|
+
poly = poly || (this.canvas.getActiveObject() as fabric.Polygon);
|
|
207
|
+
if (poly && poly.type === 'polygon') {
|
|
208
|
+
poly.cornerColor = 'blue';
|
|
209
|
+
poly.cornerStyle = 'rect';
|
|
210
|
+
poly.controls = fabric.Object.prototype.controls;
|
|
211
|
+
poly.hasBorders = !this.isEdit;
|
|
212
|
+
poly.set({
|
|
213
|
+
objectCaching: true,
|
|
214
|
+
});
|
|
215
|
+
if (this._onDeselected) {
|
|
216
|
+
poly.off('deselected', this._onDeselected);
|
|
217
|
+
this._onDeselected = noop;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
this.canvas.requestRenderAll();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export default PolygonModifyPlugin;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { fabric } from '@hprint/core';
|
|
2
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
3
|
+
import { io } from 'socket.io-client';
|
|
4
|
+
|
|
5
|
+
type IPlugin = Pick<PrintPlugin, 'printPDF' | 'getPrinterList'>;
|
|
6
|
+
|
|
7
|
+
export interface Option {
|
|
8
|
+
'printer-location': string;
|
|
9
|
+
'printer-make-and-model': string;
|
|
10
|
+
system_driverinfo: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface IPrinterItem {
|
|
14
|
+
name: string;
|
|
15
|
+
displayName: string;
|
|
16
|
+
description: string;
|
|
17
|
+
status: number;
|
|
18
|
+
isDefault: boolean;
|
|
19
|
+
options: Option;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface IPrintPDFOption {
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
25
|
+
base64: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare module '@hprint/core' {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
30
|
+
interface IEditor extends IPlugin {}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default class PrintPlugin implements IPluginTempl {
|
|
34
|
+
static pluginName = 'PrintPlugin';
|
|
35
|
+
static apis = ['printPDF', 'getPrinterList'];
|
|
36
|
+
|
|
37
|
+
socket?: any;
|
|
38
|
+
isConnect = false;
|
|
39
|
+
printerList: IPrinterItem[] = [];
|
|
40
|
+
|
|
41
|
+
constructor(
|
|
42
|
+
public canvas: fabric.Canvas,
|
|
43
|
+
public editor: IEditor
|
|
44
|
+
) {
|
|
45
|
+
this.editor = editor;
|
|
46
|
+
this.canvas = canvas;
|
|
47
|
+
|
|
48
|
+
this.socket = io('http://localhost:17521', {
|
|
49
|
+
transports: ['websocket'],
|
|
50
|
+
auth: {
|
|
51
|
+
token: 'vue-plugin-hiprint',
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
this.socket.on('connect', () => {
|
|
56
|
+
this.isConnect = true;
|
|
57
|
+
// globalThis.connect = true;
|
|
58
|
+
// TODO: Do something for your project
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this.socket.on('printerList', (printerList: IPrinterItem[]) => {
|
|
62
|
+
console.log('printerList', printerList);
|
|
63
|
+
this.printerList = printerList;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
printPDF(options: IPrintPDFOption) {
|
|
68
|
+
this.socket.emit('printPDF', {
|
|
69
|
+
...options,
|
|
70
|
+
unit: 'mm',
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
getPrinterList() {
|
|
75
|
+
return this.printerList;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
destroy() {
|
|
79
|
+
console.log('pluginDestroy');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { fabric } from '@hprint/core';
|
|
2
|
+
import { utils } from '@hprint/shared';
|
|
3
|
+
import psdToJson from '../utils/psd';
|
|
4
|
+
import Psd from '@webtoon/psd';
|
|
5
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
6
|
+
|
|
7
|
+
type IPlugin = Pick<PsdPlugin, 'insertPSD'>;
|
|
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 PsdPlugin implements IPluginTempl {
|
|
15
|
+
static pluginName = 'PsdPlugin';
|
|
16
|
+
static apis = ['insertPSD'];
|
|
17
|
+
constructor(
|
|
18
|
+
public canvas: fabric.Canvas,
|
|
19
|
+
public editor: IEditor
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
insertPSD(callback?: () => void) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
utils
|
|
25
|
+
.selectFiles({ accept: '.psd' })
|
|
26
|
+
.then((files) => {
|
|
27
|
+
if (files && files.length > 0) {
|
|
28
|
+
const file = files[0];
|
|
29
|
+
const reader = new FileReader();
|
|
30
|
+
reader.readAsText(file, 'UTF-8');
|
|
31
|
+
reader.onload = async () => {
|
|
32
|
+
const result = await file.arrayBuffer();
|
|
33
|
+
// 解析PSD文件
|
|
34
|
+
const psdFile = Psd.parse(result as ArrayBuffer);
|
|
35
|
+
console.log(psdFile, '11111');
|
|
36
|
+
const json = await psdToJson(psdFile);
|
|
37
|
+
// 加载json
|
|
38
|
+
this.loadJSON(json, callback);
|
|
39
|
+
resolve('');
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
.catch(reject);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
loadJSON(json: string, callback?: () => void) {
|
|
48
|
+
this.editor.loadJSON(json, callback);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default PsdPlugin;
|