polotno 4.17.0 → 4.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
package/utils/animations.d.ts
CHANGED
|
@@ -1,4 +1,40 @@
|
|
|
1
|
+
import type { IMSTArray, SnapshotIn, SnapshotOut } from 'mobx-state-tree';
|
|
2
|
+
import { type TextPresetName } from '@polotno/core/animation-presets';
|
|
3
|
+
import type { Animation } from '../model/shape-model.js';
|
|
4
|
+
export type WipeBand = {
|
|
5
|
+
axis: 'x' | 'y';
|
|
6
|
+
from: number;
|
|
7
|
+
to: number;
|
|
8
|
+
};
|
|
9
|
+
export declare const KEN_BURNS_ZOOM = 0.15;
|
|
10
|
+
export declare const KEN_BURNS_PAN: Record<string, {
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
}>;
|
|
14
|
+
export declare const glowColor: (element: {
|
|
15
|
+
type: string;
|
|
16
|
+
shadowEnabled: boolean;
|
|
17
|
+
shadowColor: string;
|
|
18
|
+
fill?: unknown;
|
|
19
|
+
color?: unknown;
|
|
20
|
+
}) => string;
|
|
21
|
+
export declare const hasNumber: (html: string) => boolean;
|
|
1
22
|
export declare const animations: {
|
|
23
|
+
kenBurns: ({ dTime, element, animation }: {
|
|
24
|
+
dTime: any;
|
|
25
|
+
element: any;
|
|
26
|
+
animation: any;
|
|
27
|
+
}) => {
|
|
28
|
+
cropX: any;
|
|
29
|
+
cropY: any;
|
|
30
|
+
cropWidth: number;
|
|
31
|
+
cropHeight: number;
|
|
32
|
+
};
|
|
33
|
+
cameraZoom: ({ dTime, element, animation }: {
|
|
34
|
+
dTime: any;
|
|
35
|
+
element: any;
|
|
36
|
+
animation: any;
|
|
37
|
+
}) => any;
|
|
2
38
|
none: () => {};
|
|
3
39
|
fade: ({ dTime, element, animation }: {
|
|
4
40
|
dTime: any;
|
|
@@ -29,26 +65,192 @@ export declare const animations: {
|
|
|
29
65
|
dTime: any;
|
|
30
66
|
element: any;
|
|
31
67
|
animation: any;
|
|
32
|
-
}) => {
|
|
68
|
+
}) => {
|
|
69
|
+
fontSize?: number | undefined;
|
|
70
|
+
x: number;
|
|
71
|
+
y: number;
|
|
72
|
+
width: number;
|
|
73
|
+
height: number;
|
|
74
|
+
};
|
|
33
75
|
move: ({ dTime, element, animation }: {
|
|
34
76
|
dTime: any;
|
|
35
77
|
element: any;
|
|
36
78
|
animation: any;
|
|
37
|
-
}) => {
|
|
79
|
+
}) => {
|
|
80
|
+
x: any;
|
|
81
|
+
y: any;
|
|
82
|
+
};
|
|
38
83
|
zoom: ({ dTime, element, animation }: {
|
|
39
84
|
dTime: any;
|
|
40
85
|
element: any;
|
|
41
86
|
animation: any;
|
|
42
|
-
}) => {
|
|
43
|
-
|
|
87
|
+
}) => {
|
|
88
|
+
fontSize?: number | undefined;
|
|
89
|
+
x: number;
|
|
90
|
+
y: number;
|
|
91
|
+
width: number;
|
|
92
|
+
height: number;
|
|
93
|
+
};
|
|
94
|
+
pop: ({ dTime, element, animation }: {
|
|
44
95
|
dTime: any;
|
|
45
96
|
element: any;
|
|
46
97
|
animation: any;
|
|
47
|
-
}) =>
|
|
98
|
+
}) => {
|
|
99
|
+
opacity: number;
|
|
100
|
+
fontSize?: number | undefined;
|
|
101
|
+
x: number;
|
|
102
|
+
y: number;
|
|
103
|
+
width: number;
|
|
104
|
+
height: number;
|
|
105
|
+
};
|
|
106
|
+
rise: ({ dTime, element, animation }: {
|
|
107
|
+
dTime: any;
|
|
108
|
+
element: any;
|
|
109
|
+
animation: any;
|
|
110
|
+
}) => {
|
|
111
|
+
y: any;
|
|
112
|
+
opacity: number;
|
|
113
|
+
};
|
|
114
|
+
flip: ({ dTime, animation }: {
|
|
115
|
+
dTime: any;
|
|
116
|
+
animation: any;
|
|
117
|
+
}) => {
|
|
118
|
+
flipScale: number;
|
|
119
|
+
};
|
|
120
|
+
growTurn: ({ dTime, element, animation }: {
|
|
121
|
+
dTime: any;
|
|
122
|
+
element: any;
|
|
123
|
+
animation: any;
|
|
124
|
+
}) => {
|
|
125
|
+
opacity: number;
|
|
126
|
+
rotation: number;
|
|
127
|
+
x: number;
|
|
128
|
+
y: number;
|
|
129
|
+
width: number;
|
|
130
|
+
height: number;
|
|
131
|
+
};
|
|
132
|
+
pulse: ({ dTime, element, animation }: {
|
|
133
|
+
dTime: any;
|
|
134
|
+
element: any;
|
|
135
|
+
animation: any;
|
|
136
|
+
}) => {
|
|
137
|
+
fontSize?: number | undefined;
|
|
138
|
+
x: number;
|
|
139
|
+
y: number;
|
|
140
|
+
width: number;
|
|
141
|
+
height: number;
|
|
142
|
+
};
|
|
143
|
+
float: ({ dTime, element, animation }: {
|
|
144
|
+
dTime: any;
|
|
145
|
+
element: any;
|
|
146
|
+
animation: any;
|
|
147
|
+
}) => {
|
|
148
|
+
y: number;
|
|
149
|
+
};
|
|
150
|
+
shake: ({ dTime, element, animation }: {
|
|
151
|
+
dTime: any;
|
|
152
|
+
element: any;
|
|
153
|
+
animation: any;
|
|
154
|
+
}) => {
|
|
155
|
+
x: any;
|
|
156
|
+
};
|
|
157
|
+
wipe: ({ dTime, animation }: {
|
|
158
|
+
dTime: any;
|
|
159
|
+
animation: any;
|
|
160
|
+
}) => {
|
|
161
|
+
wipe: WipeBand;
|
|
162
|
+
};
|
|
163
|
+
glow: ({ dTime, element, animation }: {
|
|
164
|
+
dTime: any;
|
|
165
|
+
element: any;
|
|
166
|
+
animation: any;
|
|
167
|
+
}) => {
|
|
168
|
+
shadowEnabled: boolean;
|
|
169
|
+
shadowColor: string;
|
|
170
|
+
shadowOffsetX: number;
|
|
171
|
+
shadowOffsetY: number;
|
|
172
|
+
shadowBlur: number;
|
|
173
|
+
shadowOpacity: number;
|
|
174
|
+
};
|
|
175
|
+
blur: ({ dTime, element, animation }: {
|
|
176
|
+
dTime: any;
|
|
177
|
+
element: any;
|
|
178
|
+
animation: any;
|
|
179
|
+
}) => {
|
|
180
|
+
blurEnabled: boolean;
|
|
181
|
+
blurRadius: any;
|
|
182
|
+
opacity: number;
|
|
183
|
+
};
|
|
184
|
+
count: ({ dTime, element, animation }: {
|
|
185
|
+
dTime: any;
|
|
186
|
+
element: any;
|
|
187
|
+
animation: any;
|
|
188
|
+
}) => {
|
|
189
|
+
text?: undefined;
|
|
190
|
+
} | {
|
|
191
|
+
text: string;
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
export declare const isBuiltIn: (name: string) => boolean;
|
|
195
|
+
type TimedAnimation = {
|
|
196
|
+
type: string;
|
|
197
|
+
name: string;
|
|
198
|
+
duration: number;
|
|
199
|
+
easing?: string;
|
|
200
|
+
};
|
|
201
|
+
type TweenArgs = {
|
|
202
|
+
element: object;
|
|
203
|
+
dTime: number;
|
|
204
|
+
animation: TimedAnimation;
|
|
205
|
+
};
|
|
206
|
+
export type TextUnitsFrame = {
|
|
207
|
+
animation: TimedAnimation & {
|
|
208
|
+
name: TextPresetName;
|
|
209
|
+
data?: {
|
|
210
|
+
strength?: number;
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
dTime: number;
|
|
214
|
+
};
|
|
215
|
+
export declare const playsTextUnits: (element: object, animation: {
|
|
216
|
+
type: string;
|
|
217
|
+
name: string;
|
|
218
|
+
}) => boolean;
|
|
219
|
+
export declare const textUnitState: ({ animation, dTime }: TextUnitsFrame, index: number, count: number) => {
|
|
220
|
+
opacity: number;
|
|
221
|
+
y: number;
|
|
222
|
+
scale: number;
|
|
223
|
+
blur: number;
|
|
224
|
+
from: number;
|
|
225
|
+
to: number;
|
|
226
|
+
};
|
|
227
|
+
export type AnimationAttrs = Omit<SnapshotIn<typeof Animation>, 'type'>;
|
|
228
|
+
export declare function setAnimationOf(animations: IMSTArray<typeof Animation>, type: 'enter' | 'exit' | 'loop', attrs: AnimationAttrs): void;
|
|
229
|
+
export type PreviewedAnimation = SnapshotOut<typeof Animation>;
|
|
230
|
+
type PlayedElement = {
|
|
231
|
+
id: string;
|
|
232
|
+
page: {
|
|
233
|
+
duration: number;
|
|
234
|
+
};
|
|
235
|
+
animations: readonly PreviewedAnimation[];
|
|
236
|
+
store?: {
|
|
237
|
+
_animationPreview: Record<string, PreviewedAnimation[]> | null;
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
export declare const playedAnimations: (element: PlayedElement) => readonly PreviewedAnimation[];
|
|
241
|
+
export declare const spansElementTime: (name: string) => boolean;
|
|
242
|
+
export declare const loopWindow: (element: PlayedElement, loop: PreviewedAnimation, played: readonly PreviewedAnimation[]) => {
|
|
243
|
+
start: number;
|
|
244
|
+
animation: {
|
|
245
|
+
duration: number;
|
|
246
|
+
delay: number;
|
|
247
|
+
enabled: boolean;
|
|
248
|
+
type: "exit" | "enter" | "loop";
|
|
249
|
+
name: string;
|
|
250
|
+
data: {};
|
|
251
|
+
easing: string | undefined;
|
|
252
|
+
};
|
|
48
253
|
};
|
|
49
|
-
export declare const animate: ({ element, dTime, animation }:
|
|
50
|
-
element: any;
|
|
51
|
-
dTime: any;
|
|
52
|
-
animation: any;
|
|
53
|
-
}) => any;
|
|
254
|
+
export declare const animate: ({ element, dTime, animation }: TweenArgs) => any;
|
|
54
255
|
export declare const registerAnimation: (name: string, animation: any) => void;
|
|
256
|
+
export {};
|
package/utils/animations.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{EASINGS as t,recommendedEasing as n,resolveEasing as e}from"@polotno/core/easing";import{bounceScale as o,MOVE_OFFSETS as i,zoomStart as a,POP_FADE as r,PULSE_AMOUNT as s,GROW_TURN_ANGLE as l,GROW_TURN_SCALE as d,TEXT_BLUR as c,TEXT_PRESETS as m,TEXT_RISE as p,TEXT_WAVE as u,TRAVEL as h,isTextPreset as g,shakeSwing as x,textLoopStart as y,textUnitTiming as v}from"@polotno/core/animation-presets";import{firstStopColor as f}from"@polotno/core/gradient";import{getCropRect as b}from"@polotno/core/image";import{flags as w}from"./flags.js";import{degToRad as M,rotateAroundCenter as T}from"./math.js";import{getElementTimeRange as O}from"./timeline.js";const j={right:{axis:"x",forward:!0},left:{axis:"x",forward:!1},down:{axis:"y",forward:!0},up:{axis:"y",forward:!1}},S=t=>{var n,e,o,i;const a=null!==(e=null===(n=t.page)||void 0===n?void 0:n.computedWidth)&&void 0!==e?e:0,r=null!==(i=null===(o=t.page)||void 0===o?void 0:o.computedHeight)&&void 0!==i?i:0;return a>0&&r>0?Math.min(a,r):Math.max(t.width,t.height)};export const KEN_BURNS_ZOOM=.15;export const KEN_BURNS_PAN={right:{x:1,y:0},left:{x:-1,y:0},up:{x:0,y:-1},down:{x:0,y:1}};export const glowColor=t=>{if(t.shadowEnabled||"black"!==t.shadowColor){return t.shadowColor}const n="line"===t.type?t.color:t.fill;return"string"==typeof n?f(n):"#000000"};const B=(t,n)=>(1-Math.cos(2*Math.PI*t/n))/2,E=(t,n)=>{const e=t%n/(n/2);return e<=1?e:2-e},X=(t,{type:n,duration:e})=>"exit"===n?1-t/e:t/e,Y=t=>Math.min(1,Math.max(0,t)),k=(t,n)=>{const e=M(t.rotation),o=Math.cos(e),i=Math.sin(e),a=t.width*(1-n),r=t.height*(1-n);return Object.assign({x:t.x+(a*o-r*i)/2,y:t.y+(a*i+r*o)/2,width:t.width*n,height:t.height*n},void 0===t.fontSize?{}:{fontSize:t.fontSize*n})},N=/\d+(?:[.,]\d+)*/,W=t=>{var n,e;const o=[...new Set(null!==(n=t.match(/[.,]/g))&&void 0!==n?n:[])],i=Math.max(t.lastIndexOf("."),t.lastIndexOf(",")),a=t.length-i-1,r=i>=0&&(o.length>1||3!==a),s=r?t[i]:"",l=null!==(e=o.find(t=>t!==s))&&void 0!==e?e:"",d=r?a:0,c=(l?t.split(l).join(""):t).replace(s||".",".");return{value:parseFloat(c),write:t=>{const[n,e]=t.toFixed(d).split("."),o=l?n.replace(/\B(?=(\d{3})+(?!\d))/g,l):n;return e?o+s+e:o}}},I=(t,n)=>{const e=t.split(/(<[^>]*>|&[#\w]+;)/);for(let o=0;o<e.length;o++){if(e[o].startsWith("<")||e[o].startsWith("&")){continue}const t=N.exec(e[o]);if(!t){continue}const{value:i,write:a}=W(t[0]);return e[o]=e[o].slice(0,t.index)+a(n(i))+e[o].slice(t.index+t[0].length),e.join("")}return null};export const hasNumber=t=>null!==I(t,t=>t);const U={right:{cropX:-1},left:{cropX:1},up:{cropY:1},down:{cropY:-1}},_=Object.fromEntries(Object.entries(m).map(([t,{like:n}])=>[t,t=>animations[n](t)]));export const animations=Object.assign(Object.assign({none:()=>({}),fade:({dTime:t,element:n,animation:e})=>{const o=t/e.duration;return"enter"===e.type?{opacity:o*n.opacity}:{opacity:(1-o)*n.opacity}},rotate:({dTime:t,element:n,animation:e})=>{const o=t/e.duration*360;return T({x:n.x,y:n.y,width:n.width,height:n.height,rotation:n.rotation},o)},blink:({dTime:t,element:n,animation:e})=>({opacity:n.opacity*(1-E(t,e.duration))}),bounce:({dTime:t,element:n,animation:e})=>{var i;const a=o(null!==(i=e.data.strength)&&void 0!==i?i:1);return k(n,1-(1-a)*E(t,e.duration))},move:({dTime:t,element:n,animation:e})=>{var o;const a=i[e.data.direction]||i.right,r=null!==(o=e.data.strength)&&void 0!==o?o:1,s=("exit"===e.type?-1:1)*S(n)*h.move*r*(1-X(t,e));return{x:n.x+a.x*s,y:n.y+a.y*s}},zoom:({dTime:t,element:n,animation:e})=>{var o;const i=a(e.data.direction,null!==(o=e.data.strength)&&void 0!==o?o:1),r=X(t,e);return k(n,i+(1-i)*r)},pop:({dTime:t,element:n,animation:e})=>{var o;const i=null!==(o=e.data.strength)&&void 0!==o?o:1,a=Math.max(0,1-i),s=X(t,e);return Object.assign(Object.assign({},k(n,a+(1-a)*s)),{opacity:n.opacity*Y(s/r)})},rise:({dTime:t,element:n,animation:e})=>{var o;const i=null!==(o=e.data.strength)&&void 0!==o?o:1,a=S(n)*h.rise*i,r=X(t,e),s="exit"===e.type?-1:1;return{y:n.y+s*a*(1-r),opacity:n.opacity*r}},flip:({dTime:t,animation:n})=>({flipScale:Math.cos(Math.PI*(1-X(t,n)))}),growTurn:({dTime:t,element:n,animation:e})=>{var o;const i=null!==(o=e.data.strength)&&void 0!==o?o:1,a="exit"===e.type,r=X(t,e),s=k(n,d+(1-d)*r),c=(a?1:-1)*l*i*(1-r);return Object.assign(Object.assign({},T(Object.assign(Object.assign({},s),{rotation:n.rotation}),c)),{opacity:n.opacity*r})},pulse:({dTime:t,element:n,animation:e})=>{var o;const i=null!==(o=e.data.strength)&&void 0!==o?o:1,a=s*i*B(t,e.duration);return k(n,1+a)},float:({dTime:t,element:n,animation:e})=>{var o;const i=null!==(o=e.data.strength)&&void 0!==o?o:1,a=S(n)*h.float*i;return{y:n.y-a*B(t,e.duration)}},shake:({dTime:t,element:n,animation:e})=>{var o;const i=null!==(o=e.data.strength)&&void 0!==o?o:1,a=t/e.duration%1,r=x(a);return{x:n.x+S(n)*h.shake*i*r}},wipe:({dTime:t,animation:n})=>{const{axis:e,forward:o}=j[n.data.direction]||j.right,i=Y(t/n.duration),a=o?i:1-i;return{wipe:o===("exit"===n.type)?{axis:e,from:a,to:1}:{axis:e,from:0,to:a}}},glow:({dTime:t,element:n,animation:e})=>{var o;const i=null!==(o=e.data.strength)&&void 0!==o?o:1;return{shadowEnabled:!0,shadowColor:glowColor(n),shadowOffsetX:0,shadowOffsetY:0,shadowBlur:.02*S(n)*i,shadowOpacity:B(t,e.duration)}},blur:({dTime:t,element:n,animation:e})=>{var o;const i=null!==(o=e.data.strength)&&void 0!==o?o:1,a=n.blurEnabled?n.blurRadius:0,r=X(t,e);return{blurEnabled:!0,blurRadius:a+20*i*(1-r),opacity:n.opacity*r}},count:({dTime:t,element:n,animation:e})=>{var o;if("string"!=typeof n.text){return{}}const i=Number(null!==(o=e.data.from)&&void 0!==o?o:0),a=X(t,e),r=I(n.text,t=>i+(t-i)*a);return null===r?{}:{text:r}}},_),{kenBurns:({dTime:t,element:n,animation:e})=>{var o,i;const a="out"===e.data.zoom,r=Y(t/e.duration),s=.15*(null!==(o=e.data.strength)&&void 0!==o?o:1)*(a?1-r:r),l=Math.max(.05,1-s),d=null!==(i=KEN_BURNS_PAN[e.data.direction])&&void 0!==i?i:{x:0,y:0},c=a?-1:1,m=n._sourceSize,p=m&&b(n,m),[u,h]=p?[p.width/m.width,p.height/m.height]:[n.cropWidth,n.cropHeight],g=u*l,x=h*l;return{cropX:n.cropX+(u-g)*(1+d.x*c)/2,cropY:n.cropY+(h-x)*(1+d.y*c)/2,cropWidth:g,cropHeight:x}},cameraZoom:({dTime:t,element:n,animation:e})=>{var o,i,a;const r={},s=e.data.direction||"right",l=U[s]||U.right,d=null!==(o=e.data.movementStrength)&&void 0!==o?o:.1,c=null!==(i=e.data.shakeStrength)&&void 0!==i?i:.015;let m=null!==(a=e.data.zoomStrength)&&void 0!==a?a:.6;const p=t/e.duration,u=c*(1-p),h=Math.sin(12*p)*u*.7+Math.sin(8*p)*u*.3;if("exit"===e.type){const t=Math.max(n.cropWidth||1,n.cropHeight||1);m=Math.min(m,1-t)}e.type;const g=1-Math.pow(1-p,2),x=n.cropX||0,y=n.cropY||0,v=n.cropWidth||1,f=n.cropHeight||1,b=v,w=f;let M,T,O=0,j=0;if(l.cropX){const t=l.cropX>0?1-(x+v):x;O=Math.min(t,Math.abs(d))*l.cropX}if(l.cropY){const t=l.cropY>0?1-(y+f):y;j=Math.min(t,Math.abs(d))*l.cropY}return"enter"===e.type?(M=l.cropX?x+O*(1-g):x,T=l.cropY?y+j*(1-g):y):(M=l.cropX?x+O*g:x,T=l.cropY?y+j*g:y),r.cropX=Math.max(0,Math.min(1-b,M+h*b)),r.cropY=Math.max(0,Math.min(1-w,T+h*w)),r.cropWidth=b,r.cropHeight=w,r}});const z=new Map(Object.entries(animations));export const isBuiltIn=t=>z.has(t)&&z.get(t)===animations[t];const A=new Set(["blink","bounce"]),C=(n,o)=>{const i=e(o),a=o.duration;if("linear"===i||!(a>0)){return n}const r=t[i];if("loop"!==o.type){return r(Y(n/a))*a}const s=A.has(o.name)?a/2:a,l=Math.floor(n/s)*s;return l+r((n-l)/s)*s};export const playsTextUnits=(t,n)=>{if(!g(n.name)||!isBuiltIn(n.name)){return!1}const e="loop"in m[n.name];return"loop"===n.type===e&&"type"in t&&"text"===t.type&&!("curveEnabled"in t&&t.curveEnabled)&&w.renderTagTextEnabled};export const textUnitState=({animation:n,dTime:o},i,a)=>{var s,l;const{like:d}=m[n.name],h="loop"===n.type,g="exit"===n.type,x=n.duration,y=g?a-1-i:i,{step:f,span:b}=v(n.name,x,a),w=y*f,M=h&&x>0?o%x:o,T=b>0?Y((M-w)/b):M>=w?1:0,O={opacity:1,y:0,scale:1,blur:0,from:0,to:1};if(h){const t=null!==(l=null===(s=n.data)||void 0===s?void 0:s.strength)&&void 0!==l?l:1;return O.y=-u*t*B(T,1),O}const j=t[e(n)](T),S=g?1-j:j;return O.opacity=Y(S),"wipe"===d?(O.opacity=1,g?O.from=1-Y(S):O.to=Y(S)):"rise"===d?O.y=(g?-1:1)*p*(1-S):"pop"===d?(O.scale=Math.max(0,S),O.opacity=Y(S/r)):"blur"===d&&(O.blur=c*Math.max(0,1-S)),O};export function setAnimationOf(t,e,o){const i=t.find(t=>t.type===e),a=void 0===o.name||o.name===(null==i?void 0:i.name)||"easing"in o?o:Object.assign(Object.assign({},o),{easing:n(e,o.name)});i?Object.assign(i,a):t.push(Object.assign({type:e},a))}export const playedAnimations=t=>{var n,e,o;return null!==(o=null===(e=null===(n=t.store)||void 0===n?void 0:n._animationPreview)||void 0===e?void 0:e[t.id])&&void 0!==o?o:t.animations};export const spansElementTime=t=>"kenBurns"===t&&isBuiltIn(t);export const loopWindow=(t,n,e)=>{if("kenBurns"===(o=n.name)&&isBuiltIn(o)){const{start:o,end:i}=O(e,t.page.duration);return{start:o,animation:Object.assign(Object.assign({},n),{duration:Math.max(1,i-o)})}}var o;return{start:playsTextUnits(t,n)?y(n,e,t.page.duration):n.delay,animation:n}};export const animate=({element:t,dTime:n,animation:e})=>{if(playsTextUnits(t,e)){return{textUnits:{animation:e,dTime:n}}}const o=animations[e.name];return o?o({element:t,dTime:C(n,e),animation:e}):(console.error("Can not find animation type: "+e.name),{})};export const registerAnimation=(t,n)=>{animations[t]&&console.warn(`Animation "${t}" already exists and will be overridden`),animations[t]=n};
|
package/utils/clipboard.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
11
11
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
12
12
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
13
13
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
14
|
-
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" | "
|
|
14
|
+
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]>;
|
|
15
15
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
16
16
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
17
17
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -32,6 +32,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
32
32
|
type: import("mobx-state-tree").ISimpleType<"exit" | "enter" | "loop">;
|
|
33
33
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
34
34
|
data: import("mobx-state-tree").IType<{} | null | undefined, {}, {}>;
|
|
35
|
+
easing: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<string>>;
|
|
35
36
|
}, {}, import("mobx-state-tree")._NotCustomized, import("mobx-state-tree")._NotCustomized>>;
|
|
36
37
|
blurEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
37
38
|
blurRadius: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
@@ -99,7 +100,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
99
100
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
100
101
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
101
102
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
102
|
-
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" | "
|
|
103
|
+
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]>;
|
|
103
104
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
104
105
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
105
106
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -111,7 +112,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
111
112
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
112
113
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
113
114
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
114
|
-
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" | "
|
|
115
|
+
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]>;
|
|
115
116
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
116
117
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
117
118
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -123,7 +124,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
123
124
|
type: string | undefined;
|
|
124
125
|
name: string | undefined;
|
|
125
126
|
opacity: number | undefined;
|
|
126
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
127
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light" | undefined;
|
|
127
128
|
custom: any;
|
|
128
129
|
visible: boolean | undefined;
|
|
129
130
|
selectable: boolean | undefined;
|
|
@@ -142,7 +143,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
142
143
|
alwaysOnTop: boolean;
|
|
143
144
|
showInExport: boolean;
|
|
144
145
|
} | {
|
|
145
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
146
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
146
147
|
id: string;
|
|
147
148
|
type: string;
|
|
148
149
|
name: string;
|
|
@@ -170,7 +171,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
170
171
|
alwaysOnTop: boolean;
|
|
171
172
|
showInExport: boolean;
|
|
172
173
|
} | {
|
|
173
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
174
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
174
175
|
id: string;
|
|
175
176
|
type: string;
|
|
176
177
|
name: string;
|
|
@@ -205,7 +206,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
205
206
|
readonly isSelected: boolean;
|
|
206
207
|
readonly isSelectedDirectly: boolean;
|
|
207
208
|
} & {
|
|
208
|
-
setAnimation(type:
|
|
209
|
+
setAnimation(type: "enter" | "exit" | "loop", attrs: import("./animations.js").AnimationAttrs): void;
|
|
209
210
|
setFilter(type: any, value: any): void;
|
|
210
211
|
} & {
|
|
211
212
|
set(attrs: any): void;
|
|
@@ -216,7 +217,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
216
217
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
217
218
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
218
219
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
219
|
-
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" | "
|
|
220
|
+
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]>;
|
|
220
221
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
221
222
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
222
223
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -237,6 +238,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
237
238
|
type: import("mobx-state-tree").ISimpleType<"exit" | "enter" | "loop">;
|
|
238
239
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
239
240
|
data: import("mobx-state-tree").IType<{} | null | undefined, {}, {}>;
|
|
241
|
+
easing: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<string>>;
|
|
240
242
|
}, {}, import("mobx-state-tree")._NotCustomized, import("mobx-state-tree")._NotCustomized>>;
|
|
241
243
|
blurEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
242
244
|
blurRadius: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
@@ -304,7 +306,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
304
306
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
305
307
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
306
308
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
307
|
-
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" | "
|
|
309
|
+
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]>;
|
|
308
310
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
309
311
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
310
312
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -316,7 +318,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
316
318
|
type: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
317
319
|
name: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
318
320
|
opacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
319
|
-
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" | "
|
|
321
|
+
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]>;
|
|
320
322
|
custom: import("mobx-state-tree").IType<any, any, any>;
|
|
321
323
|
visible: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
322
324
|
selectable: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
@@ -328,7 +330,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
328
330
|
type: string | undefined;
|
|
329
331
|
name: string | undefined;
|
|
330
332
|
opacity: number | undefined;
|
|
331
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
333
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light" | undefined;
|
|
332
334
|
custom: any;
|
|
333
335
|
visible: boolean | undefined;
|
|
334
336
|
selectable: boolean | undefined;
|
|
@@ -347,7 +349,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
347
349
|
alwaysOnTop: boolean;
|
|
348
350
|
showInExport: boolean;
|
|
349
351
|
} | {
|
|
350
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
352
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
351
353
|
id: string;
|
|
352
354
|
type: string;
|
|
353
355
|
name: string;
|
|
@@ -375,7 +377,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
375
377
|
alwaysOnTop: boolean;
|
|
376
378
|
showInExport: boolean;
|
|
377
379
|
} | {
|
|
378
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
380
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
379
381
|
id: string;
|
|
380
382
|
type: string;
|
|
381
383
|
name: string;
|
|
@@ -410,7 +412,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
410
412
|
readonly isSelected: boolean;
|
|
411
413
|
readonly isSelectedDirectly: boolean;
|
|
412
414
|
} & {
|
|
413
|
-
setAnimation(type:
|
|
415
|
+
setAnimation(type: "enter" | "exit" | "loop", attrs: import("./animations.js").AnimationAttrs): void;
|
|
414
416
|
setFilter(type: any, value: any): void;
|
|
415
417
|
} & {
|
|
416
418
|
set(attrs: any): void;
|
|
@@ -428,7 +430,7 @@ export declare const insertTextElement: (store: StoreType, text: string, extra?:
|
|
|
428
430
|
alwaysOnTop: boolean;
|
|
429
431
|
showInExport: boolean;
|
|
430
432
|
} | {
|
|
431
|
-
blendMode: "normal" | "color" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "
|
|
433
|
+
blendMode: "normal" | "color" | "overlay" | "color-burn" | "color-dodge" | "darken" | "difference" | "exclusion" | "hard-light" | "hue" | "lighten" | "luminosity" | "multiply" | "saturation" | "screen" | "soft-light";
|
|
432
434
|
id: string;
|
|
433
435
|
type: string;
|
|
434
436
|
name: string;
|
package/utils/clipboard.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{forEveryChild as e}from"../model/group-model.js";import{nanoid as t}from"nanoid";import{getTotalClientRect as
|
|
1
|
+
import{forEveryChild as e}from"../model/group-model.js";import{nanoid as t}from"nanoid";import{getTotalClientRect as a}from"./math.js";import{removeTags as o}from"@polotno/core/text";import{insertFileIntoPage as r}from"./insert-file.js";import{decodeBase64 as l,encodeBase64 as n}from"./base64.js";let i={data:[],pageId:"",payloadId:""};const s="polotno_clipboard",d=(e,t,a)=>{i={data:JSON.parse(JSON.stringify(e)),pageId:t||"",payloadId:a||""};try{localStorage.setItem(s,JSON.stringify(e)),localStorage.setItem(s+"_page",t||""),localStorage.setItem(s+"_payload",a||"")}catch(o){}},c=()=>{try{const e=localStorage.getItem(s),t=localStorage.getItem(s+"_page"),a=localStorage.getItem(s+"_payload");if(e){return{data:JSON.parse(e),pageId:t,payloadId:a}}}catch(e){}return i},p="data-polotno-clipboard";export const copy=e=>{var t;0!==e.selectedElements.length&&d(e.selectedElements.map(e=>e.toJSON()),null===(t=e.activePage)||void 0===t?void 0:t.id)};export const cut=e=>{if(0===e.selectedElements.length){return}copy(e);const t=e.selectedElements.filter(e=>e.removable).map(e=>e.id);e.deleteElements(t)};const m=(t,o)=>{t.history.transaction(()=>{var r,l;let n=0,i=0;const{data:s,pageId:c,payloadId:p}=o;if(c===(null===(r=t.activePage)||void 0===r?void 0:r.id)){n=t.width/20,i=t.height/20}else{const o=[];e({children:s},e=>{"group"!==e.type&&o.push(e)});const r=a(o);r.maxX>t.width&&(n=-r.minX),r.maxY>t.height&&(i=-r.minY)}const m=[];e({children:s},e=>{delete e.id,"group"!==e.type&&(e.x+=n,e.y+=i)}),s.forEach(e=>{var a;const o=null===(a=t.activePage)||void 0===a?void 0:a.addElement(e);o&&m.push(o.id)}),d(s,null===(l=t.activePage)||void 0===l?void 0:l.id,p),t.selectElements(m)})};export const paste=e=>{m(e,c())};export const isClipboardEmpty=()=>0===c().data.length;export const resetClipboard=()=>{i={data:[],pageId:"",payloadId:""};try{localStorage.removeItem(s),localStorage.removeItem(s+"_page"),localStorage.removeItem(s+"_payload")}catch(e){}};export const copyToEvent=(a,r)=>{var l,i;if(!r.clipboardData||0===a.selectedElements.length){return!1}const s=a.selectedElements.map(e=>e.toJSON()),c=t(),m={v:1,id:c,pageId:(null===(l=a.activePage)||void 0===l?void 0:l.id)||"",elements:s};r.clipboardData.setData("text/html",`<div ${p}="${(e=>n((new TextEncoder).encode(JSON.stringify(e))))(m)}"></div>`);const g=[];return e({children:s},e=>{"text"===e.type&&g.push(o(e.text))}),r.clipboardData.setData("text/plain",g.join("\n")||" "),r.preventDefault(),d(s,null===(i=a.activePage)||void 0===i?void 0:i.id,c),!0};export const cutToEvent=(e,t)=>{const a=copyToEvent(e,t);if(a){const t=e.selectedElements.filter(e=>e.removable).map(e=>e.id);e.deleteElements(t)}return a};export const insertTextElement=(e,t,a={})=>{var o;const r=e.width/2,l=(e.width+e.height)/2160;return null===(o=e.activePage)||void 0===o?void 0:o.addElement(Object.assign({type:"text",x:e.width/2-r/2,y:e.height/2-15,width:r,fontSize:30*l,text:t},a))};export const pasteFromEvent=(e,t,a={})=>{const o=t.clipboardData;if(!o){return!1}const n=o.getData("text/html");if(n&&n.includes(p)){const a=(e=>{var a;try{const t=null===(a=(new DOMParser).parseFromString(e,"text/html").querySelector(`[${p}]`))||void 0===a?void 0:a.getAttribute(p);if(!t){return null}const o=JSON.parse((new TextDecoder).decode(l(t)));return Array.isArray(o.elements)?o:null}catch(t){return null}})(n);if(a){t.preventDefault();const o=c();return a.id&&a.id===o.payloadId?m(e,o):m(e,{data:a.elements,pageId:a.pageId,payloadId:a.id}),!0}}const i=o.getData("text/plain"),s=o.files?Array.from(o.files):[];if(0===s.length&&(e=>{if(!/<svg[\s>]/i.test(e)){return!1}const t=(new DOMParser).parseFromString(e.trim(),"image/svg+xml");return"svg"===t.documentElement.localName&&0===t.getElementsByTagName("parsererror").length})(i)&&s.push(new File([i.trim()],"pasted.svg",{type:"image/svg+xml"})),s.length>0){return t.preventDefault(),(async()=>{for(const a of s){try{await r({file:a,store:e})}catch(t){console.error("polotno: failed to insert pasted file",t)}}})(),!0}if(i&&i.trim()){if(t.preventDefault(),a.onText){a.onText(i)}else{const t=insertTextElement(e,(e=>e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">"))(i));t&&e.selectElements([t.id])}return!0}return 0===o.types.length&&0!==c().data.length&&(t.preventDefault(),paste(e),!0)};
|
package/utils/duplicate.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{isAlive as e}from"mobx-state-tree";import{forEveryChild as t}from"../model/group-model.js";const n={x:50,y:50},o=new WeakMap;function c(e){const n="group"===e.type?[]:[e];return t(e,e=>{"group"!==e.type&&n.push(e)}),n}export function duplicateElements(t,r){const s=t.filter(t=>e(t));if(!s.length){return}const d=function(e,t){const r=o.get(t),s=e.map(e=>e.id);if(!r||s.length!==r.copyIds.length||!r.copyIds.every(e=>s.includes(e))){return n}const d=t.getElementById(r.sourceId),p=t.getElementById(r.copyIds[0]),l=d&&c(d)[0],i=p&&c(p)[0];return l&&i?{x:i.x-l.x,y:i.y-l.y}:n}(s,r),p=s.filter(e=>!1!==e.removable).map(e=>{const t=e.clone();return c(t).forEach(e=>{e.set({x:e.x+d.x,y:e.y+d.y})}),{sourceId:e.id,copyId:t.id}}),l=p.map(e=>e.copyId);p.length&&o.set(r,{sourceId:p[0].sourceId,copyIds:l}),r.selectElements(l)}export function cloneBehind(e,t,n){const c=e.map(e=>{const n=e.clone({},{skipSelect:!0});return t.setElementZIndex(n.id,t.children.indexOf(e)),n.id});e.length&&o.set(n,{sourceId:c[0],copyIds:e.map(e=>e.id)})}
|
package/utils/fonts.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as A from"mobx";import{triggerLoadError as t,getFontLoadTimeout as o}from"./loader.js";import{polotnoError as e}from"@polotno/core/errors";import{removeTags as n}from"@polotno/core/text";import{decodeBase64 as r}from"./base64.js";import{DEFAULT_RETRIES as s,retryDelayMs as i}from"@polotno/core/retry";import{getGoogleFontsUrl as c,getGoogleFontsVariants as a}from"@polotno/core/google-fonts-url";import{resolveFontFaceStyles as l,googleFontVariantUrl as u,cssFontFamily as f,parseUnicodeRange as d,rangesCover as m,toNumericWeight as p}from"@polotno/core/fonts";export{setGoogleFontsVariants,getGoogleFontsVariants,getGoogleFontsUrl}from"@polotno/core/google-fonts-url";const g=["Roboto","Amatic SC","Press Start 2P","Marck Script","Rubik Mono One"];let h=A.observable([...g]),F=A.observable({value:!1});export function isGoogleFontChanged(){return F.value}export function setGoogleFonts(A){if("default"===A){return h.replace([...g]),void(F.value=!1)}F.value=!0,h.splice(0,h.length),h.push(...A)}export function getFontsList(){return h}export const globalFonts=A.observable([]);export function addGlobalFont(A){globalFonts.push(A)}export function removeGlobalFont(A){const t=globalFonts.findIndex(t=>t.fontFamily===A);-1!==t&&globalFonts.splice(t,1)}export function replaceGlobalFonts(A){globalFonts.replace(A)}const y="BESbswy 0123456789 Il1Ww";function w(A){const t=function(A=""){const t=n(A).replace(/\s+/g," ").trim();return t?Array.from(t).slice(0,10).join(""):""}(A);return t?`${y} ${t}`:y}export function _getFontProbeTextForTesting(A){return w(A)}const b="PolotnoBlank";let x,B;function $(A,t,o,e){null!=B||(B=document.createElement("canvas"));const n=B.getContext("2d");if(!n){return!1}const r=(A,e)=>{n.font=`${t} ${o} 40px ${A}`;const r=n.measureText(e);return r.width>0||r.actualBoundingBoxLeft+r.actualBoundingBoxRight>0};if(r(b,y)){return!1}const s=`${f(A)}, ${b}`;return e.some(A=>r(s,A))}export function measureFontDom(A,t="sans-serif",o="normal",e="normal",n=y){if("undefined"==typeof document||!document.body){return 0}const r=document.createElement("span");r.textContent=n,r.style.cssText=`\n position:absolute;\n visibility:hidden;\n white-space:nowrap;\n top:-9999px;\n left:-9999px;\n font:${o} ${e} 90px ${f(A)}, ${t};\n `,document.body.appendChild(r);const s=r.getBoundingClientRect().width;return r.remove(),s}const E={Arial:!0},v={};export const isFontLoaded=(A,t,o,e)=>{if(t&&o){const n=`${A}_${t}_${o}`;if(!E[n]){return!1}const r=textChars(e),s=v[n];return!![...r].every(A=>null==s?void 0:s.has(A))||function(A,t,o,e){if("undefined"==typeof document||!document.fonts){return!1}const n="italic"===t?"italic":"normal",r=p(o),s=V(A),i=[...e].map(A=>A.codePointAt(0)),c=new Set;let a=!1;for(const u of document.fonts){if(V(u.family)!==s){continue}if(a=!0,u.style!==n){continue}const[A,t=A]=u.weight.split(/\s+/).map(p);if(r<A||r>t){continue}const o=d(u.unicodeRange),e=i.filter(A=>m(o,A));if(e.length){if("loaded"!==u.status){return!1}e.forEach(A=>c.add(A))}}if(a){return i.every(A=>c.has(A))}try{return document.fonts.check(G(A,t,o),e)}catch(l){return!1}}(A,t,o,r)}return Object.keys(E).some(t=>t.startsWith(A+"_"))||!!E[A]};export function _setFontLoadedForTesting(A,t){t?E[A]=!0:delete E[A]}export async function loadFont(A,t,e,n){var r;const s=performance.now()+o();await loadFontFace(A,t,e,n);const i=textChars(n),c=()=>{var o,n;const r=null!==(o=v[n=`${A}_${t}_${e}`])&&void 0!==o?o:v[n]=new Set;for(const A of i){r.add(A)}};if(!i||!(null===(r=document.fonts)||void 0===r?void 0:r.load)){return c()}let a;await Promise.race([document.fonts.load(G(A,t,e),i).catch(()=>{}).then(c),new Promise(A=>{a=setTimeout(A,Math.max(0,s-performance.now()))})]),clearTimeout(a)}const G=(A,t,o)=>`${t} ${o} 16px ${f(A)}`;export function textChars(A){return[...new Set(n(null!=A?A:"").replace(/\s/g,""))].join("")}export async function loadFontFace(A,n,s,c){var a;const l=`${A}_${n}_${s}`;if(injectGoogleFontVariant(A,n,s),E[l]&&!j(A,n,s)){return}const u=!!(null===(a=document.fonts)||void 0===a?void 0:a.load),f=w(c),d=[...textChars(c)];await(null!=x||(x=(async()=>{if("undefined"==typeof FontFace){return}const A=new FontFace(b,r("AAEAAAAKAIAAAwAgT1MvMlHNUusAAACsAAAAYGNtYXAAIQA0AAABDAAAAChnbHlmAAAAAAAAATQAAAAKaGVhZF8WQOUAAAFAAAAANmhoZWEDIf87AAABeAAAACRobXR4AAAAAAAAAZwAAAAIbG9jYQAFAAAAAAGkAAAABm1heHAAAwAAAAABrAAAACBuYW1lCUoZTgAAAcwAAACMcG9zdAADAAAAAAJYAAAAIAAEAAABkAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQT0xPAEAAAP//AyD/OAAAAyAAyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAwAKAAAADAANAAAAAAAcAAAAAAAAAAEAAAAAABD//wAAAAEAAAAAAAAAAAAAAAAAAQAAAAEAAAAAAABfDzz1AAMD6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAgAAAAAAAAABAAADIP84AAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAFAAAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQANgADAAEECQABABgAAAADAAEECQACAA4AGAADAAEECQAEABgAJgADAAEECQAGABgAPgBQAG8AbABvAHQAbgBvAEIAbABhAG4AawBSAGUAZwB1AGwAYQByAFAAbwBsAG8AdABuAG8AQgBsAGEAbgBrAFAAbwBsAG8AdABuAG8AQgBsAGEAbgBrAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));document.fonts.add(A),await A.load()})().catch(A=>{console.error("Polotno could not load its blank probe font: no font can be verified as ready.",A)})),x);const m=o()/60;for(let o=0;o<m;o++){const r=!j(A,n,s);let c=[];if(u){try{c=await document.fonts.load(G(A,n,s),f)}catch(p){}}const a=t=>$(A,n,s,t),m=c.length>0&&c.every(A=>"loaded"===A.status);if(r&&(a([f])||m&&a(d))){return void Q(l,A)}if("missing"===T[A]){if(I(A)){const o=`Font "${A}" not found: not registered and no matching Google Font.`;console.error(o),t(e("FONT_FAILED",o,{family:A,reason:"not-found"}))}return}if(A in C&&!C[A]&&"errored"===T[A]&&injectGoogleFont(A),N[A]&&Y(A)){const n=k(A);if("wait"===n){await new Promise(A=>setTimeout(A,60));continue}if("spent"!==n){W(A);const t=i(n);await new Promise(A=>setTimeout(A,t)),o+=Math.ceil(t/60);continue}if(I(A)){const o=`Failed to load font "${A}": its font file could not be fetched.`;console.error(o),t(e("FONT_FAILED",o,{family:A,reason:"fetch-failed"}))}return}await new Promise(A=>setTimeout(A,60))}console.warn(`Timeout for loading font "${A}". Looks like polotno can't load it. Is it a correct font family?`),t(e("FONT_FAILED",`Timeout for loading font "${A}"`,{family:A,reason:"timeout"}))}const C={},T={},D=new Set;function I(A){return!D.has(A)&&(D.add(A),setTimeout(()=>D.delete(A),o()),!0)}function Q(A,t){E[A]=!0,delete T[t],delete P[t]}const P={};function k(A){const t=P[A],e=!t||Date.now()-t.at>o()?0:t.spent;return e>=s-1?"spent":t&&e>0&&Date.now()-t.at<i(e)?"wait":(P[A]={spent:e+1,at:Date.now()},e+1)}const _={};function j(A,t,o){return"loading"===C[A]||"loading"===_[`${A}|${"italic"===t?1:0}|${parseInt(o,10)}`]}export function injectGoogleFontVariant(A,t,o){const e=parseInt(o,10);if(!Number.isFinite(e)){return}if(function(A){const t=new Set;for(const o of a().split(",")){const e=o.trim().match(/^(\d+)(italic|i)?$/);e&&!!e[2]===A&&t.add(parseInt(e[1],10))}return t}("italic"===t).has(e)){return}if(!(A in C)){return}const n="italic"===t,r=`${A}|${n?1:0}|${e}`;if(_[r]){return}_[r]="loading";const s=document.createElement("link");s.type="text/css",s.href=u(A,n,e),s.rel="stylesheet",s.onload=()=>{_[r]=!0},s.onerror=()=>{_[r]=!0,s.remove()},document.getElementsByTagName("head")[0].appendChild(s)}export function injectGoogleFont(A){if(C[A]){return}const t=c(A),o=document.createElement("link");o.type="text/css",o.href=t,o.rel="stylesheet",o.onload=()=>{C[A]=!0},o.onerror=()=>{o.remove(),C[A]=!1,"errored"===T[A]?function(A){T[A]="probing",fetch(c(A),{mode:"no-cors"}).then(()=>{T[A]="missing"}).catch(()=>{delete T[A]})}(A):T[A]||(T[A]="errored")},document.getElementsByTagName("head")[0].appendChild(o),C[A]="loading"}const N={};let R;function S(){return R||(R=document.getElementById("polotno-font-style"),R||(R=document.createElement("style"),R.id="polotno-font-style",document.head.appendChild(R)),R)}function O(A){const t=A.fontFamily,o=l(A),e=S().sheet;return o.map(A=>{const o=e.insertRule(`\n @font-face{\n font-family:${f(t)};\n src:${A.src};\n font-style:${A.fontStyle};\n font-weight:${A.fontWeight};\n font-display:swap; /* optional but recommended */\n }`,e.cssRules.length);return e.cssRules[o]})}export function injectCustomFont(A){const t=A.fontFamily;if(!A.url&&!A.styles){return}const o=JSON.stringify(l(A)),e=N[t];if(e){if(e.src===o){return}L(e.rules),M(t)}delete T[t],N[t]={font:A,src:o,rules:O(A)}}function L(A){const t=S().sheet;for(const o of A){const A=Array.prototype.indexOf.call(t.cssRules,o);A>=0&&t.deleteRule(A)}}export function removeCustomFont(A){const t=N[A];t&&(L(t.rules),delete N[A],M(A))}function M(A){delete P[A];for(const t of Object.keys(E)){(t===A||t.startsWith(A+"_"))&&(delete E[t],delete v[t])}}function W(A){const t=N[A];t&&(L(t.rules),t.rules=O(t.font))}const V=A=>A.trim().replace(/^["']|["']$/g,"").toLowerCase();function Y(A){const t=document.fonts;if(!t||"function"!=typeof t.forEach){return!1}const o=V(A);let e=!1;return t.forEach(A=>{"error"===A.status&&V(A.family)===o&&(e=!0)}),e}
|
|
1
|
+
import*as A from"mobx";import{triggerLoadError as t,getFontLoadTimeout as o}from"./loader.js";import{polotnoError as e}from"@polotno/core/errors";import{removeTags as n}from"@polotno/core/text";import{decodeBase64 as r}from"./base64.js";import{DEFAULT_RETRIES as s,retryDelayMs as i}from"@polotno/core/retry";import{getGoogleFontsUrl as c,getGoogleFontsVariants as a}from"@polotno/core/google-fonts-url";import{resolveFontFaceStyles as l,googleFontVariantUrl as u,cssFontFamily as f,parseUnicodeRange as d,rangesCover as m,toNumericWeight as p}from"@polotno/core/fonts";export{setGoogleFontsVariants,getGoogleFontsVariants,getGoogleFontsUrl}from"@polotno/core/google-fonts-url";const g=["Roboto","Amatic SC","Press Start 2P","Marck Script","Rubik Mono One"];let h=A.observable([...g]),F=A.observable({value:!1});export function isGoogleFontChanged(){return F.value}export function setGoogleFonts(A){if("default"===A){return h.replace([...g]),void(F.value=!1)}F.value=!0,h.splice(0,h.length),h.push(...A)}export function getFontsList(){return h}export const globalFonts=A.observable([]);export function addGlobalFont(A){globalFonts.push(A)}export function removeGlobalFont(A){const t=globalFonts.findIndex(t=>t.fontFamily===A);-1!==t&&globalFonts.splice(t,1)}export function replaceGlobalFonts(A){globalFonts.replace(A)}const y="BESbswy 0123456789 Il1Ww";function w(A){const t=function(A=""){const t=n(A).replace(/\s+/g," ").trim();return t?Array.from(t).slice(0,10).join(""):""}(A);return t?`${y} ${t}`:y}export function _getFontProbeTextForTesting(A){return w(A)}const b="PolotnoBlank";let x,B;function $(A,t,o,e){null!=B||(B=document.createElement("canvas"));const n=B.getContext("2d");if(!n){return!1}const r=(A,e)=>{n.font=`${t} ${o} 40px ${A}`;const r=n.measureText(e);return r.width>0||r.actualBoundingBoxLeft+r.actualBoundingBoxRight>0};if(r(b,y)){return!1}const s=`${f(A)}, ${b}`;return e.some(A=>r(s,A))}export function measureFontDom(A,t="sans-serif",o="normal",e="normal",n=y){if("undefined"==typeof document||!document.body){return 0}const r=document.createElement("span");r.textContent=n,r.style.cssText=`\n position:absolute;\n visibility:hidden;\n white-space:nowrap;\n top:-9999px;\n left:-9999px;\n font:${o} ${e} 90px ${f(A)}, ${t};\n `,document.body.appendChild(r);const s=r.getBoundingClientRect().width;return r.remove(),s}const E={Arial:!0},v={};export const isFontLoaded=(A,t,o,e)=>{if(t&&o){const n=`${A}_${t}_${o}`;if(!E[n]){return!1}const r=textChars(e),s=v[n];return!![...r].every(A=>null==s?void 0:s.has(A))||function(A,t,o,e){if("undefined"==typeof document||!document.fonts){return!1}const n="italic"===t?"italic":"normal",r=p(o),s=V(A),i=[...e].map(A=>A.codePointAt(0)),c=new Set;let a=!1;for(const u of document.fonts){if(V(u.family)!==s){continue}if(a=!0,u.style!==n){continue}const[A,t=A]=u.weight.split(/\s+/).map(p);if(r<A||r>t){continue}const o=d(u.unicodeRange),e=i.filter(A=>m(o,A));if(e.length){if("loaded"!==u.status){return!1}e.forEach(A=>c.add(A))}}if(a){return i.every(A=>c.has(A))}try{return document.fonts.check(G(A,t,o),e)}catch(l){return!1}}(A,t,o,r)}return Object.keys(E).some(t=>t.startsWith(A+"_"))||!!E[A]};export function _setFontLoadedForTesting(A,t){t?E[A]=!0:delete E[A]}export async function loadFont(A,t,e,n){var r;const s=performance.now()+o();await loadFontFace(A,t,e,n);const i=textChars(n),c=()=>{var o,n;const r=null!==(o=v[n=`${A}_${t}_${e}`])&&void 0!==o?o:v[n]=new Set;for(const A of i){r.add(A)}};if(!i||!(null===(r=document.fonts)||void 0===r?void 0:r.load)){return c()}let a;await Promise.race([document.fonts.load(G(A,t,e),i).catch(()=>{}).then(c),new Promise(A=>{a=setTimeout(A,Math.max(0,s-performance.now()))})]),clearTimeout(a)}const G=(A,t,o)=>`${t} ${o} 16px ${f(A)}`;export function textChars(A){return[...new Set(n(null!=A?A:"").replace(/\s/g,""))].join("")}export async function loadFontFace(A,n,s,c){var a;const l=`${A}_${n}_${s}`;if(injectGoogleFontVariant(A,n,s),E[l]&&!j(A,n,s)){return}const u=!!(null===(a=document.fonts)||void 0===a?void 0:a.load),f=w(c),d=[...textChars(c)];await(null!=x||(x=(async()=>{if("undefined"==typeof FontFace){return}const A=new FontFace(b,r("AAEAAAAKAIAAAwAgT1MvMlHNUusAAACsAAAAYGNtYXAAIQA0AAABDAAAAChnbHlmAAAAAAAAATQAAAAKaGVhZF8WQOUAAAFAAAAANmhoZWEDIf87AAABeAAAACRobXR4AAAAAAAAAZwAAAAIbG9jYQAFAAAAAAGkAAAABm1heHAAAwAAAAABrAAAACBuYW1lCUoZTgAAAcwAAACMcG9zdAADAAAAAAJYAAAAIAAEAAABkAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQT0xPAEAAAP//AyD/OAAAAyAAyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAwAKAAAADAANAAAAAAAcAAAAAAAAAAEAAAAAABD//wAAAAEAAAAAAAAAAAAAAAAAAQAAAAEAAAAAAABfDzz1AAMD6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAgAAAAAAAAABAAADIP84AAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAFAAAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQANgADAAEECQABABgAAAADAAEECQACAA4AGAADAAEECQAEABgAJgADAAEECQAGABgAPgBQAG8AbABvAHQAbgBvAEIAbABhAG4AawBSAGUAZwB1AGwAYQByAFAAbwBsAG8AdABuAG8AQgBsAGEAbgBrAFAAbwBsAG8AdABuAG8AQgBsAGEAbgBrAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="),{weight:"1 1000"});document.fonts.add(A),await A.load()})().catch(A=>{console.error("Polotno could not load its blank probe font: no font can be verified as ready.",A)})),x);const m=o()/60;for(let o=0;o<m;o++){const r=!j(A,n,s);let c=[];if(u){try{c=await document.fonts.load(G(A,n,s),f)}catch(p){}}const a=t=>$(A,n,s,t),m=c.length>0&&c.every(A=>"loaded"===A.status);if(r&&(a([f])||m&&a(d))){return void Q(l,A)}if("missing"===T[A]){if(I(A)){const o=`Font "${A}" not found: not registered and no matching Google Font.`;console.error(o),t(e("FONT_FAILED",o,{family:A,reason:"not-found"}))}return}if(A in C&&!C[A]&&"errored"===T[A]&&injectGoogleFont(A),N[A]&&Y(A)){const n=k(A);if("wait"===n){await new Promise(A=>setTimeout(A,60));continue}if("spent"!==n){W(A);const t=i(n);await new Promise(A=>setTimeout(A,t)),o+=Math.ceil(t/60);continue}if(I(A)){const o=`Failed to load font "${A}": its font file could not be fetched.`;console.error(o),t(e("FONT_FAILED",o,{family:A,reason:"fetch-failed"}))}return}await new Promise(A=>setTimeout(A,60))}console.warn(`Timeout for loading font "${A}". Looks like polotno can't load it. Is it a correct font family?`),t(e("FONT_FAILED",`Timeout for loading font "${A}"`,{family:A,reason:"timeout"}))}const C={},T={},D=new Set;function I(A){return!D.has(A)&&(D.add(A),setTimeout(()=>D.delete(A),o()),!0)}function Q(A,t){E[A]=!0,delete T[t],delete P[t]}const P={};function k(A){const t=P[A],e=!t||Date.now()-t.at>o()?0:t.spent;return e>=s-1?"spent":t&&e>0&&Date.now()-t.at<i(e)?"wait":(P[A]={spent:e+1,at:Date.now()},e+1)}const _={};function j(A,t,o){return"loading"===C[A]||"loading"===_[`${A}|${"italic"===t?1:0}|${parseInt(o,10)}`]}export function injectGoogleFontVariant(A,t,o){const e=parseInt(o,10);if(!Number.isFinite(e)){return}if(function(A){const t=new Set;for(const o of a().split(",")){const e=o.trim().match(/^(\d+)(italic|i)?$/);e&&!!e[2]===A&&t.add(parseInt(e[1],10))}return t}("italic"===t).has(e)){return}if(!(A in C)){return}const n="italic"===t,r=`${A}|${n?1:0}|${e}`;if(_[r]){return}_[r]="loading";const s=document.createElement("link");s.type="text/css",s.href=u(A,n,e),s.rel="stylesheet",s.onload=()=>{_[r]=!0},s.onerror=()=>{_[r]=!0,s.remove()},document.getElementsByTagName("head")[0].appendChild(s)}export function injectGoogleFont(A){if(C[A]){return}const t=c(A),o=document.createElement("link");o.type="text/css",o.href=t,o.rel="stylesheet",o.onload=()=>{C[A]=!0},o.onerror=()=>{o.remove(),C[A]=!1,"errored"===T[A]?function(A){T[A]="probing",fetch(c(A),{mode:"no-cors"}).then(()=>{T[A]="missing"}).catch(()=>{delete T[A]})}(A):T[A]||(T[A]="errored")},document.getElementsByTagName("head")[0].appendChild(o),C[A]="loading"}const N={};let R;function S(){return R||(R=document.getElementById("polotno-font-style"),R||(R=document.createElement("style"),R.id="polotno-font-style",document.head.appendChild(R)),R)}function O(A){const t=A.fontFamily,o=l(A),e=S().sheet;return o.map(A=>{const o=e.insertRule(`\n @font-face{\n font-family:${f(t)};\n src:${A.src};\n font-style:${A.fontStyle};\n font-weight:${A.fontWeight};\n font-display:swap; /* optional but recommended */\n }`,e.cssRules.length);return e.cssRules[o]})}export function injectCustomFont(A){const t=A.fontFamily;if(!A.url&&!A.styles){return}const o=JSON.stringify(l(A)),e=N[t];if(e){if(e.src===o){return}L(e.rules),M(t)}delete T[t],N[t]={font:A,src:o,rules:O(A)}}function L(A){const t=S().sheet;for(const o of A){const A=Array.prototype.indexOf.call(t.cssRules,o);A>=0&&t.deleteRule(A)}}export function removeCustomFont(A){const t=N[A];t&&(L(t.rules),delete N[A],M(A))}function M(A){delete P[A];for(const t of Object.keys(E)){(t===A||t.startsWith(A+"_"))&&(delete E[t],delete v[t])}}function W(A){const t=N[A];t&&(L(t.rules),t.rules=O(t.font))}const V=A=>A.trim().replace(/^["']|["']$/g,"").toLowerCase();function Y(A){const t=document.fonts;if(!t||"function"!=typeof t.forEach){return!1}const o=V(A);let e=!1;return t.forEach(A=>{"error"===A.status&&V(A.family)===o&&(e=!0)}),e}
|
package/utils/from-svg.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"konva";import{getImageSize as e,getCrop as
|
|
1
|
+
import t from"konva";import{getImageSize as e,getCrop as n}from"@polotno/core/image";import{parseSanitizedSvg as o,resolveSvgSize as r}from"@polotno/core/svg";let i=0;const s=()=>(i+=1,i.toString());function a(t){const{width:e,height:n}=r({documentElement:t});return t.setAttribute("width",`${e}px`),t.setAttribute("height",`${n}px`),{width:e,height:n}}function g(e){const n=function(e){var n,o,r;const i=(null===(r=null===(o=null===(n=e.transform)||void 0===n?void 0:n.baseVal)||void 0===o?void 0:o.consolidate())||void 0===r?void 0:r.matrix)||new DOMMatrix;return new t.Transform([i.a,i.b,i.c,i.d,i.e,i.f])}(e);return e.parentNode&&"svg"!==e.parentNode.nodeName?g(e.parentNode).multiply(n):n}function l(t){return g(t).decompose()}function u(t){const{x:e,y:n}=t.getBoundingClientRect();if("svg"===t.nodeName){return{x:e,y:n}}const o=u(t.parentNode);return{x:e+o.x,y:n+o.y}}function h(t){return t.getElementsByTagName("text").length>0}function d(t){const e=Array.from(t.getElementsByTagName("rect")).find(t=>{const e=t.getAttribute("fill");return e&&e.indexOf("url")>=0});return t.getElementsByTagName("image").length>0||e}export function svgToURL(t){return"data:image/svg+xml;base64,"+window.btoa(unescape(encodeURIComponent(t)))}function c(t="url(#pattern)"){return t.replace(/url\(([^)]+)\)/,"$1").replace(/'/g,"")}function f(t){if(!t){return"Roboto"}if(t.includes("OpenSans")){return"Open Sans"}if(t.includes("Montserrat")){return"Montserrat"}const e=t.split(",").map(t=>t.trim());return e[1]||e[0]}function m(t){return t.parentNode&&"svg"===t.parentNode.nodeName?t.parentNode:t.parentElement?m(t.parentElement):null}async function x(t){if("defs"===t.nodeName){return[]}const o=t.getAttribute("id")||s();if("text"===t.nodeName){let e=Array.from(t.children).map(t=>t.textContent);0===e.length&&(e=[t.textContent]);const n=e.join("\n"),r=parseFloat(t.getAttribute("font-size")||t.style.fontSize||"20"),{x:i,y:s,rotation:a}=l(t),g=t.getAttribute("text-anchor")||"start";let u="left";return"middle"===g?u="center":"end"===g&&(u="right"),[{type:"text",text:n,id:o,x:i+t.getBBox().x,y:s+t.getBBox().y,fontSize:r,rotation:a,lineHeight:t.getBBox().height/e.length/r,width:t.getBBox().width,height:t.getBBox().height,fontFamily:f(t.getAttribute("font-family")||void 0),fontWeight:t.getAttribute("font-weight")||"normal",fontStyle:t.getAttribute("font-style")||"normal",align:u,fill:t.getAttribute("fill")||t.style.fill||"black"}]}if("image"===t.nodeName){const{x:r,y:i,rotation:s,scaleX:a,scaleY:g}=l(t),u=t.getBBox().width*a,h=t.getBBox().height*g,d=t.getAttribute("xlink:href")||"",c=await e(d),f=n({width:u,height:h},c);return[Object.assign({type:"image",id:o,x:r+t.getBBox().x,y:i+t.getBBox().y,width:u,height:h,src:d,rotation:s},f)]}if("rect"===t.nodeName){const{x:r,y:i,rotation:s}=l(t),a=parseFloat(t.getAttribute("width")||"0"),g=parseFloat(t.getAttribute("height")||"0"),u=t.getAttribute("fill")||"",h=u.indexOf("url")>=0,d=h?c(u):null,f=d?document.querySelector(d):null;if(h){const l=null==f?void 0:f.querySelector("use"),u=null==l?void 0:l.getAttribute("xlink:href"),h=u&&document.querySelector(u),d=h&&h.getAttribute("xlink:href");if(d){const l=await e(d),u=n({width:a,height:g},l);return[Object.assign({type:"image",id:o,x:r+t.getBBox().x,y:i+t.getBBox().y,width:a,height:g,src:d,rotation:s},u)]}}t.setAttribute("transform","");const m=t.getBBox();t.setAttribute("x","0"),t.setAttribute("y","0");const x=`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${a}" height="${g}" viewBox="0 0 ${a} ${g}">${(null==f?void 0:f.outerHTML)||""}${t.outerHTML}</svg>`;return[{type:"svg",id:o,keepRatio:!1,x:r+m.x,y:i+m.y,width:a,height:g,src:svgToURL(x)}]}if("path"===t.nodeName||"polygon"===t.nodeName||"circle"===t.nodeName){const{x:e,y:n,rotation:r,scaleX:i,scaleY:s}=l(t),a=t.getBBox(),{width:g,height:u}=a;t.setAttribute("transform","");const h=t.getAttribute("fill")||"",d=h.indexOf("url")>=0?c(h):null,f=d?document.querySelector(d):null;let m=f?[f]:[];if(f&&f.getAttribute("xlink:href")){m=[f];const t=document.querySelector(f.getAttribute("xlink:href"));t&&m.push(t)}const x=`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${g}" height="${u}" viewBox="0 0 ${g} ${u}"><defs>${m.map(t=>t.outerHTML).join("")}</defs><g transform="translate(${-a.x} ${-a.y})">${t.outerHTML}</g></svg>`;return[{type:"svg",id:o,keepRatio:!1,x:e+a.x*i,y:n+a.y*s,width:g*i,height:u*s,rotation:r,src:svgToURL(x)}]}if((t=>"g"===t.nodeName&&t.getBBox().width<450&&t.getBBox().height<450)(t)&&!h(t)&&!d(t)){const{x:e,y:n}=t.getBBox(),{width:r,height:i}=(u(t),t.getBBox()),s=Array.from(t.querySelectorAll("*"));let g=e,l=n;s.forEach(t=>{if("g"===t.nodeName){return}const e=t.getBBox();g=Math.min(g,e.x),l=Math.min(l,e.y)});const h=t.getBoundingClientRect(),d=m(t),c=null==d?void 0:d.getBoundingClientRect(),f=t.getAttribute("transform");t.setAttribute("transform","");const x=`<svg xmlns="http://www.w3.org/2000/svg" width="${r}" height="${i}" viewBox="0 0 ${r} ${i}"><g transform="translate(${-e} ${-n})">${t.outerHTML}</g></svg>`;t.setAttribute("transform",f||"");const p=a(d);return[{type:"svg",id:o,keepRatio:!1,x:(h.x-c.x)/c.width*p.width,y:(h.y-c.y)/c.height*p.height,width:r,height:i,name:t.getAttribute("id")||"",src:svgToURL(x)}]}if(!h(t)&&!d(t)){const e=[];for(const n of Array.from(t.children)){e.push(...await x(n))}return e}const r=[];for(const e of Array.from(t.children)){r.push(...await x(e))}return r}export async function svgToJson(t){const e=o(t);if(!e){throw new Error("svgToJson: the markup contains no <svg> element")}const n=document.createElement("div");document.body.appendChild(n);const r=n.appendChild(document.importNode(e,!0)),{width:i,height:g}=a(r),l=[];for(const o of Array.from(r.children)){l.push(...await x(o))}return n.parentElement.removeChild(n),{width:i,height:g,pages:[{id:s(),children:l}]}}
|
package/utils/image-dpi.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{decodeBase64 as t,encodeBase64 as e}from"./base64.js";const n=[112,72,89,115],
|
|
1
|
+
import{decodeBase64 as t,encodeBase64 as e}from"./base64.js";const n=[137,80,78,71,13,10,26,10],r=n.length,s=[255,216],o=[112,72,89,115],l=(()=>{const t=new Uint32Array(256);for(let e=0;e<256;e++){let n=e;for(let t=0;t<8;t++){n=1&n?3988292384^n>>>1:n>>>1}t[e]=n>>>0}return t})(),i=(t,e,n)=>{t[e]=n>>>24&255,t[e+1]=n>>>16&255,t[e+2]=n>>>8&255,t[e+3]=255&n},a=(t,e)=>{const n=(t=>{const e=new Uint8Array(9),n=new DataView(e.buffer);n.setUint32(0,t,!1),n.setUint32(4,t,!1),e[8]=1;const r=new Uint8Array(8+e.length+4);i(r,0,e.length),r.set(o,4),r.set(e,8);const s=new Uint8Array(4+e.length);return s.set(o,0),s.set(e,4),i(r,8+e.length,(t=>{let e=4294967295;for(let n=0;n<t.length;n++){e=l[255&(e^t[n])]^e>>>8}return(4294967295^e)>>>0})(s)),r})(Math.round(e/.0254)),s=((t,e)=>{let n=r;for(;n+8<=t.length;){const r=t[n]<<24|t[n+1]<<16|t[n+2]<<8|t[n+3],s=n+4,o=t.subarray(s,s+4),l=8+r+4;if(e.every((t,e)=>o[e]===t)){return{offset:n,totalLength:l}}n+=l}return null})(t,o);if(s){const e=new Uint8Array(t.length-s.totalLength+n.length);return e.set(t.slice(0,s.offset),0),e.set(n,s.offset),e.set(t.slice(s.offset+s.totalLength),s.offset+n.length),e}const a=t[r]<<24|t[r+1]<<16|t[r+2]<<8|t[r+3],f=r+8+a+4;if(f>t.length){return t}const g=new Uint8Array(t.length+n.length);return g.set(t.slice(0,f),0),g.set(n,f),g.set(t.slice(f),f+n.length),g};export const addDpiMetadataToImage=(r,o,l)=>{const i=r.indexOf(","),f=i>=0?r.slice(i+1):r,g=t(f);if(!("image/png"===o?n:s).every((t,e)=>g[e]===t)){return r}const c="image/png"===o?a(g,l):((t,e)=>{const n=(t=>{let e=2;for(;e+4<t.length;){if(255!==t[e]){return-1}const n=t[e+1];if(216===n||217===n){e+=2;continue}const r=t[e+2]<<8|t[e+3];if(r<2){return-1}if(224===n&&74===t[e+4]&&70===t[e+5]&&73===t[e+6]&&70===t[e+7]&&0===t[e+8]){return e}const s=e+2+r;if(s<=e||s>t.length){return-1}e=s}return-1})(t);if(n>=0){const r=new Uint8Array(t),s=n+11;return r[s]=1,r[s+1]=e>>8&255,r[s+2]=255&e,r[s+3]=e>>8&255,r[s+4]=255&e,r}const r=(t=>{const e=new Uint8Array(18);return e[0]=255,e[1]=224,e[2]=0,e[3]=16,e[4]=74,e[5]=70,e[6]=73,e[7]=70,e[8]=0,e[9]=1,e[10]=1,e[11]=1,e[12]=t>>8&255,e[13]=255&t,e[14]=t>>8&255,e[15]=255&t,e})(e),s=new Uint8Array(t.length+r.length);return s.set(t.subarray(0,2),0),s.set(r,2),s.set(t.subarray(2),2+r.length),s})(g,l);return`data:${o};base64,${e(c)}`};
|