@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.
Files changed (153) hide show
  1. package/dist/index.css +1 -0
  2. package/dist/index.js +478 -0
  3. package/dist/index.mjs +41731 -0
  4. package/dist/src/index.d.ts +8 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/objects/Arrow.d.ts +2 -0
  7. package/dist/src/objects/Arrow.d.ts.map +1 -0
  8. package/dist/src/objects/ThinTailArrow.d.ts +2 -0
  9. package/dist/src/objects/ThinTailArrow.d.ts.map +1 -0
  10. package/dist/src/plugins/AddBaseTypePlugin.d.ts +26 -0
  11. package/dist/src/plugins/AddBaseTypePlugin.d.ts.map +1 -0
  12. package/dist/src/plugins/AlignGuidLinePlugin.d.ts +16 -0
  13. package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -0
  14. package/dist/src/plugins/BarCodePlugin.d.ts +68 -0
  15. package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -0
  16. package/dist/src/plugins/CenterAlignPlugin.d.ts +29 -0
  17. package/dist/src/plugins/CenterAlignPlugin.d.ts.map +1 -0
  18. package/dist/src/plugins/ControlsPlugin.d.ts +11 -0
  19. package/dist/src/plugins/ControlsPlugin.d.ts.map +1 -0
  20. package/dist/src/plugins/ControlsRotatePlugin.d.ts +11 -0
  21. package/dist/src/plugins/ControlsRotatePlugin.d.ts.map +1 -0
  22. package/dist/src/plugins/CopyPlugin.d.ts +30 -0
  23. package/dist/src/plugins/CopyPlugin.d.ts.map +1 -0
  24. package/dist/src/plugins/CreateElementPlugin.d.ts +121 -0
  25. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -0
  26. package/dist/src/plugins/DeleteHotKeyPlugin.d.ts +25 -0
  27. package/dist/src/plugins/DeleteHotKeyPlugin.d.ts.map +1 -0
  28. package/dist/src/plugins/DrawLinePlugin.d.ts +26 -0
  29. package/dist/src/plugins/DrawLinePlugin.d.ts.map +1 -0
  30. package/dist/src/plugins/DrawPolygonPlugin.d.ts +41 -0
  31. package/dist/src/plugins/DrawPolygonPlugin.d.ts.map +1 -0
  32. package/dist/src/plugins/DringPlugin.d.ts +33 -0
  33. package/dist/src/plugins/DringPlugin.d.ts.map +1 -0
  34. package/dist/src/plugins/FlipPlugin.d.ts +26 -0
  35. package/dist/src/plugins/FlipPlugin.d.ts.map +1 -0
  36. package/dist/src/plugins/FontPlugin.d.ts +33 -0
  37. package/dist/src/plugins/FontPlugin.d.ts.map +1 -0
  38. package/dist/src/plugins/FreeDrawPlugin.d.ts +23 -0
  39. package/dist/src/plugins/FreeDrawPlugin.d.ts.map +1 -0
  40. package/dist/src/plugins/GroupAlignPlugin.d.ts +24 -0
  41. package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -0
  42. package/dist/src/plugins/GroupPlugin.d.ts +24 -0
  43. package/dist/src/plugins/GroupPlugin.d.ts.map +1 -0
  44. package/dist/src/plugins/GroupTextEditorPlugin.d.ts +18 -0
  45. package/dist/src/plugins/GroupTextEditorPlugin.d.ts.map +1 -0
  46. package/dist/src/plugins/HistoryPlugin.d.ts +30 -0
  47. package/dist/src/plugins/HistoryPlugin.d.ts.map +1 -0
  48. package/dist/src/plugins/ImageStroke.d.ts +18 -0
  49. package/dist/src/plugins/ImageStroke.d.ts.map +1 -0
  50. package/dist/src/plugins/LayerPlugin.d.ts +31 -0
  51. package/dist/src/plugins/LayerPlugin.d.ts.map +1 -0
  52. package/dist/src/plugins/LockPlugin.d.ts +27 -0
  53. package/dist/src/plugins/LockPlugin.d.ts.map +1 -0
  54. package/dist/src/plugins/MaskPlugin.d.ts +38 -0
  55. package/dist/src/plugins/MaskPlugin.d.ts.map +1 -0
  56. package/dist/src/plugins/MaterialPlugin.d.ts +45 -0
  57. package/dist/src/plugins/MaterialPlugin.d.ts.map +1 -0
  58. package/dist/src/plugins/MiddleMousePlugin.d.ts +18 -0
  59. package/dist/src/plugins/MiddleMousePlugin.d.ts.map +1 -0
  60. package/dist/src/plugins/MoveHotKeyPlugin.d.ts +12 -0
  61. package/dist/src/plugins/MoveHotKeyPlugin.d.ts.map +1 -0
  62. package/dist/src/plugins/PathTextPlugin.d.ts +30 -0
  63. package/dist/src/plugins/PathTextPlugin.d.ts.map +1 -0
  64. package/dist/src/plugins/PolygonModifyPlugin.d.ts +28 -0
  65. package/dist/src/plugins/PolygonModifyPlugin.d.ts.map +1 -0
  66. package/dist/src/plugins/PrintPlugin.d.ts +39 -0
  67. package/dist/src/plugins/PrintPlugin.d.ts.map +1 -0
  68. package/dist/src/plugins/PsdPlugin.d.ts +17 -0
  69. package/dist/src/plugins/PsdPlugin.d.ts.map +1 -0
  70. package/dist/src/plugins/QrCodePlugin.d.ts +137 -0
  71. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -0
  72. package/dist/src/plugins/ResizePlugin.d.ts +44 -0
  73. package/dist/src/plugins/ResizePlugin.d.ts.map +1 -0
  74. package/dist/src/plugins/RulerPlugin.d.ts +24 -0
  75. package/dist/src/plugins/RulerPlugin.d.ts.map +1 -0
  76. package/dist/src/plugins/SimpleClipImagePlugin.d.ts +18 -0
  77. package/dist/src/plugins/SimpleClipImagePlugin.d.ts.map +1 -0
  78. package/dist/src/plugins/UnitPlugin.d.ts +84 -0
  79. package/dist/src/plugins/UnitPlugin.d.ts.map +1 -0
  80. package/dist/src/plugins/WaterMarkPlugin.d.ts +40 -0
  81. package/dist/src/plugins/WaterMarkPlugin.d.ts.map +1 -0
  82. package/dist/src/plugins/WorkspacePlugin.d.ts +57 -0
  83. package/dist/src/plugins/WorkspacePlugin.d.ts.map +1 -0
  84. package/dist/src/types/eventType.d.ts +11 -0
  85. package/dist/src/types/eventType.d.ts.map +1 -0
  86. package/dist/src/utils/psd.d.ts +3 -0
  87. package/dist/src/utils/psd.d.ts.map +1 -0
  88. package/dist/src/utils/ruler/guideline.d.ts +4 -0
  89. package/dist/src/utils/ruler/guideline.d.ts.map +1 -0
  90. package/dist/src/utils/ruler/index.d.ts +5 -0
  91. package/dist/src/utils/ruler/index.d.ts.map +1 -0
  92. package/dist/src/utils/ruler/ruler.d.ts +147 -0
  93. package/dist/src/utils/ruler/ruler.d.ts.map +1 -0
  94. package/dist/src/utils/ruler/utils.d.ts +50 -0
  95. package/dist/src/utils/ruler/utils.d.ts.map +1 -0
  96. package/dist/src/utils/units.d.ts +22 -0
  97. package/dist/src/utils/units.d.ts.map +1 -0
  98. package/package.json +51 -0
  99. package/src/assets/edgecontrol.svg +17 -0
  100. package/src/assets/lock.svg +7 -0
  101. package/src/assets/middlecontrol.svg +17 -0
  102. package/src/assets/middlecontrolhoz.svg +17 -0
  103. package/src/assets/rotateicon.svg +20 -0
  104. package/src/assets/style/resizePlugin.css +27 -0
  105. package/src/index.ts +121 -0
  106. package/src/objects/Arrow.js +47 -0
  107. package/src/objects/ThinTailArrow.js +50 -0
  108. package/src/plugins/AddBaseTypePlugin.ts +107 -0
  109. package/src/plugins/AlignGuidLinePlugin.ts +1141 -0
  110. package/src/plugins/BarCodePlugin.ts +860 -0
  111. package/src/plugins/CenterAlignPlugin.ts +133 -0
  112. package/src/plugins/ControlsPlugin.ts +251 -0
  113. package/src/plugins/ControlsRotatePlugin.ts +111 -0
  114. package/src/plugins/CopyPlugin.ts +255 -0
  115. package/src/plugins/CreateElementPlugin.ts +548 -0
  116. package/src/plugins/DeleteHotKeyPlugin.ts +57 -0
  117. package/src/plugins/DrawLinePlugin.ts +162 -0
  118. package/src/plugins/DrawPolygonPlugin.ts +205 -0
  119. package/src/plugins/DringPlugin.ts +125 -0
  120. package/src/plugins/FlipPlugin.ts +59 -0
  121. package/src/plugins/FontPlugin.ts +165 -0
  122. package/src/plugins/FreeDrawPlugin.ts +49 -0
  123. package/src/plugins/GroupAlignPlugin.ts +365 -0
  124. package/src/plugins/GroupPlugin.ts +82 -0
  125. package/src/plugins/GroupTextEditorPlugin.ts +198 -0
  126. package/src/plugins/HistoryPlugin.ts +181 -0
  127. package/src/plugins/ImageStroke.ts +121 -0
  128. package/src/plugins/LayerPlugin.ts +108 -0
  129. package/src/plugins/LockPlugin.ts +240 -0
  130. package/src/plugins/MaskPlugin.ts +155 -0
  131. package/src/plugins/MaterialPlugin.ts +224 -0
  132. package/src/plugins/MiddleMousePlugin.ts +45 -0
  133. package/src/plugins/MoveHotKeyPlugin.ts +46 -0
  134. package/src/plugins/PathTextPlugin.ts +89 -0
  135. package/src/plugins/PolygonModifyPlugin.ts +224 -0
  136. package/src/plugins/PrintPlugin.ts +81 -0
  137. package/src/plugins/PsdPlugin.ts +52 -0
  138. package/src/plugins/QrCodePlugin.ts +393 -0
  139. package/src/plugins/ResizePlugin.ts +274 -0
  140. package/src/plugins/RulerPlugin.ts +78 -0
  141. package/src/plugins/SimpleClipImagePlugin.ts +244 -0
  142. package/src/plugins/UnitPlugin.ts +327 -0
  143. package/src/plugins/WaterMarkPlugin.ts +257 -0
  144. package/src/plugins/WorkspacePlugin.ts +307 -0
  145. package/src/types/eventType.ts +11 -0
  146. package/src/utils/psd.js +432 -0
  147. package/src/utils/ruler/guideline.ts +145 -0
  148. package/src/utils/ruler/index.ts +91 -0
  149. package/src/utils/ruler/ruler.ts +924 -0
  150. package/src/utils/ruler/utils.ts +162 -0
  151. package/src/utils/units.ts +133 -0
  152. package/tsconfig.json +10 -0
  153. 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
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./",
6
+ "composite": true
7
+ },
8
+ "include": ["**/*.ts"],
9
+ "exclude": ["node_modules", "dist"]
10
+ }
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
+ });