@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 type { Rect } from './ruler';
|
|
2
|
+
import { fabric } from '@hprint/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 计算尺子间距
|
|
6
|
+
* @param zoom 缩放比例
|
|
7
|
+
* @returns 返回计算出的尺子间距
|
|
8
|
+
*/
|
|
9
|
+
const getGap = (zoom: number) => {
|
|
10
|
+
const zooms = [0.02, 0.03, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 18];
|
|
11
|
+
const gaps = [5000, 2500, 1000, 500, 250, 100, 50, 25, 10, 5, 2];
|
|
12
|
+
|
|
13
|
+
let i = 0;
|
|
14
|
+
while (i < zooms.length && zooms[i] < zoom) {
|
|
15
|
+
i++;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return gaps[i - 1] || 5000;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 线段合并
|
|
23
|
+
* @param rect Rect数组
|
|
24
|
+
* @param isHorizontal
|
|
25
|
+
* @returns 合并后的Rect数组
|
|
26
|
+
*/
|
|
27
|
+
const mergeLines = (rect: Rect[], isHorizontal: boolean) => {
|
|
28
|
+
const axis = isHorizontal ? 'left' : 'top';
|
|
29
|
+
const length = isHorizontal ? 'width' : 'height';
|
|
30
|
+
// 先按照 axis 的大小排序
|
|
31
|
+
rect.sort((a, b) => a[axis] - b[axis]);
|
|
32
|
+
const mergedLines = [];
|
|
33
|
+
let currentLine = Object.assign({}, rect[0]);
|
|
34
|
+
for (const item of rect) {
|
|
35
|
+
const line = Object.assign({}, item);
|
|
36
|
+
if (currentLine[axis] + currentLine[length] >= line[axis]) {
|
|
37
|
+
// 当前线段和下一个线段相交,合并宽度
|
|
38
|
+
currentLine[length] =
|
|
39
|
+
Math.max(
|
|
40
|
+
currentLine[axis] + currentLine[length],
|
|
41
|
+
line[axis] + line[length]
|
|
42
|
+
) - currentLine[axis];
|
|
43
|
+
} else {
|
|
44
|
+
// 当前线段和下一个线段不相交,将当前线段加入结果数组中,并更新当前线段为下一个线段
|
|
45
|
+
mergedLines.push(currentLine);
|
|
46
|
+
currentLine = Object.assign({}, line);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// 加入数组
|
|
50
|
+
mergedLines.push(currentLine);
|
|
51
|
+
return mergedLines;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const darwLine = (
|
|
55
|
+
ctx: CanvasRenderingContext2D,
|
|
56
|
+
options: {
|
|
57
|
+
left: number;
|
|
58
|
+
top: number;
|
|
59
|
+
width: number;
|
|
60
|
+
height: number;
|
|
61
|
+
stroke?: string | CanvasGradient | CanvasPattern;
|
|
62
|
+
lineWidth?: number;
|
|
63
|
+
}
|
|
64
|
+
) => {
|
|
65
|
+
ctx.save();
|
|
66
|
+
const { left, top, width, height, stroke, lineWidth } = options;
|
|
67
|
+
ctx.beginPath();
|
|
68
|
+
stroke && (ctx.strokeStyle = stroke);
|
|
69
|
+
ctx.lineWidth = lineWidth ?? 1;
|
|
70
|
+
ctx.moveTo(left, top);
|
|
71
|
+
ctx.lineTo(left + width, top + height);
|
|
72
|
+
ctx.stroke();
|
|
73
|
+
ctx.restore();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const darwText = (
|
|
77
|
+
ctx: CanvasRenderingContext2D,
|
|
78
|
+
options: {
|
|
79
|
+
left: number;
|
|
80
|
+
top: number;
|
|
81
|
+
text: string;
|
|
82
|
+
fill?: string | CanvasGradient | CanvasPattern;
|
|
83
|
+
align?: CanvasTextAlign;
|
|
84
|
+
angle?: number;
|
|
85
|
+
fontSize?: number;
|
|
86
|
+
}
|
|
87
|
+
) => {
|
|
88
|
+
ctx.save();
|
|
89
|
+
const { left, top, text, fill, align, angle, fontSize } = options;
|
|
90
|
+
fill && (ctx.fillStyle = fill);
|
|
91
|
+
ctx.textAlign = align ?? 'left';
|
|
92
|
+
ctx.textBaseline = 'top';
|
|
93
|
+
ctx.font = `${fontSize ?? 10}px sans-serif`;
|
|
94
|
+
if (angle) {
|
|
95
|
+
ctx.translate(left, top);
|
|
96
|
+
ctx.rotate((Math.PI / 180) * angle);
|
|
97
|
+
ctx.translate(-left, -top);
|
|
98
|
+
}
|
|
99
|
+
ctx.fillText(text, left, top);
|
|
100
|
+
ctx.restore();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const darwRect = (
|
|
104
|
+
ctx: CanvasRenderingContext2D,
|
|
105
|
+
options: {
|
|
106
|
+
left: number;
|
|
107
|
+
top: number;
|
|
108
|
+
width: number;
|
|
109
|
+
height: number;
|
|
110
|
+
fill?: string | CanvasGradient | CanvasPattern;
|
|
111
|
+
stroke?: string;
|
|
112
|
+
strokeWidth?: number;
|
|
113
|
+
}
|
|
114
|
+
) => {
|
|
115
|
+
ctx.save();
|
|
116
|
+
const { left, top, width, height, fill, stroke, strokeWidth } = options;
|
|
117
|
+
ctx.beginPath();
|
|
118
|
+
fill && (ctx.fillStyle = fill);
|
|
119
|
+
ctx.rect(left, top, width, height);
|
|
120
|
+
ctx.fill();
|
|
121
|
+
if (stroke) {
|
|
122
|
+
ctx.strokeStyle = stroke;
|
|
123
|
+
ctx.lineWidth = strokeWidth ?? 1;
|
|
124
|
+
ctx.stroke();
|
|
125
|
+
}
|
|
126
|
+
ctx.restore();
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const drawMask = (
|
|
130
|
+
ctx: CanvasRenderingContext2D,
|
|
131
|
+
options: {
|
|
132
|
+
isHorizontal: boolean;
|
|
133
|
+
left: number;
|
|
134
|
+
top: number;
|
|
135
|
+
width: number;
|
|
136
|
+
height: number;
|
|
137
|
+
backgroundColor: string;
|
|
138
|
+
}
|
|
139
|
+
) => {
|
|
140
|
+
ctx.save();
|
|
141
|
+
const { isHorizontal, left, top, width, height, backgroundColor } = options;
|
|
142
|
+
// 创建一个线性渐变对象
|
|
143
|
+
const gradient = isHorizontal
|
|
144
|
+
? ctx.createLinearGradient(left, height / 2, left + width, height / 2)
|
|
145
|
+
: ctx.createLinearGradient(width / 2, top, width / 2, height + top);
|
|
146
|
+
const transparentColor = new fabric.Color(backgroundColor);
|
|
147
|
+
transparentColor.setAlpha(0);
|
|
148
|
+
gradient.addColorStop(0, transparentColor.toRgba());
|
|
149
|
+
gradient.addColorStop(0.33, backgroundColor);
|
|
150
|
+
gradient.addColorStop(0.67, backgroundColor);
|
|
151
|
+
gradient.addColorStop(1, transparentColor.toRgba());
|
|
152
|
+
darwRect(ctx, {
|
|
153
|
+
left,
|
|
154
|
+
top,
|
|
155
|
+
width,
|
|
156
|
+
height,
|
|
157
|
+
fill: gradient,
|
|
158
|
+
});
|
|
159
|
+
ctx.restore();
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export { getGap, mergeLines, darwRect, darwText, darwLine, drawMask };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { fabric, IEditor } from '@hprint/core';
|
|
2
|
+
import { LengthConvert } from '@hprint/shared';
|
|
3
|
+
|
|
4
|
+
export type MmOptions = {
|
|
5
|
+
left?: number;
|
|
6
|
+
top?: number;
|
|
7
|
+
width?: number;
|
|
8
|
+
height?: number;
|
|
9
|
+
strokeWidth?: number;
|
|
10
|
+
fontSize?: number;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function applyMmToObject(
|
|
14
|
+
obj: fabric.Object,
|
|
15
|
+
mm: MmOptions,
|
|
16
|
+
dpi?: number
|
|
17
|
+
) {
|
|
18
|
+
const toPx = (v: number | undefined) =>
|
|
19
|
+
v === undefined ? undefined : LengthConvert.mmToPx(v, dpi, { direct: true });
|
|
20
|
+
const leftPx = toPx(mm.left);
|
|
21
|
+
const topPx = toPx(mm.top);
|
|
22
|
+
const widthPx = toPx(mm.width);
|
|
23
|
+
const heightPx = toPx(mm.height);
|
|
24
|
+
const strokeWidthPx = toPx(mm.strokeWidth);
|
|
25
|
+
const fontSizePx = toPx(mm.fontSize);
|
|
26
|
+
|
|
27
|
+
if (leftPx !== undefined) obj.set('left', leftPx);
|
|
28
|
+
if (topPx !== undefined) obj.set('top', topPx);
|
|
29
|
+
if (widthPx !== undefined) obj.set('width', widthPx);
|
|
30
|
+
if (heightPx !== undefined) obj.set('height', heightPx);
|
|
31
|
+
if (strokeWidthPx !== undefined) obj.set('strokeWidth', strokeWidthPx);
|
|
32
|
+
if (fontSizePx !== undefined) (obj as any).fontSize = fontSizePx;
|
|
33
|
+
|
|
34
|
+
(obj as any)._originSize = {
|
|
35
|
+
...(obj as any)._originSize,
|
|
36
|
+
mm: {
|
|
37
|
+
left: mm.left,
|
|
38
|
+
top: mm.top,
|
|
39
|
+
width: mm.width,
|
|
40
|
+
height: mm.height,
|
|
41
|
+
strokeWidth: mm.strokeWidth,
|
|
42
|
+
fontSize: mm.fontSize,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function syncMmFromObject(obj: fabric.Object, dpi?: number, precision?: number) {
|
|
48
|
+
const toMm = (v: number | undefined) =>
|
|
49
|
+
v === undefined ? undefined : applyPrecision(LengthConvert.pxToMm(v, dpi), precision);
|
|
50
|
+
const left = obj.left as number | undefined;
|
|
51
|
+
const top = obj.top as number | undefined;
|
|
52
|
+
const width = obj.width as number | undefined;
|
|
53
|
+
const height = obj.height as number | undefined;
|
|
54
|
+
const strokeWidth = obj.strokeWidth as number | undefined;
|
|
55
|
+
const fontSize = (obj as any).fontSize as number | undefined;
|
|
56
|
+
|
|
57
|
+
(obj as any)._originSize = {
|
|
58
|
+
...(obj as any)._originSize,
|
|
59
|
+
mm: {
|
|
60
|
+
left: toMm(left),
|
|
61
|
+
top: toMm(top),
|
|
62
|
+
width: toMm(width),
|
|
63
|
+
height: toMm(height),
|
|
64
|
+
strokeWidth: toMm(strokeWidth),
|
|
65
|
+
fontSize: toMm(fontSize),
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type UnitType = 'px' | 'mm' | 'inch';
|
|
71
|
+
|
|
72
|
+
export function getUnit(editor: IEditor): UnitType {
|
|
73
|
+
return (editor as any).getUnit?.() ?? 'px';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function convertSingle(
|
|
77
|
+
value: number | string,
|
|
78
|
+
unit: UnitType,
|
|
79
|
+
dpi?: number
|
|
80
|
+
): number {
|
|
81
|
+
if (unit === 'px') return typeof value === 'string' ? Number(value) : value;
|
|
82
|
+
if (unit === 'mm') return LengthConvert.mmToPx(value as any, dpi, { direct: true });
|
|
83
|
+
const num = typeof value === 'string' ? Number(value) : (value as number);
|
|
84
|
+
return LengthConvert.mmToPx(num * LengthConvert.CONSTANTS.INCH_TO_MM, dpi, { direct: true });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function processOptions(
|
|
88
|
+
opts: Record<string, any> = {},
|
|
89
|
+
unit: UnitType,
|
|
90
|
+
dpi?: number,
|
|
91
|
+
fields: string[] = ['left', 'top', 'width', 'height', 'fontSize', 'boxWidth']
|
|
92
|
+
): { processed: Record<string, number>; originByUnit: Record<string, Record<string, any>> } {
|
|
93
|
+
const fieldSet = new Set(fields);
|
|
94
|
+
const processed: Record<string, number> = {};
|
|
95
|
+
const originUnit: Record<string, any> = {};
|
|
96
|
+
for (const key of Object.keys(opts)) {
|
|
97
|
+
if (!fieldSet.has(key)) continue;
|
|
98
|
+
const val = opts[key];
|
|
99
|
+
if (val === undefined) continue;
|
|
100
|
+
originUnit[key] = val;
|
|
101
|
+
processed[key] = convertSingle(val, unit, dpi);
|
|
102
|
+
}
|
|
103
|
+
return { processed, originByUnit: { [unit]: originUnit } };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function applyPrecision(value: number, precision?: number) {
|
|
107
|
+
if (precision === undefined) return value;
|
|
108
|
+
const factor = Math.pow(10, precision);
|
|
109
|
+
return Math.round(value * factor) / factor;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function applyPrecisionDeep(value: any, precision?: number): any {
|
|
113
|
+
if (typeof value === 'number') return applyPrecision(value, precision);
|
|
114
|
+
if (Array.isArray(value)) return value.map((item) => applyPrecisionDeep(item, precision));
|
|
115
|
+
if (value && typeof value === 'object') {
|
|
116
|
+
const result: Record<string, any> = {};
|
|
117
|
+
Object.keys(value).forEach((key) => {
|
|
118
|
+
result[key] = applyPrecisionDeep(value[key], precision);
|
|
119
|
+
});
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function formatOriginValues(origin: Record<string, any>, precision?: number): Record<string, any> {
|
|
126
|
+
return applyPrecisionDeep(origin, precision);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function formatFixedString(num: number, precision = 2): string {
|
|
130
|
+
const s = Number(num).toFixed(precision);
|
|
131
|
+
return s.includes('.') ? s.replace(/\.?0+$/, '') : s;
|
|
132
|
+
}
|
|
133
|
+
|
package/tsconfig.json
ADDED
package/vite.config.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import dts from 'vite-plugin-dts';
|
|
5
|
+
|
|
6
|
+
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
plugins: [
|
|
10
|
+
dts({
|
|
11
|
+
outDir: 'dist',
|
|
12
|
+
include: ['src/**/*'],
|
|
13
|
+
}),
|
|
14
|
+
],
|
|
15
|
+
build: {
|
|
16
|
+
lib: {
|
|
17
|
+
entry: resolve(__dirname, 'src/index.ts'),
|
|
18
|
+
name: '@hprint/plugins',
|
|
19
|
+
fileName: 'index',
|
|
20
|
+
formats: ['es', 'cjs'],
|
|
21
|
+
},
|
|
22
|
+
rollupOptions: {
|
|
23
|
+
external: ['@hprint/core'],
|
|
24
|
+
output: {
|
|
25
|
+
globals: {},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
});
|