polotno 3.13.0 → 3.14.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/blueprint/side-panel/select-video.d.ts +105 -36
- package/canvas/apply-filters.js +1 -1
- package/canvas/downscale.d.ts +4 -0
- package/canvas/downscale.js +1 -1
- package/canvas/element.d.ts +1 -1
- package/canvas/element.js +1 -1
- package/canvas/figure-element.js +1 -1
- package/canvas/gif-element.js +1 -1
- package/canvas/html-element.js +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/line-element.js +1 -1
- package/canvas/page.js +1 -1
- package/canvas/paint-opacity.d.ts +12 -0
- package/canvas/paint-opacity.js +1 -0
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.js +11 -3
- package/canvas/table-element.js +1 -1
- package/canvas/text-element/font-specs-loader.js +1 -1
- package/canvas/text-element.js +1 -1
- package/canvas/use-filtered-source.js +1 -1
- package/canvas/video-element.js +1 -1
- package/canvas/workspace-canvas.js +1 -1
- package/model/audio-model.d.ts +54 -2
- package/model/figure-model.d.ts +65 -24
- package/model/gif-model.d.ts +65 -24
- package/model/group-model.d.ts +497 -182
- package/model/group-model.js +1 -1
- package/model/history.js +1 -1
- package/model/image-model.d.ts +65 -24
- package/model/line-model.d.ts +65 -24
- package/model/node-model.d.ts +65 -24
- package/model/node-model.js +1 -1
- package/model/page-model.d.ts +4369 -445
- package/model/page-model.js +1 -1
- package/model/shape-model.d.ts +65 -24
- package/model/store.d.ts +480 -18060
- package/model/store.js +1 -1
- package/model/svg-model.d.ts +64 -24
- package/model/table-model.d.ts +40 -12
- package/model/table-model.js +1 -1
- package/model/text-model.d.ts +64 -24
- package/model/text-model.js +1 -1
- package/model/video-model.d.ts +65 -24
- package/package.json +8 -8
- package/polotno.bundle.js +167 -156
- package/primitives/panel-header.js +1 -1
- package/primitives/panel-scroll-area.d.ts +4 -0
- package/primitives/panel-scroll-area.js +1 -0
- package/primitives/select.js +1 -1
- package/primitives/slider.js +1 -1
- package/side-panel/animations-panel.js +1 -1
- package/side-panel/draw-panel.js +1 -1
- package/side-panel/effects-panel.js +1 -1
- package/side-panel/elements-panel.js +1 -1
- package/side-panel/image-clip-panel.js +1 -1
- package/side-panel/layers-panel.js +1 -1
- package/side-panel/select-video.d.ts +105 -36
- package/side-panel/side-panel.js +1 -1
- package/side-panel/size-panel.js +1 -1
- package/side-panel/text-panel.js +1 -1
- package/toolbar/color-picker.js +1 -1
- package/toolbar/html-toolbar.js +3 -3
- package/toolbar/opacity-picker.js +1 -1
- package/toolbar/text-toolbar.js +1 -1
- package/ui.css +1 -1
- package/utils/canvas.d.ts +0 -3
- package/utils/canvas.js +1 -1
- package/utils/clipboard.d.ts +104 -36
- package/utils/fonts.js +1 -1
- package/utils/from-svg.js +1 -1
- package/utils/l10n.d.ts +21 -0
- package/utils/l10n.js +1 -1
- package/utils/pointer-drag.js +1 -1
- package/utils/text-format-state.d.ts +3 -0
- package/utils/text-format-state.js +1 -1
- package/utils/validate-key.js +1 -1
|
@@ -15,6 +15,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
15
15
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
16
16
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
17
17
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
18
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
18
19
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
19
20
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
20
21
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -91,6 +92,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
91
92
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
92
93
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
93
94
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
95
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
94
96
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
95
97
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
96
98
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -102,6 +104,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
102
104
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
103
105
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
104
106
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
107
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
105
108
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
106
109
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
107
110
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -113,24 +116,37 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
113
116
|
type: string | undefined;
|
|
114
117
|
name: string | undefined;
|
|
115
118
|
opacity: number | undefined;
|
|
119
|
+
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light" | undefined;
|
|
116
120
|
custom: any;
|
|
117
121
|
visible: boolean | undefined;
|
|
118
122
|
selectable: boolean | undefined;
|
|
119
123
|
removable: boolean | undefined;
|
|
120
124
|
alwaysOnTop: boolean | undefined;
|
|
121
125
|
showInExport: boolean | undefined;
|
|
122
|
-
}>,
|
|
123
|
-
id:
|
|
124
|
-
type:
|
|
125
|
-
name:
|
|
126
|
-
opacity:
|
|
127
|
-
custom:
|
|
128
|
-
visible:
|
|
129
|
-
selectable:
|
|
130
|
-
removable:
|
|
131
|
-
alwaysOnTop:
|
|
132
|
-
showInExport:
|
|
133
|
-
}
|
|
126
|
+
}>, {
|
|
127
|
+
id: string;
|
|
128
|
+
type: string;
|
|
129
|
+
name: string;
|
|
130
|
+
opacity: number;
|
|
131
|
+
custom: any;
|
|
132
|
+
visible: boolean;
|
|
133
|
+
selectable: boolean;
|
|
134
|
+
removable: boolean;
|
|
135
|
+
alwaysOnTop: boolean;
|
|
136
|
+
showInExport: boolean;
|
|
137
|
+
} | {
|
|
138
|
+
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light";
|
|
139
|
+
id: string;
|
|
140
|
+
type: string;
|
|
141
|
+
name: string;
|
|
142
|
+
opacity: number;
|
|
143
|
+
custom: any;
|
|
144
|
+
visible: boolean;
|
|
145
|
+
selectable: boolean;
|
|
146
|
+
removable: boolean;
|
|
147
|
+
alwaysOnTop: boolean;
|
|
148
|
+
showInExport: boolean;
|
|
149
|
+
}>>;
|
|
134
150
|
readonly parent: any;
|
|
135
151
|
readonly zIndex: any;
|
|
136
152
|
} & {
|
|
@@ -145,6 +161,18 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
145
161
|
removable: boolean;
|
|
146
162
|
alwaysOnTop: boolean;
|
|
147
163
|
showInExport: boolean;
|
|
164
|
+
} | {
|
|
165
|
+
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light";
|
|
166
|
+
id: string;
|
|
167
|
+
type: string;
|
|
168
|
+
name: string;
|
|
169
|
+
opacity: number;
|
|
170
|
+
custom: any;
|
|
171
|
+
visible: boolean;
|
|
172
|
+
selectable: boolean;
|
|
173
|
+
removable: boolean;
|
|
174
|
+
alwaysOnTop: boolean;
|
|
175
|
+
showInExport: boolean;
|
|
148
176
|
};
|
|
149
177
|
} & {
|
|
150
178
|
clone(attrs?: any, { skipSelect }?: {
|
|
@@ -183,6 +211,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
183
211
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
184
212
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
185
213
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
214
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
186
215
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
187
216
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
188
217
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -259,6 +288,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
259
288
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
260
289
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
261
290
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
291
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
262
292
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
263
293
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
264
294
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -270,6 +300,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
270
300
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
271
301
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
272
302
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
303
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
273
304
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
274
305
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
275
306
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -281,24 +312,37 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
281
312
|
type: string | undefined;
|
|
282
313
|
name: string | undefined;
|
|
283
314
|
opacity: number | undefined;
|
|
315
|
+
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light" | undefined;
|
|
284
316
|
custom: any;
|
|
285
317
|
visible: boolean | undefined;
|
|
286
318
|
selectable: boolean | undefined;
|
|
287
319
|
removable: boolean | undefined;
|
|
288
320
|
alwaysOnTop: boolean | undefined;
|
|
289
321
|
showInExport: boolean | undefined;
|
|
290
|
-
}>,
|
|
291
|
-
id:
|
|
292
|
-
type:
|
|
293
|
-
name:
|
|
294
|
-
opacity:
|
|
295
|
-
custom:
|
|
296
|
-
visible:
|
|
297
|
-
selectable:
|
|
298
|
-
removable:
|
|
299
|
-
alwaysOnTop:
|
|
300
|
-
showInExport:
|
|
301
|
-
}
|
|
322
|
+
}>, {
|
|
323
|
+
id: string;
|
|
324
|
+
type: string;
|
|
325
|
+
name: string;
|
|
326
|
+
opacity: number;
|
|
327
|
+
custom: any;
|
|
328
|
+
visible: boolean;
|
|
329
|
+
selectable: boolean;
|
|
330
|
+
removable: boolean;
|
|
331
|
+
alwaysOnTop: boolean;
|
|
332
|
+
showInExport: boolean;
|
|
333
|
+
} | {
|
|
334
|
+
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light";
|
|
335
|
+
id: string;
|
|
336
|
+
type: string;
|
|
337
|
+
name: string;
|
|
338
|
+
opacity: number;
|
|
339
|
+
custom: any;
|
|
340
|
+
visible: boolean;
|
|
341
|
+
selectable: boolean;
|
|
342
|
+
removable: boolean;
|
|
343
|
+
alwaysOnTop: boolean;
|
|
344
|
+
showInExport: boolean;
|
|
345
|
+
}>>;
|
|
302
346
|
readonly parent: any;
|
|
303
347
|
readonly zIndex: any;
|
|
304
348
|
} & {
|
|
@@ -313,6 +357,18 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
313
357
|
removable: boolean;
|
|
314
358
|
alwaysOnTop: boolean;
|
|
315
359
|
showInExport: boolean;
|
|
360
|
+
} | {
|
|
361
|
+
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light";
|
|
362
|
+
id: string;
|
|
363
|
+
type: string;
|
|
364
|
+
name: string;
|
|
365
|
+
opacity: number;
|
|
366
|
+
custom: any;
|
|
367
|
+
visible: boolean;
|
|
368
|
+
selectable: boolean;
|
|
369
|
+
removable: boolean;
|
|
370
|
+
alwaysOnTop: boolean;
|
|
371
|
+
showInExport: boolean;
|
|
316
372
|
};
|
|
317
373
|
} & {
|
|
318
374
|
clone(attrs?: any, { skipSelect }?: {
|
|
@@ -351,24 +407,37 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
351
407
|
type: string | undefined;
|
|
352
408
|
name: string | undefined;
|
|
353
409
|
opacity: number | undefined;
|
|
410
|
+
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light" | undefined;
|
|
354
411
|
custom: any;
|
|
355
412
|
visible: boolean | undefined;
|
|
356
413
|
selectable: boolean | undefined;
|
|
357
414
|
removable: boolean | undefined;
|
|
358
415
|
alwaysOnTop: boolean | undefined;
|
|
359
416
|
showInExport: boolean | undefined;
|
|
360
|
-
}>,
|
|
361
|
-
id:
|
|
362
|
-
type:
|
|
363
|
-
name:
|
|
364
|
-
opacity:
|
|
365
|
-
custom:
|
|
366
|
-
visible:
|
|
367
|
-
selectable:
|
|
368
|
-
removable:
|
|
369
|
-
alwaysOnTop:
|
|
370
|
-
showInExport:
|
|
371
|
-
}
|
|
417
|
+
}>, {
|
|
418
|
+
id: string;
|
|
419
|
+
type: string;
|
|
420
|
+
name: string;
|
|
421
|
+
opacity: number;
|
|
422
|
+
custom: any;
|
|
423
|
+
visible: boolean;
|
|
424
|
+
selectable: boolean;
|
|
425
|
+
removable: boolean;
|
|
426
|
+
alwaysOnTop: boolean;
|
|
427
|
+
showInExport: boolean;
|
|
428
|
+
} | {
|
|
429
|
+
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "overlay" | "saturation" | "screen" | "soft-light";
|
|
430
|
+
id: string;
|
|
431
|
+
type: string;
|
|
432
|
+
name: string;
|
|
433
|
+
opacity: number;
|
|
434
|
+
custom: any;
|
|
435
|
+
visible: boolean;
|
|
436
|
+
selectable: boolean;
|
|
437
|
+
removable: boolean;
|
|
438
|
+
alwaysOnTop: boolean;
|
|
439
|
+
showInExport: boolean;
|
|
440
|
+
}>> & {
|
|
372
441
|
type: "video";
|
|
373
442
|
}) | undefined>;
|
|
374
443
|
export {};
|
package/canvas/apply-filters.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"konva";import{isAlive as e}from"mobx-state-tree";import{getEffectiveBlurRadius as r}from"@polotno/core/filters";import{pixelFilters as i}from"@polotno/core/pixel-filters";export{getEffectiveBlurRadius,getElementBlurRadius}from"@polotno/core/filters";const
|
|
1
|
+
import t from"konva";import{isAlive as e}from"mobx-state-tree";import{getEffectiveBlurRadius as r}from"@polotno/core/filters";import{pixelFilters as i,isPixelFilter as s,hasPixelFilters as l}from"@polotno/core/pixel-filters";export{getEffectiveBlurRadius,getElementBlurRadius}from"@polotno/core/filters";const n=i;export function getFilterAttrs(e){const r={filters:[]};return e.brightnessEnabled&&(r.filters.push(t.Filters.Brighten),r.brightness=e.brightness),e.blurEnabled&&(r.filters.push(t.Filters.Blur),r.blurRadius=e.blurRadius),e.sepiaEnabled&&r.filters.push(t.Filters.Sepia),e.grayscaleEnabled&&r.filters.push(t.Filters.Grayscale),e.filters.forEach((t,e)=>{const i=n[e];s(e)&&r.filters.push(i(t.intensity))}),r}export function hasActiveFilters(t){return l(t,t.filters.keys())}export function applyFiltersToCanvas(t,i){if(!e(i)){return}const s=getFilterAttrs(i);if(!s.filters.length||!t.width||!t.height){return}const l=t.getContext("2d");if(!l){return}const n=l.getImageData(0,0,t.width,t.height),o={brightness:()=>s.brightness,blurRadius:()=>{var e;return r(null!==(e=s.blurRadius)&&void 0!==e?e:0,t.width,t.height)}};s.filters.forEach(t=>{"function"==typeof t&&t.call(o,n)}),l.putImageData(n,0,0)}export function getFiltersDepsKey(t){if(!e(t)){return""}const r=[];return t.filters.forEach((t,e)=>{r.push(e+":"+t.intensity)}),[t.brightnessEnabled&&t.brightness,t.blurEnabled&&t.blurRadius,t.sepiaEnabled,t.grayscaleEnabled,...r].join("|")}export function applyFilter(t,r){var i;if(!t||!e(r)){return}const s=getFilterAttrs(r),l={};r.blurEnabled&&"text"===r.type&&r.lineHeight<1&&(l.offset=r.fontSize),t.setAttrs(s),s.filters.length?t.cache(Object.assign(Object.assign({},l),{pixelRatio:r.store._elementsPixelRatio})):t.clearCache(),null===(i=t.getLayer())||void 0===i||i.batchDraw()}
|
package/canvas/downscale.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
type DownscaleSource = HTMLCanvasElement | HTMLImageElement | HTMLVideoElement;
|
|
2
|
+
export declare function sourceSize(image: DownscaleSource): {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
};
|
|
2
6
|
export declare const RESAMPLE_PIXEL_BUDGET = 16000000;
|
|
3
7
|
export declare function shouldResample(ratio: number, deviceWidth: number, deviceHeight: number): boolean;
|
|
4
8
|
export declare function exportKernelEnabled(page: {
|
package/canvas/downscale.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"konva";import{flags as e}from"../utils/flags.js";import{runResample as n}from"./downscale-kernel.js";function
|
|
1
|
+
import t from"konva";import{flags as e}from"../utils/flags.js";import{runResample as n}from"./downscale-kernel.js";export function sourceSize(t){return t instanceof HTMLVideoElement?{width:t.videoWidth,height:t.videoHeight}:{width:t.width,height:t.height}}export const RESAMPLE_PIXEL_BUDGET=16e6;export function shouldResample(t,e,n){return t<.95&&e*n<=16e6}export function exportKernelEnabled(t){return Boolean(null==t?void 0:t._exporting)&&!(null==t?void 0:t._rendering)&&e.imageDownScalingEnabled}function a(t){const e=Math.abs(t);if(e<=.5){return.75-e*e}if(e<=1.5){const t=1.5-e;return.5*t*t}return 0}function i(t,e,n,i,r,o){const h=new Int32Array(i),c=new Int32Array(i),s=o/n;if(!(s>0)){return{starts:h,counts:c,weights:new Float32Array(0),maxTaps:0}}const l=Math.max(1,1/s),d=1.5*l,u=Math.max(0,Math.floor(e)),g=Math.min(t-1,Math.ceil(e+n)-1),w=t=>e+(t+.5-r)/s-.5;let f=0;for(let a=0;a<i;a++){const t=Math.max(u,Math.floor(w(a)-d)),e=Math.min(g,Math.ceil(w(a)+d));e<t||(h[a]=t,c[a]=e-t+1,f=Math.max(f,c[a]))}const m=new Float32Array(i*f);for(let x=0;x<i;x++){const t=x*f;let e=0;for(let n=0;n<c[x];n++){const i=a((h[x]+n-w(x))/l);m[t+n]=i,e+=i}if(e>0){for(let n=0;n<c[x];n++){m[t+n]/=e}}}return{starts:h,counts:c,weights:m,maxTaps:f}}let r;export function forceDownscaleEngine(t){r=t}function o(t,e,n){const a=document.createElement("canvas");a.width=t,a.height=e;const i=a.getContext("2d",n);if(!i){throw new RangeError(`canvas ${t}x${e} refused`)}return{canvas:a,ctx:i}}function h(t,e){return o(t,e,{willReadFrequently:!0})}export function resampleCanvas(e,a,c,s,l={x:0,y:0,width:c,height:s}){if("refused"!==r){try{return function(e,a,r,c,s,l){const d=sourceSize(e),{canvas:u,ctx:g}=function(e,n,a){let i,{width:r,height:o}=sourceSize(e),c=e;for(;r>2*n||o>2*a;){let e;r=Math.max(2*n,Math.round(r/2)),o=Math.max(2*a,Math.round(o/2));try{e=h(r,o)}catch(s){throw i&&t.Util.releaseCanvas(i.canvas),s}e.ctx.imageSmoothingQuality="high",e.ctx.drawImage(c,0,0,r,o),i&&t.Util.releaseCanvas(i.canvas),i=e,c=e.canvas}return i||(i=h(r,o),i.ctx.drawImage(e,0,0)),i}(e,d.width*r/a.width,d.height*c/a.height);try{const t={width:u.width,rows:(t,e)=>g.getImageData(0,t,u.width,e).data},e=u.width/d.width,h=u.height/d.height,w=n(t,r,c,i(u.width,a.x*e,a.width*e,r,s.x,s.width),i(u.height,a.y*h,a.height*h,c,s.y,s.height),.1065*function(t){const e=Math.log2(1/t);return e>0?Math.min(1,e/2):0}(Math.max(s.width/a.width,s.height/a.height)),l),f=o(r,c);return f.ctx.putImageData(w,0,0),f.canvas}finally{t.Util.releaseCanvas(u)}}(e,a,c,s,l,r)}catch(d){if(void 0===r&&(d instanceof RangeError||d instanceof DOMException)){return}throw d}}}export function drawReduced(e,n,a,i,r,o={x:0,y:0,width:i,height:r}){const h=resampleCanvas(n,a,i,r,o);if(h){return e.drawImage(h,0,0),void t.Util.releaseCanvas(h)}e.imageSmoothingQuality="high",e.drawImage(n,a.x,a.y,a.width,a.height,o.x,o.y,o.width,o.height)}
|
package/canvas/element.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ type ShapeProps = {
|
|
|
6
6
|
element: ShapeType;
|
|
7
7
|
onClick: Function;
|
|
8
8
|
};
|
|
9
|
-
export declare function registerShapeComponent(type: string, component: React.
|
|
9
|
+
export declare function registerShapeComponent(type: string, component: React.ComponentType<ShapeProps>): void;
|
|
10
10
|
declare const Element: ((props: ShapeProps) => React.JSX.Element | null) & React.NamedExoticComponent<ShapeProps> & {
|
|
11
11
|
readonly type: (props: ShapeProps) => React.JSX.Element | null;
|
|
12
12
|
};
|
package/canvas/element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as e,jsxs as t}from"react/jsx-runtime";import{createElement as o}from"react";import r from"react";import{observer as n}from"mobx-react-lite";import{ROLES as i}from"../model/store.js";import s from"konva";import{MAIN_TRANSFORMER_NAME as l}from"../utils/px.js";import{TextElement as
|
|
1
|
+
import{jsx as e,jsxs as t}from"react/jsx-runtime";import{createElement as o}from"react";import r from"react";import{observer as n}from"mobx-react-lite";import{ROLES as i}from"../model/store.js";import s from"konva";import{MAIN_TRANSFORMER_NAME as l}from"../utils/px.js";import{TextElement as a}from"./text-element.js";import{ImageElement as m}from"./image-element.js";import{HTMLElement as f}from"./html-element.js";import{RenderTagElement as p}from"./render-tag-element.js";import{LineElement as c}from"./line-element.js";import{VideoElement as d}from"./video-element.js";import{FigureElement as u}from"./figure-element.js";import{GifElement as g}from"./gif-element.js";import{TableElement as h}from"./table-element.js";import{Group as v}from"react-konva";import{Highlighter as x}from"./highlighter.js";import{forEveryChild as y}from"../model/group-model.js";import{flags as j}from"../utils/flags.js";import{blendModeOf as b,isIsolatedGroup as E}from"@polotno/core/page";const w={text:a,image:m,svg:m,line:c,video:d,figure:u,group:n(t=>{var r;const{element:n,store:s}=t,l=n,{children:a}=l,m=n.selectable||s.role===i.ADMIN,f=l.a;return e(v,{x:f.x,y:f.y,offsetX:f.offsetX,offsetY:f.offsetY,scaleX:f.scaleX,scaleY:f.scaleY,rotation:f.rotation,opacity:f.opacity,isolated:E(l),globalCompositeOperation:null!==(r=b(l))&&void 0!==r?r:"source-over",listening:m,hideInExport:!n.showInExport,id:n.id,children:a.map(e=>o(S,Object.assign({},t,{key:e.id,store:s,element:e})))})}),gif:g,table:h};export function registerShapeComponent(e,t){w[e]=t}const O=(e,t)=>{const o=[];y(e,e=>{"group"!==e.type&&o.push(e.a)});const r=[];o.forEach(e=>{const t=[{x:0,y:0},{x:0+e.width,y:0},{x:0+e.width,y:0+e.height},{x:0,y:0+e.height}],o=new s.Transform;o.translate(e.x,e.y),o.rotate(s.Util.degToRad(e.rotation)),t.forEach(e=>{const t=o.point(e);r.push(t)})});const n=new s.Transform;n.rotate(-s.Util.degToRad(t));let i=1/0,l=1/0,a=-1/0,m=-1/0;r.forEach(e=>{const t=n.point(e);i=Math.min(i,t.x),l=Math.min(l,t.y),a=Math.max(a,t.x),m=Math.max(m,t.y)}),n.invert();const f=n.point({x:i,y:l});return{x:f.x,y:f.y,width:a-i,height:m-l,rotation:t}},S=n(o=>{var n,i;const{element:s,store:a}=o,[m,c]=r.useState(!1),d=s.isSelectedDirectly,u="group"===(null===(n=s.parent)||void 0===n?void 0:n.type),g="group"!==s.type||u?null:a.selectedElements,h=!!g&&1===g.length&&g[0]!==s&&g[0].top===s,[y,E]=r.useState(!1),[S,T]=r.useState(null),C=r.useCallback(e=>{T(e?e.getStage():null)},[]),M=r.useRef(void 0),k=()=>(M.current||(M.current=null==S?void 0:S.findOne(`.${l}`)),M.current);r.useEffect(()=>{if(S){const e=e=>{const t=e.target.findAncestor(".element",!0),o=a.getElementById(null==t?void 0:t.id()),r=null==o?void 0:o.top,n=null==r?void 0:r.id;c(n===s.id)};S.on("mouseover",e);const t=()=>{c(!1)};return S.on("mouseleave",t),()=>{S.off("mouseover",e),S.off("mouseleave",t)}}},[S]),r.useEffect(()=>{if(!h){return}const e=k();if(!S||!e){return}E(!1);const t=()=>E(!0),o=()=>E(!1);return S.on("dragstart",t),S.on("dragend",o),e.on("transformstart",t),e.on("transformend",o),()=>{S.off("dragstart",t),S.off("dragend",o),e.off("transformstart",t),e.off("transformend",o)}},[S,h]);let I=w[o.element.type];if("text"===o.element.type&&(j.renderTagTextEnabled?I=p:j.htmlRenderEnabled&&(I=f)),!o.element.visible){return null}if(!I){return console.error("Can not find component for "+o.element.type),null}const R=b(s);return t(v,{name:"element-container",ref:C,visible:s.active,listening:s.active,children:["group"===s.type?e(I,Object.assign({},o)):e(v,{isolated:!!R,opacity:R?s.a.opacity:1,globalCompositeOperation:null!=R?R:"source-over",children:e(I,Object.assign({},o))}),(m||h)&&!d&&!u&&!y&&e(x,{element:"group"===s.type?{a:O(s,h?"rotation"in g[0]?g[0].rotation:0:(null===(i=k())||void 0===i?void 0:i.rotation())||0)}:s})]})});export default S;
|
package/canvas/figure-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,Fragment as e,jsxs as a}from"react/jsx-runtime";import r from"react";import{observer as
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as a}from"react/jsx-runtime";import r from"react";import{observer as o}from"mobx-react-lite";import{Group as s,Rect as i,Shape as h}from"react-konva";import{ROLES as n}from"../model/store.js";import l from"konva";import{getFigureFillClip as d,subTypeToPathDataFunc as c}from"@polotno/core/figure-to-svg";import{isOpaquePaint as g}from"@polotno/core/gradient";import{isTouchDevice as m}from"../utils/screen.js";import{roundEnabledPx as p,activeAnchorOf as f,isCornerAnchor as u,scaledBoxStyleAttrs as b,roundedBoxStyleAttrs as w,roundedGeometryAttrs as k,roundedPosition as j}from"../utils/px.js";import{useColor as x}from"./use-color.js";import{elementOpacity as y,paintOpacity as O}from"./paint-opacity.js";const v=(t,e)=>{t.beginPath();for(const{command:a,points:r}of e){switch(a){case"L":t.lineTo(r[0],r[1]);break;case"M":t.moveTo(r[0],r[1]);break;case"C":t.bezierCurveTo(r[0],r[1],r[2],r[3],r[4],r[5]);break;case"Q":t.quadraticCurveTo(r[0],r[1],r[2],r[3]);break;case"A":t.ellipse(r[0],r[1],r[2],r[3],r[6],r[4],r[4]+r[5],!r[7]);break;case"z":t.closePath()}}},E={};export const FigureElement=o(({element:P,store:T})=>{const W=r.useRef(null),M=r.useRef(!1),S=P.selectable||T.role===n.ADMIN,C=m(),D=P.isSelected,R=Math.min(P.strokeWidth,P.width/2,P.height/2),F=x(P,P.fill,"fill"),I=Object.assign(Object.assign({width:P.a.width,height:P.a.height},F),{shadowEnabled:P.shadowEnabled,shadowBlur:P.shadowBlur,shadowOffsetX:P.shadowOffsetX,shadowOffsetY:P.shadowOffsetY,shadowColor:P.shadowColor,shadowOpacity:P.shadowOpacity,preventDefault:!C||D,hideInExport:!P.showInExport}),X=x(P,P.stroke,"stroke"),Y=Object.assign(Object.assign({},X),{strokeWidth:2*R,dash:P.dash.map(t=>t*R)}),z=Math.max(0,10-Math.min(P.a.width,P.a.height)),A=d({subType:P.subType,width:P.a.width,height:P.a.height,cornerRadius:P.cornerRadius,strokeWidth:R,stroke:P.stroke,dash:P.dash,opacity:O(P)}),B=!P.shadowEnabled||g(P.fill)?A:null,q=(L=P.subType,E[L]||(E[L]=(N=c(L),o(({element:o,fillProps:s,strokeProps:n,hitStrokeWidth:d,fillClip:c})=>{const g=N({width:o.a.width,height:o.a.height,cornerRadius:o.cornerRadius}),m=r.useMemo(()=>l.Path.parsePathData(g),[g]),p=r.useMemo(()=>c?l.Path.parsePathData(c):null,[c]),f=!!p;return a(e,{children:[t(i,{width:o.a.width,height:o.a.height,fill:"transparent",hitStrokeWidth:d}),t(h,Object.assign({},s,n,{perfectDrawEnabled:f,shadowForStrokeEnabled:f,selfRectFunc:t=>({x:0,y:0,width:t.width(),height:t.height()}),sceneFunc:(t,e)=>{t.save(),p?(v(t,m),t.clip(),v(t,p),t.clip()):v(t,m),t.fillShape(e),t.restore(),t.save(),v(t,m),t.clip(),t.strokeShape(e),t.restore()}}))]})}))),E[L]||s);var L,N;return t(e,{children:t(s,{ref:W,id:P.id,x:P.a.x,y:P.a.y,rotation:P.a.rotation,opacity:y(P),listening:S,draggable:C?P.draggable&&D:P.draggable,name:"element",onDragMove:t=>{P.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{P.set(j(t.target))},onTransformStart:t=>{M.current=u(f(t.target))},onTransform:t=>{const e=t.target.scale();t.target.scaleX(1),t.target.scaleY(1);const a=M.current?Math.min(e.x,e.y):1;P.set(Object.assign({width:P.width*e.x,height:P.height*e.y,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()},1!==a&&Object.assign(Object.assign({},b(P,a)),{strokeWidth:P.strokeWidth*a})))},onTransformEnd:()=>{P.set(Object.assign(Object.assign(Object.assign({},k(P)),w(P)),{strokeWidth:p(P.strokeWidth)}))},children:t(q,{element:P,fillProps:I,strokeProps:Y,hitStrokeWidth:z,fillClip:B})})})});
|
package/canvas/gif-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import i from"react";import{observer as
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import{elementOpacity as i}from"./paint-opacity.js";import o from"react";import{observer as a}from"mobx-react-lite";import{nodeAutorun as n}from"../utils/mobx.js";import{Image as s,Group as h,Rect as d,Arc as l,Text as c}from"react-konva";import{ROLES as m}from"../model/store.js";import g from"konva";import{parseGIF as f,decompressFrames as u}from"gifuct-js";import{useClip as p,useCornerRadiusAndCrop as w}from"./video-element.js";import{useImageLoader as x}from"./image-element.js";import{fetchWithRetry as y}from"@polotno/core/retry";import{useWorkspaceStyle as b}from"./workspace-style.js";import{useFilteredSource as E}from"./use-filtered-source.js";import{useFadeIn as v}from"./use-fadein.js";import{isTouchDevice as j}from"../utils/screen.js";import{roundEnabledPx as S,activeAnchorOf as I,isCornerAnchor as R,scaledBoxStyleAttrs as M,roundedBoxStyleAttrs as O,roundedGeometryAttrs as z,roundedPosition as C}from"../utils/px.js";import{useEvent as T}from"../hooks/use-event.js";function D(t,e,r){const i=e.getContext("2d"),o=r.getContext("2d");if(!i||!o){return}2===t.disposalType&&i.clearRect(0,0,e.width,e.height),r.width=t.width,r.height=t.height;const a=o.createImageData(t.width,t.height);a.data.set(t.patch),o.putImageData(a,0,0),i.drawImage(r,t.left,t.top)}const X=a(({element:r})=>{const a=Math.min(30,r.width/4,r.height/4),n=o.useRef(null);o.useEffect(()=>{const t=n.current;if(!t){return}const e=new g.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}},[]);const{mediaLoadingStyle:s}=b();return e(h,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:i(r),hideInExport:!r.showInExport,children:[t(d,{width:r.width,height:r.height,fill:s.fill}),t(l,{ref:n,x:r.width/2,y:r.height/2,fill:s.textFill,outerRadius:Math.abs(a),innerRadius:Math.max(1,a-5),angle:270})]})}),Y=a(({element:r})=>{const{mediaErrorStyle:o}=b(),a=Math.max(10,Math.min(30,r.width/22));return e(h,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:i(r),hideInExport:!r.showInExport,children:[t(d,{width:r.width,height:r.height,fill:o.fill}),t(c,{text:"Cannot load the GIF...",fontSize:a,width:r.width,height:r.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});export const GifElement=a(({element:a,store:h})=>{var l,c;const[b,k]=o.useState(!1),F=o.useRef(!1),_=a.isSelected,A=o.useRef(null),P=o.useRef(void 0),[B,G,L,U]=function(t){const[e,r]=o.useState([]),[i,a]=o.useState(0),[n,s]=o.useState("loading"),[h,d]=o.useState({width:0,height:0});return o.useEffect(()=>{(async()=>{try{const e=await y(t),i=await e.arrayBuffer(),o=f(i),n=u(o,!0),h=o.lsd.width,l=o.lsd.height;d({width:h,height:l});const c=n.map(t=>({patch:new Uint8ClampedArray(t.patch),delay:t.delay,width:t.dims.width,height:t.dims.height,left:t.dims.left,top:t.dims.top,disposalType:t.disposalType})),m=c.reduce((t,e)=>t+e.delay,0);r(c),a(m),s("loaded")}catch(e){console.error("Failed to load GIF:",e),s("failed")}})()},[t]),[e,i,h,n]}(a.src);x(U,a.src,a.id),o.useEffect(()=>(P.current=document.createElement("canvas"),()=>{P.current&&g.Util.releaseCanvas(P.current)}),[]),o.useEffect(()=>{if("loaded"===U&&P.current&&(P.current.width=L.width,P.current.height=L.height,B.length>0)){const t=P.current.getContext("2d");if(t){t.clearRect(0,0,L.width,L.height);const e=document.createElement("canvas");D(B[0],P.current,e)}}},[L,U,B]),o.useEffect(()=>{var t;if(!B.length||!P.current){return}const e=P.current;e.width=L.width,e.height=L.height;const r=document.createElement("canvas"),i=e.getContext("2d");i&&i.clearRect(0,0,e.width,e.height);let o=-1;D(B[0],e,r),rt(),o=0;const s=t=>{const i=(t=>{const e=t%G;let r=0;for(let i=0;i<B.length;i++){if(r+=B[i].delay,r>e){return i}}return 0})(t);i!==o&&(D(B[i],e,r),rt(),A.current.getLayer().draw(),o=i)};if(h.isPlaying||(null===(t=a.page)||void 0===t?void 0:t._exportingOrRendering)){return n(a,()=>{s(h.currentTime-a.page.startTime)})}{const t=window.setInterval(()=>{s(h.currentTime||performance.now())},16);return()=>{clearInterval(t)}}},[h.isPlaying,B,G,null===(l=a.page)||void 0===l?void 0:l._exportingOrRendering]),o.useEffect(()=>{G&&h.history.ignore(()=>{a.set({duration:G})})},[G]);let{cropX:W,cropY:H,cropWidth:N,cropHeight:q}=a;"loaded"!==U&&(W=H=0,N=q=1);const J={x:L.width*W,y:L.height*H,width:L.width*N,height:L.height*q},K=null!==(c=a.cornerRadius)&&void 0!==c?c:0,[Q,V]=E(a,P.current),[Z,$]=w(a,Q,J,h._elementsPixelRatio,K,b||a._cropModeEnabled),[tt,et]=p(a,Z,h._elementsPixelRatio,[J,K]),rt=T(()=>{V(),$(),et()}),it="loading"===U,ot="failed"===U,at=it||ot?0:i(a);v(A,at);const nt=a.selectable||h.role===m.ADMIN,st=j();return e(r,{children:[it&&t(X,{element:a}),ot&&t(Y,{element:a}),t(s,{ref:A,name:"element",id:a.id,image:tt,x:a.a.x,y:a.a.y,width:a.a.width||1,height:a.a.height||1,rotation:a.a.rotation,opacity:at,shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:a.shadowColor,shadowOpacity:a.shadowOpacity,customCrop:J,listening:nt,draggable:st?a.draggable&&_:a.draggable,preventDefault:!st||_,hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(C(t.target))},onTransformStart:t=>{k(!0),F.current=R(I(t.target))},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),i=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const o=F.current?Math.min(r,i):1;a.set(Object.assign({x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:e.rotation()},1!==o&&Object.assign(Object.assign({},M(a,o)),{borderSize:a.borderSize*o})))},onTransformEnd:()=>{a.set(Object.assign(Object.assign(Object.assign({},z(a)),O(a)),{borderSize:S(a.borderSize)})),k(!1)}}),t(d,{x:a.a.x,y:a.a.y,width:Math.max(a.a.width-a.borderSize,0),height:Math.max(a.a.height-a.borderSize,0),opacity:at,offsetX:-a.borderSize/2,offsetY:-a.borderSize/2,stroke:a.borderColor,strokeWidth:a.borderSize,listening:!1,visible:!!a.borderSize,rotation:a.rotation,cornerRadius:Math.max(0,K-a.borderSize),hideInExport:!a.showInExport})]})});
|
package/canvas/html-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{observer as n}from"mobx-react-lite";import{Group as i,Image as a,Path as s,Rect as l}from"react-konva";import{activeAnchorOf as c,scaledFontSizeAttrs as f,roundedFontSizeAttrs as d,roundedTextDecorationAttrs as h,roundedGeometryPx as u,roundedPosition as g,textMinResizeWidth as m}from"../utils/px.js";import x from"konva";import{isAlive as p}from"mobx-state-tree";import{nodeAutorun as y}from"../utils/mobx.js";import{flags as w}from"../utils/flags.js";import{applyFilter as b}from"./apply-filters.js";import{getLineHeight as v,useFontFamiliesLoader as E,usePrevious as S}from"./text-element.js";import{getCurvePath as O}from"@polotno/core/text-html";import{useColor as k}from"./use-color.js";import{useWorkspaceStyle as z}from"./workspace-style.js";import{detectSize as F,detectLineRects as j,htmlToCanvas as M}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as R}from"@polotno/core/background-shape";import{useFadeIn as X}from"./use-fadein.js";import{Html as Y}from"react-konva-utils";import{incrementLoader as I,triggerLoadError as C}from"../utils/loader.js";import{removeTags as P}from"@polotno/core/text";import{cssFontFamily as T}from"@polotno/core/fonts";import*as B from"../utils/fonts.js";import{isTouchDevice as A}from"../utils/screen.js";import{getLimitedFontSize as W}from"./text-element/max-font-size.js";import{visiblePlaceholder as $}from"./text-element/placeholder.js";import{useDelayer as D}from"./use-delayer.js";import{getHtml as H,textLayoutWidth as _}from"@polotno/core/text-html";import{hasMeasuredHeight as L,resolveTextBoxHeight as J}from"@polotno/core/text-types";import{normalizeRichTextHtml as N,extractFontFamiliesFromHtml as q,scaleRichTextFontSizesInHtml as V}from"../utils/rich-text-html.js";import{findFitFontSize as G}from"./text-element/fit-font-size.js";import{RichEditor as K,RichTextContainer as Q,applyOpacityToColor as U,quillRef as Z,createQuill as tt,setQuillContent as et,setQuillFormats as ot,syncQuillFormatState as rt,setCursorFromCoords as nt}from"./rich-editor.js";export{Z as quillRef,tt as createQuill,et as setQuillContent,ot as setQuillFormats,rt as syncQuillFormatState,nt as setCursorFromCoords};function it(t){const e=document.createElement("canvas"),o=e.getContext("2d");return null==o||o.drawImage(t,0,0,e.width,e.height),function(t){return!function(t){const e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;for(let o=0;o<e.length;o+=4){if(0!==e[o+3]){return!0}}return!1}(t)}(e)}const at=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),st=navigator.userAgent.includes("Firefox");export const HTMLElement=n(({element:n,store:Z})=>{var tt,et;const{textOverflowIndicatorStyle:ot}=z(),rt=r.useRef(null),[nt,lt]=r.useState(),[ct,ft]=r.useState(!1),[dt,ht]=r.useState(!1),ut=r.useRef(n.height),gt=n.isSelected,mt=S(gt),xt=n.fontSize/3,pt=_(n.a.width,n.letterSpacing,n.fontSize)+2*xt,{textVerticalResizeEnabled:yt}=w,wt=w.autoDeleteEmptyText,bt=S(n.fontFamily),vt=r.useMemo(()=>N(n.text,{fontSize:n.fontSize,fontFamily:n.fontFamily}),[n.text,n.fontSize,n.fontFamily]),Et=r.useMemo(()=>[...new Set([n.fontFamily,...q(vt)])].filter(Boolean),[n.fontFamily,vt]),[St]=E(Z,Et,n.fontStyle,n.fontWeight,n.text||n.placeholder),Ot=n._editModeEnabled;X(rt);const kt=St?n.fontFamily:bt!==n.fontFamily?bt:"Arial",zt=k(n).fill,Ft=H(n,{fontFamily:kt,color:zt,textOverflow:w.textOverflow}),jt=r.useRef([]),Mt=r.useRef(!0),[,Rt]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=jt.current;t.length>0&&(jt.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Mt.current=!1;const t=jt.current;t.length>0&&(jt.current=[],t.forEach(t=>t()))},[]);let{width:Xt,height:Yt}=function(t,e,o){return r.useMemo(()=>F(t),[t,e.width,o])}(Ft,n,St);r.useEffect(()=>{if(!St){return}const t=(t,e,o,r)=>{const i=I(`text ${n.id}`),a=()=>{Z.history.ignore(t,e,o).then(()=>{Mt.current?(jt.current.push(i),Rt()):i()})};r?queueMicrotask(a):a()};if(n.curveEnabled&&Math.abs(Xt-n.width)>1&&!ct&&t(()=>{if(p(n)&&Math.abs(Xt-n.width)>1){const t=n.x+n.width/2;n.set({width:Xt,x:t-Xt/2})}},!1,!0),!L(n)){return void t(()=>{p(n)&&(L(n)||n.set({height:Yt}))},void 0,void 0,!0)}const{textOverflow:e}=w;if("change-font-size"!==e||ct||Ot||n.curveEnabled){"resize"===e&&(yt&&n.height<Yt&&!ct&&t(()=>{p(n)&&n.set({height:Yt})},!1,!0),yt||n.height===Yt||ct||t(()=>{p(n)&&n.set({height:Yt})},!1,!0))}else{const e=G(n,{fontFamily:n.fontFamily,textOverflow:w.textOverflow});e.fontSize!==n.fontSize?t(()=>{if(!p(n)){return}const t=f(n,e.fontSize);void 0!==e.text&&e.text!==n.text&&(t.text=e.text),n.set(t)},!1,!0):n.height!==Yt&&(yt&&n.height<Yt?t(()=>{p(n)&&n.set({height:Yt})},!1,!0):yt||t(()=>{p(n)&&n.set({height:Yt})},!1,!0))}});const It=r.useMemo(()=>{const t={lastArgs:null,lastResult:null};return async function(e){return t.lastArgs&&t.lastResult&&(o=t.lastArgs,r=e,JSON.stringify(o)===JSON.stringify(r))||(t.lastResult=await M(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Ct=r.useRef(0),Pt=r.useRef(null);r.useEffect(()=>{ct||Ot||(async()=>{Ct.current++;const t=Ct.current;let e=I(`text ${n.id} ${t}`);Pt.current&&Pt.current(),Pt.current=e,ht(!0);let o=null;const r=at?5:1;for(let a=0;a<r;a++){const e=a>0?It:M;try{if(o=await e({skipCache:a>0,html:Ft,width:Xt||n.width||1,height:J(n,Yt)||1,fontFamily:kt,fontFamilies:q(Ft),resolveFont:t=>Z.fonts.find(e=>e.fontFamily===t)||B.globalFonts.find(e=>e.fontFamily===t),padding:xt,pixelRatio:Z._elementsPixelRatio,font:Z.fonts.find(t=>t.fontFamily===kt)||B.globalFonts.find(t=>t.fontFamily===kt)}),t!==Ct.current){return}if((at||st)&&o&&it(o)){await new Promise(t=>setTimeout(t,50*(a+1)));continue}break}catch(i){console.error(i),C(`Error rendering rich text with id ${n.id}`);break}}o?lt(o):e?(e(),e=null):console.error("Finish function is called twice!"),ht(!1)})()},[Ft,ct,Yt,Ot,kt,n.height,Z._elementsPixelRatio,St]);const[Tt,Bt]=D(dt,300),[At]=D(ct,300,!0),Wt=At||Tt;r.useEffect(()=>{var t;if(!Wt){return y(n,()=>{const t=rt.current;b(t,n)})}null===(t=rt.current)||void 0===t||t.clearCache()},[nt,Wt,n.shadowColor,n.shadowOffsetX,n.shadowOffsetY,n.shadowOpacity]),r.useEffect(()=>{nt&&!dt&&Pt.current&&(Pt.current(),Pt.current=null)},[nt,dt]),r.useLayoutEffect(()=>{if(!St){return}if(!n.curveEnabled){return}const t=new x.TextPath({data:O(n.a.width,n.a.height,n.curvePower,n.a.fontSize),text:P(n.text),letterSpacing:n.letterSpacing*n.a.fontSize,fontSize:n.a.fontSize,fontFamily:T(n.fontFamily),fontWeight:n.fontWeight,fontStyle:n.fontStyle,align:"center",textBaseline:"middle",fill:n.fill}),e=t.getSelfRect().width;if(e){const t=e-n.a.width,o=n.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{p(n)&&n.set({width:e,x:n.a.x+r,y:n.a.y+i})})}t.destroy()},[St,n.curveEnabled,n.curvePower,n.text,n.fontSize,n.fontFamily,n.fontWeight,n.fontStyle,n.letterSpacing]),r.useEffect(()=>()=>{Pt.current&&Pt.current()},[]),r.useEffect(()=>{mt&&!gt&&wt&&""===P(n.text)&&n.removable&&!$(n.placeholder)&&Z.deleteElements([n.id])},[wt,n.placeholder,n.removable,n.text,gt,mt]);const $t=J(n,Yt);let Dt=0;"middle"===n.verticalAlign&&(Dt=($t-Yt)/2),"bottom"===n.verticalAlign&&(Dt=$t-Yt);const Ht=v({fontLoaded:St,fontFamily:n.fontFamily,fontSize:n.fontSize,lineHeight:n.lineHeight}),_t=n.backgroundPadding*(n.fontSize*Ht*.5),Lt=n.backgroundCornerRadius*(n.fontSize*Ht*.5),Jt=r.useMemo(()=>{if(!n.backgroundEnabled||n.curveEnabled||n.legacyBackground){return""}const t=j(Ft);if(0===t.length){return""}const e=t.map(t=>({left:t.left+xt,right:t.right+xt,top:t.top+xt,bottom:t.bottom+xt}));return R({rects:e,padding:_t,cornerRadius:Lt})},[n.backgroundEnabled,n.curveEnabled,n.legacyBackground,Ft,St,_t,Lt,xt]),Nt=A(),qt=O(n.a.width,Yt,n.curvePower,n.fontSize),Vt=r.useRef(null),Gt=r.useRef(n.width),Kt=r.useRef(""),Qt=r.useRef(0),Ut=r.useRef(0),Zt=r.useRef(null),te=Ot&&n.strokeWidth>0&&!n.curveEnabled,ee=Xt>0?pt/(Xt+2*xt):1;return o(e,{children:[n.curveEnabled||n.legacyBackground?t(l,{x:n.a.x,y:n.a.y,offsetX:_t/ee,offsetY:_t/ee,rotation:n.a.rotation,scaleX:ee,scaleY:ee,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,width:n.width+2*_t,height:$t+2*_t,cornerRadius:Lt}):t(s,{name:"html-background",x:n.a.x,y:n.a.y,offsetX:xt/ee,offsetY:(xt-Dt)/ee,rotation:n.a.rotation,scaleX:ee,scaleY:ee,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled&&Jt.length>0,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,data:Jt}),t(s,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,data:qt,stroke:ot.stroke,strokeWidth:1,visible:!1}),t(l,{ref:rt,name:"element",x:n.a.x,y:n.a.y,listening:n.selectable,rotation:n.a.rotation,width:n.a.width,height:n.a.height,visible:!Wt,draggable:Nt?n.draggable&>:n.draggable,preventDefault:!Nt||gt,opacity:Ot?0:n.a.opacity,hideInExport:!n.showInExport,onDragMove:t=>{n.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{n.set(g(t.target))},id:n.id,onDblClick:t=>{n.contentEditable&&(Zt.current={x:t.evt.clientX,y:t.evt.clientY},n.toggleEditMode(!0))},onDblTap:t=>{var e;if(n.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Zt.current=o?{x:o.clientX,y:o.clientY}:null,n.toggleEditMode(!0)}},onTransformStart:t=>{ft(!0),ut.current=n.height,Gt.current=n.width,Vt.current=t.target.position(),Kt.current=c(t.target),Qt.current=n.a.rotation,Ut.current=n.height},onTransform:t=>{const e=t.target,o=Kt.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,a=e.scaleX();if(r){const t=e.scaleX(),o=e.width()*t,r=m(Gt.current,n.fontSize);let i=o;o<r&&(i=r,Vt.current&&e.position(Vt.current)),e.width(i),e.scaleX(1),e.scaleY(1);const a=H(Object.assign(Object.assign({},n.toJSON()),{width:i}),{fontFamily:kt,color:zt,textOverflow:w.textOverflow}),s=F(a).height,l=w.textVerticalResizeEnabled?Math.max(s,ut.current):s;e.height(l),n.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===w.textOverflow,o="number"==typeof n.lineHeight?n.lineHeight:1,r=t?Yt:o*n.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),Vt.current){if("top-center"===Kt.current){const t=Qt.current*Math.PI/180,o=Ut.current-r;e.x(Vt.current.x-o*Math.sin(t)),e.y(Vt.current.y+o*Math.cos(t))}else{e.position(Vt.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),Ut.current=Math.max(r,i),n.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),n.set({fontSize:n.fontSize*a,letterSpacing:n.letterSpacing,width:e.width()*a,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*a,shadowBlur:n.shadowBlur*a,shadowOffsetX:n.shadowOffsetX*a,shadowOffsetY:n.shadowOffsetY*a,strokeWidth:n.strokeWidth*a,text:V(n.text,a)})}Vt.current=t.target.position()},onTransformEnd:t=>{ft(!1),ht(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),n.set(Object.assign(Object.assign({},d(n,n.fontSize*e)),{width:u(t.target.width()*e),x:u(t.target.x()),y:u(t.target.y()),rotation:u(t.target.rotation()),text:V(n.text,e)}))}}),t(a,{ref:rt,image:nt,x:n.a.x,y:n.a.y,offsetX:xt,offsetY:xt-Dt,listening:!1,rotation:n.a.rotation,width:pt,height:pt*((null!==(tt=null==nt?void 0:nt.height)&&void 0!==tt?tt:0)/((null==nt?void 0:nt.width)||1)||1),visible:!Wt&&!Ot,opacity:n.a.opacity,shadowEnabled:n.shadowEnabled,shadowBlur:n.shadowBlur,shadowOffsetX:n.shadowOffsetX,shadowOffsetY:n.shadowOffsetY,shadowColor:n.shadowColor,shadowOpacity:n.shadowOpacity,hideInExport:!n.showInExport,editModeEnabled:n._editModeEnabled||ct||Wt}),(Wt&&!Ot||te)&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Dt,children:t(Y,{divProps:{style:{pointerEvents:"none"}},children:t(Q,{dangerouslySetInnerHTML:{__html:Ft},style:{pointerEvents:"none",opacity:n.a.opacity,textShadow:n.shadowEnabled?`${n.shadowOffsetX}px ${n.shadowOffsetY}px ${n.shadowBlur}px ${U(n.shadowColor,null!==(et=n.shadowOpacity)&&void 0!==et?et:1)}`:void 0}})})}),Ot&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Dt,children:t(Y,{divProps:{style:{zIndex:20}},children:t(K,{element:n,onChange:t=>{const e=W({oldText:P(n.text),newText:P(t),element:n});n.set(Object.assign({text:t},f(n,e)))},onBlur:t=>{p(n)&&(n.set(h(n)),n.toggleEditMode(!1),Bt(!0))},clickCoords:Zt.current})})})]})});
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{elementOpacity as n}from"./paint-opacity.js";import{observer as i}from"mobx-react-lite";import{Group as a,Image as s,Path as l,Rect as c}from"react-konva";import{activeAnchorOf as f,scaledFontSizeAttrs as d,roundedFontSizeAttrs as h,roundedTextDecorationAttrs as u,roundedGeometryPx as g,roundedPosition as m,textMinResizeWidth as x}from"../utils/px.js";import p from"konva";import{isAlive as y}from"mobx-state-tree";import{nodeAutorun as w}from"../utils/mobx.js";import{flags as b}from"../utils/flags.js";import{applyFilter as v}from"./apply-filters.js";import{getLineHeight as E,useFontFamiliesLoader as S,usePrevious as O}from"./text-element.js";import{getCurvePath as k}from"@polotno/core/text-html";import{useColor as z}from"./use-color.js";import{useWorkspaceStyle as F}from"./workspace-style.js";import{detectSize as j,detectLineRects as M,htmlToCanvas as R}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as X}from"@polotno/core/background-shape";import{useFadeIn as Y}from"./use-fadein.js";import{Html as I}from"react-konva-utils";import{incrementLoader as C,triggerLoadError as P}from"../utils/loader.js";import{removeTags as T}from"@polotno/core/text";import{cssFontFamily as B}from"@polotno/core/fonts";import*as A from"../utils/fonts.js";import{isTouchDevice as W}from"../utils/screen.js";import{getLimitedFontSize as $}from"./text-element/max-font-size.js";import{visiblePlaceholder as D}from"./text-element/placeholder.js";import{useDelayer as _}from"./use-delayer.js";import{getHtml as H,textLayoutWidth as L}from"@polotno/core/text-html";import{hasMeasuredHeight as J,resolveTextBoxHeight as N}from"@polotno/core/text-types";import{normalizeRichTextHtml as q,extractFontFamiliesFromHtml as V,scaleRichTextFontSizesInHtml as G}from"../utils/rich-text-html.js";import{findFitFontSize as K}from"./text-element/fit-font-size.js";import{RichEditor as Q,RichTextContainer as U,applyOpacityToColor as Z,quillRef as tt,createQuill as et,setQuillContent as ot,setQuillFormats as rt,syncQuillFormatState as nt,setCursorFromCoords as it}from"./rich-editor.js";export{tt as quillRef,et as createQuill,ot as setQuillContent,rt as setQuillFormats,nt as syncQuillFormatState,it as setCursorFromCoords};function at(t){const e=document.createElement("canvas"),o=e.getContext("2d");return null==o||o.drawImage(t,0,0,e.width,e.height),function(t){return!function(t){const e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;for(let o=0;o<e.length;o+=4){if(0!==e[o+3]){return!0}}return!1}(t)}(e)}const st=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),lt=navigator.userAgent.includes("Firefox");export const HTMLElement=i(({element:i,store:tt})=>{var et,ot,rt;const{textOverflowIndicatorStyle:nt}=F(),it=r.useRef(null),[ct,ft]=r.useState(),[dt,ht]=r.useState(!1),[ut,gt]=r.useState(!1),mt=r.useRef(i.height),xt=i.isSelected,pt=O(xt),yt=i.fontSize/3,wt=L(i.a.width,i.letterSpacing,i.fontSize)+2*yt,{textVerticalResizeEnabled:bt}=b,vt=b.autoDeleteEmptyText,Et=O(i.fontFamily),St=r.useMemo(()=>q(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),Ot=r.useMemo(()=>[...new Set([i.fontFamily,...V(St)])].filter(Boolean),[i.fontFamily,St]),[kt]=S(tt,Ot,i.fontStyle,i.fontWeight,i.text||i.placeholder),zt=i._editModeEnabled,Ft=zt&&!(null===(et=i.page)||void 0===et?void 0:et._exporting);Y(it);const jt=kt?i.fontFamily:Et!==i.fontFamily?Et:"Arial",Mt=z(i).fill,Rt=H(i,{fontFamily:jt,color:Mt,textOverflow:b.textOverflow}),Xt=r.useRef([]),Yt=r.useRef(!0),[,It]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Yt.current=!1;const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))},[]);let{width:Ct,height:Pt}=function(t,e,o){return r.useMemo(()=>j(t),[t,e.width,o])}(Rt,i,kt);r.useEffect(()=>{if(!kt){return}const t=(t,e,o,r)=>{const n=C(`text ${i.id}`),a=()=>{tt.history.ignore(t,e,o).then(()=>{Yt.current?(Xt.current.push(n),It()):n()})};r?queueMicrotask(a):a()};if(i.curveEnabled&&Math.abs(Ct-i.width)>1&&!dt&&t(()=>{if(y(i)&&Math.abs(Ct-i.width)>1){const t=i.x+i.width/2;i.set({width:Ct,x:t-Ct/2})}},!1,!0),!J(i)){return void t(()=>{y(i)&&(J(i)||i.set({height:Pt}))},void 0,void 0,!0)}const{textOverflow:e}=b;if("change-font-size"!==e||dt||zt||i.curveEnabled){"resize"===e&&(bt&&i.height<Pt&&!dt&&t(()=>{y(i)&&i.set({height:Pt})},!1,!0),bt||i.height===Pt||dt||t(()=>{y(i)&&i.set({height:Pt})},!1,!0))}else{const e=K(i,{fontFamily:i.fontFamily,textOverflow:b.textOverflow});e.fontSize!==i.fontSize?t(()=>{if(!y(i)){return}const t=d(i,e.fontSize);void 0!==e.text&&e.text!==i.text&&(t.text=e.text),i.set(t)},!1,!0):i.height!==Pt&&(bt&&i.height<Pt?t(()=>{y(i)&&i.set({height:Pt})},!1,!0):bt||t(()=>{y(i)&&i.set({height:Pt})},!1,!0))}});const Tt=r.useMemo(()=>{const t={lastArgs:null,lastResult:null};return async function(e){return t.lastArgs&&t.lastResult&&(o=t.lastArgs,r=e,JSON.stringify(o)===JSON.stringify(r))||(t.lastResult=await R(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Bt=r.useRef(0),At=r.useRef(null);r.useEffect(()=>{dt||Ft||(async()=>{Bt.current++;const t=Bt.current;let e=C(`text ${i.id} ${t}`);At.current&&At.current(),At.current=e,gt(!0);let o=null;const r=st?5:1;for(let a=0;a<r;a++){const e=a>0?Tt:R;try{if(o=await e({skipCache:a>0,html:Rt,width:Ct||i.width||1,height:N(i,Pt)||1,fontFamily:jt,fontFamilies:V(Rt),resolveFont:t=>tt.fonts.find(e=>e.fontFamily===t)||A.globalFonts.find(e=>e.fontFamily===t),padding:yt,pixelRatio:tt._elementsPixelRatio,font:tt.fonts.find(t=>t.fontFamily===jt)||A.globalFonts.find(t=>t.fontFamily===jt)}),t!==Bt.current){return}if((st||lt)&&o&&at(o)){await new Promise(t=>setTimeout(t,50*(a+1)));continue}break}catch(n){console.error(n),P(`Error rendering rich text with id ${i.id}`);break}}o?ft(o):e?(e(),e=null):console.error("Finish function is called twice!"),gt(!1)})()},[Rt,dt,Pt,Ft,jt,i.height,tt._elementsPixelRatio,kt]);const[Wt,$t]=_(ut,300),[Dt]=_(dt,300,!0),_t=Dt||Wt;r.useEffect(()=>{var t;if(!_t){return w(i,()=>{const t=it.current;v(t,i)})}null===(t=it.current)||void 0===t||t.clearCache()},[ct,_t,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]),r.useEffect(()=>{ct&&!ut&&At.current&&(At.current(),At.current=null)},[ct,ut]),r.useLayoutEffect(()=>{if(!kt){return}if(!i.curveEnabled){return}const t=new p.TextPath({data:k(i.a.width,i.a.height,i.curvePower,i.a.fontSize),text:T(i.text),letterSpacing:i.letterSpacing*i.a.fontSize,fontSize:i.a.fontSize,fontFamily:B(i.fontFamily),fontWeight:i.fontWeight,fontStyle:i.fontStyle,align:"center",textBaseline:"middle",fill:i.fill}),e=t.getSelfRect().width;if(e){const t=e-i.a.width,o=i.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),n=-t/2*Math.sin(o);queueMicrotask(()=>{y(i)&&i.set({width:e,x:i.a.x+r,y:i.a.y+n})})}t.destroy()},[kt,i.curveEnabled,i.curvePower,i.text,i.fontSize,i.fontFamily,i.fontWeight,i.fontStyle,i.letterSpacing]),r.useEffect(()=>()=>{At.current&&At.current()},[]),r.useEffect(()=>{pt&&!xt&&vt&&""===T(i.text)&&i.removable&&!D(i.placeholder)&&tt.deleteElements([i.id])},[vt,i.placeholder,i.removable,i.text,xt,pt]);const Ht=N(i,Pt);let Lt=0;"middle"===i.verticalAlign&&(Lt=(Ht-Pt)/2),"bottom"===i.verticalAlign&&(Lt=Ht-Pt);const Jt=E({fontLoaded:kt,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),Nt=i.backgroundPadding*(i.fontSize*Jt*.5),qt=i.backgroundCornerRadius*(i.fontSize*Jt*.5),Vt=r.useMemo(()=>{if(!i.backgroundEnabled||i.curveEnabled||i.legacyBackground){return""}const t=M(Rt);if(0===t.length){return""}const e=t.map(t=>({left:t.left+yt,right:t.right+yt,top:t.top+yt,bottom:t.bottom+yt}));return X({rects:e,padding:Nt,cornerRadius:qt})},[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,Rt,kt,Nt,qt,yt]),Gt=W(),Kt=k(i.a.width,Pt,i.curvePower,i.fontSize),Qt=r.useRef(null),Ut=r.useRef(i.width),Zt=r.useRef(""),te=r.useRef(0),ee=r.useRef(0),oe=r.useRef(null),re=zt&&i.strokeWidth>0&&!i.curveEnabled,ne=Ct>0?wt/(Ct+2*yt):1;return o(e,{children:[i.curveEnabled||i.legacyBackground?t(c,{x:i.a.x,y:i.a.y,offsetX:Nt/ne,offsetY:Nt/ne,rotation:i.a.rotation,scaleX:ne,scaleY:ne,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*n(i),fill:i.backgroundColor,width:i.width+2*Nt,height:Ht+2*Nt,cornerRadius:qt}):t(l,{name:"html-background",x:i.a.x,y:i.a.y,offsetX:yt/ne,offsetY:(yt-Lt)/ne,rotation:i.a.rotation,scaleX:ne,scaleY:ne,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&Vt.length>0,opacity:i.backgroundOpacity*n(i),fill:i.backgroundColor,data:Vt}),t(l,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,data:Kt,stroke:nt.stroke,strokeWidth:1,visible:!1}),t(c,{ref:it,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:i.a.height,visible:!_t,draggable:Gt?i.draggable&&xt:i.draggable,preventDefault:!Gt||xt,opacity:zt?0:n(i),hideInExport:!i.showInExport,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set(m(t.target))},id:i.id,onDblClick:t=>{i.contentEditable&&(oe.current={x:t.evt.clientX,y:t.evt.clientY},i.toggleEditMode(!0))},onDblTap:t=>{var e;if(i.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];oe.current=o?{x:o.clientX,y:o.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{ht(!0),mt.current=i.height,Ut.current=i.width,Qt.current=t.target.position(),Zt.current=f(t.target),te.current=i.a.rotation,ee.current=i.height},onTransform:t=>{const e=t.target,o=Zt.current,r="middle-left"===o||"middle-right"===o,n="top-center"===o||"bottom-center"===o,a=e.scaleX();if(r){const t=e.scaleX(),o=e.width()*t,r=x(Ut.current,i.fontSize);let n=o;o<r&&(n=r,Qt.current&&e.position(Qt.current)),e.width(n),e.scaleX(1),e.scaleY(1);const a=H(Object.assign(Object.assign({},i.toJSON()),{width:n}),{fontFamily:jt,color:Mt,textOverflow:b.textOverflow}),s=j(a).height,l=b.textVerticalResizeEnabled?Math.max(s,mt.current):s;e.height(l),i.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(n){const t="resize"===b.textOverflow,o="number"==typeof i.lineHeight?i.lineHeight:1,r=t?Pt:o*i.fontSize,n=e.height()*e.scaleY();if(n<r){if(e.height(r),Qt.current){if("top-center"===Zt.current){const t=te.current*Math.PI/180,o=ee.current-r;e.x(Qt.current.x-o*Math.sin(t)),e.y(Qt.current.y+o*Math.cos(t))}else{e.position(Qt.current)}}}else{e.height(n)}e.scaleY(1),e.scaleX(1),ee.current=Math.max(r,n),i.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),i.set({fontSize:i.fontSize*a,letterSpacing:i.letterSpacing,width:e.width()*a,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*a,shadowBlur:i.shadowBlur*a,shadowOffsetX:i.shadowOffsetX*a,shadowOffsetY:i.shadowOffsetY*a,strokeWidth:i.strokeWidth*a,text:G(i.text,a)})}Qt.current=t.target.position()},onTransformEnd:t=>{ht(!1),gt(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},h(i,i.fontSize*e)),{width:g(t.target.width()*e),x:g(t.target.x()),y:g(t.target.y()),rotation:g(t.target.rotation()),text:G(i.text,e)}))}}),t(s,{ref:it,image:ct,x:i.a.x,y:i.a.y,offsetX:yt,offsetY:yt-Lt,listening:!1,rotation:i.a.rotation,width:wt,height:wt*((null!==(ot=null==ct?void 0:ct.height)&&void 0!==ot?ot:0)/((null==ct?void 0:ct.width)||1)||1),visible:!_t&&!zt,opacity:n(i),shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,hideInExport:!i.showInExport,editModeEnabled:i._editModeEnabled||dt||_t}),(_t&&!zt||re)&&t(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-Lt,children:t(I,{divProps:{style:{pointerEvents:"none"}},children:t(U,{dangerouslySetInnerHTML:{__html:Rt},style:{pointerEvents:"none",opacity:i.a.opacity,textShadow:i.shadowEnabled?`${i.shadowOffsetX}px ${i.shadowOffsetY}px ${i.shadowBlur}px ${Z(i.shadowColor,null!==(rt=i.shadowOpacity)&&void 0!==rt?rt:1)}`:void 0}})})}),zt&&t(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-Lt,children:t(I,{divProps:{style:{zIndex:20}},children:t(Q,{element:i,onChange:t=>{const e=$({oldText:T(i.text),newText:T(t),element:i});i.set(Object.assign({text:t},d(i,e)))},onBlur:t=>{y(i)&&(i.set(u(i)),i.toggleEditMode(!1),$t(!0))},clickCoords:oe.current})})})]})});
|
package/canvas/image-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import i from"react";import{observer as o}from"mobx-react-lite";import{isAlive as a}from"mobx-state-tree";import{Arc as h,Group as n,Image as s,Rect as d,Text as c}from"react-konva";import{ROLES as g}from"../model/store.js";import l from"./use-image.js";import u from"konva";import{useWorkspaceStyle as p}from"./workspace-style.js";import{incrementLoader as m,triggerLoadError as f}from"../utils/loader.js";import{polotnoError as w}from"@polotno/core/errors";import{getBorderInset as x,getCropRect as v}from"@polotno/core/image";import*as M from"@polotno/core/svg";import{trySetCanvasSize as y}from"../utils/canvas.js";import{roundEnabledPx as b,activeAnchorOf as S,roundedCropFraction as E,roundedGeometryPx as C,roundedPosition as I,isCornerAnchor as R,scaledBoxStyleAttrs as X,roundedBoxStyleAttrs as Y}from"../utils/px.js";import{useFilteredSource as j}from"./use-filtered-source.js";import{useFadeIn as H}from"./use-fadein.js";import{isTouchDevice as O}from"../utils/screen.js";import{useDelayer as W}from"./use-delayer.js";import{useColor as z}from"./use-color.js";import{commitCropSource as A,CropOverlay as D}from"./crop-overlay.js";import{drawReduced as k,exportKernelEnabled as L,shouldResample as T}from"./downscale.js";const _=1e4;function N(){return document.createElement("canvas")}const U=t=>"svg"===t.type||M.isSvgUrl(t.src||""),F=t=>{const[e,r]=i.useState({src:"",value:"",status:"loaded"}),o=!!t&&M.isSvgUrl(t);return i.useEffect(()=>{if(!o){return}let e=!0;return(async()=>{try{const i=M.prepareSvgSource(await M.urlToString(t));e&&r({src:t,value:i,status:"loaded"})}catch(i){e&&r({src:t,value:"",status:"failed"})}})(),()=>{e=!1}},[t,o]),o?[e.value,e.src===t?e.status:"loading"]:[t,"loaded"]};export const useSizeFixer=t=>F(t)[0]||t;function P(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}export function svgSourceRatio(t,e,r){if(!(e.width>0&&e.height>0)){return 1}const i=Math.max(t.width/e.width*r,t.height/e.height*r),o=Math.min(t.cropWidth,t.cropHeight),a=o>0&&o<=1?Math.pow(2,Math.ceil(Math.log2(1/o))):1;return Math.min(i*a,_/Math.max(e.width,e.height),Math.sqrt(16777216/(e.width*e.height)))}export function maskCompositeSize(t,e){const r=v({stretchEnabled:e.stretchEnabled,cropX:0,cropY:0,cropWidth:1,cropHeight:1,width:Math.max(e.width,1),height:Math.max(e.height,1)},t);return{width:Math.max(Math.round(r.width),1),height:Math.max(Math.round(r.height),1)}}const q=(t,e,r)=>Math.max(e,Math.min(r,t)),B=(t,e,r)=>Math.max(t.a.width/e.width,t.a.height/e.height)*r;export const useCornerRadiusAndCrop=(t,e,r,o,a=0,h=!1,n=!0,s=0,d)=>{var c;const g=_,l=_,p=d.aligned?q(d.deviceWidth,1,g):Math.floor(q(t.a.width*o,1,g)),m=d.aligned?q(d.deviceHeight,1,l):Math.floor(q(t.a.height*o,1,l)),f=Math.min(a*o,p/2,m/2),w=Math.min(s*o,p/2,m/2),x=B(t,r,o),v=L(t.page)&&!h&&(T(x,p,m)||d.aligned),M=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),b=i.useMemo(()=>{if(e&&e.width&&e.height){return!M||0!==f||v||w?N():void 0}},[e,f,v,M,w]);return i.useLayoutEffect(()=>{var i;if(!b||!e){return}y(b,p,m);const o=b.getContext("2d");if(o){if((null===(i=t.page)||void 0===i?void 0:i._exportingOrRendering)&&(o.imageSmoothingQuality="high"),(f||w)&&(o.beginPath(),o.roundRect(w,w,p-2*w,m-2*w,Math.max(0,f-w)),o.clip()),v){const t=b.width===p&&b.height===m&&d.aligned;return void k(o,e,r,b.width,b.height,{x:t?d.phaseX:0,y:t?d.phaseY:0,width:t?d.spanX:b.width,height:t?d.spanY:b.height})}o.drawImage(e,r.x,r.y,r.width,r.height,0,0,b.width,b.height)}},[b,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,o,h,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,v,d.phaseX,d.phaseY,w]),i.useEffect(()=>()=>{b&&"CANVAS"===b.nodeName&&u.Util.releaseCanvas(b)},[b]),b||e};const V=N(),$=o(({element:r})=>{const o=Math.min(30,r.a.width/4,r.a.height/4),a=i.useRef(null);i.useEffect(()=>{const t=a.current;if(!t){return}const e=new u.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:s}=p();return e(n,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(d,{width:r.a.width,height:r.a.height,fill:s.fill}),t(h,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:s.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),G=o(({element:r})=>{const{mediaErrorStyle:i}=p(),o=Math.max(10,Math.min(30,r.a.width/25));return e(n,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(d,{width:r.a.width,height:r.a.height,fill:i.fill}),t(c,{text:"Can not load the image...",fontSize:o,width:r.a.width,height:r.a.height,align:"center",fill:i.textFill,verticalAlign:"middle",padding:5})]})});let J=l;export const setImageLoaderHook=t=>{J=t};export const useCanvasImage=(t,e)=>J(t,e);export const useClipImage=t=>{const[e,r]=F(t),[o,a]=useCanvasImage(e,"anonymous"),h="loaded"===r?a:r,n=i.useRef(void 0);return t&&"failed"!==h?o&&(n.current=o):n.current=void 0,[t&&"failed"!==h?o||n.current:void 0,h]};export const useImageLoader=(t,e="",r="")=>{const o=i.useRef(void 0),a=()=>{var t;null===(t=o.current)||void 0===t||t.call(o),o.current=void 0};i.useEffect(()=>a,[]),i.useLayoutEffect(()=>{const i=e.slice(0,200),h=`image with id ${r} url: ${i}`;"loading"!==t||o.current||(o.current=m(h)),"loading"!==t&&a(),"failed"===t&&f(w("IMAGE_FAILED",h,{src:i,elementId:r,reason:"load"}))},[t])};export const ImageElement=o(({element:o,store:h})=>{var n,c,l,p;const[m,f]=i.useState(!1),w=i.useRef(null),k=o.isSelected,[q,Q]=(t=>{var e;const[r,o]=i.useReducer(t=>t+1,0),a=U(t),h=a?t.src+"\n"+JSON.stringify(null!==(e=t.colorsReplace)&&void 0!==e?e:null):"",n=i.useRef("loading"),s=i.useRef(""),d=i.useRef(h);return d.current!==h&&(d.current=h,n.current="loading"),i.useEffect(()=>{if(!a||!t.src){return}let e=!1;return(async()=>{const r=M.prepareSvgSource(await M.urlToString(t.src),t.colorsReplace);e||(s.current=r,n.current="loaded",o())})().catch(()=>{e||(n.current="failed",o())}),()=>{e=!0}},[h]),a?[s.current,n.current]:[t.src,"loaded"]})(o),[K,Z]=J(q,"anonymous"),tt="loaded"===Q?Z:Q;useImageLoader(tt,o.src,o.id);const et=(null===(n=o.page)||void 0===n?void 0:n._exportingOrRendering)?1:Math.max(1,h.scale),rt=h._elementsPixelRatio*et,it=(({image:t,status:e})=>{const r=i.useRef(void 0);return i.useEffect(()=>{r.current=t||r.current},[t]),"failed"!==e?r.current:void 0})({image:K,status:tt}),ot=((t,e,r)=>{const o=U(t)&&e?svgSourceRatio({cropWidth:t.a.cropWidth,cropHeight:t.a.cropHeight,width:t.a.width,height:t.a.height},e,r):1,a=i.useMemo(()=>{var r,i;const{flipX:a,flipY:h}=t,n=U(t),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,d=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&n||t.maskSrc;if(!a&&!h&&!d){return e}if(!e||!e.width||!e.height){return null}const c=N();y(c,Math.max(e.width*o,1),Math.max(e.height*o,1));let g=a?-c.width:0,l=h?-c.height:0;return null===(r=c.getContext("2d"))||void 0===r||r.scale(a?-1:1,h?-1:1),null===(i=c.getContext("2d"))||void 0===i||i.drawImage(e,g,l,c.width,c.height),c},[t.maskSrc,t.flipX,t.flipY,e,o]);return i.useEffect(()=>()=>{a&&"CANVAS"===a.nodeName&&u.Util.releaseCanvas(a)},[a]),a})(o,K||it,rt),at=((t,e)=>{const[r,o]=F(t.maskSrc||""),[a,h]=J(r,"anonymous"),n=t.maskSrc?"loaded"===o?h:o:"loaded";return useImageLoader(n,t.maskSrc||"",t.id),i.useMemo(()=>{if(!a){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:i}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),o=N();o.width=r,o.height=i;const h=o.getContext("2d");if(!h){return e}h.drawImage(e,0,0,r,i,0,0,r,i),h.globalCompositeOperation="source-in";const n=function(t,e){const r=e.width/e.height;let i,o;return r>=t.width/t.height?(i=t.width,o=t.width/r):(i=t.height*r,o=t.height),{x:(t.width-i)/2,y:(t.height-o)/2,width:i,height:o}}(a,t);return h.drawImage(a,n.x,n.y,n.width,n.height,0,0,r,i),o},[e,a,t.a.width,t.a.height,t.stretchEnabled])})(o,ot)||V,[ht=V]=j(o,at);let{cropX:nt,cropY:st,cropWidth:dt,cropHeight:ct}=o.a;"loaded"!==Z&&(nt=st=0,dt=ct=1);const gt=v({stretchEnabled:o.stretchEnabled,cropX:nt,cropY:st,cropWidth:dt,cropHeight:ct,width:o.a.width,height:o.a.height},ht),lt=gt.width,ut=gt.height,pt=o.stretchEnabled,mt=null!==(c=o.cornerRadius)&&void 0!==c?c:0,ft=(null===(l=o.page)||void 0===l?void 0:l._exportingOrRendering)?1:Math.min(2,h.scale),wt=h._elementsPixelRatio*ft,xt=m||o._cropModeEnabled||"svg"===o.type,vt=function(t,e,r,i){const{x:o,y:a,width:h,height:n}=t.a,s={x:o,y:a,width:h,height:n,deviceWidth:0,deviceHeight:0,phaseX:0,phaseY:0,spanX:0,spanY:0,aligned:!1};if(!i){return s}const d=o*e,c=a*e,g=Math.round(d),l=Math.round(c),u=Math.max(1,Math.round(d+h*e)-g),p=Math.max(1,Math.round(c+n*e)-l),m=t=>Math.abs(t-Math.round(t))>.001;return m(d)||m(c)||m(h*e)||m(n*e)?u>_||p>_?s:T(r,u,p)||r<=1&&u*p<=4e6?{x:g/e,y:l/e,width:u/e,height:p/e,deviceWidth:u,deviceHeight:p,phaseX:d-g,phaseY:c-l,spanX:h*e,spanY:n*e,aligned:!0}:s:s}(o,wt,B(o,gt,wt),L(o.page)&&0===o.a.rotation&&"group"!==(null===(p=o.parent)||void 0===p?void 0:p.type)&&!xt),Mt=vt.aligned?vt:{x:o.x,y:o.y,width:o.a.width,height:o.a.height};let yt=((t,e,r,o)=>{const[a,h]=useClipImage(t.clipSrc||""),n=t.clipSrc?h:"loaded";useImageLoader(n,t.clipSrc,t.id);const s=i.useMemo(()=>{if(e&&a){return N()}},[e,a]);return i.useLayoutEffect(()=>{var i;if(!a){return}if(!e||!e.width||!e.height){return}if(!a||!a.width||!a.height){return}if(!s){return}const o=N(),h=Math.max(t.a.width/a.width*r,t.a.height/a.height*r);y(o,Math.min(1e4,Math.max(a.width*h,1)),Math.min(1e4,Math.max(a.height*h,1))),null===(i=o.getContext("2d"))||void 0===i||i.drawImage(a,0,0,o.width,o.height),y(s,Math.min(1e4,Math.max(e.width,Math.ceil(t.a.width*r),1)),Math.min(1e4,Math.max(e.height,Math.ceil(t.a.height*r),1)));const n=s.getContext("2d");n&&(n.save(),n.drawImage(o,0,0,s.width,s.height),u.Util.releaseCanvas(o),n.globalCompositeOperation="source-in",n.drawImage(e,0,0,s.width,s.height),n.restore())},[s,e,a,t.a.width,t.a.height,r,...o]),i.useEffect(()=>()=>{s&&u.Util.releaseCanvas(s)},[s]),t.clipSrc&&a?s:e})(o,useCornerRadiusAndCrop(o,ht,gt,wt,mt,xt,!0,x({borderSize:o.borderSize,borderColor:o.borderColor,width:o.a.width,height:o.a.height}),vt),wt,[gt,mt,wt]);var bt=i.useRef(null),St=i.useRef(0),Et=i.useRef(!1);const Ct=()=>{o._cropEnabled&&setTimeout(()=>{a(o)&&o.toggleCropMode(!0)})},It=U(o)&&it,Rt="loading"===tt&&!It,[Xt]=W(Rt,100,!1,!1),Yt="failed"===tt,jt=!Xt&&!Yt,Ht=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Ot=i.useRef(!1),Wt=jt?o.a.opacity:0;H(w,Wt);const zt=o.selectable||h.role===g.ADMIN,At=O(),Dt=z(o,o.borderColor,"stroke");return e(r,{children:[Xt&&t($,{element:o}),Yt&&t(G,{element:o}),t(s,{ref:w,name:"element",id:o.id,image:yt,x:vt.x,y:vt.y,width:vt.width||1,height:vt.height||1,rotation:o.a.rotation,opacity:Wt,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,customCrop:gt,listening:zt,draggable:At?o.draggable&&k:o.draggable,preventDefault:!At||k,hideInExport:!o.showInExport,onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{o.set(I(t.target))},onDblClick:Ct,onDblTap:Ct,onTransformStart:t=>{f(!0),Ot.current=R(S(t.target)),Ht.current={cropX:o.cropX,cropY:o.cropY,cropWidth:o.cropWidth,cropHeight:o.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),i=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=1-lt/ht.width,h=Math.min(a,Math.max(0,o.cropX)),n=1-ut/ht.height,s=Math.min(n,Math.max(0,o.cropY)),d=Ot.current,c=!d&&r<1&&Ht.current.cropHeight>ut/ht.height;let g=d?o.cropWidth:o.cropWidth*r;c&&(g=o.cropWidth);const l=!d&&i<1&&Ht.current.cropWidth>lt/ht.width;let u=d?o.cropHeight:o.cropHeight*i;l&&(u=o.cropHeight),pt&&(g=o.cropWidth,u=o.cropHeight);const p=d?Math.min(r,i):1;o.set(Object.assign({cropX:h,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:t.target.rotation(),cropWidth:Math.min(g,1-h),cropHeight:Math.min(u,1-s)},1!==p&&Object.assign(Object.assign({},X(o,p)),{borderSize:o.borderSize*p})))},onTransformEnd:t=>{const e=t.currentTarget;o.set(Object.assign(Object.assign({width:C(e.width()),height:C(e.height()),x:C(e.x()),y:C(e.y()),rotation:C(t.target.rotation()),cropX:E(o.cropX),cropY:E(o.cropY),cropWidth:E(lt/ht.width),cropHeight:E(ut/ht.height)},Y(o)),{borderSize:b(o.borderSize)})),f(!1)}}),t(d,Object.assign({x:Mt.x,y:Mt.y,width:Math.max(Mt.width-o.borderSize,0),height:Math.max(Mt.height-o.borderSize,0),opacity:Wt,offsetX:-o.borderSize/2,offsetY:-o.borderSize/2},Dt,{strokeWidth:o.borderSize,listening:!1,visible:!!o.borderSize,rotation:o.rotation,cornerRadius:Math.max(0,mt-o.borderSize),hideInExport:!o.showInExport})),o._cropModeEnabled&&t(D,{element:o,store:h,previewImage:yt,sourceImage:ht,sourceWidth:ht.width,sourceHeight:ht.height,cropAbsoluteWidth:lt,cropAbsoluteHeight:ut,onSourceTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],i=e[1];const a=t.target;if(r&&!i&&!a.isDragging()&&Et.current&&(a.startDrag(),Et.current=!1),r&&i){u.hitOnDragEnabled=!0,a.isDragging()&&(Et.current=!0,a.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var h={x:r.x,y:r.y},n={x:i.x,y:i.y};if(!bt.current){return void(bt.current=P(h,n))}var s=P(h,n),d=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(h,n);St.current||(St.current=d);const f=a.position();var c={x:s.x-f.x,y:s.y-f.y},g=d/St.current;a.scaleX(g),a.scaleY(g);const w=e.point(s),x=e.point(bt.current);var l=w.x-x.x,p=w.y-x.y,m={x:Math.min(0,s.x-c.x*g+l),y:Math.min(0,s.y-c.y*g+p)};a.position(m),St.current=d,bt.current=s,(t=>{A(t,o,ht.width,ht.height,lt,ut)})(t)}},onSourceTouchEnd:t=>{St.current=0,bt.current=null,u.hitOnDragEnabled=!1}})]})});
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import i from"react";import{observer as o}from"mobx-react-lite";import{isAlive as a}from"mobx-state-tree";import{Arc as h,Group as n,Image as s,Rect as d,Text as c}from"react-konva";import{ROLES as l}from"../model/store.js";import g from"./use-image.js";import u from"konva";import{useWorkspaceStyle as p}from"./workspace-style.js";import{incrementLoader as m,triggerLoadError as f}from"../utils/loader.js";import{polotnoError as w}from"@polotno/core/errors";import{getBorderInset as x,getCropRect as v}from"@polotno/core/image";import{elementOpacity as M,paintOpacity as y}from"./paint-opacity.js";import*as b from"@polotno/core/svg";import{trySetCanvasSize as S}from"../utils/canvas.js";import{roundEnabledPx as E,activeAnchorOf as C,roundedCropFraction as I,roundedGeometryPx as R,roundedPosition as X,isCornerAnchor as Y,scaledBoxStyleAttrs as j,roundedBoxStyleAttrs as H}from"../utils/px.js";import{useFilteredSource as O}from"./use-filtered-source.js";import{useFadeIn as W}from"./use-fadein.js";import{isTouchDevice as z}from"../utils/screen.js";import{useDelayer as A}from"./use-delayer.js";import{useColor as D}from"./use-color.js";import{commitCropSource as k,CropOverlay as L}from"./crop-overlay.js";import{drawReduced as T,exportKernelEnabled as _,shouldResample as N}from"./downscale.js";const U=1e4,F=t=>"svg"===t.type||b.isSvgUrl(t.src||""),P=t=>{const[e,r]=i.useState({src:"",value:"",status:"loaded"}),o=!!t&&b.isSvgUrl(t);return i.useEffect(()=>{if(!o){return}let e=!0;return(async()=>{try{const i=b.prepareSvgSource(await b.urlToString(t));e&&r({src:t,value:i,status:"loaded"})}catch(i){e&&r({src:t,value:"",status:"failed"})}})(),()=>{e=!1}},[t,o]),o?[e.value,e.src===t?e.status:"loading"]:[t,"loaded"]};export const useSizeFixer=t=>P(t)[0]||t;function q(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}export function svgSourceRatio(t,e,r){if(!(e.width>0&&e.height>0)){return 1}const i=Math.max(t.width/e.width*r,t.height/e.height*r),o=Math.min(t.cropWidth,t.cropHeight),a=o>0&&o<=1?Math.pow(2,Math.ceil(Math.log2(1/o))):1;return Math.min(i*a,U/Math.max(e.width,e.height),Math.sqrt(16777216/(e.width*e.height)))}export function maskCompositeSize(t,e){const r=v({stretchEnabled:e.stretchEnabled,cropX:0,cropY:0,cropWidth:1,cropHeight:1,width:Math.max(e.width,1),height:Math.max(e.height,1)},t);return{width:Math.max(Math.round(r.width),1),height:Math.max(Math.round(r.height),1)}}const B=(t,e,r)=>Math.max(e,Math.min(r,t)),V=(t,e,r)=>Math.max(t.a.width/e.width,t.a.height/e.height)*r;export const useCornerRadiusAndCrop=(t,e,r,o,a=0,h=!1,n=!0,s=0,d)=>{var c;const l=U,g=U,p=d.aligned?B(d.deviceWidth,1,l):Math.floor(B(t.a.width*o,1,l)),m=d.aligned?B(d.deviceHeight,1,g):Math.floor(B(t.a.height*o,1,g)),f=Math.min(a*o,p/2,m/2),w=Math.min(s*o,p/2,m/2),x=V(t,r,o),v=_(t.page)&&!h&&(N(x,p,m)||d.aligned),M=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),y=i.useMemo(()=>{if(e&&e.width&&e.height){return!M||0!==f||v||w?document.createElement("canvas"):void 0}},[e,f,v,M,w]);return i.useLayoutEffect(()=>{var i;if(!y||!e){return}S(y,p,m);const o=y.getContext("2d");if(o){if((null===(i=t.page)||void 0===i?void 0:i._exportingOrRendering)&&(o.imageSmoothingQuality="high"),(f||w)&&(o.beginPath(),o.roundRect(w,w,p-2*w,m-2*w,Math.max(0,f-w)),o.clip()),v){const t=y.width===p&&y.height===m&&d.aligned;return void T(o,e,r,y.width,y.height,{x:t?d.phaseX:0,y:t?d.phaseY:0,width:t?d.spanX:y.width,height:t?d.spanY:y.height})}o.drawImage(e,r.x,r.y,r.width,r.height,0,0,y.width,y.height)}},[y,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,o,h,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,v,d.phaseX,d.phaseY,w]),i.useEffect(()=>()=>{y&&"CANVAS"===y.nodeName&&u.Util.releaseCanvas(y)},[y]),y||e};const $=document.createElement("canvas"),G=o(({element:r})=>{const o=Math.min(30,r.a.width/4,r.a.height/4),a=i.useRef(null);i.useEffect(()=>{const t=a.current;if(!t){return}const e=new u.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:s}=p();return e(n,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:M(r),hideInExport:!r.showInExport,children:[t(d,{width:r.a.width,height:r.a.height,fill:s.fill}),t(h,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:s.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),J=o(({element:r})=>{const{mediaErrorStyle:i}=p(),o=Math.max(10,Math.min(30,r.a.width/25));return e(n,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:M(r),hideInExport:!r.showInExport,children:[t(d,{width:r.a.width,height:r.a.height,fill:i.fill}),t(c,{text:"Can not load the image...",fontSize:o,width:r.a.width,height:r.a.height,align:"center",fill:i.textFill,verticalAlign:"middle",padding:5})]})});let Q=g;export const setImageLoaderHook=t=>{Q=t};export const useCanvasImage=(t,e)=>Q(t,e);export const useClipImage=t=>{const[e,r]=P(t),[o,a]=useCanvasImage(e,"anonymous"),h="loaded"===r?a:r,n=i.useRef(void 0);return t&&"failed"!==h?o&&(n.current=o):n.current=void 0,[t&&"failed"!==h?o||n.current:void 0,h]};export const useImageLoader=(t,e="",r="")=>{const o=i.useRef(void 0),a=()=>{var t;null===(t=o.current)||void 0===t||t.call(o),o.current=void 0};i.useEffect(()=>a,[]),i.useLayoutEffect(()=>{const i=e.slice(0,200),h=`image with id ${r} url: ${i}`;"loading"!==t||o.current||(o.current=m(h)),"loading"!==t&&a(),"failed"===t&&f(w("IMAGE_FAILED",h,{src:i,elementId:r,reason:"load"}))},[t])};export const ImageElement=o(({element:o,store:h})=>{var n,c,g,p;const[m,f]=i.useState(!1),w=i.useRef(null),T=o.isSelected,[B,K]=(t=>{var e;const[r,o]=i.useReducer(t=>t+1,0),a=F(t),h=a?t.src+"\n"+JSON.stringify(null!==(e=t.colorsReplace)&&void 0!==e?e:null):"",n=i.useRef("loading"),s=i.useRef(""),d=i.useRef(h);return d.current!==h&&(d.current=h,n.current="loading"),i.useEffect(()=>{if(!a||!t.src){return}let e=!1;return(async()=>{const r=b.prepareSvgSource(await b.urlToString(t.src),t.colorsReplace);e||(s.current=r,n.current="loaded",o())})().catch(()=>{e||(n.current="failed",o())}),()=>{e=!0}},[h]),a?[s.current,n.current]:[t.src,"loaded"]})(o),[Z,tt]=Q(B,"anonymous"),et="loaded"===K?tt:K;useImageLoader(et,o.src,o.id);const rt=(null===(n=o.page)||void 0===n?void 0:n._exportingOrRendering)?1:Math.max(1,h.scale),it=h._elementsPixelRatio*rt,ot=(({image:t,status:e})=>{const r=i.useRef(void 0);return i.useEffect(()=>{r.current=t||r.current},[t]),"failed"!==e?r.current:void 0})({image:Z,status:et}),at=((t,e,r)=>{const o=F(t)&&e?svgSourceRatio({cropWidth:t.a.cropWidth,cropHeight:t.a.cropHeight,width:t.a.width,height:t.a.height},e,r):1,a=i.useMemo(()=>{var r,i;const{flipX:a,flipY:h}=t,n=F(t),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,d=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&n||t.maskSrc;if(!a&&!h&&!d){return e}if(!e||!e.width||!e.height){return null}const c=document.createElement("canvas");S(c,Math.max(e.width*o,1),Math.max(e.height*o,1));let l=a?-c.width:0,g=h?-c.height:0;return null===(r=c.getContext("2d"))||void 0===r||r.scale(a?-1:1,h?-1:1),null===(i=c.getContext("2d"))||void 0===i||i.drawImage(e,l,g,c.width,c.height),c},[t.maskSrc,t.flipX,t.flipY,e,o]);return i.useEffect(()=>()=>{a&&"CANVAS"===a.nodeName&&u.Util.releaseCanvas(a)},[a]),a})(o,Z||ot,it),ht=((t,e)=>{const[r,o]=P(t.maskSrc||""),[a,h]=Q(r,"anonymous"),n=t.maskSrc?"loaded"===o?h:o:"loaded";return useImageLoader(n,t.maskSrc||"",t.id),i.useMemo(()=>{if(!a){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:i}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),o=document.createElement("canvas");o.width=r,o.height=i;const h=o.getContext("2d");if(!h){return e}h.drawImage(e,0,0,r,i,0,0,r,i),h.globalCompositeOperation="source-in";const n=function(t,e){const r=e.width/e.height;let i,o;return r>=t.width/t.height?(i=t.width,o=t.width/r):(i=t.height*r,o=t.height),{x:(t.width-i)/2,y:(t.height-o)/2,width:i,height:o}}(a,t);return h.drawImage(a,n.x,n.y,n.width,n.height,0,0,r,i),o},[e,a,t.a.width,t.a.height,t.stretchEnabled])})(o,at)||$,[nt=$]=O(o,ht);let{cropX:st,cropY:dt,cropWidth:ct,cropHeight:lt}=o.a;"loaded"!==tt&&(st=dt=0,ct=lt=1);const gt=v({stretchEnabled:o.stretchEnabled,cropX:st,cropY:dt,cropWidth:ct,cropHeight:lt,width:o.a.width,height:o.a.height},nt),ut=gt.width,pt=gt.height,mt=o.stretchEnabled,ft=null!==(c=o.cornerRadius)&&void 0!==c?c:0,wt=(null===(g=o.page)||void 0===g?void 0:g._exportingOrRendering)?1:Math.min(2,h.scale),xt=h._elementsPixelRatio*wt,vt=m||o._cropModeEnabled||"svg"===o.type,Mt=function(t,e,r,i){const{x:o,y:a,width:h,height:n}=t.a,s={x:o,y:a,width:h,height:n,deviceWidth:0,deviceHeight:0,phaseX:0,phaseY:0,spanX:0,spanY:0,aligned:!1};if(!i){return s}const d=o*e,c=a*e,l=Math.round(d),g=Math.round(c),u=Math.max(1,Math.round(d+h*e)-l),p=Math.max(1,Math.round(c+n*e)-g),m=t=>Math.abs(t-Math.round(t))>.001;return m(d)||m(c)||m(h*e)||m(n*e)?u>U||p>U?s:N(r,u,p)||r<=1&&u*p<=4e6?{x:l/e,y:g/e,width:u/e,height:p/e,deviceWidth:u,deviceHeight:p,phaseX:d-l,phaseY:c-g,spanX:h*e,spanY:n*e,aligned:!0}:s:s}(o,xt,V(o,gt,xt),_(o.page)&&0===o.a.rotation&&"group"!==(null===(p=o.parent)||void 0===p?void 0:p.type)&&!vt),yt=Mt.aligned?Mt:{x:o.x,y:o.y,width:o.a.width,height:o.a.height};let bt=((t,e,r,o)=>{const[a,h]=useClipImage(t.clipSrc||""),n=t.clipSrc?h:"loaded";useImageLoader(n,t.clipSrc,t.id);const s=i.useMemo(()=>{if(e&&a){return document.createElement("canvas")}},[e,a]);return i.useLayoutEffect(()=>{var i;if(!a){return}if(!e||!e.width||!e.height){return}if(!a||!a.width||!a.height){return}if(!s){return}const o=document.createElement("canvas"),h=Math.max(t.a.width/a.width*r,t.a.height/a.height*r);S(o,Math.min(1e4,Math.max(a.width*h,1)),Math.min(1e4,Math.max(a.height*h,1))),null===(i=o.getContext("2d"))||void 0===i||i.drawImage(a,0,0,o.width,o.height),S(s,Math.min(1e4,Math.max(e.width,Math.ceil(t.a.width*r),1)),Math.min(1e4,Math.max(e.height,Math.ceil(t.a.height*r),1)));const n=s.getContext("2d");n&&(n.save(),n.drawImage(o,0,0,s.width,s.height),u.Util.releaseCanvas(o),n.globalCompositeOperation="source-in",n.drawImage(e,0,0,s.width,s.height),n.restore())},[s,e,a,t.a.width,t.a.height,r,...o]),i.useEffect(()=>()=>{s&&u.Util.releaseCanvas(s)},[s]),t.clipSrc&&a?s:e})(o,useCornerRadiusAndCrop(o,nt,gt,xt,ft,vt,!0,x({borderSize:o.borderSize,borderColor:o.borderColor,width:o.a.width,height:o.a.height,opacity:y(o)}),Mt),xt,[gt,ft,xt]);var St=i.useRef(null),Et=i.useRef(0),Ct=i.useRef(!1);const It=()=>{o._cropEnabled&&setTimeout(()=>{a(o)&&o.toggleCropMode(!0)})},Rt=F(o)&&ot,Xt="loading"===et&&!Rt,[Yt]=A(Xt,100,!1,!1),jt="failed"===et,Ht=!Yt&&!jt,Ot=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Wt=i.useRef(!1),zt=Ht?M(o):0;W(w,zt);const At=o.selectable||h.role===l.ADMIN,Dt=z(),kt=D(o,o.borderColor,"stroke");return e(r,{children:[Yt&&t(G,{element:o}),jt&&t(J,{element:o}),t(s,{ref:w,name:"element",id:o.id,image:bt,x:Mt.x,y:Mt.y,width:Mt.width||1,height:Mt.height||1,rotation:o.a.rotation,opacity:zt,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,customCrop:gt,listening:At,draggable:Dt?o.draggable&&T:o.draggable,preventDefault:!Dt||T,hideInExport:!o.showInExport,onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{o.set(X(t.target))},onDblClick:It,onDblTap:It,onTransformStart:t=>{f(!0),Wt.current=Y(C(t.target)),Ot.current={cropX:o.cropX,cropY:o.cropY,cropWidth:o.cropWidth,cropHeight:o.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),i=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=1-ut/nt.width,h=Math.min(a,Math.max(0,o.cropX)),n=1-pt/nt.height,s=Math.min(n,Math.max(0,o.cropY)),d=Wt.current,c=!d&&r<1&&Ot.current.cropHeight>pt/nt.height;let l=d?o.cropWidth:o.cropWidth*r;c&&(l=o.cropWidth);const g=!d&&i<1&&Ot.current.cropWidth>ut/nt.width;let u=d?o.cropHeight:o.cropHeight*i;g&&(u=o.cropHeight),mt&&(l=o.cropWidth,u=o.cropHeight);const p=d?Math.min(r,i):1;o.set(Object.assign({cropX:h,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:t.target.rotation(),cropWidth:Math.min(l,1-h),cropHeight:Math.min(u,1-s)},1!==p&&Object.assign(Object.assign({},j(o,p)),{borderSize:o.borderSize*p})))},onTransformEnd:t=>{const e=t.currentTarget;o.set(Object.assign(Object.assign({width:R(e.width()),height:R(e.height()),x:R(e.x()),y:R(e.y()),rotation:R(t.target.rotation()),cropX:I(o.cropX),cropY:I(o.cropY),cropWidth:I(ut/nt.width),cropHeight:I(pt/nt.height)},H(o)),{borderSize:E(o.borderSize)})),f(!1)}}),t(d,Object.assign({x:yt.x,y:yt.y,width:Math.max(yt.width-o.borderSize,0),height:Math.max(yt.height-o.borderSize,0),opacity:zt,offsetX:-o.borderSize/2,offsetY:-o.borderSize/2},kt,{strokeWidth:o.borderSize,listening:!1,visible:!!o.borderSize,rotation:o.rotation,cornerRadius:Math.max(0,ft-o.borderSize),hideInExport:!o.showInExport})),o._cropModeEnabled&&t(L,{element:o,store:h,previewImage:bt,sourceImage:nt,sourceWidth:nt.width,sourceHeight:nt.height,cropAbsoluteWidth:ut,cropAbsoluteHeight:pt,onSourceTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],i=e[1];const a=t.target;if(r&&!i&&!a.isDragging()&&Ct.current&&(a.startDrag(),Ct.current=!1),r&&i){u.hitOnDragEnabled=!0,a.isDragging()&&(Ct.current=!0,a.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var h={x:r.x,y:r.y},n={x:i.x,y:i.y};if(!St.current){return void(St.current=q(h,n))}var s=q(h,n),d=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(h,n);Et.current||(Et.current=d);const f=a.position();var c={x:s.x-f.x,y:s.y-f.y},l=d/Et.current;a.scaleX(l),a.scaleY(l);const w=e.point(s),x=e.point(St.current);var g=w.x-x.x,p=w.y-x.y,m={x:Math.min(0,s.x-c.x*l+g),y:Math.min(0,s.y-c.y*l+p)};a.position(m),Et.current=d,St.current=s,(t=>{k(t,o,nt.width,nt.height,ut,pt)})(t)}},onSourceTouchEnd:t=>{Et.current=0,St.current=null,u.hitOnDragEnabled=!1}})]})});
|
package/canvas/line-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e,Fragment as o}from"react/jsx-runtime";import r from"react";import{observer as
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as o}from"react/jsx-runtime";import{elementOpacity as r}from"./paint-opacity.js";import a from"react";import{observer as n}from"mobx-react-lite";import{Group as i,Line as s,Rect as l,Circle as h}from"react-konva";import{ROLES as d}from"../model/store.js";import{useColor as c}from"./use-color.js";import{getLineHeadGeometry as g}from"@polotno/core/line-head";import{Portal as m}from"react-konva-utils";import{useAnchorSnap as f}from"./use-transformer-snap.js";import{useWorkspaceStyle as p}from"./workspace-style.js";import{isTouchDevice as x}from"../utils/screen.js";import{roundedGeometryAttrs as u,roundedPosition as y}from"../utils/px.js";export function getMiddlePoints(t){const{x:e,y:o,width:r,height:a,rotation:n}=t.a,i=n*Math.PI/180,s={x:e+a/2*Math.cos(i+Math.PI/2),y:o+a/2*Math.sin(i+Math.PI/2)};return{middleLeft:s,middleRight:{x:s.x+r*Math.cos(i),y:s.y+r*Math.sin(i)}}}export function getLinePositionFromMiddlePoints(t,e,o){const r=Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),a=Math.atan2(e.y-t.y,e.x-t.x),n=180*a/Math.PI;return{x:t.x+Math.cos(a-Math.PI/2)*o/2,y:t.y+Math.sin(a-Math.PI/2)*o/2,width:r,height:o,rotation:n}}const w=(t,e,o)=>{const r=o.anchorSize;return{offsetX:r/2/t,offsetY:r/2/t,width:r/t,height:r/t,cornerRadius:o.anchorCornerRadius/t,fill:o.anchorFill,stroke:o.anchorStroke,strokeWidth:o.anchorStrokeWidth,strokeScaleEnabled:!1,draggable:!0,dragDistance:0,onMouseEnter:t=>{t.target.getStage().container().style.cursor="crosshair"},onMouseLeave:t=>{t.target.getStage().container().style.cursor=""},onDragStart:t=>{e.history.startTransaction()},onDragEnd:t=>{e.history.endTransaction()}}},M=n(({element:e,type:o})=>{const a=c(e,e.a.color,"fill"),n=c(e,e.a.color,"stroke"),i=g(o,e.height);if(!i){return null}const l=Object.assign(Object.assign(Object.assign({strokeWidth:e.height,lineCap:"round",lineJoin:"round"},n),a),{opacity:r(e),hideInExport:!e.showInExport});return"circle"===i.kind?t(h,Object.assign({x:i.cx,y:i.cy,radius:i.r},l)):t(s,Object.assign({points:i.points.flat(),closed:i.filled},l))});export const LineElement=n(({element:n,store:h})=>{const{transformerStyle:g}=p(),b=a.useRef(null),v=a.useRef(null),j=a.useRef(null),k=n.selectable||h.role===d.ADMIN,E=x(),O=h.selectedElements.indexOf(n)>=0&&1===h.selectedElements.length,P=n.isSelected,{middleLeft:I,middleRight:S}=getMiddlePoints(n);f(v,[b],[O]),f(j,[b],[O]);const D=c(n,n.a.color,"stroke");return e(o,{children:[t(s,Object.assign({ref:b,name:"element",id:n.id,x:n.a.x,y:n.a.y,points:[0,0,n.a.width,0],offsetY:-n.a.height/2,strokeWidth:n.a.height,hitStrokeWidth:Math.max(n.a.height,20),dash:n.dash.map(t=>t*n.a.height)},D,{rotation:n.a.rotation,opacity:r(n),shadowEnabled:n.shadowEnabled,shadowBlur:n.shadowBlur,shadowOffsetX:n.shadowOffsetX,shadowOffsetY:n.shadowOffsetY,shadowColor:n.shadowColor,shadowOpacity:n.shadowOpacity,listening:k,draggable:E?n.draggable&&P:n.draggable,preventDefault:!E||P,hideInExport:!n.showInExport,onDragMove:t=>{n.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{n.set(y(t.target))},onTransform:t=>{const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),n.set({height:n.height*e,width:n.width*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()})},onTransformEnd:()=>{n.set(u(n))}})),t(i,{x:I.x,y:I.y,rotation:n.rotation,hideInExport:!n.showInExport,onClick:t=>{var e;return null===(e=b.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=b.current)||void 0===e?void 0:e.fire("tap",t)},children:t(M,{element:n,type:n.startHead})}),t(i,{x:S.x,y:S.y,rotation:n.rotation+180,onClick:t=>{var e;return null===(e=b.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=b.current)||void 0===e?void 0:e.fire("tap",t)},children:t(M,{element:n,type:n.endHead})}),O&&n.resizable&&t(m,{selector:".page-abs-container",enabled:!0,children:e(i,{visible:O,children:[t(l,Object.assign({x:I.x,y:I.y,ref:v,name:"line-anchor"},w(h.scale,h,g),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(t.target.position(),S,n.height);n.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}})),t(l,Object.assign({x:S.x,y:S.y,ref:j,name:"line-anchor"},w(h.scale,h,g),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(I,t.target.position(),n.height);n.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}}))]})})]})});
|