@hprint/plugins 0.0.9-alpha.1 → 0.0.11-alpha.1
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.js +53 -53
- package/dist/index.mjs +10140 -9611
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/plugins/ActualContentLayoutPlugin.d.ts.map +1 -1
- package/dist/src/plugins/ControlsPlugin.d.ts +7 -2
- package/dist/src/plugins/ControlsPlugin.d.ts.map +1 -1
- package/dist/src/plugins/GroupAlignPlugin.d.ts +2 -0
- package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -1
- package/dist/src/plugins/TablePlugin.d.ts +95 -0
- package/dist/src/plugins/TablePlugin.d.ts.map +1 -0
- package/dist/src/plugins/UnitPlugin.d.ts +1 -0
- package/dist/src/plugins/UnitPlugin.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +21 -11
- package/src/plugins/ActualContentLayoutPlugin.ts +104 -96
- package/src/plugins/ControlsPlugin.ts +73 -23
- package/src/plugins/CreateElementPlugin.ts +5 -5
- package/src/plugins/GroupAlignPlugin.ts +59 -37
- package/src/plugins/TablePlugin.ts +872 -0
- package/src/plugins/UnitPlugin.ts +48 -15
- package/src/utils/units.ts +5 -5
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
2
|
-
import { LengthConvert } from '@hprint/shared';
|
|
3
|
-
import {
|
|
4
|
-
import { fabric } from '@hprint/core';
|
|
1
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
2
|
+
import { LengthConvert } from '@hprint/shared';
|
|
3
|
+
import { fabric } from '@hprint/core';
|
|
5
4
|
import { throttle } from 'lodash-es';
|
|
6
5
|
|
|
7
6
|
type IPlugin = Pick<
|
|
@@ -305,14 +304,17 @@ class UnitPlugin implements IPluginTempl {
|
|
|
305
304
|
}
|
|
306
305
|
}
|
|
307
306
|
|
|
308
|
-
_bindEvents() {
|
|
309
|
-
const throttledSync = throttle((obj: fabric.Object) => {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
307
|
+
_bindEvents() {
|
|
308
|
+
const throttledSync = throttle((obj: fabric.Object) => {
|
|
309
|
+
// 多选坐标只需在 object:modified 时同步,避免拖动过程中反复创建计算对象。
|
|
310
|
+
if (obj.type !== 'activeSelection') {
|
|
311
|
+
(obj as any).syncOriginSizeByUnit?.();
|
|
312
|
+
}
|
|
313
|
+
}, 30);
|
|
314
|
+
|
|
315
|
+
this.canvas.on('object:modified', (e: any) => {
|
|
316
|
+
const target = e.target as fabric.Object | undefined;
|
|
317
|
+
if (target) this._syncTargetOriginSize(target);
|
|
316
318
|
});
|
|
317
319
|
|
|
318
320
|
this.canvas.on('object:moving', (e: any) => {
|
|
@@ -330,15 +332,46 @@ class UnitPlugin implements IPluginTempl {
|
|
|
330
332
|
if (target) throttledSync(target);
|
|
331
333
|
});
|
|
332
334
|
|
|
333
|
-
this.editor.on?.('sizeChange', (event: { width: number, height: number }) => {
|
|
335
|
+
this.editor.on?.('sizeChange', (event: { width: number, height: number }) => {
|
|
334
336
|
const unit = this.getUnit();
|
|
335
337
|
if (unit === 'px') return;
|
|
336
338
|
if (unit === 'mm') {
|
|
337
339
|
this._syncOriginSize(event.width, event.height);
|
|
338
340
|
return
|
|
339
341
|
}
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
_syncTargetOriginSize(target: fabric.Object) {
|
|
346
|
+
if (target.type !== 'activeSelection') {
|
|
347
|
+
(target as any).syncOriginSizeByUnit?.();
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// 将 ActiveSelection 的平移和旋转真正应用到每个子元素。
|
|
352
|
+
// 之后重新创建选区,避免子元素继续保留相对于旧选区的坐标。
|
|
353
|
+
const objects = (target as fabric.ActiveSelection).getObjects();
|
|
354
|
+
const absoluteAngles = new Map<fabric.Object, number>();
|
|
355
|
+
objects.forEach((item: fabric.Object) => {
|
|
356
|
+
// item.angle 在 ActiveSelection 内是相对角度。必须先通过完整变换矩阵
|
|
357
|
+
// 取得画布上的绝对角度,再销毁选区并写回子元素。
|
|
358
|
+
const { angle } = fabric.util.qrDecompose(item.calcTransformMatrix());
|
|
359
|
+
absoluteAngles.set(item, ((angle % 360) + 360) % 360);
|
|
360
|
+
});
|
|
361
|
+
this.canvas.discardActiveObject();
|
|
362
|
+
objects.forEach((item: fabric.Object) => {
|
|
363
|
+
item.set('angle', absoluteAngles.get(item) ?? 0);
|
|
364
|
+
item.setCoords();
|
|
365
|
+
// 仅同步位置;旋转角度已由 ActiveSelection 变换写回 item.angle。
|
|
366
|
+
(item as any).syncOriginSizeByUnit?.(['left', 'top']);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
const normalizedSelection = new fabric.ActiveSelection(objects, {
|
|
370
|
+
canvas: this.canvas,
|
|
371
|
+
});
|
|
372
|
+
this.canvas.setActiveObject(normalizedSelection);
|
|
373
|
+
this.canvas.requestRenderAll();
|
|
374
|
+
}
|
|
342
375
|
|
|
343
376
|
destroy() {
|
|
344
377
|
console.log('pluginDestroy');
|
package/src/utils/units.ts
CHANGED
|
@@ -49,7 +49,7 @@ export function applyMmToObject(
|
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
export function syncMmFromObject(obj: fabric.Object, dpi?: number, precision?: number) {
|
|
52
|
+
export function syncMmFromObject(obj: fabric.Object, dpi?: number, precision?: number) {
|
|
53
53
|
const isImage = obj.type === 'image';
|
|
54
54
|
const toMm = (v: number | undefined) =>
|
|
55
55
|
v === undefined ? undefined : applyPrecision(LengthConvert.pxToMm(v, dpi), precision);
|
|
@@ -76,10 +76,10 @@ export function syncMmFromObject(obj: fabric.Object, dpi?: number, precision?: n
|
|
|
76
76
|
strokeWidth: toMm(strokeWidth),
|
|
77
77
|
fontSize: toMm(fontSize),
|
|
78
78
|
},
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export type UnitType = 'px' | 'mm' | 'inch';
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type UnitType = 'px' | 'mm' | 'inch';
|
|
83
83
|
|
|
84
84
|
export function getUnit(editor: IEditor): UnitType {
|
|
85
85
|
return (editor as any).getUnit?.() ?? 'px';
|