@hprint/plugins 0.0.9-alpha.0 → 0.0.10

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.
@@ -45,11 +45,29 @@ class QrCodePlugin implements IPluginTempl {
45
45
  }
46
46
  }
47
47
 
48
- async hookTransformObjectEnd({ originObject, fabricObject }: { originObject: any, fabricObject: any }) {
49
- if (originObject.extensionType === 'qrcode') {
50
- this.initQrcodeEvents(fabricObject);
51
- }
52
- }
48
+ async hookTransformObjectEnd({ originObject, fabricObject }: { originObject: any, fabricObject: any }) {
49
+ if (originObject.extensionType === 'qrcode') {
50
+ this.initQrcodeEvents(fabricObject);
51
+ }
52
+ }
53
+
54
+ async hookImportAfter() {
55
+ const qrcodeObjects = this.canvas
56
+ .getObjects()
57
+ .filter(
58
+ (obj: any) =>
59
+ obj.type === 'image' && obj.extensionType === 'qrcode'
60
+ ) as fabric.Image[];
61
+
62
+ await Promise.all(
63
+ qrcodeObjects.map(async (imgEl) => {
64
+ this.initQrcodeEvents(imgEl);
65
+ await this._updateQrCodeImage(imgEl, true);
66
+ })
67
+ );
68
+
69
+ this.canvas.renderAll();
70
+ }
53
71
 
54
72
  async _getQrCodeResult(options: any): Promise<{ url: string; width: number; height: number }> {
55
73
  const zoom = this.canvas.getZoom() || 1;
@@ -123,11 +141,11 @@ class QrCodePlugin implements IPluginTempl {
123
141
  return options;
124
142
  }
125
143
 
126
- private async _updateQrCodeImage(imgEl: fabric.Image, immediate = false) {
127
- const extension = imgEl.get('extension');
128
- if (!extension) return;
129
- const updateFn = async () => {
130
- const currentWidth = imgEl.getScaledWidth();
144
+ private async _updateQrCodeImage(imgEl: fabric.Image, immediate = false) {
145
+ const extension = imgEl.get('extension');
146
+ if (!extension) return;
147
+ const updateFn = async () => {
148
+ const currentWidth = imgEl.getScaledWidth();
131
149
  const currentHeight = imgEl.getScaledHeight();
132
150
  const size = Math.max(currentWidth, currentHeight);
133
151
  const options = {
@@ -135,11 +153,11 @@ class QrCodePlugin implements IPluginTempl {
135
153
  width: size,
136
154
  height: size,
137
155
  };
138
- const paramsOption = this._paramsToOption(options);
139
- try {
140
- const url = await this._getBase64Str(paramsOption);
141
- await new Promise<void>((resolve) => {
142
- imgEl.setSrc(url, () => {
156
+ const paramsOption = this._paramsToOption(options);
157
+ try {
158
+ const url = await this._getBase64Str(paramsOption);
159
+ await new Promise<void>((resolve) => {
160
+ imgEl.setSrc(url, () => {
143
161
  this._setImageScale(imgEl, currentWidth, currentHeight);
144
162
  imgEl.set('extension', options);
145
163
  this.canvas.renderAll();
@@ -1,7 +1,6 @@
1
- import type { IEditor, IPluginTempl } from '@hprint/core';
2
- import { LengthConvert } from '@hprint/shared';
3
- import { syncMmFromObject } from '../utils/units';
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
- syncMmFromObject(obj, undefined, this.precision);
311
- }, 30);
312
-
313
- this.canvas.on('object:modified', (e: any) => {
314
- const target = e.target as fabric.Object | undefined;
315
- if (target) syncMmFromObject(target, undefined, this.precision);
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');
@@ -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';