polotno 3.7.0 → 3.9.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/text-panel.js +3 -3
- package/blueprint/toolbar/text-toolbar.js +2 -2
- package/canvas/downscale.d.ts +3 -0
- package/canvas/downscale.js +1 -0
- package/canvas/html-element.js +1 -1
- package/canvas/image-element.d.ts +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/quill-font-formats.js +1 -1
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.js +5 -2
- package/canvas/text-element.js +1 -1
- package/canvas/video-element.js +1 -1
- package/model/group-model.d.ts +2 -12
- package/model/page-model.d.ts +2 -12
- package/model/store.d.ts +18 -108
- package/model/table-model.d.ts +2 -12
- package/model/table-model.js +1 -1
- package/model/text-model.js +1 -1
- package/package.json +7 -7
- package/polotno.bundle.js +171 -170
- package/side-panel/images-grid.d.ts +2 -1
- package/side-panel/images-grid.js +1 -1
- package/side-panel/select-svg.js +1 -1
- package/side-panel/text-panel.js +1 -1
- package/side-panel/text-template.d.ts +12 -0
- package/side-panel/text-template.js +1 -0
- package/toolbar/text-toolbar.js +1 -1
- package/ui.css +1 -1
- package/utils/animations.d.ts +7 -1
- package/utils/crop.d.ts +2 -2
- package/utils/flags.d.ts +1 -1
- package/utils/font-metric.js +1 -1
- package/utils/fonts.js +1 -1
- package/utils/from-svg.js +1 -1
- package/utils/html-to-svg/font-inliner.js +1 -1
- package/utils/html2canvas.js +1 -1
- package/utils/loader.d.ts +1 -1
- package/utils/math.d.ts +21 -6
- package/utils/measure-text.js +1 -1
- package/utils/rich-text-html.js +1 -1
- package/utils/text-format-state.js +1 -1
- package/utils/text-format.js +1 -1
- package/utils/unit.d.ts +6 -5
- package/utils/validate-key.d.ts +15 -2
- package/utils/validate-key.js +1 -1
- package/utils/wait.d.ts +1 -1
package/utils/crop.d.ts
CHANGED
package/utils/flags.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare const setRemoveBackgroundEnabled: (value: boolean) => void;
|
|
|
17
17
|
export declare const useRemoveBackground: (value: boolean) => void;
|
|
18
18
|
export declare const setAiTextEnabled: (value: boolean) => void;
|
|
19
19
|
export declare const useHtmlTextRender: (value: boolean) => void;
|
|
20
|
-
export declare const setLegacyRichTextEnabled: (value:
|
|
20
|
+
export declare const setLegacyRichTextEnabled: (value: boolean) => void;
|
|
21
21
|
export declare const setRichTextEnabled: (_value: boolean) => void;
|
|
22
22
|
export declare const setRenderTagTextEnabled: (_value: boolean) => void;
|
|
23
23
|
export declare const setDownScalingEnabled: (value: boolean) => void;
|
package/utils/font-metric.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let
|
|
1
|
+
import{cssFontFamily as t}from"@polotno/core/fonts";let e,n,i,o=!1;const h={chars:{capHeight:"S",baseline:"n",xHeight:"x",descent:"p",ascent:"h",tittle:"i"}},a=(t="top")=>{const e="bottom"===t?i.height:0;n.setTransform(1,0,0,1,0,e),n.textBaseline=t},s=()=>{a("bottom");const t=i.height-g("A");return a("top"),g("A")+t},r=t=>((t=>{n.clearRect(0,0,i.width,i.height),n.fillText(t,i.width/2,e,i.width)})(t),n.getImageData(0,0,i.width,i.height).data),l=t=>Math.round((t=>{for(let e=3,n=t.length;e<n;e+=4){if(t[e]>0){return(e-3)/4}}return t.length})(r(t))/i.width)-e,g=t=>Math.round((t=>{for(let e=t.length-1;e>=3;e-=4){if(t[e]>0){return e/4}}return 0})(r(t))/i.width)-e,c=({fontFamily:a="Times",fontWeight:r="normal",fontSize:c=200,origin:d="baseline"}={})=>(((h,a,s)=>{o||(i=document.createElement("canvas"),n=i.getContext("2d"),o=!0),e=.5*a,i.width=2*a,i.height=2*a+e,n.font=`${s} ${a}px ${t(h)}`,n.textBaseline="top",n.textAlign="center"})(a,c,r),Object.assign(Object.assign({},((t=h.chars)=>({capHeight:l(t.capHeight),baseline:g(t.baseline),xHeight:l(t.xHeight),descent:g(t.descent),bottom:s(),ascent:l(t.ascent),tittle:l(t.tittle),top:0}))()),{fontFamily:a,fontWeight:r,fontSize:c}));c.settings=h;export default c;
|
package/utils/fonts.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as t from"mobx";import{triggerLoadError as o,getFontLoadTimeout as n}from"./loader.js";import{polotnoError as e}from"@polotno/core/errors";import{removeTags as r}from"@polotno/core/text";import{getGoogleFontsUrl as s,getGoogleFontsVariants as a}from"@polotno/core/google-fonts-url";import{resolveFontFaceStyles as i,googleFontVariantUrl as c}from"@polotno/core/fonts";export{setGoogleFontsVariants,getGoogleFontsVariants,getGoogleFontsUrl}from"@polotno/core/google-fonts-url";let
|
|
1
|
+
import*as t from"mobx";import{triggerLoadError as o,getFontLoadTimeout as n}from"./loader.js";import{polotnoError as e}from"@polotno/core/errors";import{removeTags as r}from"@polotno/core/text";import{getGoogleFontsUrl as s,getGoogleFontsVariants as a}from"@polotno/core/google-fonts-url";import{resolveFontFaceStyles as i,googleFontVariantUrl as c,cssFontFamily as l}from"@polotno/core/fonts";export{setGoogleFontsVariants,getGoogleFontsVariants,getGoogleFontsUrl}from"@polotno/core/google-fonts-url";let u=t.observable(["Roboto","Amatic SC","Press Start 2P","Marck Script","Rubik Mono One"]),f=t.observable({value:!1});export function isGoogleFontChanged(){return f.value}export function setGoogleFonts(t){"default"!==t?(f.value=!0,u.splice(0,u.length),u.push(...t)):f.value=!1}export function getFontsList(){return u}export const globalFonts=t.observable([]);export function addGlobalFont(t){globalFonts.push(t)}export function removeGlobalFont(t){const o=globalFonts.findIndex(o=>o.fontFamily===t);-1!==o&&globalFonts.splice(o,1)}export function replaceGlobalFonts(t){globalFonts.replace(t)}const d="BESbswy 0123456789 Il1Ww",m=["sans-serif","serif","monospace"];function p(t){const o=function(t=""){const o=r(t).replace(/\s+/g," ").trim();return o?Array.from(o).slice(0,10).join(""):""}(t);return o?`${d} ${o}`:d}export function _getFontProbeTextForTesting(t){return p(t)}let g;function x(t){return{width:t.width,actualBoundingBoxLeft:t.actualBoundingBoxLeft||0,actualBoundingBoxRight:t.actualBoundingBoxRight||0,actualBoundingBoxAscent:t.actualBoundingBoxAscent||0,actualBoundingBoxDescent:t.actualBoundingBoxDescent||0}}function h(t="sans-serif",o="normal",n="normal",e=d){g||(g=document.createElement("canvas"));const r=g.getContext("2d");return r.font=`${o} ${n} 40px ${t}`,x(r.measureText(e))}function B(t,o,n,e,r){return m.some(s=>{const a=function(t,o="sans-serif",n="normal",e="normal",r=d){g||(g=document.createElement("canvas"));const s=g.getContext("2d");return s.font=`${n} ${e} 40px ${l(t)}, ${o}`,x(s.measureText(r))}(t,s,o,n,e);return function(t,o,n=.01){return Math.abs(t.width-o.width)>n||Math.abs(t.actualBoundingBoxLeft-o.actualBoundingBoxLeft)>n||Math.abs(t.actualBoundingBoxRight-o.actualBoundingBoxRight)>n||Math.abs(t.actualBoundingBoxAscent-o.actualBoundingBoxAscent)>n||Math.abs(t.actualBoundingBoxDescent-o.actualBoundingBoxDescent)>n}(a,r[s])})}export function measureFontDom(t,o="sans-serif",n="normal",e="normal",r=d){if("undefined"==typeof document||!document.body){return 0}const s=document.createElement("span");s.textContent=r,s.style.cssText=`\n position:absolute;\n visibility:hidden;\n white-space:nowrap;\n top:-9999px;\n left:-9999px;\n font:${n} ${e} 90px ${l(t)}, ${o};\n `,document.body.appendChild(s);const a=s.getBoundingClientRect().width;return s.remove(),a}const F={Arial:!0};export const isFontLoaded=(t,o,n)=>o&&n?!!F[`${t}_${o}_${n}`]:Object.keys(F).some(o=>o.startsWith(t+"_"))||!!F[t];export function _setFontLoadedForTesting(t,o){o?F[t]=!0:delete F[t]}export async function loadFont(t,r,s,a){var i;const c=`${t}_${r}_${s}`;if(F[c]){return}injectGoogleFontVariant(t,r,s);const u=!!(null===(i=document.fonts)||void 0===i?void 0:i.load),f=p(a),d={"sans-serif":h("sans-serif",r,s,f),serif:h("serif",r,s,f),monospace:h("monospace",r,s,f)};if(u){try{if(await document.fonts.load(`${r} ${s} 16px ${l(t)}`,f),B(t,r,s,f,d)){return void w(c,t)}}catch(g){}}const m=n()/60;for(let n=0;n<m;n++){if("missing"===b[t]){if(!y.has(t)){y.add(t),setTimeout(()=>y.delete(t),v);const n=`Font "${t}" not found: not registered and no matching Google Font.`;console.error(n),o(e("FONT_FAILED",n,{family:t,reason:"not-found"}))}return}if(t in $&&!$[t]&&"errored"===b[t]&&injectGoogleFont(t),u){try{await document.fonts.load(`${r} ${s} 16px ${l(t)}`,f)}catch(x){}}if(B(t,r,s,f,d)){return void w(c,t)}await new Promise(t=>setTimeout(t,60))}console.warn(`Timeout for loading font "${t}". Looks like polotno can't load it. Is it a correct font family?`),o(e("FONT_FAILED",`Timeout for loading font "${t}"`,{family:t,reason:"timeout"}))}const $={},b={},y=new Set,v=500;function w(t,o){F[t]=!0,delete b[o]}const G={};export function injectGoogleFontVariant(t,o,n){const e=parseInt(n,10);if(!Number.isFinite(e)){return}if(function(t){const o=new Set;for(const n of a().split(",")){const e=n.trim().match(/^(\d+)(italic|i)?$/);e&&!!e[2]===t&&o.add(parseInt(e[1],10))}return o}("italic"===o).has(e)){return}if(!(t in $)){return}const r="italic"===o,s=`${t}|${r?1:0}|${e}`;if(G[s]){return}G[s]=!0;const i=document.createElement("link");i.type="text/css",i.href=c(t,r,e),i.rel="stylesheet",i.onerror=()=>{i.remove()},document.getElementsByTagName("head")[0].appendChild(i)}export function injectGoogleFont(t){if($[t]){return}const o=s(t),n=document.createElement("link");n.type="text/css",n.href=o,n.rel="stylesheet",n.onerror=()=>{n.remove(),$[t]=!1,"errored"===b[t]?function(t){b[t]="probing",fetch(s(t),{mode:"no-cors"}).then(()=>{b[t]="missing"}).catch(()=>{delete b[t]})}(t):b[t]||(b[t]="errored")},document.getElementsByTagName("head")[0].appendChild(n),$[t]=!0}const T={};let E;export function injectCustomFont(t){const o=t.fontFamily;if(T[o]){return}if(!t.url&&!t.styles){return}delete b[o];const n=i(t),e=(E||(E=document.getElementById("polotno-font-style"),E||(E=document.createElement("style"),E.id="polotno-font-style",document.head.appendChild(E)),E)).sheet;n.forEach(t=>{e.insertRule(`\n @font-face{\n font-family:${l(o)};\n src:${t.src};\n font-style:${t.fontStyle};\n font-weight:${t.fontWeight};\n font-display:swap; /* optional but recommended */\n }`,e.cssRules.length)}),T[t.fontFamily]=!0}
|
package/utils/from-svg.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"konva";import{getImageSize as e,getCrop as r}from"@polotno/core/image";let n=0;const o=()=>(n+=1,n.toString());function i(t){const e=t.getAttribute("viewBox"),[r,n,o,i]=(null==e?void 0:e.split(" "))||[];return(!t.getAttribute("width")||t.getAttribute("width").indexOf("%")>=0)&&t.setAttribute("width",o+"px"),(!t.getAttribute("height")||t.getAttribute("height").indexOf("%")>=0)&&t.setAttribute("height",i+"px"),{width:parseFloat(t.getAttribute("width")||o),height:parseFloat(t.getAttribute("height")||i)}}function a(e){const r=function(e){var r,n,o;const i=(null===(o=null===(n=null===(r=e.transform)||void 0===r?void 0:r.baseVal)||void 0===n?void 0:n.consolidate())||void 0===o?void 0:o.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?a(e.parentNode).multiply(r):r}function s(t){return a(t).decompose()}function l(t){const{x:e,y:r}=t.getBoundingClientRect();if("svg"===t.nodeName){return{x:e,y:r}}const n=l(t.parentNode);return{x:e+n.x,y:r+n.y}}function g(t){return t.getElementsByTagName("text").length>0}function u(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 h(t="url(#pattern)"){return t.replace(/url\(([^)]+)\)/,"$1").replace(/'/g,"")}function c(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 d(t){return t.parentNode&&"svg"===t.parentNode.nodeName?t.parentNode:t.parentElement?d(t.parentElement):null}async function f(t){if("defs"===t.nodeName){return[]}const n=t.getAttribute("id")||o();if("text"===t.nodeName){let e=Array.from(t.children).map(t=>t.textContent);0===e.length&&(e=[t.textContent]);const r=e.join("\n"),o=parseFloat(t.getAttribute("font-size")||t.style.fontSize||"20"),{x:i,y:a,rotation:l}=s(t),g=t.getAttribute("text-anchor")||"start";let u="left";return"middle"===g?u="center":"end"===g&&(u="right"),[{type:"text",text:r,id:n,x:i+t.getBBox().x,y:a+t.getBBox().y,fontSize:o,rotation:l,lineHeight:t.getBBox().height/e.length/o,width:t.getBBox().width,height:t.getBBox().height,fontFamily:c(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:o,y:i,rotation:a,scaleX:l,scaleY:g}=s(t),u=t.getBBox().width*l,h=t.getBBox().height*g,c=t.getAttribute("xlink:href")||"",d=await e(c),f=r({width:u,height:h},d);return[Object.assign({type:"image",id:n,x:o+t.getBBox().x,y:i+t.getBBox().y,width:u,height:h,src:c,rotation:a},f)]}if("rect"===t.nodeName){const{x:o,y:i,rotation:a}=s(t),l=parseFloat(t.getAttribute("width")||"0"),g=parseFloat(t.getAttribute("height")||"0"),u=t.getAttribute("fill")||"",c=u.indexOf("url")>=0,d=c?h(u):null,f=d?document.querySelector(d):null;if(c){const s=null==f?void 0:f.querySelector("use"),u=null==s?void 0:s.getAttribute("xlink:href"),h=u&&document.querySelector(u)
|
|
1
|
+
import t from"konva";import{getImageSize as e,getCrop as r}from"@polotno/core/image";let n=0;const o=()=>(n+=1,n.toString());function i(t){const e=t.getAttribute("viewBox"),[r,n,o,i]=(null==e?void 0:e.split(" "))||[];return(!t.getAttribute("width")||t.getAttribute("width").indexOf("%")>=0)&&t.setAttribute("width",o+"px"),(!t.getAttribute("height")||t.getAttribute("height").indexOf("%")>=0)&&t.setAttribute("height",i+"px"),{width:parseFloat(t.getAttribute("width")||o),height:parseFloat(t.getAttribute("height")||i)}}function a(e){const r=function(e){var r,n,o;const i=(null===(o=null===(n=null===(r=e.transform)||void 0===r?void 0:r.baseVal)||void 0===n?void 0:n.consolidate())||void 0===o?void 0:o.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?a(e.parentNode).multiply(r):r}function s(t){return a(t).decompose()}function l(t){const{x:e,y:r}=t.getBoundingClientRect();if("svg"===t.nodeName){return{x:e,y:r}}const n=l(t.parentNode);return{x:e+n.x,y:r+n.y}}function g(t){return t.getElementsByTagName("text").length>0}function u(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 h(t="url(#pattern)"){return t.replace(/url\(([^)]+)\)/,"$1").replace(/'/g,"")}function c(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 d(t){return t.parentNode&&"svg"===t.parentNode.nodeName?t.parentNode:t.parentElement?d(t.parentElement):null}async function f(t){if("defs"===t.nodeName){return[]}const n=t.getAttribute("id")||o();if("text"===t.nodeName){let e=Array.from(t.children).map(t=>t.textContent);0===e.length&&(e=[t.textContent]);const r=e.join("\n"),o=parseFloat(t.getAttribute("font-size")||t.style.fontSize||"20"),{x:i,y:a,rotation:l}=s(t),g=t.getAttribute("text-anchor")||"start";let u="left";return"middle"===g?u="center":"end"===g&&(u="right"),[{type:"text",text:r,id:n,x:i+t.getBBox().x,y:a+t.getBBox().y,fontSize:o,rotation:l,lineHeight:t.getBBox().height/e.length/o,width:t.getBBox().width,height:t.getBBox().height,fontFamily:c(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:o,y:i,rotation:a,scaleX:l,scaleY:g}=s(t),u=t.getBBox().width*l,h=t.getBBox().height*g,c=t.getAttribute("xlink:href")||"",d=await e(c),f=r({width:u,height:h},d);return[Object.assign({type:"image",id:n,x:o+t.getBBox().x,y:i+t.getBBox().y,width:u,height:h,src:c,rotation:a},f)]}if("rect"===t.nodeName){const{x:o,y:i,rotation:a}=s(t),l=parseFloat(t.getAttribute("width")||"0"),g=parseFloat(t.getAttribute("height")||"0"),u=t.getAttribute("fill")||"",c=u.indexOf("url")>=0,d=c?h(u):null,f=d?document.querySelector(d):null;if(c){const s=null==f?void 0:f.querySelector("use"),u=null==s?void 0:s.getAttribute("xlink:href"),h=u&&document.querySelector(u),c=h&&h.getAttribute("xlink:href");if(c){const s=await e(c),u=r({width:l,height:g},s);return[Object.assign({type:"image",id:n,x:o+t.getBBox().x,y:i+t.getBBox().y,width:l,height:g,src:c,rotation:a},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="${l}" height="${g}" viewBox="0 0 ${l} ${g}">${(null==f?void 0:f.outerHTML)||""}${t.outerHTML}</svg>`;return[{type:"svg",id:n,x:o+m.x,y:i+m.y,width:l,height:g,src:svgToURL(x)}]}if("path"===t.nodeName||"polygon"===t.nodeName||"circle"===t.nodeName){const{x:e,y:r,rotation:o,scaleX:i,scaleY:a}=s(t),l=t.getBBox(),{width:g,height:u}=l;t.setAttribute("transform","");const c=t.getAttribute("fill")||"",d=c.indexOf("url")>=0?h(c):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(${-l.x} ${-l.y})">${t.outerHTML}</g></svg>`;return[{type:"svg",id:n,x:e+l.x*i,y:r+l.y*a,width:g*i,height:u*a,rotation:o,src:svgToURL(x)}]}if((t=>"g"===t.nodeName&&t.getBBox().width<450&&t.getBBox().height<450)(t)&&!g(t)&&!u(t)){const{x:e,y:r}=t.getBBox(),{width:o,height:a}=(l(t),t.getBBox()),s=Array.from(t.querySelectorAll("*"));let g=e,u=r;s.forEach(t=>{if("g"===t.nodeName){return}const e=t.getBBox();g=Math.min(g,e.x),u=Math.min(u,e.y)});const h=t.getBoundingClientRect(),c=d(t),f=null==c?void 0:c.getBoundingClientRect(),m=t.getAttribute("transform");t.setAttribute("transform","");const x=`<svg xmlns="http://www.w3.org/2000/svg" width="${o}" height="${a}" viewBox="0 0 ${o} ${a}"><g transform="translate(${-e} ${-r})">${t.outerHTML}</g></svg>`;t.setAttribute("transform",m||"");const w=i(d(t));return[{type:"svg",id:n,x:(h.x-f.x)/f.width*w.width,y:(h.y-f.y)/f.height*w.height,width:o,height:a,name:t.getAttribute("id")||"",src:svgToURL(x)}]}if(!g(t)&&!u(t)){const e=[];for(const r of Array.from(t.children)){e.push(...await f(r))}return e}const a=[];for(const e of Array.from(t.children)){a.push(...await f(e))}return a}function m(t){t.querySelectorAll("script, foreignObject").forEach(t=>t.remove()),t.querySelectorAll("*").forEach(t=>{for(const e of Array.from(t.attributes)){e.name.startsWith("on")&&t.removeAttribute(e.name),"href"!==e.name&&"xlink:href"!==e.name||!e.value.trim().toLowerCase().startsWith("javascript:")||t.removeAttribute(e.name)}})}export async function svgToJson(t){const e=document.createElement("div");document.body.appendChild(e),e.innerHTML=function(t){const e=(new DOMParser).parseFromString(t,"image/svg+xml");if(e.querySelector("parsererror")){const e=(new DOMParser).parseFromString(t,"text/html").querySelector("svg");return e?(m(e),e.outerHTML):t}return m(e.documentElement),e.documentElement.outerHTML}(t);const r=e.querySelector("svg"),{width:n,height:a}=i(r),s=[];for(const o of Array.from(r.children)){s.push(...await f(o))}return e.parentElement.removeChild(e),{width:n,height:a,pages:[{id:o(),children:s}]}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{fetchFontResource as t,looksLikeFont as n,sniffFontMime as
|
|
1
|
+
import{fetchFontResource as t,looksLikeFont as n,sniffFontMime as o}from"@polotno/core/fonts/fetch";import{parseUnicodeRange as e,unicodeRangeIntersects as r}from"@polotno/core/fonts";import{bytesToBase64 as c}from"@polotno/core/svg";const s=new Map,a=new Map,i=/@font-face\s*\{[^}]*\}/g,f=/url\(\s*(['"]?)(.*?)\1\s*\)/g,l=/unicode-range:\s*([^;}]+)/i;function u(t,n){if(!n){return!0}const o=t.match(l);if(!o){return!0}const c=e(o[1]);return 0===c.length||r(c,n)}function p(t,n){const o=new Set;for(const e of t.matchAll(i)){if(u(e[0],n)){for(const t of e[0].matchAll(f)){const n=t[2];n&&!n.startsWith("data:")&&o.add(n)}}}return Array.from(o)}function m(e){let r=s.get(e);return r||(r=async function(e){try{const r=await t(e,{validate:n});return`data:${o(r)};base64,${c(r)}`}catch(r){return e}}(e),s.set(e,r)),r}export function inlineFontsInCss(t,n){const o=function(t,n){const o=t.length;let e=`${o}|${t.slice(0,500)}|${o>200?t.slice((o>>1)-50,50+(o>>1)):""}|${o>100?t.slice(-100):""}`;if(n&&n.size>0){let t=5381;const o=[...n].sort((t,n)=>t-n);for(let n=0;n<o.length;n++){t=(t<<5)+t+o[n]|0}e+="\0"+(t>>>0)}return e}(t,n),e=a.get(o);if(e){return e}const r=async function(t,n){const o=p(t,n);if(0===o.length&&!n){return t}const e=await Promise.all(o.map(async t=>[t,await m(t)])),r=new Map(e);return t.replace(i,t=>u(t,n)?t.replace(f,(t,n,o)=>{if(!o||o.startsWith("data:")){return t}const e=r.get(o);return e?`url("${e}")`:t}):"")}(t,n);return a.set(o,r),r}export async function prefetchFonts(t){const n=p(t);await Promise.all(n.map(t=>m(t)))}export function clearFontCache(){s.clear(),a.clear()}
|
package/utils/html2canvas.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"@polotno/core/measure-html";import{getGoogleFontsVariants as t}from"@polotno/core/google-fonts-url";import{resolveFontFaceStyles as e}from"@polotno/core/fonts";import{optimizeCss as
|
|
1
|
+
export*from"@polotno/core/measure-html";import{getGoogleFontsVariants as t}from"@polotno/core/google-fonts-url";import{resolveFontFaceStyles as o,cssFontFamily as e}from"@polotno/core/fonts";import{optimizeCss as n}from"./css-optimizer.js";import{resetStyle as r}from"@polotno/core/reset-style";import{getHiddenElement as s}from"@polotno/core/measure-html";import{parsePrimaryFontFamily as i}from"@polotno/core/rich-text-html";import{shrinkParagraphStrutForRaster as l}from"./rich-text-html.js";import{htmlToImage as c}from"./html-to-svg/index.js";const a=new Map;export const prerenderFont=async t=>{if(a.has(t)){return a.get(t)}const o=(async()=>{const o=`<div style="font-family: ${e(t)}; font-size: 100px;">${t}<strong>${t}</strong></div>`,n=document.createElement("div");n.innerHTML=o,n.style.visibility="hidden",n.style.position="absolute";const r=s();r.appendChild(n);const i="undefined"!=typeof navigator&&navigator.userAgent.includes("Firefox")?180:50;await new Promise(t=>setTimeout(t,i)),r.removeChild(n)})();return a.set(t,o),o};const f={},m=t=>{if(f[t]){return f[t]}const o=function(t){const o=[];for(let n=0;n<document.styleSheets.length;n++){const r=document.styleSheets[n];try{const e=r.cssRules;if(e){for(let n=0;n<e.length;n++){const r=e[n];r instanceof CSSFontFaceRule&&i(r.style.fontFamily)===t&&o.push(r)}}}catch(e){console.warn(`Could not access stylesheet: ${r.href}`,e)}}return o}(t),e=o.map(t=>({src:t.style.getPropertyValue("src"),fontStyle:t.style.getPropertyValue("font-style")||"normal",fontWeight:t.style.getPropertyValue("font-weight")||"normal"}));return f[t]=e,e},p=new Map;async function h(t,o){const e=`${t}|${o}`;if(p.has(e)){return p.get(e)}const n=`https://fonts.googleapis.com/css?family=${encodeURIComponent(t)}:${o}&display=swap`;try{const t=await fetch(n);if(!t.ok){return p.set(e,""),""}const o=await t.text();return p.set(e,o),o}catch(r){return p.set(e,""),""}}export async function htmlToCanvas({html:s,width:i,height:a,fontFamily:f,fontFamilies:p,padding:u,font:y,resolveFont:d,pixelRatio:g}){s=l(s);const w=(null==p?void 0:p.length)?[...new Set([f,...p])]:[f],v=await async function(n,r,s,i){const l=[...new Set(n.filter(Boolean))];let c="";for(const a of l){if("Arial"===a){continue}const n=a===r&&s||(null==i?void 0:i(a));if(n){(n.styles||n.url?o(n):m(a)).forEach(t=>{c+=`\n @font-face {\n font-family: ${e(a)};\n src: ${t.src};\n font-style: ${t.fontStyle||"normal"};\n font-weight: ${t.fontWeight||"normal"};\n }\n `})}else{const o=t();c+=await h(a,o)}}return c}(w,f,y,d);let $="";return v&&($=n(v,s)),c({html:`<div style="padding: ${u}px;" dir="auto">${s+=$+r}</div>`,width:i+2*u,height:a+2*u,cache:{maxEntries:100},pixelRatio:g})}
|
package/utils/loader.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare const setAssetLoadTimeout: (timeout: number) => void;
|
|
|
3
3
|
export declare const getAssetLoadTimeout: () => number;
|
|
4
4
|
export declare const setFontLoadTimeout: (timeout: number) => void;
|
|
5
5
|
export declare const getFontLoadTimeout: () => number;
|
|
6
|
-
export declare function incrementLoader(id:
|
|
6
|
+
export declare function incrementLoader(id: string): () => void;
|
|
7
7
|
export declare function decrementLoader(): void;
|
|
8
8
|
export declare function whenLoaded(): Promise<unknown>;
|
|
9
9
|
export declare function onLoadError(func: (error: PolotnoError) => void): void;
|
package/utils/math.d.ts
CHANGED
|
@@ -5,7 +5,10 @@ type Shape = {
|
|
|
5
5
|
height: number;
|
|
6
6
|
rotation: number;
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
type CenterShape = Omit<Shape, 'rotation'> & {
|
|
9
|
+
rotation?: number;
|
|
10
|
+
};
|
|
11
|
+
export declare function degToRad(angle: number): number;
|
|
9
12
|
export declare function getClientRect(element: Shape): {
|
|
10
13
|
x: number;
|
|
11
14
|
y: number;
|
|
@@ -32,10 +35,22 @@ export declare function getTotalClientRect(elements: Shape[]): {
|
|
|
32
35
|
maxX: number;
|
|
33
36
|
maxY: number;
|
|
34
37
|
};
|
|
35
|
-
export declare function getCenter(shape:
|
|
36
|
-
x:
|
|
37
|
-
y:
|
|
38
|
+
export declare function getCenter(shape: CenterShape): {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
};
|
|
42
|
+
export declare function rotateAroundPoint(shape: Shape, deltaDeg: number, point: Point): {
|
|
43
|
+
rotation: number;
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
width: number;
|
|
47
|
+
height: number;
|
|
48
|
+
};
|
|
49
|
+
export declare function rotateAroundCenter(shape: Shape, deltaDeg: number): {
|
|
50
|
+
rotation: number;
|
|
51
|
+
x: number;
|
|
52
|
+
y: number;
|
|
53
|
+
width: number;
|
|
54
|
+
height: number;
|
|
38
55
|
};
|
|
39
|
-
export declare function rotateAroundPoint(shape: any, deltaDeg: any, point: any): any;
|
|
40
|
-
export declare function rotateAroundCenter(shape: any, deltaDeg: any): any;
|
|
41
56
|
export {};
|
package/utils/measure-text.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"konva";import{flags as e}from"./flags.js";import{detectSize as n}from"@polotno/core/measure-html";import{removeTags as
|
|
1
|
+
import t from"konva";import{flags as e}from"./flags.js";import{detectSize as n}from"@polotno/core/measure-html";import{removeTags as o}from"@polotno/core/text";import{applyTextDefaults as i}from"@polotno/core/text-types";import{cssFontFamily as r}from"@polotno/core/fonts";import{getHtml as l,getCurvePath as f,getCurveTextHeight as a,resolveLineHeight as h}from"@polotno/core/text-html";export async function measureText(c){var g;return(null!==(g=c.richTextEnabled)&&void 0!==g?g:e.htmlRenderEnabled||e.renderTagTextEnabled)?function(t){const o=i(Object.assign(Object.assign({},t),{text:t.text,width:t.width}));if(o.curveEnabled){return{height:a(o)}}const r=l(o,{fontFamily:o.fontFamily,textOverflow:e.textOverflow}),{height:f}=n(r);return{height:f}}(c):function(e){var n;const l=i(Object.assign(Object.assign({},e),{text:e.text,width:e.width})),a=h({fontFamily:l.fontFamily,fontSize:l.fontSize,lineHeight:l.lineHeight});let c=o(l.text);if("uppercase"===l.textTransform&&(c=c.toUpperCase()),l.curveEnabled){const e=new t.TextPath({data:f(l.width,null!==(n=l.height)&&void 0!==n?n:0,l.curvePower,l.fontSize),align:"center",textBaseline:"middle",text:c,fontSize:l.fontSize,fontFamily:r(l.fontFamily),fontWeight:l.fontWeight,fontStyle:l.fontStyle,letterSpacing:l.letterSpacing*l.fontSize,fill:l.fill}),o=e.getSelfRect();return e.destroy(),{height:o.height||l.fontSize}}const g=new t.Text({text:c,width:l.width,fontSize:l.fontSize,fontFamily:r(l.fontFamily),fontStyle:`${l.fontStyle} ${l.fontWeight}`,lineHeight:a,letterSpacing:l.letterSpacing*l.fontSize,align:l.align}),m=Math.ceil(g.fontSize()*g.lineHeight()*g.textArr.length+1);return g.destroy(),{height:m}}(c)}
|
package/utils/rich-text-html.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"@polotno/core/rich-text-html";import{parseHtmlRoot as t,cleanupInlineWrappers as e,parsePrimaryFontFamily as r,toNumericWeight as o,extractFontFamiliesFromHtml as n,
|
|
1
|
+
export*from"@polotno/core/rich-text-html";import{parseHtmlRoot as t,cleanupInlineWrappers as e,parsePrimaryFontFamily as r,toNumericWeight as o,extractFontFamiliesFromHtml as n,qlClassToFontFamilyName as i,normalizeRichTextHtml as l}from"@polotno/core/rich-text-html";import{LRUCache as s}from"./lru-cache.js";const c=new s(500);export function htmlInflatesMinContentWidth(e){if(!e||!e.includes("<")){return!1}let r=c.get(e);if(void 0===r){r=!1;const o=t(e);if(o){if(o.querySelector("ol, ul")){r=!0}else{for(const t of o.querySelectorAll("[style]")){if(t.style.fontSize||t.style.letterSpacing){r=!0;break}}}}c.set(e,r)}return r}export function quillHtmlToModelText(t,e){return l(function(t){const e=(t||"").trim();if("<p><br></p>"===e||"<p></p>"===e){return""}if(e.startsWith("<p>")&&e.endsWith("</p>")){const t=e.slice(3,-4);if(!/(<\/?(p|div|h[1-6]|ul|ol|li|table|thead|tbody|tr|td|blockquote)\b)/i.test(t)){return t}}return t}(t),e)}export function shrinkParagraphStrutForRaster(e){var r;if(!e||!e.includes("<p")){return e}const o=t(e);if(!o){return e}for(const t of Array.from(o.querySelectorAll("p"))){let e=!1;const n=[],i=o.ownerDocument.createTreeWalker(t,NodeFilter.SHOW_TEXT);let l=i.nextNode();for(;l;){if((l.textContent||"").length>0){let o=l.parentElement,i=null;for(;o&&o!==t;){const t=null===(r=o.style)||void 0===r?void 0:r.fontSize;if(t){const e=parseFloat(t);if(!Number.isNaN(e)){i=e;break}}o=o.parentElement}if(null==i){e=!0;break}n.push(i)}l=i.nextNode()}!e&&n.length>0&&(t.style.fontSize=`${Math.min(...n)}px`)}return o.innerHTML}export function extractFontSpecsFromHtml(e,n){const l=n.fontFamily,s=String(o(n.fontWeight)),c="italic"===n.fontStyle?"italic":"normal",f=new Map,u=(t,e,r)=>{const o=`${t}\0${e}\0${r}`;f.has(o)||f.set(o,{family:t,weight:e,style:r})};if(u(l,s,c),e&&e.includes("<")){const n=t(e);if(n){const t=n.ownerDocument.createTreeWalker(n,NodeFilter.SHOW_TEXT);let e=t.nextNode();for(;e;){if((e.textContent||"").trim().length>0){let t="",f="",a="",y=e.parentElement;for(;y&&y!==n;){if(!t){if(y.style.fontFamily){t=r(y.style.fontFamily)||""}else{const e=(y.getAttribute("class")||"").split(/\s+/).find(t=>t.startsWith("ql-font-"));e&&(t=i(e))}}f||(y.style.fontWeight?f=String(o(y.style.fontWeight)):"STRONG"!==y.tagName&&"B"!==y.tagName||(f="700")),a||(y.style.fontStyle?a=y.style.fontStyle:"EM"!==y.tagName&&"I"!==y.tagName||(a="italic")),y=y.parentElement}u(t||l,f||s,a||c)}e=t.nextNode()}}}return[...f.values()]}const f={bold:["STRONG","B"],italic:["EM","I"],underline:["U"],strike:["S","DEL"]};export function isClipGradientSpan(t){return!!(null==t?void 0:t.style)&&("text"===(t.style.getPropertyValue("-webkit-background-clip")||t.style.getPropertyValue("background-clip"))&&t.style.backgroundImage.includes("gradient"))}const u={bold:t=>t.style.removeProperty("font-weight"),italic:t=>t.style.removeProperty("font-style"),color:t=>{!function(t){if(isClipGradientSpan(t)){for(const e of["background-image","background-size","-webkit-background-clip","background-clip","-webkit-text-fill-color"]){t.style.removeProperty(e)}}}(t),t.style.removeProperty("color")},underline:t=>{const e=t.style.textDecoration.replace(/\bunderline\b/g,"").trim();e?t.style.textDecoration=e:t.style.removeProperty("text-decoration")},strike:t=>{const e=t.style.textDecoration.replace(/\bline-through\b/g,"").trim();e?t.style.textDecoration=e:t.style.removeProperty("text-decoration")}};export function stripInlineFormat(r,o){var n,i;if(!r||!r.includes("<")){return r}const l=t(r);if(!l){return r}const s=f[o]||[],c=u[o];for(const t of s){let e;for(;e=l.querySelector(t);){const t=e.parentNode;c&&c(e);const r=null===(n=e.getAttribute("style"))||void 0===n?void 0:n.trim();if(r){const o=l.ownerDocument.createElement("span");for(o.setAttribute("style",r);e.firstChild;){o.appendChild(e.firstChild)}t.insertBefore(o,e)}else{for(;e.firstChild;){t.insertBefore(e.firstChild,e)}}t.removeChild(e)}}if(c){for(const t of Array.from(l.querySelectorAll("[style]"))){c(t),(null===(i=t.getAttribute("style"))||void 0===i?void 0:i.trim())||t.removeAttribute("style")}}return e(l),l.innerHTML}const a=Symbol("unset");export function isQuillFormatMixed(t,e){var r;let o=a;for(const n of t.ops){if("string"!=typeof n.insert||"\n"===n.insert){continue}const t=null===(r=n.attributes)||void 0===r?void 0:r[e];if(o===a){o=t}else if(t!==o){return!0}}return!1}export function stripInlineFontSizes(r){var o;if(!r||!r.includes("font-size")){return r}const n=t(r);if(!n){return r}const i=n.querySelectorAll("[style]");for(let t=0;t<i.length;t++){const e=i[t];e.style.fontSize&&(e.style.removeProperty("font-size"),(null===(o=e.getAttribute("style"))||void 0===o?void 0:o.trim())||e.removeAttribute("style"))}return e(n),n.innerHTML}export function stripInlineFontFamilies(r){var o;if(!r||!r.includes("font-family")&&!r.includes("ql-font-")){return r}const n=t(r);if(!n){return r}for(const t of Array.from(n.querySelectorAll("[style]"))){t.style.fontFamily&&(t.style.removeProperty("font-family"),(null===(o=t.getAttribute("style"))||void 0===o?void 0:o.trim())||t.removeAttribute("style"))}for(const t of Array.from(n.querySelectorAll("[class]"))){const e=t.getAttribute("class")||"",r=e.split(/\s+/).filter(t=>t&&!t.startsWith("ql-font-"));r.length!==e.split(/\s+/).filter(Boolean).length&&(r.length?t.setAttribute("class",r.join(" ")):t.removeAttribute("class"))}return e(n),n.innerHTML}export function hasMultipleDistinctInlineFontSizes(e){if(!e||!e.includes("<")){return!1}const r=t(e);if(!r){return!1}const o=new Set;for(const t of r.querySelectorAll("[style]")){const e=t.style.fontSize;if(e){const t=e.match(/^(\d+(?:\.\d+)?)px$/i);if(t&&(o.add(parseFloat(t[1])),o.size>1)){return!0}}}return!1}export function listEffectiveFontFamilies(t,e){const r=[];e&&r.push(e),r.push(...n(t||""));const o=new Set,i=[];for(const n of r){o.has(n)||(o.add(n),i.push(n))}return i.sort((t,e)=>t.localeCompare(e,void 0,{sensitivity:"base"})),i}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as t from"react";import*as e from"mobx";import{
|
|
1
|
+
import*as t from"react";import*as e from"mobx";import{parseFontSizeToPx as o,toNumericWeight as n}from"@polotno/core/rich-text-html";import{isGradient as i}from"@polotno/core/gradient";import{getTextFormat as r,applyTextFormat as l,FORMAT_KEYS as c,DECORATION_TOKENS as a,decorationTokensOf as s,decorationTokensToString as f,decorationToQuillKey as u}from"./text-format.js";const d=e.observable({active:null},{},{deep:!1});export function getActiveTextEditor(){return d.active}export function setActiveTextEditor(t){e.runInAction(()=>{d.active=Object.assign(Object.assign({},t),{currentFormat:{},mixedFormats:{},lastSelection:null})})}export function updateActiveTextFormat(t,o,n){e.runInAction(()=>{d.active&&(d.active=Object.assign(Object.assign({},d.active),{currentFormat:t,mixedFormats:o,lastSelection:n||d.active.lastSelection}))})}export function clearActiveTextEditor(t){e.runInAction(()=>{var e;t&&(null===(e=d.active)||void 0===e?void 0:e.instance)!==t||(d.active=null)})}const m={fontSize:["size"],fontFamily:["font"],fill:["color","gradient"],fontWeight:["font-weight"],fontStyle:["italic"],textDecoration:["underline","strike"]};export const ALL_QUILL_MIXED_KEYS=Array.from(new Set(c.flatMap(t=>m[t])));function p(t,e,i){var r,l;switch(t){case"fontSize":return null!==(r=o(e.size,i.fontSize))&&void 0!==r?r:i.fontSize;case"fontFamily":return e.font?String(e.font):i.fontFamily;case"fill":return e.gradient||(null!==(l=e.color)&&void 0!==l?l:i.fill);case"fontWeight":return null!=e["font-weight"]?n(e["font-weight"]):n(i.fontWeight);case"fontStyle":return e.italic?"italic":i.fontStyle;case"textDecoration":return f(s(i.textDecoration))}}function x(t,e,o){const n=s(o.textDecoration),i=(o,i)=>n.has(o)?"on":e[i]?"mixed":t[i]?"on":"off",r=function(t,e){const o=t=>"on"===t?[!0]:"off"===t?[!1]:[!0,!1],n=[];for(const i of o(t)){for(const t of o(e)){const e=new Set;i&&e.add("underline"),t&&e.add("line-through"),n.push(f(e))}}return n}(i("underline","underline"),i("line-through","strike"));return{value:1===r.length?r[0]:void 0,mixed:r.length>1,values:r}}export function quillToTextFormat(t,e,o){const n={};for(const i of c){if("textDecoration"!==i){if(m[i].some(t=>!0===e[t])){n[i]={value:void 0,mixed:!0,values:[]}}else{const e=p(i,t,o);n[i]={value:e,mixed:!1,values:[e]}}}else{n.textDecoration=x(t,e,o)}}return n}export function attrsToQuillOps(t,e){var o;const r=[];if(void 0!==t.fontFamily&&r.push(["font",t.fontFamily]),void 0!==t.fontSize&&r.push(["size",`${t.fontSize}px`]),void 0!==t.fill&&(i(String(t.fill))?r.push(["color",!1],["gradient",t.fill]):r.push(["gradient",!1],["color",t.fill])),void 0!==t.fontWeight&&r.push(["font-weight",String(n(t.fontWeight))]),void 0!==t.fontStyle&&r.push(["italic","italic"===t.fontStyle]),void 0!==t.textDecoration){const n=null!==(o=null==e?void 0:e.decorationTokens)&&void 0!==o?o:a,i=s(t.textDecoration);for(const t of a){n.includes(t)&&r.push([u(t),i.has(t)])}}return r}function v(t){return((null==t?void 0:t.selectedElements)||[]).filter(t=>"text"===t.type)}function g(t){const e=getActiveTextEditor();return e&&e.instance&&e.store===t?e:null}export function resolveSelectionScope(t,e){const o=t.getSelection()||e||{index:0,length:0},n="function"==typeof t.getLength?t.getLength():0,i=n<=1;return o.length<=0?{scope:"collapsed",sel:o,isEmpty:i}:{scope:o.length>=n-1?"full":"partial",sel:o,isEmpty:i}}export function applyToLiveEditor(t,e,o,n){const i=attrsToQuillOps(e,n);if(!i.length){return}const{scope:r,sel:l}=resolveSelectionScope(t,null!=o?o:null);for(const[c,a]of i){"collapsed"===r?t.format(c,a,"user"):t.formatText(l.index,l.length,c,a,"user")}"collapsed"===r&&updateActiveTextFormat(t.getFormat(l.index,0),{},l)}function h(t,e){const o="function"==typeof t.getLength?t.getLength():0;t.formatText(0,o,e,!0,"api")}export function getTextFormatState(t){const e=g(t),o=v(t),n=!!e||o.length>0;var i;return{format:e?quillToTextFormat(e.currentFormat||{},e.mixedFormats||{},{fontSize:(i=e.element).fontSize,fontFamily:i.fontFamily,fill:i.fill,fontWeight:i.fontWeight,fontStyle:i.fontStyle,textDecoration:i.textDecoration}):r(o),enabled:n,applyTextFormat:(e,o)=>function(t,e,o){const n=g(t);if(!n){return void l(v(t),e,o)}const{scope:i,sel:r,isEmpty:c}=resolveSelectionScope(n.instance,n.lastSelection);"full"===i||"collapsed"===i&&!c?l([n.element],e,o):function(t,e,o,n){var i,r;const l=t.instance,c=t.element;let d=!1;if(n.length>0){if(void 0!==e.fontStyle&&"italic"!==e.fontStyle&&"italic"===c.fontStyle&&(h(l,"italic"),c.set({fontStyle:"normal"}),d=!0),void 0!==e.textDecoration){const t=null!==(i=null==o?void 0:o.decorationTokens)&&void 0!==i?i:a,n=s(e.textDecoration),r=s(c.textDecoration),m=t.filter(t=>!n.has(t)&&r.has(t));for(const e of m){h(l,u(e)),r.delete(e)}m.length&&(c.set({textDecoration:f(r)}),d=!0)}d&&"function"==typeof(null===(r=l.history)||void 0===r?void 0:r.clear)&&l.history.clear()}applyToLiveEditor(l,e,n,o)}(n,e,o,r)}(t,e,o)}}export function observeTextFormatState(t,o){return e.reaction(()=>getTextFormatState(t),t=>o(t),{fireImmediately:!0})}export function useTextFormat(e){const[o,n]=t.useState(()=>getTextFormatState(e));return t.useEffect(()=>observeTextFormatState(e,n),[e]),o}
|
package/utils/text-format.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parsePrimaryFontFamily as t,parseFontSizeToPx as e,parseHtmlRoot as o,stripInlineFontSizes as n,stripInlineFontFamilies as r,stripInlineFormat as i,toNumericWeight as s,
|
|
1
|
+
import{parsePrimaryFontFamily as t,parseFontSizeToPx as e,parseHtmlRoot as o,stripInlineFontSizes as n,stripInlineFontFamilies as r,stripInlineFormat as i,toNumericWeight as s,qlClassToFontFamilyName as c,isClipGradientSpan as a}from"./rich-text-html.js";export const FORMAT_KEYS=["fontSize","fontFamily","fill","fontWeight","fontStyle","textDecoration"];export const DECORATION_TOKENS=["underline","line-through"];export function decorationTokensOf(t){const e=new Set;for(const o of(t||"").split(/\s+/)){"underline"!==o&&"line-through"!==o||e.add(o)}return e}export function decorationTokensToString(t){return DECORATION_TOKENS.filter(e=>t.has(e)).join(" ")}export function isDecorationActive(t,e){const o=t.textDecoration.values;return o.length>0&&o.every(t=>decorationTokensOf(t).has(e))}export const decorationToQuillKey=t=>"underline"===t?"underline":"strike";function f(o,n,r){switch(n){case"fontSize":return e(o.style.fontSize,r.fontSize);case"fontFamily":{if(o.style.fontFamily){return t(o.style.fontFamily)||void 0}const e=(o.getAttribute("class")||"").split(/\s+/).find(t=>t.startsWith("ql-font-"));return e&&c(e)||void 0}case"fill":return a(o)?o.style.backgroundImage:o.style.color||void 0;case"fontWeight":return o.style.fontWeight?s(o.style.fontWeight):"STRONG"===o.tagName||"B"===o.tagName?700:void 0;case"fontStyle":return o.style.fontStyle?o.style.fontStyle:"EM"===o.tagName||"I"===o.tagName?"italic":void 0;case"textDecoration":return o.style.textDecoration?o.style.textDecoration:"U"===o.tagName?"underline":"S"===o.tagName||"DEL"===o.tagName?"line-through":void 0}}function l(t,e){return"fontWeight"===e?s(t.fontWeight):t[e]}function u(t,e,o){const n={},r=new Set,i=decorationTokensOf(o.textDecoration);let s=t.parentElement;for(;s&&s!==e;){for(const t of FORMAT_KEYS){if("textDecoration"===t){const t=f(s,"textDecoration",o);if("string"==typeof t){for(const e of decorationTokensOf(t)){i.add(e)}}continue}if(r.has(t)){continue}const e=f(s,t,o);void 0!==e&&(n[t]=e,r.add(t))}s=s.parentElement}for(const c of FORMAT_KEYS){"textDecoration"===c||r.has(c)||(n[c]=l(o,c))}return n.textDecoration=decorationTokensToString(i),n}function d(t,e){return 0===t.length?{value:e,mixed:!1,values:void 0===e?[]:[e]}:1===t.length?{value:t[0],mixed:!1,values:t}:{value:void 0,mixed:!0,values:t}}function h(t){const e=function(t){const e=t.text||"";if(!e.includes("<")){if(0===e.trim().length){return[]}const o={};for(const e of FORMAT_KEYS){o[e]=l(t,e)}return[o]}const n=o(e);if(!n){return[]}const r=[],i=n.ownerDocument.createTreeWalker(n,NodeFilter.SHOW_TEXT);let s=i.nextNode();for(;s;){const e=s;(e.textContent||"").trim().length>0&&r.push(u(e,n,t)),s=i.nextNode()}return r}(t),n={};for(const o of FORMAT_KEYS){const r=[];for(const t of e){const e=t[o];void 0===e||r.includes(e)||r.push(e)}const i=0===r.length?l(t,o):void 0;n[o]=d(r,i)}return n}export function getTextFormat(t){return Array.isArray(t)?function(t){const e={};for(const o of FORMAT_KEYS){const n=[];for(const e of t){for(const t of e[o].values){n.includes(t)||n.push(t)}}e[o]=d(n,void 0)}return e}(t.map(h)):h(t)}function g(t,e){switch(e){case"fontSize":return n(t);case"fontFamily":return r(t);case"fill":return i(t,"color");case"fontWeight":return i(t,"bold");case"fontStyle":return i(t,"italic");case"textDecoration":return i(i(t,"underline"),"strike")}}let x=!1;function T(t){return"string"!=typeof t||/^\d+$/.test(t.trim())||x||(x=!0,console.warn("[polotno] applyTextFormat({ fontWeight }) with a string such as 'bold'/'normal' is deprecated; pass a number (100–900), e.g. 700.")),s(t)}export function patchTextFormat(t,e,o){var n;const r=FORMAT_KEYS.filter(t=>void 0!==e[t]);if(0===r.length){return{attrsToSet:{}}}const s=null!==(n=null==o?void 0:o.decorationTokens)&&void 0!==n?n:DECORATION_TOKENS,c={};let a=t.text||"";for(const f of r){if("textDecoration"===f){const o=decorationTokensOf(e.textDecoration),n=decorationTokensOf(t.textDecoration);for(const t of s){o.has(t)?n.add(t):n.delete(t),a=i(a,decorationToQuillKey(t))}c.textDecoration=decorationTokensToString(n);continue}c[f]="fontWeight"===f?String(T(e.fontWeight)):e[f],a=g(a,f)}return a!==(t.text||"")&&(c.text=a),{attrsToSet:c}}export function applyTextFormat(t,e,o){var n;const r=Array.isArray(t)?t:[t];if(0===r.length){return}const i=t=>{const{attrsToSet:n}=patchTextFormat(t,e,o);Object.keys(n).length&&t.set(n)};if(1===r.length){return void i(r[0])}const s=new Map;for(const c of r){const t=c.store,e=s.get(t)||[];e.push(c),s.set(t,e)}for(const[c,a]of s){(null===(n=null==c?void 0:c.history)||void 0===n?void 0:n.transaction)?c.history.transaction(()=>a.forEach(i)):a.forEach(i)}}
|
package/utils/unit.d.ts
CHANGED
|
@@ -4,14 +4,15 @@ type fromPx = {
|
|
|
4
4
|
unit: UnitType;
|
|
5
5
|
dpi: number;
|
|
6
6
|
};
|
|
7
|
+
type toPx = {
|
|
8
|
+
unitVal: number;
|
|
9
|
+
unit: UnitType;
|
|
10
|
+
dpi: number;
|
|
11
|
+
};
|
|
7
12
|
export declare const pxToUnit: ({ px, unit, dpi }: fromPx) => number;
|
|
8
13
|
export declare const pxToUnitRounded: ({ px, precious, dpi, unit, }: fromPx & {
|
|
9
14
|
precious?: number;
|
|
10
15
|
}) => number;
|
|
11
|
-
export declare const unitToPx: ({ unitVal, dpi, unit }:
|
|
12
|
-
unitVal: any;
|
|
13
|
-
dpi: any;
|
|
14
|
-
unit: any;
|
|
15
|
-
}) => number;
|
|
16
|
+
export declare const unitToPx: ({ unitVal, dpi, unit }: toPx) => number;
|
|
16
17
|
export declare const pxToUnitString: (conf: fromPx) => string;
|
|
17
18
|
export {};
|
package/utils/validate-key.d.ts
CHANGED
|
@@ -3,6 +3,19 @@ export declare const ____: () => number;
|
|
|
3
3
|
export declare const isCreditVisible: () => boolean;
|
|
4
4
|
export declare const getKey: () => string;
|
|
5
5
|
export declare const setKey: (key: string) => void;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
type LicenseValidation = {
|
|
7
|
+
is_valid: boolean;
|
|
8
|
+
is_paid: boolean;
|
|
9
|
+
is_domain_valid: boolean;
|
|
10
|
+
remove_background_enabled: boolean;
|
|
11
|
+
};
|
|
12
|
+
type LicenseResponse = {
|
|
13
|
+
status: number;
|
|
14
|
+
headers: Pick<Headers, 'get'>;
|
|
15
|
+
json: () => Promise<LicenseValidation>;
|
|
16
|
+
};
|
|
17
|
+
type LicenseFetcher = (input: RequestInfo | URL, init?: RequestInit) => Promise<LicenseResponse>;
|
|
18
|
+
export declare const __: (DOM_scheme: LicenseFetcher) => void;
|
|
19
|
+
export declare function isKeyPaid(key: string): Promise<boolean>;
|
|
8
20
|
export declare function validateKey(forceCredit: boolean): Promise<void>;
|
|
21
|
+
export {};
|
package/utils/validate-key.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as e from"mobx";import{getAPI as o}from"./api.js";import{setRemoveBackgroundEnabled as t}from"./flags.js";const n=e.observable({value:!1}),a=e.observable({value:"v1"});export const ___=()=>a.value;const i=e.observable({value:0});export const ____=()=>i.value;export const isCreditVisible=()=>n.value;const r=e.action(()=>{n.value=!0});let s="";export const getKey=()=>s||"";export const setKey=e=>{s=e||""};let l="undefined"!=typeof window?window.location.origin:"";const c="undefined"!=typeof navigator&&navigator.userAgent.indexOf("Headless")>-1,d="undefined"!=typeof navigator&&navigator.userAgent.indexOf("Electron")>-1;"file://"===l&&c&&(l="headless"),"file://"===l&&d&&(l="electron");const p=`%cPolotno error! Current domain is not allowed. It may lead to unexpected behavior and stop working. Please add "${l}" here: https://polotno.com/cabinet`;let u=fetch;export const __=e=>{u=e};export async function isKeyPaid(n){for(let s=0;s<5;s++){try{const r=await u(o()+"/validate-key",{method:"POST",body:JSON.stringify({key:n,site:location.host,skdVersion:"3.
|
|
1
|
+
import*as e from"mobx";import{getAPI as o}from"./api.js";import{setRemoveBackgroundEnabled as t}from"./flags.js";const n=e.observable({value:!1}),a=e.observable({value:"v1"});export const ___=()=>a.value;const i=e.observable({value:0});export const ____=()=>i.value;export const isCreditVisible=()=>n.value;const r=e.action(()=>{n.value=!0});let s="";export const getKey=()=>s||"";export const setKey=e=>{s=e||""};let l="undefined"!=typeof window?window.location.origin:"";const c="undefined"!=typeof navigator&&navigator.userAgent.indexOf("Headless")>-1,d="undefined"!=typeof navigator&&navigator.userAgent.indexOf("Electron")>-1;"file://"===l&&c&&(l="headless"),"file://"===l&&d&&(l="electron");const p=`%cPolotno error! Current domain is not allowed. It may lead to unexpected behavior and stop working. Please add "${l}" here: https://polotno.com/cabinet`;let u=fetch;export const __=e=>{u=e};export async function isKeyPaid(n){for(let s=0;s<5;s++){try{const r=await u(o()+"/validate-key",{method:"POST",body:JSON.stringify({key:n,site:location.host,skdVersion:"3.9.0"})});if(e.runInAction(()=>{a.value=r.headers.get("x-api-version")||""}),!n){return console.error("Polotno API is initialized without API key. It may lead to unexpected behavior and stop working. Please create API key here: https://polotno.com/cabinet"),e.runInAction(()=>{i.value=1}),!1}if(200!==r.status){await new Promise(e=>setTimeout(e,3e3));continue}const s=await r.json();return s.is_valid||(console.error("Polotno API key is not valid. Please get new API key here: https://polotno.com/cabinet"),e.runInAction(()=>{i.value=1})),s.is_paid||(console.log("%cPolotno Free Version. For development usage only. https://polotno.com/","background: rgb(0, 161, 255); color: white; padding: 5px; margin: 5px;"),e.runInAction(()=>{0===i.value&&(i.value=2)})),s.is_domain_valid||console.log(p,"background: rgba(247, 101, 68, 1); color: white; padding: 5px; margin: 5px;"),t(s.remove_background_enabled),s.is_paid||!1}catch(r){await new Promise(e=>setTimeout(e,3e3))}}return console.error("Can not validate Polotno API key. Please report to anton@polotno.com immediately."),!0}export async function validateKey(e){await isKeyPaid(s)&&!e||r()}
|
package/utils/wait.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function waitTillAvailable(func:
|
|
1
|
+
export declare function waitTillAvailable<T>(func: () => T | Promise<T>): Promise<NonNullable<Awaited<T>> | undefined>;
|