polotno 4.17.0 → 4.18.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.
@@ -1 +1 @@
1
- var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(e);i<n.length;i++){t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(e,n[i])&&(o[n[i]]=e[n[i]])}}return o};import{jsx as t,jsxs as o}from"react/jsx-runtime";import*as n from"react";import{observer as i}from"mobx-react-lite";import l from"swr";import{PolotnoIcon as a}from"../icons/registry.js";import{t as r}from"../utils/l10n.js";import{isMobile as s}from"../utils/screen.js";import{textTemplateList as p}from"../utils/api.js";import{fetcher as c}from"../utils/use-api.js";import{localFileToURL as f}from"../utils/file.js";import{cssFontFamily as m}from"@polotno/core/fonts";import{registerNextDomDrop as d}from"../canvas/page.js";import{Button as h}from"../primitives/button.js";import{Tabs as y,TabsList as u,TabsTrigger as x}from"../primitives/tabs.js";import{ImagesGrid as g}from"./images-grid.js";import{loadTextTemplate as v}from"./text-template.js";import{PanelScrollArea as b}from"../primitives/panel-scroll-area.js";let j=async e=>f(e);export function setFontUploadFunc(e){j=e}const w=i(({onSelect:e,onRemove:n,font:i})=>o("div",{style:{fontFamily:m(i.fontFamily)},className:"polotno-font-item pn:relative pn:mb-2.5 pn:flex pn:h-[100px] pn:cursor-pointer pn:flex-col pn:justify-center pn:rounded-md pn:bg-black/40 pn:text-center pn:text-[25px] pn:text-white pn:shadow-[0_0_5px_rgba(16,22,26,0.3)]",onClick:e,children:[i.fontFamily," text",t(h,{style:{position:"absolute",right:0,bottom:0},variant:"ghost",size:"icon-sm",onClick:e=>{e.stopPropagation(),n()},children:t(a,{name:"action.remove"})})]})),S=o=>{var{onSelect:n}=o,i=e(o,["onSelect"]);return t(h,Object.assign({},i,{draggable:!0,className:"polotno-close-panel",onClick:()=>n(),onDragStart:()=>{d(({x:e,y:t})=>{n({x:e,y:t})})},onDragEnd:()=>{d(null)}}))},F=[{previewSize:"25px",fontSize:76,labelKey:"sidePanel.createHeader",textKey:"sidePanel.headerText"},{previewSize:"18px",fontSize:44,labelKey:"sidePanel.createSubHeader",textKey:"sidePanel.subHeaderText"},{previewSize:"14px",fontSize:30,labelKey:"sidePanel.createBody",textKey:"sidePanel.bodyText"}];export const TextPanel=i(({store:e})=>{n.useEffect(()=>{e.loadFont("Roboto")},[]);const i=t=>{var o;const n=t.width||e.width/2,i=((null==t?void 0:t.x)||e.width/2)-n/2,l=((null==t?void 0:t.y)||e.height/2)-t.fontSize/2,a=(e.width+e.height)/2160,r=null===(o=e.activePage)||void 0===o?void 0:o.addElement(Object.assign(Object.assign({type:"text",fontFamily:"Roboto"},t),{x:i,y:l,width:n,fontSize:t.fontSize*a}));s()||null==r||r.toggleEditMode(!0)};n.useEffect(()=>{e.fonts.forEach(t=>e.loadFont(t.fontFamily))},[e.fonts]);const{data:f,error:m}=l(p(),c),[d,P]=n.useState("text"),[z,O]=n.useState(null),E=n.useRef(null),C=e=>{E.current=e,O(e)};return o("div",{style:{height:"100%",display:"flex",flexDirection:"column"},children:[t(y,{value:d,onValueChange:e=>P(String(e)),className:"pn:mb-3",children:o(u,{className:"pn:w-full pn:h-10",children:[t(x,{value:"text",children:r("sidePanel.text")}),t(x,{value:"font",children:r("sidePanel.myFonts")})]})}),"text"===d&&o("div",{className:"polotno-text-panel-container pn:flex pn:flex-col",style:{height:"calc(100% - 52px)"},children:[F.map(({previewSize:e,fontSize:o,labelKey:n,textKey:l})=>t(S,{style:{fontSize:e},variant:"ghost",className:"pn:mb-[5px] pn:w-full pn:h-auto pn:min-h-[2.5rem] pn:py-1 pn:justify-start pn:font-['Roboto']",onSelect:e=>{i(Object.assign(Object.assign({},e),{fontSize:o,text:r(l),fontFamily:"Roboto"}))},children:r(n)},n)),t(g,{shadowEnabled:!1,images:null==f?void 0:f.items,getPreview:e=>e.preview,getImageClassName:e=>e.json.indexOf("plain")>=0?"polotno-text-preview-plain":"",isLoading:!f,error:m,getIsLoading:e=>e.json===z,onSelect:async(t,o)=>{if(E.current===t.json){return}C(t.json);const n=await v(e,t).finally(()=>{E.current===t.json&&C(null)});if(!e.activePage){return}const i=(e.width+e.height)/2160,l=o?o.x-n.width/2*i:e.width/2-n.width/2*i,a=o?o.y-n.height/2*i:e.height/2-n.height/2*i;e.history.transaction(()=>{const t=n.pages[0].children,o=[];t.forEach(t=>{delete t.id;const{id:n}=e.activePage.addElement(Object.assign(Object.assign({},t),{fontSize:t.fontSize*i,x:t.x*i+l,y:t.y*i+a,width:t.width*i,height:t.height*i}));o.push(n)}),e.selectElements(o)})}})]}),"font"===d&&o("div",{style:{display:"flex",flexDirection:"column",height:"calc(100% - 62px)"},children:[o("label",{htmlFor:"polotno-font-upload",children:[o(h,{style:{width:"100%"},onClick:()=>{var e;null===(e=document.querySelector("#polotno-font-upload"))||void 0===e||e.click()},children:[t(a,{name:"text.uploadFont"}),r("sidePanel.uploadFont")]}),t("input",{type:"file",accept:".ttf, .otf, .woff, .woff2, .eot",id:"polotno-font-upload",style:{display:"none"},onChange:async t=>{const{target:o}=t;for(const n of Array.from(o.files||[])){const t=await j(n),o=n.name.split(".")[0].replace(/,/g,"");e.addFont({fontFamily:o,url:t})}o.value=""}})]}),t(b,{className:"pn:mt-4",children:e.fonts.map((o,n)=>t(w,{font:o,onSelect:()=>{i({fontSize:80,text:"Cool text",fontFamily:o.fontFamily})},onRemove:()=>{e.find(e=>("text"===e.type&&e.fontFamily===o.fontFamily&&e.set({fontFamily:"Roboto"}),!1)),e.removeFont(o.fontFamily)}},n))})]})]})});
1
+ var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(e);i<n.length;i++){t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(e,n[i])&&(o[n[i]]=e[n[i]])}}return o};import{jsx as t,jsxs as o}from"react/jsx-runtime";import*as n from"react";import{observer as i}from"mobx-react-lite";import l from"swr";import{PolotnoIcon as a}from"../icons/registry.js";import{t as r}from"../utils/l10n.js";import{isMobile as s}from"../utils/screen.js";import{textTemplateList as p}from"../utils/api.js";import{fetcher as c}from"../utils/use-api.js";import{localFileToURL as f}from"../utils/file.js";import{cssFontFamily as m}from"@polotno/core/fonts";import{registerNextDomDrop as d}from"../canvas/page.js";import{Button as h}from"../primitives/button.js";import{Tabs as u,TabsList as y,TabsTrigger as x}from"../primitives/tabs.js";import{ImagesGrid as g}from"./images-grid.js";import{loadTextTemplate as v}from"./text-template.js";import{PanelScrollArea as b}from"../primitives/panel-scroll-area.js";let j=async e=>f(e);export function setFontUploadFunc(e){j=e}const w=i(({onSelect:e,onRemove:n,font:i})=>o("div",{style:{fontFamily:m(i.fontFamily)},className:"polotno-font-item pn:relative pn:mb-2.5 pn:flex pn:h-[100px] pn:cursor-pointer pn:flex-col pn:justify-center pn:rounded-md pn:bg-black/40 pn:text-center pn:text-[25px] pn:text-white pn:shadow-[0_0_5px_rgba(16,22,26,0.3)]",onClick:e,children:[i.fontFamily," text",t(h,{style:{position:"absolute",right:0,bottom:0},variant:"ghost",size:"icon-sm",onClick:e=>{e.stopPropagation(),n()},children:t(a,{name:"action.remove"})})]})),S=o=>{var{onSelect:n}=o,i=e(o,["onSelect"]);return t(h,Object.assign({},i,{draggable:!0,className:"polotno-close-panel",onClick:()=>n(),onDragStart:()=>{d(({x:e,y:t})=>{n({x:e,y:t})})},onDragEnd:()=>{d(null)}}))},F=[{previewSize:"25px",fontSize:76,labelKey:"sidePanel.createHeader",textKey:"sidePanel.headerText"},{previewSize:"18px",fontSize:44,labelKey:"sidePanel.createSubHeader",textKey:"sidePanel.subHeaderText"},{previewSize:"14px",fontSize:30,labelKey:"sidePanel.createBody",textKey:"sidePanel.bodyText"}];export const TextPanel=i(({store:e})=>{n.useEffect(()=>{e.loadFont("Roboto")},[]);const i=t=>{var o;const n=t.width||e.width/2,i=((null==t?void 0:t.x)||e.width/2)-n/2,l=((null==t?void 0:t.y)||e.height/2)-t.fontSize/2,a=(e.width+e.height)/2160,r=null===(o=e.activePage)||void 0===o?void 0:o.addElement(Object.assign(Object.assign({type:"text",fontFamily:"Roboto"},t),{x:i,y:l,width:n,fontSize:t.fontSize*a}));s()||null==r||r.toggleEditMode(!0)};n.useEffect(()=>{e.fonts.forEach(t=>e.loadFont(t.fontFamily))},[e.fonts]);const{data:f,error:m}=l(p(),c),[d,P]=n.useState("text"),[z,O]=n.useState(null),E=n.useRef(null),C=e=>{E.current=e,O(e)},[K,N]=n.useState(null);return o("div",{style:{height:"100%",display:"flex",flexDirection:"column"},children:[t(u,{value:d,onValueChange:e=>P(String(e)),className:"pn:mb-3",children:o(y,{className:"pn:w-full pn:h-10",children:[t(x,{value:"text",children:r("sidePanel.text")}),t(x,{value:"font",children:r("sidePanel.myFonts")})]})}),"text"===d&&o("div",{className:"polotno-text-panel-container pn:flex pn:flex-col",style:{height:"calc(100% - 52px)"},children:[F.map(({previewSize:e,fontSize:o,labelKey:n,textKey:l})=>t(S,{style:{fontSize:e},variant:"ghost",className:"pn:mb-[5px] pn:w-full pn:h-auto pn:min-h-[2.5rem] pn:py-1 pn:justify-start pn:font-['Roboto']",onSelect:e=>{i(Object.assign(Object.assign({},e),{fontSize:o,text:r(l),fontFamily:"Roboto"}))},children:r(n)},n)),t(g,{shadowEnabled:!1,images:null==f?void 0:f.items,getPreview:e=>e.preview,getImageClassName:e=>e.json.indexOf("plain")>=0?"polotno-text-preview-plain":"",isLoading:!f,error:m||K,getIsLoading:e=>e.json===z,onSelect:async(t,o)=>{if(E.current===t.json){return}let n;N(null),C(t.json);try{n=await v(e,t)}catch(r){return console.warn("polotno: failed to load the text template",r),void(E.current===t.json&&N(r))}finally{E.current===t.json&&C(null)}if(!e.activePage){return}const i=(e.width+e.height)/2160,l=o?o.x-n.width/2*i:e.width/2-n.width/2*i,a=o?o.y-n.height/2*i:e.height/2-n.height/2*i;e.history.transaction(()=>{const t=n.pages[0].children,o=[];t.forEach(t=>{delete t.id;const{id:n}=e.activePage.addElement(Object.assign(Object.assign({},t),{fontSize:t.fontSize*i,x:t.x*i+l,y:t.y*i+a,width:t.width*i,height:t.height*i}));o.push(n)}),e.selectElements(o)})}})]}),"font"===d&&o("div",{style:{display:"flex",flexDirection:"column",height:"calc(100% - 62px)"},children:[o("label",{htmlFor:"polotno-font-upload",children:[o(h,{style:{width:"100%"},onClick:()=>{var e;null===(e=document.querySelector("#polotno-font-upload"))||void 0===e||e.click()},children:[t(a,{name:"text.uploadFont"}),r("sidePanel.uploadFont")]}),t("input",{type:"file",accept:".ttf, .otf, .woff, .woff2, .eot",id:"polotno-font-upload",style:{display:"none"},onChange:async t=>{const{target:o}=t;for(const n of Array.from(o.files||[])){const t=await j(n),o=n.name.split(".")[0].replace(/,/g,"");e.addFont({fontFamily:o,url:t})}o.value=""}})]}),t(b,{className:"pn:mt-4",children:e.fonts.map((o,n)=>t(w,{font:o,onSelect:()=>{i({fontSize:80,text:"Cool text",fontFamily:o.fontFamily})},onRemove:()=>{e.find(e=>("text"===e.type&&e.fontFamily===o.fontFamily&&e.set({fontFamily:"Roboto"}),!1)),e.removeFont(o.fontFamily)}},n))})]})]})});
@@ -1 +1 @@
1
- import{extractFontSpecsFromHtml as t}from"../utils/rich-text-html.js";export async function loadTextTemplate(o,e){const a=Date.now(),l=((o=[])=>o.filter(t=>t.fontFamily).flatMap(o=>{var e,a;return t("",{fontFamily:o.fontFamily,fontWeight:null!==(e=o.fontWeight)&&void 0!==e?e:"normal",fontStyle:null!==(a=o.fontStyle)&&void 0!==a?a:"normal"})}))(e.fonts).map(t=>o.loadFont(t.family,t.style,t.weight).catch(()=>{})),n=await(await fetch(e.json)).json(),i=3e3-(Date.now()-a);if(i>0){let t;await Promise.race([Promise.all(l),new Promise(o=>{t=setTimeout(o,i)})]),clearTimeout(t)}return n}
1
+ import{extractFontSpecsFromHtml as t}from"../utils/rich-text-html.js";export async function loadTextTemplate(o,e){const a=Date.now(),l=((o=[])=>o.filter(t=>t.fontFamily).flatMap(o=>{var e,a;return t("",{fontFamily:o.fontFamily,fontWeight:null!==(e=o.fontWeight)&&void 0!==e?e:"normal",fontStyle:null!==(a=o.fontStyle)&&void 0!==a?a:"normal"})}))(e.fonts).map(t=>o.loadFont(t.family,t.style,t.weight).catch(()=>{})),n=await fetch(e.json);if(!n.ok){throw new Error(`polotno: text template request failed with ${n.status}`)}const i=await n.json(),r=3e3-(Date.now()-a);if(r>0){let t;await Promise.race([Promise.all(l),new Promise(o=>{t=setTimeout(o,r)})]),clearTimeout(t)}return i}
@@ -1 +1 @@
1
- import{forEveryChild as e}from"../model/group-model.js";const t={x:50,y:50},n=new WeakMap;function o(t){const n="group"===t.type?[]:[t];return e(t,e=>{"group"!==e.type&&n.push(e)}),n}export function duplicateElements(e,c){const s=function(e,c){const s=n.get(c),d=e.map(e=>e.id);if(!s||d.length!==s.copyIds.length||!s.copyIds.every(e=>d.includes(e))){return t}const r=c.getElementById(s.sourceId),p=c.getElementById(s.copyIds[0]),l=r&&o(r)[0],u=p&&o(p)[0];return l&&u?{x:u.x-l.x,y:u.y-l.y}:t}(e,c),d=e.filter(e=>!1!==e.removable).map(e=>{const t=e.clone();return o(t).forEach(e=>{e.set({x:e.x+s.x,y:e.y+s.y})}),{sourceId:e.id,copyId:t.id}}),r=d.map(e=>e.copyId);d.length&&n.set(c,{sourceId:d[0].sourceId,copyIds:r}),c.selectElements(r)}export function cloneBehind(e,t,o){const c=e.map(e=>{const n=e.clone({},{skipSelect:!0});return t.setElementZIndex(n.id,t.children.indexOf(e)),n.id});e.length&&n.set(o,{sourceId:c[0],copyIds:e.map(e=>e.id)})}
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/from-svg.js CHANGED
@@ -1 +1 @@
1
- import t from"konva";import{getImageSize as e,getCrop as r}from"@polotno/core/image";import{resolveSvgSize as n}from"@polotno/core/svg";let o=0;const i=()=>(o+=1,o.toString());function a(t){const{width:e,height:r}=n({documentElement:t});return t.setAttribute("width",`${e}px`),t.setAttribute("height",`${r}px`),{width:e,height:r}}function s(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?s(e.parentNode).multiply(r):r}function l(t){return s(t).decompose()}function g(t){const{x:e,y:r}=t.getBoundingClientRect();if("svg"===t.nodeName){return{x:e,y:r}}const n=g(t.parentNode);return{x:e+n.x,y:r+n.y}}function u(t){return t.getElementsByTagName("text").length>0}function c(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 d(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 f(t){if("defs"===t.nodeName){return[]}const n=t.getAttribute("id")||i();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:s}=l(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:s,lineHeight:t.getBBox().height/e.length/o,width:t.getBBox().width,height:t.getBBox().height,fontFamily:d(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:s,scaleY:g}=l(t),u=t.getBBox().width*s,c=t.getBBox().height*g,h=t.getAttribute("xlink:href")||"",d=await e(h),m=r({width:u,height:c},d);return[Object.assign({type:"image",id:n,x:o+t.getBBox().x,y:i+t.getBBox().y,width:u,height:c,src:h,rotation:a},m)]}if("rect"===t.nodeName){const{x:o,y:i,rotation:a}=l(t),s=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,m=d?document.querySelector(d):null;if(c){const l=null==m?void 0:m.querySelector("use"),u=null==l?void 0:l.getAttribute("xlink:href"),c=u&&document.querySelector(u),h=c&&c.getAttribute("xlink:href");if(h){const l=await e(h),u=r({width:s,height:g},l);return[Object.assign({type:"image",id:n,x:o+t.getBBox().x,y:i+t.getBBox().y,width:s,height:g,src:h,rotation:a},u)]}}t.setAttribute("transform","");const f=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="${s}" height="${g}" viewBox="0 0 ${s} ${g}">${(null==m?void 0:m.outerHTML)||""}${t.outerHTML}</svg>`;return[{type:"svg",id:n,keepRatio:!1,x:o+f.x,y:i+f.y,width:s,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}=l(t),s=t.getBBox(),{width:g,height:u}=s;t.setAttribute("transform","");const c=t.getAttribute("fill")||"",d=c.indexOf("url")>=0?h(c):null,m=d?document.querySelector(d):null;let f=m?[m]:[];if(m&&m.getAttribute("xlink:href")){f=[m];const t=document.querySelector(m.getAttribute("xlink:href"));t&&f.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>${f.map(t=>t.outerHTML).join("")}</defs><g transform="translate(${-s.x} ${-s.y})">${t.outerHTML}</g></svg>`;return[{type:"svg",id:n,keepRatio:!1,x:e+s.x*i,y:r+s.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)&&!u(t)&&!c(t)){const{x:e,y:r}=t.getBBox(),{width:o,height:i}=(g(t),t.getBBox()),s=Array.from(t.querySelectorAll("*"));let l=e,u=r;s.forEach(t=>{if("g"===t.nodeName){return}const e=t.getBBox();l=Math.min(l,e.x),u=Math.min(u,e.y)});const c=t.getBoundingClientRect(),h=m(t),d=null==h?void 0:h.getBoundingClientRect(),f=t.getAttribute("transform");t.setAttribute("transform","");const x=`<svg xmlns="http://www.w3.org/2000/svg" width="${o}" height="${i}" viewBox="0 0 ${o} ${i}"><g transform="translate(${-e} ${-r})">${t.outerHTML}</g></svg>`;t.setAttribute("transform",f||"");const p=a(h);return[{type:"svg",id:n,keepRatio:!1,x:(c.x-d.x)/d.width*p.width,y:(c.y-d.y)/d.height*p.height,width:o,height:i,name:t.getAttribute("id")||"",src:svgToURL(x)}]}if(!u(t)&&!c(t)){const e=[];for(const r of Array.from(t.children)){e.push(...await f(r))}return e}const o=[];for(const e of Array.from(t.children)){o.push(...await f(e))}return o}function x(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?(x(e),e.outerHTML):t}return x(e.documentElement),e.documentElement.outerHTML}(t);const r=e.querySelector("svg"),{width:n,height:o}=a(r),s=[];for(const i of Array.from(r.children)){s.push(...await f(i))}return e.parentElement.removeChild(e),{width:n,height:o,pages:[{id:i(),children:s}]}}
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}]}}
@@ -1 +1 @@
1
- import{decodeBase64 as t,encodeBase64 as e}from"./base64.js";const n=[112,72,89,115],r=(()=>{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})(),s=(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};export const addDpiMetadataToImage=(o,l,a)=>{const i=o.indexOf(","),f=i>=0?o.slice(i+1):o,c=t(f),g="image/png"===l?((t,e)=>{const o=(t=>{const e=new Uint8Array(9),o=new DataView(e.buffer);o.setUint32(0,t,!1),o.setUint32(4,t,!1),e[8]=1;const l=new Uint8Array(8+e.length+4);s(l,0,e.length),l.set(n,4),l.set(e,8);const a=new Uint8Array(4+e.length);return a.set(n,0),a.set(e,4),s(l,8+e.length,(t=>{let e=4294967295;for(let n=0;n<t.length;n++){e=r[255&(e^t[n])]^e>>>8}return(4294967295^e)>>>0})(a)),l})(Math.round(e/.0254)),l=((t,e)=>{let n=8;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,n);if(l){const e=new Uint8Array(t.length-l.totalLength+o.length);return e.set(t.slice(0,l.offset),0),e.set(o,l.offset),e.set(t.slice(l.offset+l.totalLength),l.offset+o.length),e}const a=16+(t[8]<<24|t[9]<<16|t[10]<<8|t[11])+4;if(a>t.length){return t}const i=new Uint8Array(t.length+o.length);return i.set(t.slice(0,a),0),i.set(o,a),i.set(t.slice(a),a+o.length),i})(c,a):((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})(c,a);return`data:${l};base64,${e(g)}`};
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)}`};
@@ -1 +1 @@
1
- import*as e from"mobx";import{getAPI as o}from"./api.js";import{removeBackgroundState 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 u=`%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 p=fetch;export const __=e=>{p=e};export async function isKeyPaid(n){for(let s=0;s<5;s++){try{const r=await p(o()+"/validate-key",{method:"POST",body:JSON.stringify({key:n,site:location.host,skdVersion:"4.17.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(u,"background: rgba(247, 101, 68, 1); color: white; padding: 5px; margin: 5px;"),e.runInAction(()=>{t.defaultEnabled=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()}
1
+ import*as e from"mobx";import{getAPI as o}from"./api.js";import{removeBackgroundState 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 u=`%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 p=fetch;export const __=e=>{p=e};export async function isKeyPaid(n){for(let s=0;s<5;s++){try{const r=await p(o()+"/validate-key",{method:"POST",body:JSON.stringify({key:n,site:location.host,skdVersion:"4.18.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(u,"background: rgba(247, 101, 68, 1); color: white; padding: 5px; margin: 5px;"),e.runInAction(()=>{t.defaultEnabled=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()}