polotno 4.5.0 → 4.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/canvas/html-element.js +1 -1
- package/canvas/page.js +1 -1
- package/canvas/quill-font-formats.js +1 -1
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.js +2 -2
- package/canvas/text-element/placeholder.d.ts +1 -0
- package/canvas/text-element/placeholder.js +1 -0
- package/canvas/text-element.js +1 -1
- package/package.json +1 -1
- package/polotno.bundle.js +144 -141
- package/toolbar/html-toolbar.js +10 -7
- package/utils/rich-text-html.d.ts +1 -0
- package/utils/rich-text-html.js +1 -1
- package/utils/text-format.js +1 -1
- package/utils/validate-key.js +1 -1
package/toolbar/html-toolbar.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e}from"react/jsx-runtime";import o from"react";import{observer as n}from"mobx-react-lite";import{PolotnoIcon as i}from"../icons/registry.js";import{Button as l}from"../primitives/button.js";import{Tooltip as r,TooltipContent as s,TooltipTrigger as a}from"../primitives/tooltip.js";import{TooltipIconButton as c}from"../primitives/tooltip-icon-button.js";import{ColorPicker as m}from"./color-picker.js";import p from"./filters-picker.js";import{ElementContainer as d,extendToolbar as
|
|
1
|
+
import{jsx as t,jsxs as e}from"react/jsx-runtime";import o from"react";import{observer as n}from"mobx-react-lite";import{PolotnoIcon as i}from"../icons/registry.js";import{Button as l}from"../primitives/button.js";import{Tooltip as r,TooltipContent as s,TooltipTrigger as a}from"../primitives/tooltip.js";import{TooltipIconButton as c}from"../primitives/tooltip-icon-button.js";import{ColorPicker as m}from"./color-picker.js";import p from"./filters-picker.js";import{ElementContainer as d,extendToolbar as f}from"./element-container.js";import u from"../utils/styled.js";import{AnimationsPicker as x}from"./animations-picker.js";import{flags as g}from"../utils/flags.js";import{TextAiWrite as v}from"./text-ai-write.js";import{t as h}from"../utils/l10n.js";import{quillRef as b,createQuill as T,setQuillContent as y}from"../canvas/html-element.js";import{FontMenu as S,fetcher as F,TextSpacing as z,TextFontSize as j,TextTransform as w,ALIGN_OPTIONS as C,VERTICAL_ALIGN_OPTIONS as N,VERTICAL_ALIGN_ICONS as A}from"./text-toolbar.js";import{cycleNext as k}from"./toolbar-shared.js";import E from"swr";import{getFontsList as D,globalFonts as L,isGoogleFontChanged as I}from"../utils/fonts.js";import{getGoogleFontsListAPI as M}from"../utils/api.js";import{quillHtmlToModelText as R,listEffectiveFontFamilies as O,isBoldWeight as W}from"../utils/rich-text-html.js";import{patchTextFormat as B,decorationTokensToString as H,isDecorationActive as P}from"../utils/text-format.js";import{useTextFormat as V,getActiveTextEditor as _,resolveSelectionScope as G,applyToLiveEditor as J}from"../utils/text-format-state.js";const Q=u("div")`
|
|
2
2
|
position: relative;
|
|
3
3
|
display: inline-block;
|
|
4
4
|
|
|
@@ -8,7 +8,14 @@ import{jsx as t,jsxs as e}from"react/jsx-runtime";import o from"react";import{ob
|
|
|
8
8
|
&.polotno-mixed:focus-within input {
|
|
9
9
|
color: inherit !important;
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
/* Hide the "—" overlay on focus (the digits are revealed above) so the two
|
|
12
|
+
never stack. Target the overlay's stable class rather than interpolating
|
|
13
|
+
\`\${FontSizeMixedOverlay}\`: our goober \`styled\` wraps components in
|
|
14
|
+
forwardRef, so that interpolation stringifies to "[object Object]". */
|
|
15
|
+
&:focus-within .polotno-fs-mixed-overlay {
|
|
16
|
+
display: none;
|
|
17
|
+
}
|
|
18
|
+
`,U=u("span")`
|
|
12
19
|
position: absolute;
|
|
13
20
|
left: 11px;
|
|
14
21
|
top: 0;
|
|
@@ -18,8 +25,4 @@ import{jsx as t,jsxs as e}from"react/jsx-runtime";import o from"react";import{ob
|
|
|
18
25
|
pointer-events: none;
|
|
19
26
|
font-size: inherit;
|
|
20
27
|
color: inherit;
|
|
21
|
-
|
|
22
|
-
${Q}:focus-within & {
|
|
23
|
-
display: none;
|
|
24
|
-
}
|
|
25
|
-
`,X=t=>{const e=G();return!!e&&e.store===t&&!!e.instance};let $=null;const q=()=>{b.pendingToolbarInteraction=!0,$&&clearTimeout($),$=setTimeout(()=>{b.pendingToolbarInteraction=!1,$=null},100)},K=t=>{var e;(!(e=t.target)||"INPUT"!==e.tagName&&"TEXTAREA"!==e.tagName&&"SELECT"!==e.tagName&&!e.isContentEditable)&&t.preventDefault(),q()},Y=n(({store:e,isActive:o,toggle:n,icon:i,ariaLabel:l,tooltipContent:r})=>{const{format:s,applyTextFormat:a}=_(e);return t(c,{label:r||l||"",side:"bottom",active:o(s),onMouseDown:t=>{t.preventDefault()},onClick:()=>n(s,a),children:i})});function Z(t,e){const o="underline"===e?"line-through":"underline",n=new Set;return V(t,e)||n.add(e),V(t,o)&&n.add(o),P(n)}const tt=(t,e,o)=>e({textDecoration:Z(t,o)},{decorationTokens:[o]});export const TextBold=n(({store:e})=>t(Y,{store:e,isActive:t=>B(t.fontWeight.value),toggle:(t,e)=>e({fontWeight:B(t.fontWeight.value)?400:700}),icon:t(i,{name:"text.bold",className:"pn:size-4"}),tooltipContent:v("toolbar.bold")}));export const FontStyleGroup=n(({element:o,store:n,elements:m,components:p})=>{const d=(null==p?void 0:p.TextBold)||TextBold;return e("div",{className:"pn:flex pn:items-center pn:gap-0.5",children:[t(d,{element:o,store:n}),t(Y,{store:n,isActive:t=>"italic"===t.fontStyle.value,toggle:(t,e)=>e({fontStyle:"italic"===t.fontStyle.value?"normal":"italic"}),icon:t(i,{name:"text.italic",className:"pn:size-4"}),tooltipContent:v("toolbar.italic")}),t(Y,{store:n,isActive:t=>V(t,"underline"),toggle:(t,e)=>tt(t,e,"underline"),icon:t(i,{name:"text.underline",className:"pn:size-4"}),tooltipContent:v("toolbar.underline")}),t(Y,{store:n,isActive:t=>V(t,"line-through"),toggle:(t,e)=>tt(t,e,"line-through"),icon:t(i,{name:"text.strikethrough",className:"pn:size-4"}),tooltipContent:v("toolbar.strikethrough")}),t(c,{label:v("toolbar.textAlign"),side:"bottom",onClick:()=>{const t=k(A,o.align);n.history.transaction(()=>{m.forEach(e=>{e.set({align:t})})})},children:"left"===o.align?t(i,{name:"text.alignLeft",className:"pn:size-4"}):"center"===o.align?t(i,{name:"text.alignCenter",className:"pn:size-4"}):"right"===o.align?t(i,{name:"text.alignRight",className:"pn:size-4"}):t(i,{name:"text.alignJustify",className:"pn:size-4"})}),g.textVerticalResizeEnabled&&e(r,{children:[t(a,{render:t(l,{variant:"ghost",size:"icon-sm",onClick:()=>{const t=k(N,o.verticalAlign);n.history.transaction(()=>{o.set({verticalAlign:t})})},"aria-label":v("toolbar.verticalAlign"),children:w[o.verticalAlign]})}),t(s,{side:"bottom",children:v("toolbar.verticalAlign")})]}),t(c,{label:v("toolbar.listFormat"),side:"bottom",onMouseDown:t=>{t.preventDefault()},onClick:()=>{var t;let e=window.__polotnoQuill;const n=!e;e=e||(({html:t})=>{const e=document.createElement("div");document.body.appendChild(e),e.style.display="none",e.style.whiteSpace="pre-wrap";const o=T(e);return y(o,t),o})({html:o.text}),n&&e.setSelection(0,e.getLength(),"api");const i=e.getSelection(!0)||(null===(t=G())||void 0===t?void 0:t.lastSelection)||{index:0,length:e.getLength()},l=e.getFormat(i);l.list?"bullet"===l.list?e.format("list","ordered"):e.format("list",!1):e.format("list","bullet"),n&&(o.set({text:R(e.root.innerHTML,{fontSize:o.fontSize,fontFamily:o.fontFamily})}),(t=>{t.root.parentElement.remove()})(e))},children:(()=>{const e=G();return"bullet"===(e&&e.store===n?e.currentFormat:{}).list?t(i,{name:"text.numberedList",className:"pn:size-5"}):t(i,{name:"text.bulletList",className:"pn:size-5"})})()})]})});export const FontColorInput=n(({element:e,store:o})=>{var n;const{format:i,applyTextFormat:l}=_(o);return t("div",{"data-polotno-keep-focus":!0,onMouseDown:K,children:t(m,{value:null!==(n=i.fill.value)&&void 0!==n?n:e.fill,gradientEnabled:!0,onClose:()=>{const t=G();(null==t?void 0:t.instance)&&t.lastSelection&&t.instance.setSelection(t.lastSelection)},onChange:t=>l({fill:t}),store:o})})});export const DirectionInput=n(({element:e})=>{const o="rtl"===e.dir,n=v("toolbar.textDirection");return t(c,{label:n,side:"bottom",onClick:()=>{e.set({dir:o?"ltr":"rtl"})},children:t(i,o?{name:"text.paragraph",className:"pn:size-5 pn:-scale-x-100"}:{name:"text.richText",className:"pn:size-5"})})});const et=D(),ot={TextFontFamily:n(({elements:e,store:n})=>{var i;const{format:l,applyTextFormat:r}=_(n),s=X(n),a=l.fontFamily.mixed,c=a?Array.from(new Set(e.flatMap(t=>W(t.text,t.fontFamily)))).sort((t,e)=>t.localeCompare(e,void 0,{sensitivity:"base"})):void 0,[m,p]=o.useState(!1),{data:d,mutate:u}=E(m?M():null,F,{isPaused:()=>I(),fallbackData:[]});o.useEffect(()=>{u()},[I()]);const f=n.fonts.concat(L).map(t=>t.fontFamily).concat((null==d?void 0:d.length)&&!I()?d:et),x=[];n.find(t=>("text"===t.type&&x.push(t.fontFamily),!1));const g=[...new Set(x.concat("_divider").concat(f))],h=null!==(i=l.fontFamily.value)&&void 0!==i?i:e[0].fontFamily;let v=h;v.length>15&&(v=v.slice(0,15)+"...");const b=t(S,{fonts:g,activeFont:h,activeFontLabel:v,store:n,mixed:a,mixedFonts:c,open:m,onOpenChange:p,onFontSelect:t=>r({fontFamily:t})});return s?t("div",{"data-polotno-keep-focus":!0,onMouseDown:K,children:b}):b}),TextFontSize:n(({elements:o,store:n})=>{const{format:i}=_(n),l=X(n),r=i.fontSize.mixed,s=i.fontSize.value,a=o.map(t=>new Proxy(t,{get:(t,e)=>"fontSize"===e?null!=s?s:t.fontSize:"set"===e?e=>((t,e)=>{if(!e.fontSize){return void t.set(e)}const o=G();if(o&&o.store===n&&o.instance){const{scope:t,isEmpty:n}=H(o.instance,o.lastSelection);if("partial"===t||"collapsed"===t&&n){return void J(o.instance,{fontSize:e.fontSize},o.lastSelection)}}const{attrsToSet:i}=O(t,{fontSize:e.fontSize});t.set(Object.assign(Object.assign({},e),i))})(t,e):Reflect.get(t,e)})),c=e(Q,{className:r?"polotno-mixed":"",children:[t(j,{elements:a,store:n}),r&&t(U,{"aria-hidden":!0,children:"—"})]});return l?t("div",{"data-polotno-keep-focus":!0,onMouseDown:q,children:c}):c}),TextFontVariant:FontStyleGroup,TextTransform:C,TextFilters:p,TextFill:FontColorInput,TextSpacing:z,TextDirection:DirectionInput,TextAnimations:x,TextAiWrite:h};export const HtmlToolbar=n(({store:e,components:o})=>{const n=e.selectedElements,i=e.selectedElements[0],l=["TextFill","TextFontFamily","TextFontSize","TextFontVariant","TextSpacing","TextTransform","TextFilters",g.animationsEnabled&&"TextAnimations",g.aiTextEnabled&&"TextAiWrite"],r=u({type:"text",usedItems:l,components:o});return t(d,{items:r,itemRender:l=>{if("TextBold"===l){return null}const r=(null==o?void 0:o[l])||ot[l];return t(r,{element:i,elements:n,store:e,components:o},l)}})});
|
|
28
|
+
`,X=t=>{const e=_();return!!e&&e.store===t&&!!e.instance};let $=null;const q=()=>{b.pendingToolbarInteraction=!0,$&&clearTimeout($),$=setTimeout(()=>{b.pendingToolbarInteraction=!1,$=null},100)},K=t=>{var e;(!(e=t.target)||"INPUT"!==e.tagName&&"TEXTAREA"!==e.tagName&&"SELECT"!==e.tagName&&!e.isContentEditable)&&t.preventDefault(),q()},Y=n(({store:e,isActive:o,toggle:n,icon:i,ariaLabel:l,tooltipContent:r})=>{const{format:s,applyTextFormat:a}=V(e);return t(c,{label:r||l||"",side:"bottom",active:o(s),onMouseDown:t=>{t.preventDefault()},onClick:()=>n(s,a),children:i})});function Z(t,e){const o="underline"===e?"line-through":"underline",n=new Set;return P(t,e)||n.add(e),P(t,o)&&n.add(o),H(n)}const tt=(t,e,o)=>e({textDecoration:Z(t,o)},{decorationTokens:[o]});export const TextBold=n(({store:e})=>t(Y,{store:e,isActive:t=>W(t.fontWeight.value),toggle:(t,e)=>e({fontWeight:W(t.fontWeight.value)?400:700}),icon:t(i,{name:"text.bold",className:"pn:size-4"}),tooltipContent:h("toolbar.bold")}));export const FontStyleGroup=n(({element:o,store:n,elements:m,components:p})=>{const d=(null==p?void 0:p.TextBold)||TextBold;return e("div",{className:"pn:flex pn:items-center pn:gap-0.5",children:[t(d,{element:o,store:n}),t(Y,{store:n,isActive:t=>"italic"===t.fontStyle.value,toggle:(t,e)=>e({fontStyle:"italic"===t.fontStyle.value?"normal":"italic"}),icon:t(i,{name:"text.italic",className:"pn:size-4"}),tooltipContent:h("toolbar.italic")}),t(Y,{store:n,isActive:t=>P(t,"underline"),toggle:(t,e)=>tt(t,e,"underline"),icon:t(i,{name:"text.underline",className:"pn:size-4"}),tooltipContent:h("toolbar.underline")}),t(Y,{store:n,isActive:t=>P(t,"line-through"),toggle:(t,e)=>tt(t,e,"line-through"),icon:t(i,{name:"text.strikethrough",className:"pn:size-4"}),tooltipContent:h("toolbar.strikethrough")}),t(c,{label:h("toolbar.textAlign"),side:"bottom",onClick:()=>{const t=k(C,o.align);n.history.transaction(()=>{m.forEach(e=>{e.set({align:t})})})},children:"left"===o.align?t(i,{name:"text.alignLeft",className:"pn:size-4"}):"center"===o.align?t(i,{name:"text.alignCenter",className:"pn:size-4"}):"right"===o.align?t(i,{name:"text.alignRight",className:"pn:size-4"}):t(i,{name:"text.alignJustify",className:"pn:size-4"})}),g.textVerticalResizeEnabled&&e(r,{children:[t(a,{render:t(l,{variant:"ghost",size:"icon-sm",onClick:()=>{const t=k(N,o.verticalAlign);n.history.transaction(()=>{o.set({verticalAlign:t})})},"aria-label":h("toolbar.verticalAlign"),children:A[o.verticalAlign]})}),t(s,{side:"bottom",children:h("toolbar.verticalAlign")})]}),t(c,{label:h("toolbar.listFormat"),side:"bottom",onMouseDown:t=>{t.preventDefault()},onClick:()=>{var t;let e=window.__polotnoQuill;const n=!e;e=e||(({html:t})=>{const e=document.createElement("div");document.body.appendChild(e),e.style.display="none",e.style.whiteSpace="pre-wrap";const o=T(e);return y(o,t),o})({html:o.text}),n&&e.setSelection(0,e.getLength(),"api");const i=e.getSelection(!0)||(null===(t=_())||void 0===t?void 0:t.lastSelection)||{index:0,length:e.getLength()},l=e.getFormat(i);l.list?"bullet"===l.list?e.format("list","ordered"):e.format("list",!1):e.format("list","bullet"),n&&(o.set({text:R(e.root.innerHTML,{fontSize:o.fontSize,fontFamily:o.fontFamily})}),(t=>{t.root.parentElement.remove()})(e))},children:(()=>{const e=_();return"bullet"===(e&&e.store===n?e.currentFormat:{}).list?t(i,{name:"text.numberedList",className:"pn:size-5"}):t(i,{name:"text.bulletList",className:"pn:size-5"})})()})]})});export const FontColorInput=n(({element:e,store:o})=>{var n;const{format:i,applyTextFormat:l}=V(o);return t("div",{"data-polotno-keep-focus":!0,onMouseDown:K,children:t(m,{value:null!==(n=i.fill.value)&&void 0!==n?n:e.fill,gradientEnabled:!0,onClose:()=>{const t=_();(null==t?void 0:t.instance)&&t.lastSelection&&t.instance.setSelection(t.lastSelection)},onChange:t=>l({fill:t}),store:o})})});export const DirectionInput=n(({element:e})=>{const o="rtl"===e.dir,n=h("toolbar.textDirection");return t(c,{label:n,side:"bottom",onClick:()=>{e.set({dir:o?"ltr":"rtl"})},children:t(i,o?{name:"text.paragraph",className:"pn:size-5 pn:-scale-x-100"}:{name:"text.richText",className:"pn:size-5"})})});const et=D(),ot={TextFontFamily:n(({elements:e,store:n})=>{var i;const{format:l,applyTextFormat:r}=V(n),s=X(n),a=l.fontFamily.mixed,c=a?Array.from(new Set(e.flatMap(t=>O(t.text,t.fontFamily)))).sort((t,e)=>t.localeCompare(e,void 0,{sensitivity:"base"})):void 0,[m,p]=o.useState(!1),{data:d,mutate:f}=E(m?M():null,F,{isPaused:()=>I(),fallbackData:[]});o.useEffect(()=>{f()},[I()]);const u=n.fonts.concat(L).map(t=>t.fontFamily).concat((null==d?void 0:d.length)&&!I()?d:et),x=[];n.find(t=>("text"===t.type&&x.push(t.fontFamily),!1));const g=[...new Set(x.concat("_divider").concat(u))],v=null!==(i=l.fontFamily.value)&&void 0!==i?i:e[0].fontFamily;let h=v;h.length>15&&(h=h.slice(0,15)+"...");const b=t(S,{fonts:g,activeFont:v,activeFontLabel:h,store:n,mixed:a,mixedFonts:c,open:m,onOpenChange:p,onFontSelect:t=>r({fontFamily:t})});return s?t("div",{"data-polotno-keep-focus":!0,onMouseDown:K,children:b}):b}),TextFontSize:n(({elements:o,store:n})=>{const{format:i}=V(n),l=X(n),r=i.fontSize.mixed,s=i.fontSize.value,a=o.map(t=>new Proxy(t,{get:(t,e)=>"fontSize"===e?null!=s?s:t.fontSize:"set"===e?e=>((t,e)=>{if(!e.fontSize){return void t.set(e)}const o=_();if(o&&o.store===n&&o.instance){const{scope:t,isEmpty:n}=G(o.instance,o.lastSelection);if("partial"===t||"collapsed"===t&&n){return void J(o.instance,{fontSize:e.fontSize},o.lastSelection)}}const{attrsToSet:i}=B(t,{fontSize:e.fontSize});t.set(Object.assign(Object.assign({},e),i))})(t,e):Reflect.get(t,e)})),c=e(Q,{className:r?"polotno-mixed":"",children:[t(j,{elements:a,store:n}),r&&t(U,{className:"polotno-fs-mixed-overlay","aria-hidden":!0,children:"—"})]});return l?t("div",{"data-polotno-keep-focus":!0,onMouseDown:q,children:c}):c}),TextFontVariant:FontStyleGroup,TextTransform:w,TextFilters:p,TextFill:FontColorInput,TextSpacing:z,TextDirection:DirectionInput,TextAnimations:x,TextAiWrite:v};export const HtmlToolbar=n(({store:e,components:o})=>{const n=e.selectedElements,i=e.selectedElements[0],l=["TextFill","TextFontFamily","TextFontSize","TextFontVariant","TextSpacing","TextTransform","TextFilters",g.animationsEnabled&&"TextAnimations",g.aiTextEnabled&&"TextAiWrite"],r=f({type:"text",usedItems:l,components:o});return t(d,{items:r,itemRender:l=>{if("TextBold"===l){return null}const r=(null==o?void 0:o[l])||ot[l];return t(r,{element:i,elements:n,store:e,components:o},l)}})});
|
|
@@ -15,6 +15,7 @@ export declare function extractFontSpecsFromHtml(html: string, defaults: {
|
|
|
15
15
|
fontWeight: string | number;
|
|
16
16
|
fontStyle: string;
|
|
17
17
|
}): FontSpec[];
|
|
18
|
+
export declare function isClipGradientSpan(el: HTMLElement): boolean;
|
|
18
19
|
export declare function stripInlineFormat(html: string, format: string): string;
|
|
19
20
|
interface QuillDeltaLike {
|
|
20
21
|
ops: Array<{
|
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,QUILL_FONT_CLASS_TO_FAMILY as i,guessFontFamilyFromQlClass as l,normalizeRichTextHtml as s}from"@polotno/core/rich-text-html";import{LRUCache as c}from"./lru-cache.js";const f=new c(500);export function htmlInflatesMinContentWidth(e){if(!e||!e.includes("<")){return!1}let r=f.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}}}}f.set(e,r)}return r}export function quillHtmlToModelText(t,e){return s(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 s=r(n.fontFamily)||n.fontFamily,c=String(o(n.fontWeight)),f="italic"===n.fontStyle?"italic":"normal",u=new Map,a=(t,e,o)=>{const n=r(t)||t||s,i=`${n}\0${e}\0${o}`;u.has(i)||u.set(i,{family:n,weight:e,style:o})};if(a(s,c,f),e&&e.includes("<")){const r=t(e);if(r){const t=r.ownerDocument.createTreeWalker(r,NodeFilter.SHOW_TEXT);let e=t.nextNode();for(;e;){if((e.textContent||"").trim().length>0){let t="",n="",u="",y=e.parentElement;for(;y&&y!==r;){if(!t){if(y.style.fontFamily){t=y.style.fontFamily}else{const e=(y.getAttribute("class")||"").split(/\s+/).find(t=>t.startsWith("ql-font-"));e&&(t=i[e]||l(e))}}n||(y.style.fontWeight?n=String(o(y.style.fontWeight)):"STRONG"!==y.tagName&&"B"!==y.tagName||(n="700")),u||(y.style.fontStyle?u=y.style.fontStyle:"EM"!==y.tagName&&"I"!==y.tagName||(u="italic")),y=y.parentElement}a(t||s,n||c,u||f)}e=t.nextNode()}}}return[...u.values()]}const u={bold:["STRONG","B"],italic:["EM","I"],underline:["U"],strike:["S","DEL"]}
|
|
1
|
+
export*from"@polotno/core/rich-text-html";import{parseHtmlRoot as t,cleanupInlineWrappers as e,parsePrimaryFontFamily as r,toNumericWeight as o,extractFontFamiliesFromHtml as n,QUILL_FONT_CLASS_TO_FAMILY as i,guessFontFamilyFromQlClass as l,normalizeRichTextHtml as s}from"@polotno/core/rich-text-html";import{LRUCache as c}from"./lru-cache.js";const f=new c(500);export function htmlInflatesMinContentWidth(e){if(!e||!e.includes("<")){return!1}let r=f.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}}}}f.set(e,r)}return r}export function quillHtmlToModelText(t,e){return s(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 s=r(n.fontFamily)||n.fontFamily,c=String(o(n.fontWeight)),f="italic"===n.fontStyle?"italic":"normal",u=new Map,a=(t,e,o)=>{const n=r(t)||t||s,i=`${n}\0${e}\0${o}`;u.has(i)||u.set(i,{family:n,weight:e,style:o})};if(a(s,c,f),e&&e.includes("<")){const r=t(e);if(r){const t=r.ownerDocument.createTreeWalker(r,NodeFilter.SHOW_TEXT);let e=t.nextNode();for(;e;){if((e.textContent||"").trim().length>0){let t="",n="",u="",y=e.parentElement;for(;y&&y!==r;){if(!t){if(y.style.fontFamily){t=y.style.fontFamily}else{const e=(y.getAttribute("class")||"").split(/\s+/).find(t=>t.startsWith("ql-font-"));e&&(t=i[e]||l(e))}}n||(y.style.fontWeight?n=String(o(y.style.fontWeight)):"STRONG"!==y.tagName&&"B"!==y.tagName||(n="700")),u||(y.style.fontStyle?u=y.style.fontStyle:"EM"!==y.tagName&&"I"!==y.tagName||(u="italic")),y=y.parentElement}a(t||s,n||c,u||f)}e=t.nextNode()}}}return[...u.values()]}const u={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 a={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=u[o]||[],c=a[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 y=Symbol("unset");export function isQuillFormatMixed(t,e){var r;let o=y;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===y){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 o=[],i=e?r(e)||e:"";i&&o.push(i);for(const c of n(t||"")){const t=r(c)||c;t&&o.push(t)}const l=new Set,s=[];for(const r of o){l.has(r)||(l.add(r),s.push(r))}return s.sort((t,e)=>t.localeCompare(e,void 0,{sensitivity:"base"})),s}
|
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,QUILL_FONT_CLASS_TO_FAMILY as c,guessFontFamilyFromQlClass 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 l(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-"));if(e){const o=c[e]||a(e);return t(o)||void 0}return}case"fill":return
|
|
1
|
+
import{parsePrimaryFontFamily as t,parseFontSizeToPx as e,parseHtmlRoot as o,stripInlineFontSizes as n,stripInlineFontFamilies as r,stripInlineFormat as i,toNumericWeight as s,QUILL_FONT_CLASS_TO_FAMILY as c,guessFontFamilyFromQlClass as a,isClipGradientSpan as f}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 l(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-"));if(e){const o=c[e]||a(e);return t(o)||void 0}return}case"fill":return f(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 u(t,e){return"fontWeight"===e?s(t.fontWeight):t[e]}function d(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=l(s,"textDecoration",o);if("string"==typeof t){for(const e of decorationTokensOf(t)){i.add(e)}}continue}if(r.has(t)){continue}const e=l(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]=u(o,c))}return n.textDecoration=decorationTokensToString(i),n}function h(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 g(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]=u(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(d(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?u(t,o):void 0;n[o]=h(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]=h(n,void 0)}return e}(t.map(g)):g(t)}function x(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 T=!1;function y(t){return"string"!=typeof t||/^\d+$/.test(t.trim())||T||(T=!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(y(e.fontWeight)):e[f],a=x(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/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:"4.5.
|
|
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:"4.5.1"})});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()}
|