polotno 3.17.0 → 3.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/blueprint/side-panel/select-video.d.ts +18 -16
- package/canvas/animated-shadow.d.ts +11 -0
- package/canvas/animated-shadow.js +1 -0
- package/canvas/apply-filters.d.ts +5 -1
- package/canvas/apply-filters.js +1 -1
- package/canvas/downscale.js +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.d.ts +1 -0
- package/canvas/image-element.js +1 -1
- package/canvas/line-element.js +1 -1
- package/canvas/page.js +1 -1
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.d.ts +2 -0
- package/canvas/rich-editor.js +8 -5
- package/canvas/text-element.js +1 -1
- package/canvas/use-animated-blur.d.ts +4 -0
- package/canvas/use-animated-blur.js +1 -0
- package/canvas/use-color.d.ts +1 -1
- package/canvas/use-filtered-source.js +1 -1
- package/canvas/use-offscreen-canvas.d.ts +2 -0
- package/canvas/use-offscreen-canvas.js +1 -0
- package/canvas/use-transformer-snap.js +1 -1
- package/canvas/video-element.js +1 -1
- package/icons/animation-icons.d.ts +30 -7
- package/icons/animation-icons.js +1 -1
- package/icons/registry.d.ts +22 -0
- package/icons/registry.js +1 -1
- package/image-tools/raster.js +1 -1
- package/model/figure-model.d.ts +10 -9
- package/model/gif-model.d.ts +10 -9
- package/model/group-model.d.ts +78 -68
- package/model/group-model.js +1 -1
- package/model/history.d.ts +3 -0
- package/model/history.js +1 -1
- package/model/image-model.d.ts +10 -9
- package/model/line-model.d.ts +10 -9
- package/model/node-model.d.ts +8 -8
- package/model/page-model.d.ts +228 -204
- package/model/shape-model.d.ts +15 -9
- package/model/shape-model.js +1 -1
- package/model/store.d.ts +31 -17
- package/model/store.js +1 -1
- package/model/svg-model.d.ts +9 -8
- package/model/table-model.d.ts +8 -7
- package/model/text-model.d.ts +9 -8
- package/model/video-model.d.ts +10 -9
- package/package.json +24 -21
- package/pages-timeline/element-track.js +1 -1
- package/pages-timeline/page-preview.js +1 -1
- package/polotno.bundle.js +195 -177
- package/side-panel/animations-panel.js +1 -1
- package/side-panel/select-video.d.ts +18 -16
- package/side-panel/text-panel.js +1 -1
- package/side-panel/text-template.js +1 -1
- package/toolbar/animations-picker.js +1 -1
- package/toolbar/use-copy-style.d.ts +1 -0
- package/toolbar/use-copy-style.js +1 -1
- package/ui.css +1 -1
- package/utils/animations.d.ts +212 -10
- package/utils/animations.js +1 -1
- package/utils/clipboard.d.ts +17 -15
- package/utils/clipboard.js +1 -1
- package/utils/duplicate.js +1 -1
- package/utils/fonts.js +1 -1
- package/utils/from-svg.js +1 -1
- package/utils/image-dpi.js +1 -1
- package/utils/image-edit.js +1 -1
- package/utils/l10n.d.ts +44 -0
- package/utils/l10n.js +1 -1
- package/utils/px.d.ts +1 -0
- package/utils/px.js +1 -1
- package/utils/timeline.d.ts +3 -1
- package/utils/timeline.js +1 -1
- package/utils/validate-key.js +1 -1
|
@@ -15,7 +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" | "
|
|
18
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
19
19
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
20
20
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
21
21
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -36,6 +36,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
36
36
|
type: import("mobx-state-tree").ISimpleType<"exit" | "enter" | "loop">;
|
|
37
37
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
38
38
|
data: import("mobx-state-tree").IType<{} | null | undefined, {}, {}>;
|
|
39
|
+
easing: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<string>>;
|
|
39
40
|
}, {}, import("mobx-state-tree")._NotCustomized, import("mobx-state-tree")._NotCustomized>>;
|
|
40
41
|
blurEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
41
42
|
blurRadius: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
@@ -92,7 +93,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
92
93
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
93
94
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
94
95
|
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" | "
|
|
96
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
96
97
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
97
98
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
98
99
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -104,7 +105,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
104
105
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
105
106
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
106
107
|
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" | "
|
|
108
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
108
109
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
109
110
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
110
111
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -116,7 +117,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
116
117
|
type: string | undefined;
|
|
117
118
|
name: string | undefined;
|
|
118
119
|
opacity: number | undefined;
|
|
119
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
120
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light" | undefined;
|
|
120
121
|
custom: any;
|
|
121
122
|
visible: boolean | undefined;
|
|
122
123
|
selectable: boolean | undefined;
|
|
@@ -135,7 +136,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
135
136
|
alwaysOnTop: boolean;
|
|
136
137
|
showInExport: boolean;
|
|
137
138
|
} | {
|
|
138
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
139
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
139
140
|
id: string;
|
|
140
141
|
type: string;
|
|
141
142
|
name: string;
|
|
@@ -163,7 +164,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
163
164
|
alwaysOnTop: boolean;
|
|
164
165
|
showInExport: boolean;
|
|
165
166
|
} | {
|
|
166
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
167
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
167
168
|
id: string;
|
|
168
169
|
type: string;
|
|
169
170
|
name: string;
|
|
@@ -198,7 +199,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
198
199
|
readonly isSelected: boolean;
|
|
199
200
|
readonly isSelectedDirectly: boolean;
|
|
200
201
|
} & {
|
|
201
|
-
setAnimation(type:
|
|
202
|
+
setAnimation(type: "enter" | "exit" | "loop", attrs: import("../../utils/animations.js").AnimationAttrs): void;
|
|
202
203
|
setFilter(type: any, value: any): void;
|
|
203
204
|
} & {
|
|
204
205
|
_setSourceSize(size: import("../../image-tools/types.js").Size, src: string): void;
|
|
@@ -222,7 +223,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
222
223
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
223
224
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
224
225
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
225
|
-
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" | "
|
|
226
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
226
227
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
227
228
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
228
229
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -243,6 +244,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
243
244
|
type: import("mobx-state-tree").ISimpleType<"exit" | "enter" | "loop">;
|
|
244
245
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
245
246
|
data: import("mobx-state-tree").IType<{} | null | undefined, {}, {}>;
|
|
247
|
+
easing: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<string>>;
|
|
246
248
|
}, {}, import("mobx-state-tree")._NotCustomized, import("mobx-state-tree")._NotCustomized>>;
|
|
247
249
|
blurEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
248
250
|
blurRadius: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
@@ -299,7 +301,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
299
301
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
300
302
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
301
303
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
302
|
-
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" | "
|
|
304
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
303
305
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
304
306
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
305
307
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -311,7 +313,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
311
313
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
312
314
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
313
315
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
314
|
-
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" | "
|
|
316
|
+
blendMode: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree").ISimpleType<"normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light">, [undefined]>;
|
|
315
317
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
316
318
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
317
319
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -323,7 +325,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
323
325
|
type: string | undefined;
|
|
324
326
|
name: string | undefined;
|
|
325
327
|
opacity: number | undefined;
|
|
326
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
328
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light" | undefined;
|
|
327
329
|
custom: any;
|
|
328
330
|
visible: boolean | undefined;
|
|
329
331
|
selectable: boolean | undefined;
|
|
@@ -342,7 +344,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
342
344
|
alwaysOnTop: boolean;
|
|
343
345
|
showInExport: boolean;
|
|
344
346
|
} | {
|
|
345
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
347
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
346
348
|
id: string;
|
|
347
349
|
type: string;
|
|
348
350
|
name: string;
|
|
@@ -370,7 +372,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
370
372
|
alwaysOnTop: boolean;
|
|
371
373
|
showInExport: boolean;
|
|
372
374
|
} | {
|
|
373
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
375
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
374
376
|
id: string;
|
|
375
377
|
type: string;
|
|
376
378
|
name: string;
|
|
@@ -405,7 +407,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
405
407
|
readonly isSelected: boolean;
|
|
406
408
|
readonly isSelectedDirectly: boolean;
|
|
407
409
|
} & {
|
|
408
|
-
setAnimation(type:
|
|
410
|
+
setAnimation(type: "enter" | "exit" | "loop", attrs: import("../../utils/animations.js").AnimationAttrs): void;
|
|
409
411
|
setFilter(type: any, value: any): void;
|
|
410
412
|
} & {
|
|
411
413
|
_setSourceSize(size: import("../../image-tools/types.js").Size, src: string): void;
|
|
@@ -429,7 +431,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
429
431
|
type: string | undefined;
|
|
430
432
|
name: string | undefined;
|
|
431
433
|
opacity: number | undefined;
|
|
432
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
434
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light" | undefined;
|
|
433
435
|
custom: any;
|
|
434
436
|
visible: boolean | undefined;
|
|
435
437
|
selectable: boolean | undefined;
|
|
@@ -448,7 +450,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
448
450
|
alwaysOnTop: boolean;
|
|
449
451
|
showInExport: boolean;
|
|
450
452
|
} | {
|
|
451
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
453
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
452
454
|
id: string;
|
|
453
455
|
type: string;
|
|
454
456
|
name: string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AnimatedAttrs } from '../model/shape-model.js';
|
|
2
|
+
export declare const animatedShadow: ({ a }: {
|
|
3
|
+
a: AnimatedAttrs;
|
|
4
|
+
}) => {
|
|
5
|
+
shadowEnabled: any;
|
|
6
|
+
shadowBlur: any;
|
|
7
|
+
shadowOffsetX: any;
|
|
8
|
+
shadowOffsetY: any;
|
|
9
|
+
shadowColor: any;
|
|
10
|
+
shadowOpacity: any;
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{toDisplayColor as o}from"@polotno/core/color";export const animatedShadow=({a})=>({shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:o(a.shadowColor),shadowOpacity:a.shadowOpacity});
|
|
@@ -2,7 +2,11 @@ import Konva from 'konva';
|
|
|
2
2
|
import { ShapeType } from '../model/shape-model.js';
|
|
3
3
|
import { Filter } from 'konva/lib/Node';
|
|
4
4
|
export { getEffectiveBlurRadius, getElementBlurRadius, } from '@polotno/core/filters';
|
|
5
|
-
|
|
5
|
+
type Blur = {
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
radius: number;
|
|
8
|
+
};
|
|
9
|
+
export declare function getFilterAttrs(element: ShapeType, blur?: Blur): {
|
|
6
10
|
filters: Filter[];
|
|
7
11
|
brightness?: number;
|
|
8
12
|
blurRadius?: number;
|
package/canvas/apply-filters.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import e from"konva";import{isAlive as t}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(t,r={enabled:t.blurEnabled,radius:t.blurRadius}){const i={filters:[]};return t.brightnessEnabled&&(i.filters.push(e.Filters.Brighten),i.brightness=t.brightness),r.enabled&&(i.filters.push(e.Filters.Blur),i.blurRadius=r.radius),t.sepiaEnabled&&i.filters.push(e.Filters.Sepia),t.grayscaleEnabled&&i.filters.push(e.Filters.Grayscale),t.filters.forEach((e,t)=>{const r=n[t];s(t)&&i.filters.push(r(e.intensity))}),i}export function hasActiveFilters(e){return l(e,e.filters.keys())}export function applyFiltersToCanvas(e,i){if(!t(i)){return}const s=getFilterAttrs(i);if(!s.filters.length||!e.width||!e.height){return}const l=e.getContext("2d");if(!l){return}const n=l.getImageData(0,0,e.width,e.height),a={brightness:()=>s.brightness,blurRadius:()=>{var t;return r(null!==(t=s.blurRadius)&&void 0!==t?t:0,e.width,e.height)}};s.filters.forEach(e=>{"function"==typeof e&&e.call(a,n)}),l.putImageData(n,0,0)}export function getFiltersDepsKey(e){if(!t(e)){return""}const r=[];return e.filters.forEach((e,t)=>{r.push(t+":"+e.intensity)}),[e.brightnessEnabled&&e.brightness,e.blurEnabled&&e.blurRadius,e.sepiaEnabled,e.grayscaleEnabled,...r].join("|")}export function applyFilter(e,r){var i;if(!e||!t(r)){return}const s={enabled:r.a.blurEnabled,radius:r.a.blurRadius},l=getFilterAttrs(r,s),n={};s.enabled&&"text"===r.type&&r.lineHeight<1&&(n.offset=r.fontSize),e.setAttrs(l),l.filters.length?e.cache(Object.assign(Object.assign({},n),{pixelRatio:r.store._elementsPixelRatio})):e.clearCache(),null===(i=e.getLayer())||void 0===i||i.batchDraw()}
|
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";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.
|
|
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.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.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as e,Fragment as t,jsxs as o}from"react/jsx-runtime";import{createElement as r}from"react";import n from"react";import{observer as i}from"mobx-react-lite";import{ROLES as s}from"../model/store.js";import l from"konva";import{MAIN_TRANSFORMER_NAME as a}from"../utils/px.js";import{TextElement as m}from"./text-element.js";import{ImageElement as d}from"./image-element.js";import{HTMLElement as
|
|
1
|
+
import{jsx as e,Fragment as t,jsxs as o}from"react/jsx-runtime";import{createElement as r}from"react";import n from"react";import{observer as i}from"mobx-react-lite";import{ROLES as s}from"../model/store.js";import l from"konva";import{MAIN_TRANSFORMER_NAME as a}from"../utils/px.js";import{TextElement as m}from"./text-element.js";import{ImageElement as d}from"./image-element.js";import{HTMLElement as c}from"./html-element.js";import{RenderTagElement as f}from"./render-tag-element.js";import{LineElement as u}from"./line-element.js";import{VideoElement as p}from"./video-element.js";import{FigureElement as g}from"./figure-element.js";import{GifElement as v}from"./gif-element.js";import{TableElement as h}from"./table-element.js";import{Group as x}from"react-konva";import{Highlighter as y}from"./highlighter.js";import{PlacedNode as b}from"./placed-node.js";import{forEveryChild as j}from"../model/group-model.js";import{flags as w}from"../utils/flags.js";import{blendModeOf as E,isIsolatedGroup as T}from"@polotno/core/page";const M={text:m,image:d,svg:d,line:u,video:p,figure:g,group:i(t=>{var o;const{element:n,store:i}=t,l=n,{children:a}=l,m=n.selectable||i.role===s.ADMIN,d=l.a;return e(b,{as:x,element:l,offsetX:d.offsetX,offsetY:d.offsetY,scaleX:d.scaleX,scaleY:d.scaleY,opacity:d.opacity,isolated:T(l),globalCompositeOperation:null!==(o=E(l))&&void 0!==o?o:"source-over",listening:m,hideInExport:!n.showInExport,id:n.id,clipFunc:d.wipe&&l._restingBbox?k(l._restingBbox,d.wipe):void 0,children:a.map(e=>r(U,Object.assign({},t,{key:e.id,store:i,element:e})))})}),gif:v,table:h};export function registerShapeComponent(e,t){M[e]=t}const R=(e,t)=>{const o=[];j(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 l.Transform;o.translate(e.x,e.y),o.rotate(l.Util.degToRad(e.rotation)),t.forEach(e=>{const t=o.point(e);r.push(t)})});const n=new l.Transform;n.rotate(-l.Util.degToRad(t));let i=1/0,s=1/0,a=-1/0,m=-1/0;r.forEach(e=>{const t=n.point(e);i=Math.min(i,t.x),s=Math.min(s,t.y),a=Math.max(a,t.x),m=Math.max(m,t.y)}),n.invert();const d=n.point({x:i,y:s});return{x:d.x,y:d.y,width:a-i,height:m-s,rotation:t}},S=".polotnoElementHover",I=new WeakMap;const O=1e6,k=(e,t)=>o=>{var r;const n="x"===t.axis?e.width:e.height,i=t.from<=0?-O:t.from*n,s=t.to>=1?O:t.to*n,[a,m,d,c]="x"===t.axis?[i,s,-O,O]:[-O,O,i,s];o.save(),o.translate(e.x,e.y),o.rotate(l.Util.degToRad(null!==(r=e.rotation)&&void 0!==r?r:0)),o.rect(a,d,m-a,c-d),o.restore()},C=(e,t)=>{const o=l.Util.degToRad(e.rotation);return(new l.Transform).translate(e.x,e.y).rotate(o).translate(e.width/2,0).scale(t,1).translate(-e.width/2,0).rotate(-o).translate(-e.x,-e.y).decompose()},U=i(r=>{var i,s;const{element:l,store:m}=r,[d,u]=n.useState(!1),p="group"===(null===(i=l.parent)||void 0===i?void 0:i.type),g="group"!==l.type||p?null:m.selectedElements,v=!!g&&1===g.length&&g[0]!==l&&g[0].top===l,[h,b]=n.useState(!1),j=n.useRef(null),T=l.visible,O=()=>{var e,t;return null!==(t=null===(e=j.current)||void 0===e?void 0:e.getStage())&&void 0!==t?t:null},U=n.useRef(void 0),X=()=>{var e;return U.current||(U.current=null===(e=O())||void 0===e?void 0:e.findOne(`.${a}`)),U.current};n.useEffect(()=>{const e=O();if(!e){return}const t=function(e,t,o,r){var n;const{subscribers:i}=function(e,t){const o=I.get(e);if(o){return o}const r={subscribers:new Map},n=e=>{var t,o;const n=r.hoveredId;r.hoveredId=e,n&&n!==e&&(null===(t=r.subscribers.get(n))||void 0===t||t.forEach(e=>e(!1))),e&&(null===(o=r.subscribers.get(e))||void 0===o||o.forEach(e=>e(!0)))};return e.on(`mouseover${S}`,e=>{var o,r;const i=e.target.findAncestor(".element",!0);n(null===(r=null===(o=t.getElementById(null==i?void 0:i.id()))||void 0===o?void 0:o.top)||void 0===r?void 0:r.id)}),e.on(`mouseleave${S}`,()=>n(void 0)),I.set(e,r),r}(e,t),s=null!==(n=i.get(o))&&void 0!==n?n:new Set;return i.set(o,s),s.add(r),()=>{s.delete(r),s.size||(i.delete(o),i.size||(e.off(S),I.delete(e)))}}(e,m,l.id,u);return()=>{t(),u(!1)}},[T,l.id]),n.useEffect(()=>{if(!v){return}const e=O(),t=X();if(!e||!t){return}b(!1);const o=()=>b(!0),r=()=>b(!1);return e.on("dragstart",o),e.on("dragend",r),t.on("transformstart",o),t.on("transformend",r),()=>{e.off("dragstart",o),e.off("dragend",r),t.off("transformstart",o),t.off("transformend",r)}},[T,v]);let Y=M[r.element.type];if("text"===r.element.type&&(w.renderTagTextEnabled?Y=f:w.htmlRenderEnabled&&(Y=c)),!T){return null}if(!Y){return console.error("Can not find component for "+r.element.type),null}const B="group"===l.type?void 0:E(l),F="group"===l.type?null:l.a,$=null==F?void 0:F.wipe,z=null==F?void 0:F.flipScale;return o(t,{children:[e(x,Object.assign({name:"element-container",ref:j,visible:l.active,listening:l.active,isolated:!!B,opacity:B?l.a.opacity:1,globalCompositeOperation:null!=B?B:"source-over",clipFunc:$?k(F,$):void 0},void 0===z?null:C(F,z),{children:e(Y,Object.assign({},r))})),((d||v)&&!p||l.isHighlighted)&&l.active&&!l.isSelectedDirectly&&!h&&e(y,{element:"group"===l.type?{a:R(l,v?"rotation"in g[0]?g[0].rotation:0:d&&(null===(s=X())||void 0===s?void 0:s.rotation())||0)}:l})]})});export default U;
|
package/canvas/figure-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as e,Fragment as t,jsxs as r}from"react/jsx-runtime";import a from"react";import{
|
|
1
|
+
import{jsx as e,Fragment as t,jsxs as r}from"react/jsx-runtime";import a from"react";import{animatedShadow as o}from"./animated-shadow.js";import{observer as s}from"mobx-react-lite";import{Group as i,Rect as n,Shape as h}from"react-konva";import{ROLES as l}from"../model/store.js";import c from"konva";import{getFigureFillClip as d,subTypeToPathDataFunc as m}from"@polotno/core/figure-to-svg";import{isOpaquePaint as g}from"@polotno/core/gradient";import{isTouchDevice as p}from"../utils/screen.js";import{activeAnchorOf as f,isCornerAnchor as u,scaledStyleAttrs as b,roundedStyleAttrs as k,roundedGeometryAttrs as w,roundedPosition as j,transformScale as x}from"../utils/px.js";import{useColor as y}from"./use-color.js";import{PlacedNode as v}from"./placed-node.js";import{elementOpacity as P,paintOpacity as T}from"./paint-opacity.js";const M=(e,t)=>{e.beginPath();for(const{command:r,points:a}of t){switch(r){case"L":e.lineTo(a[0],a[1]);break;case"M":e.moveTo(a[0],a[1]);break;case"C":e.bezierCurveTo(a[0],a[1],a[2],a[3],a[4],a[5]);break;case"Q":e.quadraticCurveTo(a[0],a[1],a[2],a[3]);break;case"A":e.ellipse(a[0],a[1],a[2],a[3],a[6],a[4],a[4]+a[5],!a[7]);break;case"z":e.closePath()}}},O={};export const FigureElement=s(({element:S,store:E})=>{const R=a.useRef(null),D=a.useRef(!1),W=S.selectable||E.role===l.ADMIN,C=p(),F=Math.min(S.strokeWidth,S.width/2,S.height/2),I=y(S,S.fill,"fill"),z=Object.assign(Object.assign(Object.assign({width:S.a.width,height:S.a.height},I),o(S)),{preventDefault:!C||S.isSelected,hideInExport:!S.showInExport}),A=y(S,S.stroke,"stroke"),X=Object.assign(Object.assign({},A),{strokeWidth:2*F,dash:S.dash.map(e=>e*F)}),Y=Math.max(0,10-Math.min(S.a.width,S.a.height)),q=d({subType:S.subType,width:S.a.width,height:S.a.height,cornerRadius:S.cornerRadius,strokeWidth:F,stroke:S.stroke,dash:S.dash,opacity:T(S)}),L=!S.a.shadowEnabled||g(S.fill)?q:null,N=(Q=S.subType,O[Q]||(O[Q]=(B=m(Q),s(({element:o,fillProps:s,strokeProps:i,hitStrokeWidth:l,fillClip:d})=>{const m=B({width:o.a.width,height:o.a.height,cornerRadius:o.cornerRadius}),g=a.useMemo(()=>c.Path.parsePathData(m),[m]),p=a.useMemo(()=>d?c.Path.parsePathData(d):null,[d]),f=!!p;return r(t,{children:[e(n,{width:o.a.width,height:o.a.height,fill:"transparent",hitStrokeWidth:l}),e(h,Object.assign({},s,i,{perfectDrawEnabled:f,shadowForStrokeEnabled:f,selfRectFunc:e=>({x:0,y:0,width:e.width(),height:e.height()}),sceneFunc:(e,t)=>{e.save(),p?(M(e,g),e.clip(),M(e,p),e.clip()):M(e,g),e.fillShape(t),e.restore(),e.save(),M(e,g),e.clip(),e.strokeShape(t),e.restore()}}))]})}))),O[Q]||i);var Q,B;return e(t,{children:e(v,{as:i,nodeRef:R,element:S,id:S.id,opacity:P(S),listening:W,draggable:C?S.draggable&&S.isSelected:S.draggable,name:"element",onDragMove:e=>{S.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{S.set(j(e.target))},onTransformStart:e=>{D.current=u(f(e.target))},onTransform:e=>{const t={x:x(e.target.scaleX()),y:x(e.target.scaleY())};e.target.scaleX(1),e.target.scaleY(1);const r=D.current?Math.min(t.x,t.y):1;S.set(Object.assign({width:S.width*t.x,height:S.height*t.y,x:e.target.x(),y:e.target.y(),rotation:e.target.rotation()},1!==r&&b(S,r)))},onTransformEnd:()=>{S.set(Object.assign(Object.assign({},w(S)),k(S)))},children:e(N,{element:S,fillProps:z,strokeProps:X,hitStrokeWidth:Y,fillClip:L})})})});
|
package/canvas/gif-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as e,jsxs as t,Fragment as
|
|
1
|
+
import{jsx as e,jsxs as t,Fragment as i}from"react/jsx-runtime";import{elementOpacity as r}from"./paint-opacity.js";import o from"react";import{toDisplayColor as n}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as s}from"mobx-react-lite";import{nodeAutorun as d}from"../utils/mobx.js";import{Image as l,Group as h,Rect as m,Arc as c,Text as f}from"react-konva";import{ROLES as g}from"../model/store.js";import u from"konva";import{parseGIF as p,decompressFrames as w}from"gifuct-js";import{useClip as x,useCornerRadiusAndCrop as v}from"./video-element.js";import{useImageLoader as y}from"./image-element.js";import{fetchWithRetry as j}from"@polotno/core/retry";import{useWorkspaceStyle as b}from"./workspace-style.js";import{useFilteredSource as E}from"./use-filtered-source.js";import{useOffscreenCanvas as I}from"./use-offscreen-canvas.js";import{useFadeIn as S}from"./use-fadein.js";import{useAnimatedBlur as R}from"./use-animated-blur.js";import{isTouchDevice as M}from"../utils/screen.js";import{activeAnchorOf as T,isCornerAnchor as z,scaledStyleAttrs as D,roundedStyleAttrs as k,roundedGeometryAttrs as C,roundedPosition as F,transformScale as O}from"../utils/px.js";import{useEvent as _}from"../hooks/use-event.js";import{PlacedNode as A}from"./placed-node.js";import{refCountedCache as P}from"../utils/ref-counted.js";async function X(e){const t=await j(e),i=await t.arrayBuffer(),r=p(i),o=w(r,!0).map(e=>({patch:new Uint8ClampedArray(e.patch),delay:e.delay,width:e.dims.width,height:e.dims.height,left:e.dims.left,top:e.dims.top,disposalType:e.disposalType}));return{frames:o,duration:o.reduce((e,t)=>e+t.delay,0),dimensions:{width:r.lsd.width,height:r.lsd.height}}}const Y=P(),G=[],L={width:0,height:0};function W(e,t,i){const r=t.getContext("2d"),o=i.getContext("2d");if(!r||!o){return}2===e.disposalType&&r.clearRect(0,0,t.width,t.height),i.width=e.width,i.height=e.height;const n=o.createImageData(e.width,e.height);n.data.set(e.patch),o.putImageData(n,0,0),r.drawImage(i,e.left,e.top)}const q=s(({element:i})=>{const n=Math.min(30,i.width/4,i.height/4),a=o.useRef(null);o.useEffect(()=>{const e=a.current;if(!e){return}const t=new u.Animation(t=>{e.rotate(((null==t?void 0:t.timeDiff)||0)/2)},e.getLayer());return t.start(),()=>{t.stop()}},[]);const{mediaLoadingStyle:s}=b();return t(h,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,listening:!1,opacity:r(i),hideInExport:!i.showInExport,children:[e(m,{width:i.width,height:i.height,fill:s.fill}),e(c,{ref:a,x:i.width/2,y:i.height/2,fill:s.textFill,outerRadius:Math.abs(n),innerRadius:Math.max(1,n-5),angle:270})]})}),B=s(({element:i})=>{const{mediaErrorStyle:o}=b(),n=Math.max(10,Math.min(30,i.width/22));return t(h,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,listening:!1,opacity:r(i),hideInExport:!i.showInExport,children:[e(m,{width:i.width,height:i.height,fill:o.fill}),e(f,{text:"Cannot load the GIF...",fontSize:n,width:i.width,height:i.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});export const GifElement=s(({element:s,store:h})=>{var c,f;const[u,p]=o.useState(!1),w=o.useRef(!1),j=M(),b=o.useRef(null),[P,H,N,U]=function(e){var t,i,r;const[{gif:n,failed:a},s]=o.useState(()=>{var t;return{gif:null===(t=Y.get(e))||void 0===t?void 0:t.value,failed:!1}});o.useEffect(()=>{const[t,i]=function(e){const[t,i]=Y.acquire(e,()=>{const t={decoded:X(e)};return t.decoded.then(e=>{t.value=e},()=>Y.remove(e,t)),t});return[t.decoded,i]}(e);let r=!0;return t.then(e=>{r&&s({gif:e,failed:!1})},e=>{r&&(console.error("Failed to load GIF:",e),s({gif:void 0,failed:!0}))}),()=>{r=!1,i()}},[e]);const d=n?"loaded":a?"failed":"loading";return[null!==(t=null==n?void 0:n.frames)&&void 0!==t?t:G,null!==(i=null==n?void 0:n.duration)&&void 0!==i?i:0,null!==(r=null==n?void 0:n.dimensions)&&void 0!==r?r:L,d]}(s.src);y(U,s.src,s.id);const J=e=>{e.width=N.width,e.height=N.height,W(P[0],e,document.createElement("canvas"))},K=I(P.length>0,J,[P,N]);o.useEffect(()=>{var e;if(!K){return}const t=document.createElement("canvas");J(K),le();let i=0;const r=e=>{const r=(e=>{const t=e%H;let i=0;for(let r=0;r<P.length;r++){if(i+=P[r].delay,i>t){return r}}return 0})(e);r!==i&&(W(P[r],K,t),le(),b.current.getLayer().draw(),i=r)};if(h.isPlaying||(null===(e=s.page)||void 0===e?void 0:e._exportingOrRendering)){return d(s,()=>{r(h.currentTime-s.page.startTime)})}{const e=window.setInterval(()=>{r(h.currentTime||performance.now())},16);return()=>{clearInterval(e)}}},[h.isPlaying,P,H,null===(c=s.page)||void 0===c?void 0:c._exportingOrRendering]),o.useEffect(()=>{H&&h.history.ignore(()=>{s.set({duration:H})})},[H]);let{cropX:Q,cropY:V,cropWidth:Z,cropHeight:$}=s.a;"loaded"!==U&&(Q=V=0,Z=$=1);const ee={x:N.width*Q,y:N.height*V,width:N.width*Z,height:N.height*$},te=null!==(f=s.cornerRadius)&&void 0!==f?f:0,[ie,re]=E(s,K),[oe,ne]=v(s,ie,ee,h._elementsPixelRatio,te,u||s._cropModeEnabled),[ae,se]=x(s,oe,h._elementsPixelRatio,[ee,te]),de=R(b,s,ae),le=_(()=>{re(),ne(),se(),de()}),he="loading"===U,me="failed"===U,ce=he||me?0:r(s);S(b,ce);const fe=s.selectable||h.role===g.ADMIN;return t(i,{children:[he&&e(q,{element:s}),me&&e(B,{element:s}),e(A,Object.assign({as:l,nodeRef:b,element:s,name:"element",id:s.id,image:ae,width:s.a.width||1,height:s.a.height||1,opacity:ce},a(s),{customCrop:ee,listening:fe,draggable:j?s.draggable&&s.isSelected:s.draggable,preventDefault:!j||s.isSelected,hideInExport:!s.showInExport,onDragMove:e=>{s.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{s.set(F(e.target))},onTransformStart:e=>{p(!0),w.current=z(T(e.target))},onTransform:e=>{const t=e.currentTarget,i=O(t.scaleX()),r=O(t.scaleY());t.scaleX(1),t.scaleY(1);const o=w.current?Math.min(i,r):1;s.set(Object.assign({x:t.x(),y:t.y(),width:t.width()*i,height:t.height()*r,rotation:t.rotation()},1!==o&&D(s,o)))},onTransformEnd:()=>{s.set(Object.assign(Object.assign({},C(s)),k(s))),p(!1)}})),!!s.borderSize&&e(A,{as:m,element:s,width:Math.max(s.a.width-s.borderSize,0),height:Math.max(s.a.height-s.borderSize,0),opacity:ce,offsetX:-s.borderSize/2,offsetY:-s.borderSize/2,stroke:n(s.borderColor),strokeWidth:s.borderSize,listening:!1,cornerRadius:Math.max(0,te-s.borderSize),hideInExport:!s.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{toDisplayColor as i}from"@polotno/core/color";import{elementOpacity as n}from"./paint-opacity.js";import{observer as a}from"mobx-react-lite";import{Group as s,Image as l,Path as c,Rect as d}from"react-konva";import{activeAnchorOf as f,scaledFontSizeAttrs as h,roundedFontSizeAttrs as u,roundedTextDecorationAttrs as g,roundedGeometryPx as m,roundedPosition as p,textMinResizeWidth as x,transformScale as b}from"../utils/px.js";import y from"konva";import{isAlive as w}from"mobx-state-tree";import{nodeAutorun as S}from"../utils/mobx.js";import{flags as E}from"../utils/flags.js";import{textOverflowOf as v}from"../utils/text-sizing.js";import{layoutTextSizing as z,legacyTextOverflow as k,renderTagLayoutAt as O,resolveTextSizing as j}from"@polotno/core/text-sizing";import{applyFilter as F}from"./apply-filters.js";import{getLineHeight as M,useFontFamiliesLoader as R,usePrevious as Y}from"./text-element.js";import{getCurvePath as X}from"@polotno/core/text-html";import{useColor as C,withDisplayTextPaints as I}from"./use-color.js";import{useWorkspaceStyle as A}from"./workspace-style.js";import{detectSize as P,detectLineRects as B,htmlToCanvas as T}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as W}from"@polotno/core/background-shape";import{useFadeIn as $}from"./use-fadein.js";import{Html as H}from"react-konva-utils";import{incrementLoader as _,triggerLoadError as D}from"../utils/loader.js";import{removeTags as L}from"@polotno/core/text";import{cssFontFamily as J}from"@polotno/core/fonts";import{shadowCastByBackground as N}from"@polotno/core/filters";import*as q from"../utils/fonts.js";import{isTouchDevice as V}from"../utils/screen.js";import{getLimitedFontSize as G}from"./text-element/max-font-size.js";import{AutoDeleteEmptyText as K}from"./text-element/auto-delete-empty-text.js";import{useDelayer as Q}from"./use-delayer.js";import{getHtml as U,textLayoutWidth as Z}from"@polotno/core/text-html";import{hasMeasuredHeight as tt,resolveTextBoxHeight as et}from"@polotno/core/text-types";import{normalizeRichTextHtml as ot,extractFontFamiliesFromHtml as rt,scaleRichTextFontSizesInHtml as it}from"../utils/rich-text-html.js";import{RichEditor as nt,RichTextContainer as at,applyOpacityToColor as st,quillRef as lt,createQuill as ct,setQuillContent as dt,setQuillFormats as ft,syncQuillFormatState as ht,setCursorFromCoords as ut}from"./rich-editor.js";export{lt as quillRef,ct as createQuill,dt as setQuillContent,ft as setQuillFormats,ht as syncQuillFormatState,ut as setCursorFromCoords};function gt(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 mt=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),pt=navigator.userAgent.includes("Firefox");export const HTMLElement=a(({element:a,store:lt})=>{var ct,dt,ft;const{textOverflowIndicatorStyle:ht}=A(),ut=r.useRef(null),[xt,bt]=r.useState(),[yt,wt]=r.useState(!1),[St,Et]=r.useState(!1),vt=r.useRef(a.height),zt=a.fontSize/3,kt=Z(a.a.width,a.letterSpacing,a.fontSize)+2*zt,{textVerticalResizeEnabled:Ot}=E,jt=Y(a.fontFamily),Ft=r.useMemo(()=>ot(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),Mt=r.useMemo(()=>[...new Set([a.fontFamily,...rt(Ft)])].filter(Boolean),[a.fontFamily,Ft]),[Rt]=R(lt,Mt,a.fontStyle,a.fontWeight,a.text||a.placeholder),Yt=a._editModeEnabled,Xt=Yt&&!(null===(ct=a.page)||void 0===ct?void 0:ct._exporting);$(ut);const Ct=Rt?a.fontFamily:jt!==a.fontFamily?jt:"Arial",It=C(a).fill,At=U(I(a),{fontFamily:Ct,color:It,textOverflow:v(a)}),Pt=r.useRef([]),Bt=r.useRef(!0),[,Tt]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Pt.current;t.length>0&&(Pt.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Bt.current=!1;const t=Pt.current;t.length>0&&(Pt.current=[],t.forEach(t=>t()))},[]);let{width:Wt,height:$t}=function(t,e,o){return r.useMemo(()=>P(t),[t,e.width,o])}(At,a,Rt);r.useEffect(()=>{if(!Rt){return}const t=(t,e,o,r)=>{const i=_(`text ${a.id}`),n=()=>{lt.history.ignore(t,e,o).then(()=>{Bt.current?(Pt.current.push(i),Tt()):i()})};r?queueMicrotask(n):n()};if(a.curveEnabled&&Math.abs(Wt-a.width)>1&&!yt&&t(()=>{if(w(a)&&Math.abs(Wt-a.width)>1){const t=a.x+a.width/2;a.set({width:Wt,x:t-Wt/2})}},!1,!0),!tt(a)){return void t(()=>{w(a)&&(tt(a)||a.set({height:$t}))},void 0,void 0,!0)}const e=k(v(a));if("change-font-size"!==e||yt||Yt||a.curveEnabled){"resize"===e&&(Ot&&a.height<$t&&!yt&&t(()=>{w(a)&&a.set({height:$t})},!1,!0),Ot||a.height===$t||yt||t(()=>{w(a)&&a.set({height:$t})},!1,!0))}else{const o=z({element:a,sizing:j({text:a.text,width:a.width,height:a.height,fontSize:a.fontSize,maxLines:a.maxLines,curveEnabled:a.curveEnabled},{textOverflow:e,textSplitAllowed:E.textSplitAllowed}),layoutAt:O(a,{getHtmlOptions:{fontFamily:a.fontFamily}})});o.fontSize!==a.fontSize?t(()=>{if(!w(a)){return}const t=h(a,o.fontSize);o.text!==a.text&&(t.text=o.text),a.set(t)},!1,!0):a.height!==$t&&(Ot&&a.height<$t?t(()=>{w(a)&&a.set({height:$t})},!1,!0):Ot||t(()=>{w(a)&&a.set({height:$t})},!1,!0))}});const Ht=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 T(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),_t=r.useRef(0),Dt=r.useRef(null);r.useEffect(()=>{yt||Xt||(async()=>{_t.current++;const t=_t.current;let e=_(`text ${a.id} ${t}`);Dt.current&&Dt.current(),Dt.current=e,Et(!0);let o=null;const r=mt?5:1;for(let n=0;n<r;n++){const e=n>0?Ht:T;try{if(o=await e({skipCache:n>0,html:At,width:Wt||a.width||1,height:et(a,$t)||1,fontFamily:Ct,fontFamilies:rt(At),resolveFont:t=>lt.fonts.find(e=>e.fontFamily===t)||q.globalFonts.find(e=>e.fontFamily===t),padding:zt,pixelRatio:lt._elementsPixelRatio,font:lt.fonts.find(t=>t.fontFamily===Ct)||q.globalFonts.find(t=>t.fontFamily===Ct)}),t!==_t.current){return}if((mt||pt)&&o&>(o)){await new Promise(t=>setTimeout(t,50*(n+1)));continue}break}catch(i){console.error(i),D(`Error rendering rich text with id ${a.id}`);break}}o?bt(o):e?(e(),e=null):console.error("Finish function is called twice!"),Et(!1)})()},[At,yt,$t,Xt,Ct,a.height,lt._elementsPixelRatio,Rt]);const[Lt,Jt]=Q(St,300),[Nt]=Q(yt,300,!0),qt=Nt||Lt;r.useEffect(()=>{var t;if(!qt){return S(a,()=>{const t=ut.current;F(t,a)})}null===(t=ut.current)||void 0===t||t.clearCache()},[xt,qt,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]),r.useEffect(()=>{xt&&!St&&Dt.current&&(Dt.current(),Dt.current=null)},[xt,St]),r.useLayoutEffect(()=>{if(!Rt){return}if(!a.curveEnabled){return}const t=new y.TextPath({data:X(a.a.width,a.a.height,a.curvePower,a.a.fontSize),text:L(a.text),letterSpacing:a.letterSpacing*a.a.fontSize,fontSize:a.a.fontSize,fontFamily:J(a.fontFamily),fontWeight:a.fontWeight,fontStyle:a.fontStyle,align:"center",textBaseline:"middle",fill:a.fill}),e=t.getSelfRect().width;if(e){const t=e-a.a.width,o=a.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{w(a)&&a.set({width:e,x:a.a.x+r,y:a.a.y+i})})}t.destroy()},[Rt,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),r.useEffect(()=>()=>{Dt.current&&Dt.current()},[]);const Vt=et(a,$t);let Gt=0;"middle"===a.verticalAlign&&(Gt=(Vt-$t)/2),"bottom"===a.verticalAlign&&(Gt=Vt-$t);const Kt=M({fontLoaded:Rt,fontFamily:a.fontFamily,fontSize:a.fontSize,lineHeight:a.lineHeight}),Qt=a.backgroundPadding*(a.fontSize*Kt*.5),Ut=a.backgroundCornerRadius*(a.fontSize*Kt*.5),Zt=r.useMemo(()=>{if(!a.backgroundEnabled&&!a.borderSize||a.curveEnabled||a.legacyBackground){return""}const t=B(At);if(0===t.length){return""}const e=t.map(t=>({left:t.left+zt,right:t.right+zt,top:t.top+zt,bottom:t.bottom+zt}));return W({rects:e,padding:Qt,cornerRadius:Ut})},[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.legacyBackground,At,Rt,Qt,Ut,zt]),te=V(),ee=X(a.a.width,$t,a.curvePower,a.fontSize),oe=r.useRef(null),re=r.useRef(a.width),ie=r.useRef(""),ne=r.useRef(0),ae=r.useRef(0),se=r.useRef(null),le=Yt&&a.strokeWidth>0&&!a.curveEnabled,ce=Wt>0?kt/(Wt+2*zt):1,de=a.shadowEnabled&&!N(a),fe={shadowEnabled:a.shadowEnabled&&(N(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return o(e,{children:[t(K,{element:a,store:lt}),a.curveEnabled||a.legacyBackground?t(d,Object.assign({x:a.a.x,y:a.a.y,offsetX:Qt/ce,offsetY:Qt/ce,rotation:a.a.rotation,scaleX:ce,scaleY:ce,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled||a.borderSize>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:st(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,width:a.width+2*Qt,height:Vt+2*Qt,cornerRadius:Ut},fe)):t(c,Object.assign({name:"html-background",x:a.a.x,y:a.a.y,offsetX:zt/ce,offsetY:(zt-Gt)/ce,rotation:a.a.rotation,scaleX:ce,scaleY:ce,hideInExport:!a.showInExport,listening:!1,visible:(a.backgroundEnabled||a.borderSize>0)&&Zt.length>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:st(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:Ut>0?"round":"miter",data:Zt},fe)),t(c,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,data:ee,stroke:ht.stroke,strokeWidth:1,visible:!1}),t(d,{ref:ut,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:a.a.height,visible:!qt,draggable:te?a.draggable&&a.isSelected:a.draggable,preventDefault:!te||a.isSelected,opacity:Yt?0:n(a),hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(p(t.target))},id:a.id,onDblClick:t=>{a.contentEditable&&(se.current={x:t.evt.clientX,y:t.evt.clientY},a.toggleEditMode(!0))},onDblTap:t=>{var e;if(a.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];se.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{wt(!0),vt.current=a.height,re.current=a.width,oe.current=t.target.position(),ie.current=f(t.target),ne.current=a.a.rotation,ae.current=a.height},onTransform:t=>{const e=t.target,o=ie.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,n=b(e.scaleX());if(r){const t=e.width()*n,o=x(re.current,a.fontSize);let r=t;t<o&&(r=o,oe.current&&e.position(oe.current)),e.width(r),e.scaleX(1),e.scaleY(1);const i=U(Object.assign(Object.assign({},a.toJSON()),{width:r}),{fontFamily:Ct,color:It,textOverflow:v(a)}),s=P(i).height,l=E.textVerticalResizeEnabled?Math.max(s,vt.current):s;e.height(l),a.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===v(a),o="number"==typeof a.lineHeight?a.lineHeight:1,r=t?$t:o*a.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),oe.current){if("top-center"===ie.current){const t=ne.current*Math.PI/180,o=ae.current-r;e.x(oe.current.x-o*Math.sin(t)),e.y(oe.current.y+o*Math.cos(t))}else{e.position(oe.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),ae.current=Math.max(r,i),a.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),a.set({fontSize:a.fontSize*n,letterSpacing:a.letterSpacing,width:e.width()*n,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*n,shadowBlur:a.shadowBlur*n,shadowOffsetX:a.shadowOffsetX*n,shadowOffsetY:a.shadowOffsetY*n,strokeWidth:a.strokeWidth*n,borderSize:a.borderSize*n,text:it(a.text,n)})}oe.current=t.target.position()},onTransformEnd:t=>{wt(!1),Et(!0);const e=b(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},u(a,a.fontSize*e)),{width:m(t.target.width()*e),x:m(t.target.x()),y:m(t.target.y()),rotation:m(t.target.rotation()),text:it(a.text,e)}))}}),t(l,Object.assign({ref:ut,image:xt,x:a.a.x,y:a.a.y,offsetX:zt,offsetY:zt-Gt,listening:!1,rotation:a.a.rotation,width:kt,height:kt*((null!==(dt=null==xt?void 0:xt.height)&&void 0!==dt?dt:0)/((null==xt?void 0:xt.width)||1)||1),visible:!qt&&!Yt,opacity:n(a)},fe,{shadowEnabled:de,hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled||yt||qt})),(qt&&!Yt||le)&&t(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-Gt,children:t(H,{divProps:{style:{pointerEvents:"none"}},children:t(at,{dangerouslySetInnerHTML:{__html:At},style:{pointerEvents:"none",opacity:a.a.opacity,textShadow:de?`${a.shadowOffsetX}px ${a.shadowOffsetY}px ${a.shadowBlur}px ${st(a.shadowColor,null!==(ft=a.shadowOpacity)&&void 0!==ft?ft:1)}`:void 0}})})}),Yt&&t(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-Gt,children:t(H,{divProps:{style:{zIndex:20}},children:t(nt,{element:a,onChange:t=>{const e=G({oldText:L(a.text),newText:L(t),element:a});a.set(Object.assign({text:t},h(a,e)))},onBlur:t=>{w(a)&&(a.set(g(a)),a.toggleEditMode(!1),Jt(!0))},clickCoords:se.current})})})]})});
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{toDisplayColor as i}from"@polotno/core/color";import{elementOpacity as n}from"./paint-opacity.js";import{observer as a}from"mobx-react-lite";import{Group as s,Image as l,Path as c,Rect as d}from"react-konva";import{activeAnchorOf as f,scaledFontSizeAttrs as h,roundedFontSizeAttrs as u,roundedTextDecorationAttrs as g,roundedGeometryPx as m,roundedPosition as p,textMinResizeWidth as x,transformScale as y}from"../utils/px.js";import b from"konva";import{isAlive as w}from"mobx-state-tree";import{nodeAutorun as S}from"../utils/mobx.js";import{flags as v}from"../utils/flags.js";import{textOverflowOf as E}from"../utils/text-sizing.js";import{layoutTextSizing as z,legacyTextOverflow as k,renderTagLayoutAt as O,resolveTextSizing as j}from"@polotno/core/text-sizing";import{applyFilter as F}from"./apply-filters.js";import{getLineHeight as M,useFontFamiliesLoader as R,usePrevious as Y}from"./text-element.js";import{getCurvePath as X}from"@polotno/core/text-html";import{useColor as C,withDisplayTextPaints as I}from"./use-color.js";import{useWorkspaceStyle as A}from"./workspace-style.js";import{detectSize as P,detectLineRects as $,htmlToCanvas as B}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as T}from"@polotno/core/background-shape";import{useFadeIn as W}from"./use-fadein.js";import{Html as H}from"react-konva-utils";import{incrementLoader as _,triggerLoadError as D}from"../utils/loader.js";import{removeTags as L}from"@polotno/core/text";import{cssFontFamily as J}from"@polotno/core/fonts";import{shadowCastByBackground as N}from"@polotno/core/filters";import*as q from"../utils/fonts.js";import{isTouchDevice as V}from"../utils/screen.js";import{getLimitedFontSize as G}from"./text-element/max-font-size.js";import{AutoDeleteEmptyText as K}from"./text-element/auto-delete-empty-text.js";import{useDelayer as Q}from"./use-delayer.js";import{geckoRtlOverhangCss as U,getHtml as Z,letterSpacingOverhang as tt,OVERHANG_PROPERTY as et,textLayoutWidth as ot}from"@polotno/core/text-html";import{hasMeasuredHeight as rt,resolveTextBoxHeight as it}from"@polotno/core/text-types";import{normalizeRichTextHtml as nt,extractFontFamiliesFromHtml as at,scaleRichTextFontSizesInHtml as st}from"../utils/rich-text-html.js";import{RichEditor as lt,RichTextContainer as ct,applyOpacityToColor as dt,quillRef as ft,createQuill as ht,setQuillContent as ut,setQuillFormats as gt,syncQuillFormatState as mt,setCursorFromCoords as pt}from"./rich-editor.js";export{ft as quillRef,ht as createQuill,ut as setQuillContent,gt as setQuillFormats,mt as syncQuillFormatState,pt as setCursorFromCoords};function xt(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 yt=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),bt=navigator.userAgent.includes("Firefox");export const HTMLElement=a(({element:a,store:ft})=>{var ht,ut,gt;const{textOverflowIndicatorStyle:mt}=A(),pt=r.useRef(null),[wt,St]=r.useState(),[vt,Et]=r.useState(!1),[zt,kt]=r.useState(!1),Ot=r.useRef(a.height),jt=a.fontSize/3,Ft=ot(a.a.width,a.letterSpacing,a.fontSize)+2*jt,{textVerticalResizeEnabled:Mt}=v,Rt=Y(a.fontFamily),Yt=r.useMemo(()=>nt(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),Xt=r.useMemo(()=>[...new Set([a.fontFamily,...at(Yt)])].filter(Boolean),[a.fontFamily,Yt]),[Ct]=R(ft,Xt,a.fontStyle,a.fontWeight,a.text||a.placeholder),It=a._editModeEnabled,At=It&&!(null===(ht=a.page)||void 0===ht?void 0:ht._exporting);W(pt);const Pt=Ct?a.fontFamily:Rt!==a.fontFamily?Rt:"Arial",$t=C(a).fill,Bt=Z(I(a),{fontFamily:Pt,color:$t,textOverflow:E(a)}),Tt=function(t,e){return e&&/dir="rtl"|direction: rtl/.test(t)?`<div class="polotno-overhang" style="${et}: ${e}px">${t}</div><style>${U(".polotno-overhang [dir]:not(.polotno-overhang [dir] [dir])")}</style>`:t}(Bt,tt(a.letterSpacing,a.fontSize)),Wt=r.useRef([]),Ht=r.useRef(!0),[,_t]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Wt.current;t.length>0&&(Wt.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Ht.current=!1;const t=Wt.current;t.length>0&&(Wt.current=[],t.forEach(t=>t()))},[]);let{width:Dt,height:Lt}=function(t,e,o){return r.useMemo(()=>P(t),[t,e.width,o])}(Tt,a,Ct);r.useEffect(()=>{if(!Ct){return}const t=(t,e,o,r)=>{const i=_(`text ${a.id}`),n=()=>{ft.history.ignore(t,e,o).then(()=>{Ht.current?(Wt.current.push(i),_t()):i()})};r?queueMicrotask(n):n()};if(a.curveEnabled&&Math.abs(Dt-a.width)>1&&!vt&&t(()=>{if(w(a)&&Math.abs(Dt-a.width)>1){const t=a.x+a.width/2;a.set({width:Dt,x:t-Dt/2})}},!1,!0),!rt(a)){return void t(()=>{w(a)&&(rt(a)||a.set({height:Lt}))},void 0,void 0,!0)}const e=k(E(a));if("change-font-size"!==e||vt||It||a.curveEnabled){"resize"===e&&(Mt&&a.height<Lt&&!vt&&t(()=>{w(a)&&a.set({height:Lt})},!1,!0),Mt||a.height===Lt||vt||t(()=>{w(a)&&a.set({height:Lt})},!1,!0))}else{const o=z({element:a,sizing:j({text:a.text,width:a.width,height:a.height,fontSize:a.fontSize,maxLines:a.maxLines,curveEnabled:a.curveEnabled},{textOverflow:e,textSplitAllowed:v.textSplitAllowed}),layoutAt:O(a,{getHtmlOptions:{fontFamily:a.fontFamily}})});o.fontSize!==a.fontSize?t(()=>{if(!w(a)){return}const t=h(a,o.fontSize);o.text!==a.text&&(t.text=o.text),a.set(t)},!1,!0):a.height!==Lt&&(Mt&&a.height<Lt?t(()=>{w(a)&&a.set({height:Lt})},!1,!0):Mt||t(()=>{w(a)&&a.set({height:Lt})},!1,!0))}});const Jt=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 B(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Nt=r.useRef(0),qt=r.useRef(null);r.useEffect(()=>{vt||At||(async()=>{Nt.current++;const t=Nt.current;let e=_(`text ${a.id} ${t}`);qt.current&&qt.current(),qt.current=e,kt(!0);let o=null;const r=yt?5:1;for(let n=0;n<r;n++){const e=n>0?Jt:B;try{if(o=await e({skipCache:n>0,html:Tt,width:Dt||a.width||1,height:it(a,Lt)||1,fontFamily:Pt,fontFamilies:at(Tt),resolveFont:t=>ft.fonts.find(e=>e.fontFamily===t)||q.globalFonts.find(e=>e.fontFamily===t),padding:jt,pixelRatio:ft._elementsPixelRatio,font:ft.fonts.find(t=>t.fontFamily===Pt)||q.globalFonts.find(t=>t.fontFamily===Pt)}),t!==Nt.current){return}if((yt||bt)&&o&&xt(o)){await new Promise(t=>setTimeout(t,50*(n+1)));continue}break}catch(i){console.error(i),D(`Error rendering rich text with id ${a.id}`);break}}o?St(o):e?(e(),e=null):console.error("Finish function is called twice!"),kt(!1)})()},[Tt,vt,Lt,At,Pt,a.height,ft._elementsPixelRatio,Ct]);const[Vt,Gt]=Q(zt,300),[Kt]=Q(vt,300,!0),Qt=Kt||Vt;r.useEffect(()=>{var t;if(!Qt){return S(a,()=>{const t=pt.current;F(t,a)})}null===(t=pt.current)||void 0===t||t.clearCache()},[wt,Qt,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]),r.useEffect(()=>{wt&&!zt&&qt.current&&(qt.current(),qt.current=null)},[wt,zt]),r.useLayoutEffect(()=>{if(!Ct){return}if(!a.curveEnabled){return}const t=new b.TextPath({data:X(a.a.width,a.a.height,a.curvePower,a.a.fontSize),text:L(a.text),letterSpacing:a.letterSpacing*a.a.fontSize,fontSize:a.a.fontSize,fontFamily:J(a.fontFamily),fontWeight:a.fontWeight,fontStyle:a.fontStyle,align:"center",textBaseline:"middle",fill:a.fill}),e=t.getSelfRect().width;if(e){const t=e-a.a.width,o=a.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{w(a)&&a.set({width:e,x:a.a.x+r,y:a.a.y+i})})}t.destroy()},[Ct,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),r.useEffect(()=>()=>{qt.current&&qt.current()},[]);const Ut=it(a,Lt);let Zt=0;"middle"===a.verticalAlign&&(Zt=(Ut-Lt)/2),"bottom"===a.verticalAlign&&(Zt=Ut-Lt);const te=M({fontLoaded:Ct,fontFamily:a.fontFamily,fontSize:a.fontSize,lineHeight:a.lineHeight}),ee=a.backgroundPadding*(a.fontSize*te*.5),oe=a.backgroundCornerRadius*(a.fontSize*te*.5),re=r.useMemo(()=>{if(!a.backgroundEnabled&&!a.borderSize||a.curveEnabled||a.legacyBackground){return""}const t=$(Bt);if(0===t.length){return""}const e=t.map(t=>({left:t.left+jt,right:t.right+jt,top:t.top+jt,bottom:t.bottom+jt}));return T({rects:e,padding:ee,cornerRadius:oe})},[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.legacyBackground,Bt,Ct,ee,oe,jt]),ie=V(),ne=X(a.a.width,Lt,a.curvePower,a.fontSize),ae=r.useRef(null),se=r.useRef(a.width),le=r.useRef(""),ce=r.useRef(0),de=r.useRef(0),fe=r.useRef(null),he=It&&a.strokeWidth>0&&!a.curveEnabled,ue=Dt>0?Ft/(Dt+2*jt):1,ge=a.shadowEnabled&&!N(a),me={shadowEnabled:a.shadowEnabled&&(N(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return o(e,{children:[t(K,{element:a,store:ft}),a.curveEnabled||a.legacyBackground?t(d,Object.assign({x:a.a.x,y:a.a.y,offsetX:ee/ue,offsetY:ee/ue,rotation:a.a.rotation,scaleX:ue,scaleY:ue,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled||a.borderSize>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:dt(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,width:a.width+2*ee,height:Ut+2*ee,cornerRadius:oe},me)):t(c,Object.assign({name:"html-background",x:a.a.x,y:a.a.y,offsetX:jt/ue,offsetY:(jt-Zt)/ue,rotation:a.a.rotation,scaleX:ue,scaleY:ue,hideInExport:!a.showInExport,listening:!1,visible:(a.backgroundEnabled||a.borderSize>0)&&re.length>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:dt(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:oe>0?"round":"miter",data:re},me)),t(c,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,data:ne,stroke:mt.stroke,strokeWidth:1,visible:!1}),t(d,{ref:pt,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:a.a.height,visible:!Qt,draggable:ie?a.draggable&&a.isSelected:a.draggable,preventDefault:!ie||a.isSelected,opacity:It?0:n(a),hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(p(t.target))},id:a.id,onDblClick:t=>{a.contentEditable&&(fe.current={x:t.evt.clientX,y:t.evt.clientY},a.toggleEditMode(!0))},onDblTap:t=>{var e;if(a.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];fe.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{Et(!0),Ot.current=a.height,se.current=a.width,ae.current=t.target.position(),le.current=f(t.target),ce.current=a.a.rotation,de.current=a.height},onTransform:t=>{const e=t.target,o=le.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,n=y(e.scaleX());if(r){const t=e.width()*n,o=x(se.current,a.fontSize);let r=t;t<o&&(r=o,ae.current&&e.position(ae.current)),e.width(r),e.scaleX(1),e.scaleY(1);const i=Z(Object.assign(Object.assign({},a.toJSON()),{width:r}),{fontFamily:Pt,color:$t,textOverflow:E(a)}),s=P(i).height,l=v.textVerticalResizeEnabled?Math.max(s,Ot.current):s;e.height(l),a.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===E(a),o="number"==typeof a.lineHeight?a.lineHeight:1,r=t?Lt:o*a.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),ae.current){if("top-center"===le.current){const t=ce.current*Math.PI/180,o=de.current-r;e.x(ae.current.x-o*Math.sin(t)),e.y(ae.current.y+o*Math.cos(t))}else{e.position(ae.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),de.current=Math.max(r,i),a.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),a.set({fontSize:a.fontSize*n,letterSpacing:a.letterSpacing,width:e.width()*n,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*n,shadowBlur:a.shadowBlur*n,shadowOffsetX:a.shadowOffsetX*n,shadowOffsetY:a.shadowOffsetY*n,strokeWidth:a.strokeWidth*n,borderSize:a.borderSize*n,text:st(a.text,n)})}ae.current=t.target.position()},onTransformEnd:t=>{Et(!1),kt(!0);const e=y(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},u(a,a.fontSize*e)),{width:m(t.target.width()*e),x:m(t.target.x()),y:m(t.target.y()),rotation:m(t.target.rotation()),text:st(a.text,e)}))}}),t(l,Object.assign({ref:pt,image:wt,x:a.a.x,y:a.a.y,offsetX:jt,offsetY:jt-Zt,listening:!1,rotation:a.a.rotation,width:Ft,height:Ft*((null!==(ut=null==wt?void 0:wt.height)&&void 0!==ut?ut:0)/((null==wt?void 0:wt.width)||1)||1),visible:!Qt&&!It,opacity:n(a)},me,{shadowEnabled:ge,hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled||vt||Qt})),(Qt&&!It||he)&&t(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-Zt,children:t(H,{divProps:{style:{pointerEvents:"none"}},children:t(ct,{dangerouslySetInnerHTML:{__html:Tt},style:{pointerEvents:"none",opacity:a.a.opacity,textShadow:ge?`${a.shadowOffsetX}px ${a.shadowOffsetY}px ${a.shadowBlur}px ${dt(a.shadowColor,null!==(gt=a.shadowOpacity)&&void 0!==gt?gt:1)}`:void 0}})})}),It&&t(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-Zt,children:t(H,{divProps:{style:{zIndex:20}},children:t(lt,{element:a,onChange:t=>{const e=G({oldText:L(a.text),newText:L(t),element:a});a.set(Object.assign({text:t},h(a,e)))},onBlur:t=>{w(a)&&(a.set(g(a)),a.toggleEditMode(!1),Gt(!0))},clickCoords:fe.current})})})]})});
|
|
@@ -65,6 +65,7 @@ type ImageProps = {
|
|
|
65
65
|
};
|
|
66
66
|
export declare const setImageLoaderHook: (customImageLoaderHook: any) => void;
|
|
67
67
|
export declare const useCanvasImage: (src: string, crossOrigin?: "anonymous" | "use-credentials") => [HTMLImageElement | undefined, ImageStatus];
|
|
68
|
+
export declare const useSizedImage: (src: string) => readonly [HTMLImageElement | undefined, ImageStatus];
|
|
68
69
|
export declare const useClipImage: (src: string) => readonly [HTMLImageElement | undefined, ImageStatus];
|
|
69
70
|
export declare const useImageLoader: (status: string, src?: string, id?: string) => void;
|
|
70
71
|
export declare const ImageElement: (({ element, store }: ImageProps) => React.JSX.Element) & React.NamedExoticComponent<ImageProps> & {
|
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{toDisplayColor as o}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{isAlive as n}from"mobx-state-tree";import{Arc as h,Group as s,Image as d,Rect as c,Text as l}from"react-konva";import{ROLES as g}from"../model/store.js";import u from"./use-image.js";import p from"konva";import{useWorkspaceStyle as m}from"./workspace-style.js";import{incrementLoader as f,triggerLoadError as w}from"../utils/loader.js";import{polotnoError as x}from"@polotno/core/errors";import{getBorderInset as v,getCropRect as M}from"@polotno/core/image";import{elementOpacity as y,paintOpacity as E}from"./paint-opacity.js";import*as b from"@polotno/core/svg";import{trySetCanvasSize as S}from"../utils/canvas.js";import{activeAnchorOf as C,roundedCropFraction as I,roundedGeometryPx as R,roundedPosition as X,isCornerAnchor as Y,scaledStyleAttrs as j,roundedStyleAttrs as H,transformScale as W}from"../utils/px.js";import{useFilteredSource as O}from"./use-filtered-source.js";import{useFadeIn as z}from"./use-fadein.js";import{PlacedNode as k}from"./placed-node.js";import{isTouchDevice as A}from"../utils/screen.js";import{useDelayer as D}from"./use-delayer.js";import{useColor as L}from"./use-color.js";import{commitCropSource as T,cropDraftWriter as _,CropOverlay as N}from"./crop-overlay.js";import{ImageToolControls as P}from"./image-tool-controls.js";import{drawReduced as U,exportKernelEnabled as F,shouldResample as q}from"./downscale.js";const B=1e4,V=new Map,$=new Map;let G=0;const J=t=>{const e=V.get(t);return void 0!==e&&(V.delete(t),V.set(t,e)),e},Q=(t,e)=>t+"\n"+JSON.stringify((null==e?void 0:e.size)?[...e]:[]),K=(t,e)=>{const r=Q(t,e),i=J(r);if(void 0!==i){return Promise.resolve(i)}let o=$.get(r);if(!o){const i=(null==e?void 0:e.size)?new Map(e):null;o=b.urlToString(t).then(t=>b.prepareSvgSource(t,i)),$.set(r,o),o.then(t=>((t,e)=>{const r=t.length+e.length;if(!(r>4e6||V.has(t))){V.set(t,e),G+=r;for(const[t,e]of V){if(G<=16e6){break}V.delete(t),G-=t.length+e.length}}})(r,t),()=>{}).finally(()=>$.delete(r))}return o},Z=t=>"svg"===t.type||b.isSvgUrl(t.src||""),tt=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=await K(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=>tt(t)[0]||t;function et(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,B/Math.max(e.width,e.height),Math.sqrt(16777216/(e.width*e.height)))}const rt=t=>{if(t.flipX||t.flipY||t.maskSrc){return!0}const e=navigator.userAgent.toLowerCase();return(e.indexOf("firefox")>-1||/^((?!chrome|android).)*safari/i.test(e))&&Z(t)};export function maskCompositeSize(t,e){const r=M({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 it=(t,e,r)=>Math.max(e,Math.min(r,t)),ot=(t,e,r)=>Math.max(t.a.width/e.width,t.a.height/e.height)*r,at=(t,e)=>0===t.x&&0===t.y&&t.width===(null==e?void 0:e.width)&&t.height===(null==e?void 0:e.height);export const useCornerRadiusAndCrop=(t,e,r,o,a=0,n=!1,h=!0,s=0,d)=>{var c;const l=B,g=B,u=d.aligned?it(d.deviceWidth,1,l):Math.floor(it(t.a.width*o,1,l)),m=d.aligned?it(d.deviceHeight,1,g):Math.floor(it(t.a.height*o,1,g)),f=Math.min(a*o,u/2,m/2),w=Math.min(s*o,u/2,m/2),x=ot(t,r,o),v=F(t.page)&&!n&&(q(x,u,m)||d.aligned),M=at(r,e),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,u,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,u-2*w,m-2*w,Math.max(0,f-w)),o.clip()),v){const t=y.width===u&&y.height===m&&d.aligned;return void U(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,n,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,v,d.phaseX,d.phaseY,w]),i.useEffect(()=>()=>{y&&"CANVAS"===y.nodeName&&p.Util.releaseCanvas(y)},[y]),y||e};const nt=document.createElement("canvas"),ht=a(({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 p.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:n}=m();return e(s,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:y(r),hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:n.fill}),t(h,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:n.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),st=a(({element:r})=>{const{mediaErrorStyle:i}=m(),o=Math.max(10,Math.min(30,r.a.width/25));return e(s,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:y(r),hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:i.fill}),t(l,{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 dt=u;export const setImageLoaderHook=t=>{dt=t};export const useCanvasImage=(t,e)=>dt(t,e);export const useClipImage=t=>{const[e,r]=tt(t),[o,a]=useCanvasImage(e,"anonymous"),n="loaded"===r?a:r,h=i.useRef(void 0);return t&&"failed"!==n?o&&(h.current=o):h.current=void 0,[t&&"failed"!==n?o||h.current:void 0,n]};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),n=`image with id ${r} url: ${i}`;"loading"!==t||o.current||(o.current=f(n)),"loading"!==t&&a(),"failed"===t&&w(x("IMAGE_FAILED",n,{src:i,elementId:r,reason:"load"}))},[t])};export const ImageElement=a(({element:a,store:h})=>{var s,l,u,m,f;const[w,x]=i.useState(!1),b=i.useRef(null),U=A(),[V,$]=(t=>{const[e,r]=i.useReducer(t=>t+1,0),o=Z(t),a=o?Q(t.src,t.colorsReplace):"",n=i.useRef("loading"),h=i.useRef(""),s=i.useRef(a);if(s.current!==a&&(s.current=a,n.current="loading"),o&&t.src&&"loading"===n.current){const t=J(a);void 0!==t&&(h.current=t,n.current="loaded")}return i.useEffect(()=>{if(!o||!t.src||"loaded"===n.current){return}let e=!1;return(async()=>{const i=await K(t.src,t.colorsReplace);e||(h.current=i,n.current="loaded",r())})().catch(()=>{e||(n.current="failed",r())}),()=>{e=!0}},[a]),o?[h.current,n.current]:[t.src,"loaded"]})(a),[G,it]=dt(V,"anonymous"),ct="loaded"===$?it:$;useImageLoader(ct,a.src,a.id);const lt=(null===(s=a.page)||void 0===s?void 0:s._exportingOrRendering)||!rt(a)?1:Math.max(1,h.scale),gt=h._elementsPixelRatio*lt,ut=(({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:G,status:ct}),pt=((t,e,r)=>{const o=Z(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:n}=t;if(!rt(t)){return e}if(!e||!e.width||!e.height){return null}const h=document.createElement("canvas");S(h,Math.max(e.width*o,1),Math.max(e.height*o,1));let s=a?-h.width:0,d=n?-h.height:0;return null===(r=h.getContext("2d"))||void 0===r||r.scale(a?-1:1,n?-1:1),null===(i=h.getContext("2d"))||void 0===i||i.drawImage(e,s,d,h.width,h.height),h},[t.maskSrc,t.flipX,t.flipY,e,o]);return i.useEffect(()=>()=>{a&&"CANVAS"===a.nodeName&&p.Util.releaseCanvas(a)},[a]),a})(a,G||ut,gt),mt=((t,e)=>{const[r,o]=tt(t.maskSrc||""),[a,n]=dt(r,"anonymous"),h=t.maskSrc?"loaded"===o?n:o:"loaded";return useImageLoader(h,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 n=o.getContext("2d");if(!n){return e}n.drawImage(e,0,0,r,i,0,0,r,i),n.globalCompositeOperation="source-in";const h=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 n.drawImage(a,h.x,h.y,h.width,h.height,0,0,r,i),o},[e,a,t.a.width,t.a.height,t.stretchEnabled])})(a,pt)||nt,[ft=nt]=O(a,mt);i.useLayoutEffect(()=>{"loaded"===ct&&a._setSourceSize({width:ft.width,height:ft.height},a.src)},[a,a.src,ct,ft.width,ft.height]);let{cropX:wt,cropY:xt,cropWidth:vt,cropHeight:Mt}=a.a;"loaded"!==it&&(wt=xt=0,vt=Mt=1);const yt=M({stretchEnabled:a.stretchEnabled,cropX:wt,cropY:xt,cropWidth:vt,cropHeight:Mt,width:a.a.width,height:a.a.height},ft),Et=yt.width,bt=yt.height,St=a.stretchEnabled,Ct=null!==(l=a.cornerRadius)&&void 0!==l?l:0,It=v({borderSize:a.borderSize,borderColor:o(a.borderColor),width:a.a.width,height:a.a.height,opacity:E(a)}),Rt=!(!Ct&&!It&&!a.clipSrc&&at(yt,ft)),Xt=(null===(u=a.page)||void 0===u?void 0:u._exportingOrRendering)||!Rt?1:Math.min(2,h.scale),Yt=h._elementsPixelRatio*Xt,jt=w||a._cropModeEnabled||"svg"===a.type,Ht=F(a.page)&&0===a.a.rotation&&"group"!==(null===(m=a.parent)||void 0===m?void 0:m.type)&&!jt,Wt=function(t,e,r,i){const{width:o,height:a}=t.a,n={position:void 0,width:o,height:a,deviceWidth:0,deviceHeight:0,phaseX:0,phaseY:0,spanX:0,spanY:0,aligned:!1};if(!i){return n}const{x:h,y:s}=t.a,d=h*e,c=s*e,l=Math.round(d),g=Math.round(c),u=Math.max(1,Math.round(d+o*e)-l),p=Math.max(1,Math.round(c+a*e)-g),m=t=>Math.abs(t-Math.round(t))>.001;return m(d)||m(c)||m(o*e)||m(a*e)?u>B||p>B?n:q(r,u,p)||r<=1&&u*p<=4e6?{position:{x:l/e,y:g/e},width:u/e,height:p/e,deviceWidth:u,deviceHeight:p,phaseX:d-l,phaseY:c-g,spanX:o*e,spanY:a*e,aligned:!0}:n:n}(a,Yt,ot(a,yt,Yt),Ht);let Ot=((t,e,r,o)=>{const[a,n]=useClipImage(t.clipSrc||""),h=t.clipSrc?n:"loaded";useImageLoader(h,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"),n=Math.max(t.a.width/a.width*r,t.a.height/a.height*r);S(o,Math.min(1e4,Math.max(a.width*n,1)),Math.min(1e4,Math.max(a.height*n,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 h=s.getContext("2d");h&&(h.save(),h.drawImage(o,0,0,s.width,s.height),p.Util.releaseCanvas(o),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,s.width,s.height),h.restore())},[s,e,a,t.a.width,t.a.height,r,...o]),i.useEffect(()=>()=>{s&&p.Util.releaseCanvas(s)},[s]),t.clipSrc&&a?s:e})(a,useCornerRadiusAndCrop(a,ft,yt,Yt,Ct,jt,!0,It,Wt),Yt,[yt,Ct,Yt]);var zt=i.useRef(null),kt=i.useRef(0),At=i.useRef(!1);const Dt=()=>{var t;a._cropEnabled&&!a.pendingEdit&&(null===(t=h.activeTool)||void 0===t?void 0:t.elementId)!==a.id&&setTimeout(()=>{n(a)&&a.toggleCropMode(!0)})},Lt=Z(a)&&ut,Tt="loading"===ct&&!Lt,[_t]=D(Tt,100,!1,!1),Nt="failed"===ct,Pt=!_t&&!Nt,Ut=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Ft=i.useRef(!1),qt=Pt?y(a):0;z(b,qt);const Bt=a.selectable||h.role===g.ADMIN,Vt=L(a,a.borderColor,"stroke");return e(r,{children:[_t&&t(ht,{element:a}),Nt&&t(st,{element:a}),t(k,Object.assign({as:d,nodeRef:b,element:a,name:"element",id:a.id,image:Ot},Wt.position,{width:Wt.width||1,height:Wt.height||1,opacity:qt,shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:o(a.shadowColor),shadowOpacity:a.shadowOpacity,customCrop:yt,listening:Bt,draggable:!a.pendingEdit&&(null===(f=h.activeTool)||void 0===f?void 0:f.elementId)!==a.id&&(U?a.draggable&&a.isSelected:a.draggable),preventDefault:!U||a.isSelected,hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(X(t.target))},onDblClick:Dt,onDblTap:Dt,onTransformStart:t=>{x(!0),Ft.current=Y(C(t.target)),Ut.current={cropX:a.cropX,cropY:a.cropY,cropWidth:a.cropWidth,cropHeight:a.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=W(e.scaleX()),i=W(e.scaleY());e.scaleX(1),e.scaleY(1);const o=1-Et/ft.width,n=Math.min(o,Math.max(0,a.cropX)),h=1-bt/ft.height,s=Math.min(h,Math.max(0,a.cropY)),d=Ft.current,c=!d&&r<1&&Ut.current.cropHeight>bt/ft.height;let l=d?a.cropWidth:a.cropWidth*r;c&&(l=a.cropWidth);const g=!d&&i<1&&Ut.current.cropWidth>Et/ft.width;let u=d?a.cropHeight:a.cropHeight*i;g&&(u=a.cropHeight),St&&(l=a.cropWidth,u=a.cropHeight);const p=d?Math.min(r,i):1;a.set(Object.assign({cropX:n,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-n),cropHeight:Math.min(u,1-s)},1!==p&&j(a,p)))},onTransformEnd:t=>{const e=t.currentTarget;a.set(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(a.cropX),cropY:I(a.cropY),cropWidth:I(Et/ft.width),cropHeight:I(bt/ft.height)},H(a))),x(!1)}})),!!a.borderSize&&t(k,Object.assign({as:c,element:a},Wt.position,{width:Math.max(Wt.width-a.borderSize,0),height:Math.max(Wt.height-a.borderSize,0),opacity:qt,offsetX:-a.borderSize/2,offsetY:-a.borderSize/2},Vt,{strokeWidth:a.borderSize,listening:!1,cornerRadius:Math.max(0,Ct-a.borderSize),hideInExport:!a.showInExport})),a._cropModeEnabled&&"loaded"===ct&&t(N,{element:a,store:h,previewImage:Ot,sourceImage:ft,sourceWidth:ft.width,sourceHeight:ft.height,cropAbsoluteWidth:Et,cropAbsoluteHeight:bt,onSourceTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],i=e[1];const o=t.target;if(r&&!i&&!o.isDragging()&&At.current&&(o.startDrag(),At.current=!1),r&&i){p.hitOnDragEnabled=!0,o.isDragging()&&(At.current=!0,o.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var n={x:r.x,y:r.y},s={x:i.x,y:i.y};if(!zt.current){return void(zt.current=et(n,s))}var d=et(n,s),c=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(n,s);kt.current||(kt.current=c);const w=o.position();var l={x:d.x-w.x,y:d.y-w.y},g=c/kt.current;o.scaleX(g),o.scaleY(g);const x=e.point(d),v=e.point(zt.current);var u=x.x-v.x,m=x.y-v.y,f={x:Math.min(0,d.x-l.x*g+u),y:Math.min(0,d.y-l.y*g+m)};o.position(f),kt.current=c,zt.current=d,(t=>{T(t,a,_(a,h),ft.width,ft.height,Et,bt)})(t)}},onSourceTouchEnd:t=>{kt.current=0,zt.current=null,p.hitOnDragEnabled=!1}}),"image"===a.type&&t(P,{element:a,store:h})]})});
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import i from"react";import{toDisplayColor as o}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as n}from"mobx-react-lite";import{isAlive as h}from"mobx-state-tree";import{Arc as d,Group as s,Image as c,Rect as l,Text as g}from"react-konva";import{ROLES as u}from"../model/store.js";import p from"./use-image.js";import m from"konva";import{useWorkspaceStyle as f}from"./workspace-style.js";import{incrementLoader as w,triggerLoadError as x}from"../utils/loader.js";import{polotnoError as v}from"@polotno/core/errors";import{getBorderInset as M,getCropRect as y,mayBeUnsizedSvg as S}from"@polotno/core/image";import{elementOpacity as b,paintOpacity as E}from"./paint-opacity.js";import*as I from"@polotno/core/svg";import{trySetCanvasSize as R}from"../utils/canvas.js";import{activeAnchorOf as C,roundedCropFraction as j,roundedGeometryPx as W,roundedPosition as X,isCornerAnchor as Y,scaledStyleAttrs as H,roundedStyleAttrs as z,transformScale as D}from"../utils/px.js";import{useFilteredSource as k}from"./use-filtered-source.js";import{useOffscreenCanvas as O}from"./use-offscreen-canvas.js";import{useFadeIn as T}from"./use-fadein.js";import{useAnimatedBlur as A}from"./use-animated-blur.js";import{PlacedNode as L}from"./placed-node.js";import{isTouchDevice as _}from"../utils/screen.js";import{useDelayer as P}from"./use-delayer.js";import{useColor as F}from"./use-color.js";import{commitCropSource as N,cropDraftWriter as U,CropOverlay as q}from"./crop-overlay.js";import{ImageToolControls as $}from"./image-tool-controls.js";import{drawReduced as G,exportKernelEnabled as J,shouldResample as Q}from"./downscale.js";const V=1e4,B=new Map,K=new Map;let Z=0;const tt=t=>{const e=B.get(t);return void 0!==e&&(B.delete(t),B.set(t,e)),e},et=(t,e)=>{const r=t.length+e.length;if(!(r>4e6||B.has(t))){B.set(t,e),Z+=r;for(const[t,e]of B){if(Z<=16e6){break}B.delete(t),Z-=t.length+e.length}}},rt=(t,e)=>t+"\n"+JSON.stringify((null==e?void 0:e.size)?[...e]:[]),it=new Map,ot=new Map,at=t=>I.isSvgUrl(t)||!0===it.get(t),nt=t=>"svg"===t.type||at(t.src||""),ht=(t,e,r,o)=>{const[a,n]=i.useState(""),h=!e&&!!r&&"loaded"===o&&!t.startsWith("data:")&&!it.has(t)&&a!==t&&S(r);return i.useEffect(()=>{if(!h){return}let e=!0;return(t=>{let e=ot.get(t);return e||(e=I.fetchSvgText(t).then(e=>{null!==e&&et(rt(t),I.prepareSvgSource(e)),it.set(t,null!==e)}).catch(()=>{}).finally(()=>ot.delete(t)),ot.set(t,e)),e})(t).then(()=>{e&&n(t)}),()=>{e=!1}},[h,t]),h?"loading":o},dt=(t,e,r)=>{const[,o]=i.useReducer(t=>t+1,0),a=e&&t?rt(t,r):"",n=i.useRef("loading"),h=i.useRef(""),d=i.useRef(a);if(d.current!==a&&(d.current=a,n.current="loading"),a&&"loading"===n.current){const t=tt(a);void 0!==t&&(h.current=t,n.current="loaded")}return i.useEffect(()=>{if(!a||"loaded"===n.current){return}let e=!1;return((t,e)=>{const r=rt(t,e),i=tt(r);if(void 0!==i){return Promise.resolve(i)}let o=K.get(r);if(!o){const i=(null==e?void 0:e.size)?new Map(e):null;o=I.urlToString(t).then(t=>I.prepareSvgSource(t,i)),K.set(r,o),o.then(t=>et(r,t),()=>{}).finally(()=>K.delete(r))}return o})(t,r).then(t=>{e||(h.current=t,n.current="loaded",o())},()=>{e||(h.current="",n.current="failed",o())}),()=>{e=!0}},[a]),a?[h.current,n.current]:[t,"loaded"]};export const useSizeFixer=t=>dt(t,at(t))[0]||t;function st(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,V/Math.max(e.width,e.height),Math.sqrt(16777216/(e.width*e.height)))}const ct=t=>{if(t.flipX||t.flipY||t.maskSrc){return!0}const e=navigator.userAgent.toLowerCase();return(e.indexOf("firefox")>-1||/^((?!chrome|android).)*safari/i.test(e))&&nt(t)};export function maskCompositeSize(t,e){const r=y({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 lt=(t,e,r)=>Math.max(e,Math.min(r,t)),gt=(t,e,r)=>Math.max(t.a.width/e.width,t.a.height/e.height)*r,ut=(t,e)=>0===t.x&&0===t.y&&t.width===(null==e?void 0:e.width)&&t.height===(null==e?void 0:e.height);export const useCornerRadiusAndCrop=(t,e,r,o,a=0,n=!1,h=!0,d=0,s)=>{var c;const l=V,g=V,u=s.aligned?lt(s.deviceWidth,1,l):Math.floor(lt(t.a.width*o,1,l)),p=s.aligned?lt(s.deviceHeight,1,g):Math.floor(lt(t.a.height*o,1,g)),f=Math.min(a*o,u/2,p/2),w=Math.min(d*o,u/2,p/2),x=gt(t,r,o),v=J(t.page)&&!n&&(Q(x,u,p)||s.aligned),M=ut(r,e),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}R(y,u,p);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,u-2*w,p-2*w,Math.max(0,f-w)),o.clip()),v){const t=y.width===u&&y.height===p&&s.aligned;return void G(o,e,r,y.width,y.height,{x:t?s.phaseX:0,y:t?s.phaseY:0,width:t?s.spanX:y.width,height:t?s.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,n,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,v,s.phaseX,s.phaseY,w]),i.useEffect(()=>()=>{y&&"CANVAS"===y.nodeName&&m.Util.releaseCanvas(y)},[y]),y||e};const pt=document.createElement("canvas"),mt=n(({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 m.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:n}=f();return e(s,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:b(r),hideInExport:!r.showInExport,children:[t(l,{width:r.a.width,height:r.a.height,fill:n.fill}),t(d,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:n.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),ft=n(({element:r})=>{const{mediaErrorStyle:i}=f(),o=Math.max(10,Math.min(30,r.a.width/25));return e(s,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:b(r),hideInExport:!r.showInExport,children:[t(l,{width:r.a.width,height:r.a.height,fill:i.fill}),t(g,{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 wt=p;export const setImageLoaderHook=t=>{wt=t};export const useCanvasImage=(t,e)=>wt(t,e);export const useSizedImage=t=>{const[e,r]=dt(t,at(t)),[i,o]=useCanvasImage(e,"anonymous"),a=ht(t,at(t),i,o);return[i,"loaded"===r?a:r]};export const useClipImage=t=>{const[e,r]=useSizedImage(t),o=i.useRef(void 0);return t&&"failed"!==r?e&&(o.current=e):o.current=void 0,[t&&"failed"!==r?e||o.current:void 0,r]};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),n=`image with id ${r} url: ${i}`;"loading"!==t||o.current||(o.current=w(n)),"loading"!==t&&a(),"failed"===t&&x(v("IMAGE_FAILED",n,{src:i,elementId:r,reason:"load"}))},[t])};export const ImageElement=n(({element:n,store:d})=>{var s,g,p,f,w;const[x,v]=i.useState(!1),S=i.useRef(null),I=_(),[G,B]=dt(n.src,nt(n),n.colorsReplace),[K,Z]=wt(G,"anonymous"),tt="loaded"===B?Z:B,et=ht(n.src,nt(n),K,tt);useImageLoader(et,n.src,n.id);const rt=(null===(s=n.page)||void 0===s?void 0:s._exportingOrRendering)||!ct(n)?1:Math.max(1,d.scale),it=d._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:K,status:et}),at=((t,e,r)=>{const i=nt(t)&&e?svgSourceRatio({cropWidth:t.a.cropWidth,cropHeight:t.a.cropHeight,width:t.a.width,height:t.a.height},e,r):1,{flipX:o,flipY:a}=t,n=ct(t),h=O(n&&!!(e&&e.width&&e.height),t=>{var r,n;if(!e){return}R(t,Math.max(e.width*i,1),Math.max(e.height*i,1));let h=o?-t.width:0,d=a?-t.height:0;null===(r=t.getContext("2d"))||void 0===r||r.scale(o?-1:1,a?-1:1),null===(n=t.getContext("2d"))||void 0===n||n.drawImage(e,h,d,t.width,t.height)},[o,a,e,i]);return n?h:e})(n,K||ot,it),lt=((t,e)=>{const[r,o]=useSizedImage(t.maskSrc||""),a=t.maskSrc?o:"loaded";return useImageLoader(a,t.maskSrc||"",t.id),i.useMemo(()=>{if(!r){return e}if(!e||!e.width||!e.height){return e}const{width:i,height:o}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),a=document.createElement("canvas");a.width=i,a.height=o;const n=a.getContext("2d");if(!n){return e}n.drawImage(e,0,0,i,o,0,0,i,o),n.globalCompositeOperation="source-in";const h=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}}(r,t);return n.drawImage(r,h.x,h.y,h.width,h.height,0,0,i,o),a},[e,r,t.a.width,t.a.height,t.stretchEnabled])})(n,at)||pt,[xt=pt]=k(n,lt);i.useLayoutEffect(()=>{"loaded"===et&&n._setSourceSize({width:xt.width,height:xt.height},n.src)},[n,n.src,et,xt.width,xt.height]);let{cropX:vt,cropY:Mt,cropWidth:yt,cropHeight:St}=n.a;"loaded"!==Z&&(vt=Mt=0,yt=St=1);const bt=y({stretchEnabled:n.stretchEnabled,cropX:vt,cropY:Mt,cropWidth:yt,cropHeight:St,width:n.a.width,height:n.a.height},xt),Et=bt.width,It=bt.height,Rt=n.stretchEnabled,Ct=null!==(g=n.cornerRadius)&&void 0!==g?g:0,jt=M({borderSize:n.borderSize,borderColor:o(n.borderColor),width:n.a.width,height:n.a.height,opacity:E(n)}),Wt=!(!Ct&&!jt&&!n.clipSrc&&ut(bt,xt)),Xt=(null===(p=n.page)||void 0===p?void 0:p._exportingOrRendering)||!Wt?1:Math.min(2,d.scale),Yt=d._elementsPixelRatio*Xt,Ht=x||n._cropModeEnabled||"svg"===n.type,zt=J(n.page)&&0===n.a.rotation&&"group"!==(null===(f=n.parent)||void 0===f?void 0:f.type)&&!Ht,Dt=function(t,e,r,i){const{width:o,height:a}=t.a,n={position:void 0,width:o,height:a,deviceWidth:0,deviceHeight:0,phaseX:0,phaseY:0,spanX:0,spanY:0,aligned:!1};if(!i){return n}const{x:h,y:d}=t.a,s=h*e,c=d*e,l=Math.round(s),g=Math.round(c),u=Math.max(1,Math.round(s+o*e)-l),p=Math.max(1,Math.round(c+a*e)-g),m=t=>Math.abs(t-Math.round(t))>.001;return m(s)||m(c)||m(o*e)||m(a*e)?u>V||p>V?n:Q(r,u,p)||r<=1&&u*p<=4e6?{position:{x:l/e,y:g/e},width:u/e,height:p/e,deviceWidth:u,deviceHeight:p,phaseX:s-l,phaseY:c-g,spanX:o*e,spanY:a*e,aligned:!0}:n:n}(n,Yt,gt(n,bt,Yt),zt);let kt=((t,e,r,o)=>{const[a,n]=useClipImage(t.clipSrc||""),h=t.clipSrc?n:"loaded";useImageLoader(h,t.clipSrc,t.id);const d=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(!d){return}const o=document.createElement("canvas"),n=Math.max(t.a.width/a.width*r,t.a.height/a.height*r);R(o,Math.min(1e4,Math.max(a.width*n,1)),Math.min(1e4,Math.max(a.height*n,1))),null===(i=o.getContext("2d"))||void 0===i||i.drawImage(a,0,0,o.width,o.height),R(d,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 h=d.getContext("2d");h&&(h.save(),h.drawImage(o,0,0,d.width,d.height),m.Util.releaseCanvas(o),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,d.width,d.height),h.restore())},[d,e,a,t.a.width,t.a.height,r,...o]),i.useEffect(()=>()=>{d&&m.Util.releaseCanvas(d)},[d]),t.clipSrc&&a?d:e})(n,useCornerRadiusAndCrop(n,xt,bt,Yt,Ct,Ht,!0,jt,Dt),Yt,[bt,Ct,Yt]);var Ot=i.useRef(null),Tt=i.useRef(0),At=i.useRef(!1);const Lt=()=>{var t;n._cropEnabled&&!n.pendingEdit&&(null===(t=d.activeTool)||void 0===t?void 0:t.elementId)!==n.id&&setTimeout(()=>{h(n)&&n.toggleCropMode(!0)})},_t=nt(n)&&ot,Pt="loading"===tt&&!_t,[Ft]=P(Pt,100,!1,!1),Nt="failed"===et,Ut=!Ft&&!Nt,qt=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),$t=i.useRef(!1),Gt=Ut?b(n):0;T(S,Gt),A(S,n,kt);const Jt=n.selectable||d.role===u.ADMIN,Qt=F(n,n.borderColor,"stroke");return e(r,{children:[Ft&&t(mt,{element:n}),Nt&&t(ft,{element:n}),t(L,Object.assign({as:c,nodeRef:S,element:n,name:"element",id:n.id,image:kt},Dt.position,{width:Dt.width||1,height:Dt.height||1,opacity:Gt},a(n),{customCrop:bt,listening:Jt,draggable:!n.pendingEdit&&(null===(w=d.activeTool)||void 0===w?void 0:w.elementId)!==n.id&&(I?n.draggable&&n.isSelected:n.draggable),preventDefault:!I||n.isSelected,hideInExport:!n.showInExport,onDragMove:t=>{n.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{n.set(X(t.target))},onDblClick:Lt,onDblTap:Lt,onTransformStart:t=>{v(!0),$t.current=Y(C(t.target)),qt.current={cropX:n.cropX,cropY:n.cropY,cropWidth:n.cropWidth,cropHeight:n.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=D(e.scaleX()),i=D(e.scaleY());e.scaleX(1),e.scaleY(1);const o=1-Et/xt.width,a=Math.min(o,Math.max(0,n.cropX)),h=1-It/xt.height,d=Math.min(h,Math.max(0,n.cropY)),s=$t.current,c=!s&&r<1&&qt.current.cropHeight>It/xt.height;let l=s?n.cropWidth:n.cropWidth*r;c&&(l=n.cropWidth);const g=!s&&i<1&&qt.current.cropWidth>Et/xt.width;let u=s?n.cropHeight:n.cropHeight*i;g&&(u=n.cropHeight),Rt&&(l=n.cropWidth,u=n.cropHeight);const p=s?Math.min(r,i):1;n.set(Object.assign({cropX:a,cropY:d,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:t.target.rotation(),cropWidth:Math.min(l,1-a),cropHeight:Math.min(u,1-d)},1!==p&&H(n,p)))},onTransformEnd:t=>{const e=t.currentTarget;n.set(Object.assign({width:W(e.width()),height:W(e.height()),x:W(e.x()),y:W(e.y()),rotation:W(t.target.rotation()),cropX:j(n.cropX),cropY:j(n.cropY),cropWidth:j(Et/xt.width),cropHeight:j(It/xt.height)},z(n))),v(!1)}})),!!n.borderSize&&t(L,Object.assign({as:l,element:n},Dt.position,{width:Math.max(Dt.width-n.borderSize,0),height:Math.max(Dt.height-n.borderSize,0),opacity:Gt,offsetX:-n.borderSize/2,offsetY:-n.borderSize/2},Qt,{strokeWidth:n.borderSize,listening:!1,cornerRadius:Math.max(0,Ct-n.borderSize),hideInExport:!n.showInExport})),n._cropModeEnabled&&"loaded"===et&&t(q,{element:n,store:d,previewImage:kt,sourceImage:xt,sourceWidth:xt.width,sourceHeight:xt.height,cropAbsoluteWidth:Et,cropAbsoluteHeight:It,onSourceTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],i=e[1];const o=t.target;if(r&&!i&&!o.isDragging()&&At.current&&(o.startDrag(),At.current=!1),r&&i){m.hitOnDragEnabled=!0,o.isDragging()&&(At.current=!0,o.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var a={x:r.x,y:r.y},h={x:i.x,y:i.y};if(!Ot.current){return void(Ot.current=st(a,h))}var s=st(a,h),c=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(a,h);Tt.current||(Tt.current=c);const w=o.position();var l={x:s.x-w.x,y:s.y-w.y},g=c/Tt.current;o.scaleX(g),o.scaleY(g);const x=e.point(s),v=e.point(Ot.current);var u=x.x-v.x,p=x.y-v.y,f={x:Math.min(0,s.x-l.x*g+u),y:Math.min(0,s.y-l.y*g+p)};o.position(f),Tt.current=c,Ot.current=s,(t=>{N(t,n,U(n,d),xt.width,xt.height,Et,It)})(t)}},onSourceTouchEnd:t=>{Tt.current=0,Ot.current=null,m.hitOnDragEnabled=!1}}),"image"===n.type&&t($,{element:n,store:d})]})});
|
package/canvas/line-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e,Fragment as o}from"react/jsx-runtime";import{elementOpacity as r}from"./paint-opacity.js";import
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as o}from"react/jsx-runtime";import{elementOpacity as r}from"./paint-opacity.js";import i from"react";import{animatedShadow as n}from"./animated-shadow.js";import{computed as a}from"mobx";import{observer as s}from"mobx-react-lite";import{Group as l,Line as h,Rect as c,Circle as d}from"react-konva";import{ROLES as g}from"../model/store.js";import{useColor as m}from"./use-color.js";import{getLineHeadGeometry as p}from"@polotno/core/line-head";import{Portal as x}from"react-konva-utils";import{useAnchorSnap as f}from"./use-transformer-snap.js";import{useWorkspaceStyle as u}from"./workspace-style.js";import{isTouchDevice as y}from"../utils/screen.js";import{roundedGeometryAttrs as M,roundedPosition as b,transformScale as v}from"../utils/px.js";export function getMiddlePoints(t){const{x:e,y:o,width:r,height:i,rotation:n}=t.a,a=n*Math.PI/180,s={x:e+i/2*Math.cos(a+Math.PI/2),y:o+i/2*Math.sin(a+Math.PI/2)};return{middleLeft:s,middleRight:{x:s.x+r*Math.cos(a),y:s.y+r*Math.sin(a)}}}export function getLinePositionFromMiddlePoints(t,e,o){const r=Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),i=Math.atan2(e.y-t.y,e.x-t.x),n=180*i/Math.PI;return{x:t.x+Math.cos(i-Math.PI/2)*o/2,y:t.y+Math.sin(i-Math.PI/2)*o/2,width:r,height:o,rotation:n}}const j=(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()}}},k=s(({element:e,type:o})=>{const i=m(e,e.a.color,"fill"),n=m(e,e.a.color,"stroke"),a=p(o,e.height);if(!a){return null}const s=Object.assign(Object.assign(Object.assign({strokeWidth:e.height,lineCap:"round",lineJoin:"round"},n),i),{opacity:r(e),hideInExport:!e.showInExport});return"circle"===a.kind?t(d,Object.assign({x:a.cx,y:a.cy,radius:a.r},s)):t(h,Object.assign({points:a.points.flat(),closed:a.filled},s))});export const LineElement=s(({element:s,store:d})=>{const{transformerStyle:w}=u(),E=i.useRef(null),P=i.useRef(null),I=i.useRef(null),S=s.selectable||d.role===g.ADMIN,D=y(),O=i.useMemo(()=>a(()=>1===d.selectedElements.length&&d.selectedElements[0]===s),[d,s]).get(),{middleLeft:L,middleRight:R}=getMiddlePoints(s);f(P,[E],[O]),f(I,[E],[O]);const T=m(s,s.a.color,"stroke");return e(o,{children:[t(h,Object.assign({ref:E,name:"element",id:s.id,x:s.a.x,y:s.a.y,points:[0,0,s.a.width,0],offsetY:-s.a.height/2,strokeWidth:s.a.height,hitStrokeWidth:Math.max(s.a.height,20),dash:s.dash.map(t=>t*s.a.height)},T,{rotation:s.a.rotation,opacity:r(s)},n(s),{listening:S,draggable:D?s.draggable&&s.isSelected:s.draggable,preventDefault:!D||s.isSelected,hideInExport:!s.showInExport,onDragMove:t=>{s.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{s.set(b(t.target))},onTransform:t=>{const e=v(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),s.set({height:s.height*e,width:s.width*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()})},onTransformEnd:()=>{s.set(M(s))}})),p(s.startHead,s.height)&&t(l,{x:L.x,y:L.y,rotation:s.rotation,hideInExport:!s.showInExport,onClick:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("tap",t)},children:t(k,{element:s,type:s.startHead})}),p(s.endHead,s.height)&&t(l,{x:R.x,y:R.y,rotation:s.rotation+180,onClick:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("tap",t)},children:t(k,{element:s,type:s.endHead})}),O&&s.resizable&&t(x,{selector:".page-abs-container",enabled:!0,children:e(l,{visible:O,children:[t(c,Object.assign({x:L.x,y:L.y,ref:P,name:"line-anchor"},j(d.scale,d,w),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(t.target.position(),R,s.height);s.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}})),t(c,Object.assign({x:R.x,y:R.y,ref:I,name:"line-anchor"},j(d.scale,d,w),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(L,t.target.position(),s.height);s.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}}))]})})]})});
|