@claspo/document-connector 16.0.5 → 16.0.6

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,5 +1,6 @@
1
1
  export default class GoogleFontsLoader {
2
2
  static getUsedGoogleFonts(documentModel: any): any;
3
+ static _getEnabledStyleEntries(node: any): any[];
3
4
  loadedGoogleFonts: any[];
4
5
  testStrings: {
5
6
  latin: string;
@@ -1 +1 @@
1
- import{ComponentPropsExtractor}from"@claspo/renderer/document-model/ComponentPropsExtractor";import DocumentUtils from"@claspo/common/document/DocumentUtils";export default class GoogleFontsLoader{constructor(){this.loadedGoogleFonts=[],this.testStrings={latin:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",cyrillic:"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"}}static getUsedGoogleFonts(t){const e=t.shared.googleFonts;if(!e||0===e.length)return[];const o=new Set,s=new Set;for(const e of t.views||[])DocumentUtils.iterateDepthFirst(e,t=>{const e=ComponentPropsExtractor.extractClassesFromStyles(t);for(const t of e)t&&t.startsWith("cl-")&&s.add(t);const n=[];t.props&&t.props.adaptiveStyles&&(n.push(...t.props.adaptiveStyles.desktop||[]),n.push(...t.props.adaptiveStyles.mobile||[])),t.props&&t.props.styles&&n.push(...t.props.styles);for(const t of n)t.styleAttributes&&t.styleAttributes.fontFamily&&o.add(t.styleAttributes.fontFamily)});const n=t.shared;for(const t of s){const e=n.textClasses&&n.textClasses[t];e&&(e.styleAttributes&&e.styleAttributes.fontFamily?o.add(e.styleAttributes.fontFamily):(e.isHeader&&n.headerFontFamily&&o.add(n.headerFontFamily),!e.isHeader&&n.textFontFamily&&o.add(n.textFontFamily)))}return n.textFontFamily&&o.add(n.textFontFamily),e.filter(t=>o.has(t))}async load(t,e){try{if(e=e||document,!(t||[]).length)return;const o=this._getDynamicUrlPart(t,e);if(!o||0===o.length)return;await this._addFontsLinkToDocument(o,e);const s=this._generateFontVariations(t),n=this._createFontTester(s,e);e.body.appendChild(n);const r=this._waitForAllFontsToLoad(s,e),a=new Promise((t,e)=>setTimeout(()=>e(new Error("Timeout")),2e3));await Promise.race([r,a]),n.remove()}catch(t){console.error("Error while loading fonts",t)}}cleanup(){this.loadedGoogleFonts.forEach(t=>t.remove()),this.loadedGoogleFonts=[]}_getDynamicUrlPart(t,e){let o=[];(e.fonts||[]).forEach(t=>o.push(`${t.family} ${t.weight} ${t.style}`));const s=new Set(o);return this._combineUrlDynamicPart(t,s)}_combineUrlDynamicPart(t,e){const o=[];let s=this._makeFontOptions();return t.forEach(t=>{let n="",r="";s.forEach(o=>{if(!e.has(`${t} ${o} normal`)){const t=`1,${o}`;r+=r?`;${t}`:t}if(!e.has(`${t} ${o} italic`)){const t=`0,${o}`;n+=n?`;${t}`:t}}),(n||r)&&o.push({fontFamily:t,urlPart:`&family=${t.replace(/ /g,"+")}:ital,wght@${n};${r}`})}),o}_makeFontOptions(){const t=[];for(let e=0;e<9;e++){const o=100*(e+1);t.push(o)}return t}_addFontsLinkToDocument(t,e){if(!e.head)return Promise.resolve();const o=t.map(({fontFamily:t,urlPart:o})=>new Promise((s,n)=>{const r=document.createElement("link");r.rel="stylesheet",r.href="https://fonts.googleapis.com/css2?display=swap"+o,r.onload=()=>s({fontFamily:t,success:!0}),r.onerror=()=>{console.error(`Failed to load font: ${t}`),n(new Error(`Failed to load font: ${t}`))},e.head.appendChild(r),this.loadedGoogleFonts.push(r)}));return Promise.allSettled(o)}_generateFontVariations(t){const e=[100,200,300,400,500,600,700,800,900],o=["normal","italic"],s=[];return t.forEach(t=>{e.forEach(e=>{o.forEach(o=>{s.push({family:t,weight:e,style:o,descriptor:`${o} ${e} 1em "${t}"`})})})}),s}_createFontTester(t,e){const o=e.createElement("div");return o.setAttribute("aria-hidden","true"),o.style.cssText="\n position: fixed;\n top: -9999px;\n left: -9999px;\n width: 1px;\n height: 1px;\n overflow: hidden;\n visibility: hidden;\n pointer-events: none;\n user-select: none;\n z-index: -1;\n ",t.forEach(t=>{Object.entries(this.testStrings).forEach(([s,n])=>{const r=e.createElement("span");r.textContent=n,r.style.cssText=`\n font-family: "${t.family}", sans-serif;\n font-weight: ${t.weight};\n font-style: ${t.style};\n font-size: 20px;\n display: inline-block;\n white-space: nowrap;\n `,r.dataset.font=`${t.family}-${t.weight}-${t.style}`,r.dataset.subset=s,o.appendChild(r)})}),o}async _waitForAllFontsToLoad(t,e){const o=[];for(let e=0;e<t.length;e+=10)o.push(t.slice(e,e+10));for(const t of o)await this._waitForFontBatchLoad(t,e)}async _waitForFontBatchLoad(t,e){const o=t.map(async t=>{try{e.fonts.check(t.descriptor)||await e.fonts.load(t.descriptor)}catch(e){console.error(`Failed to load: ${t.descriptor}`,e)}});return Promise.all(o)}}
1
+ import DocumentUtils from"@claspo/common/document/DocumentUtils";export default class GoogleFontsLoader{constructor(){this.loadedGoogleFonts=[],this.testStrings={latin:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",cyrillic:"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"}}static getUsedGoogleFonts(t){const e=t.shared.googleFonts;if(!e||0===e.length)return[];const o=new Set,s=new Set;for(const e of t.views||[])DocumentUtils.iterateDepthFirst(e,t=>{const e=GoogleFontsLoader._getEnabledStyleEntries(t);for(const t of e)if(t.classes)for(const e of t.classes.split(" "))e&&e.startsWith("cl-")&&s.add(e);for(const t of e)t.styleAttributes&&t.styleAttributes.fontFamily&&o.add(t.styleAttributes.fontFamily)});const n=t.shared;for(const t of s){const e=n.textClasses&&n.textClasses[t];e&&(e.styleAttributes&&e.styleAttributes.fontFamily?o.add(e.styleAttributes.fontFamily):(e.isHeader&&n.headerFontFamily&&o.add(n.headerFontFamily),!e.isHeader&&n.textFontFamily&&o.add(n.textFontFamily)))}return n.textFontFamily&&o.add(n.textFontFamily),e.filter(t=>o.has(t))}static _getEnabledStyleEntries(t){const e=[];return t.props&&t.props.adaptiveStyles&&(e.push(...t.props.adaptiveStyles.desktop||[]),e.push(...t.props.adaptiveStyles.mobile||[])),t.props&&t.props.styles&&e.push(...t.props.styles||[]),e.filter(t=>!(t.params&&!1===t.params.enabled))}async load(t,e){try{if(e=e||document,!(t||[]).length)return;const o=this._getDynamicUrlPart(t,e);if(!o||0===o.length)return;await this._addFontsLinkToDocument(o,e);const s=this._generateFontVariations(t),n=this._createFontTester(s,e);e.body.appendChild(n);const a=this._waitForAllFontsToLoad(s,e),r=new Promise((t,e)=>setTimeout(()=>e(new Error("Timeout")),2e3));await Promise.race([a,r]),n.remove()}catch(t){console.error("Error while loading fonts",t)}}cleanup(){this.loadedGoogleFonts.forEach(t=>t.remove()),this.loadedGoogleFonts=[]}_getDynamicUrlPart(t,e){let o=[];(e.fonts||[]).forEach(t=>o.push(`${t.family} ${t.weight} ${t.style}`));const s=new Set(o);return this._combineUrlDynamicPart(t,s)}_combineUrlDynamicPart(t,e){const o=[];let s=this._makeFontOptions();return t.forEach(t=>{let n="",a="";s.forEach(o=>{if(!e.has(`${t} ${o} normal`)){const t=`1,${o}`;a+=a?`;${t}`:t}if(!e.has(`${t} ${o} italic`)){const t=`0,${o}`;n+=n?`;${t}`:t}}),(n||a)&&o.push({fontFamily:t,urlPart:`&family=${t.replace(/ /g,"+")}:ital,wght@${n};${a}`})}),o}_makeFontOptions(){const t=[];for(let e=0;e<9;e++){const o=100*(e+1);t.push(o)}return t}_addFontsLinkToDocument(t,e){if(!e.head)return Promise.resolve();const o=t.map(({fontFamily:t,urlPart:o})=>new Promise((s,n)=>{const a=document.createElement("link");a.rel="stylesheet",a.href="https://fonts.googleapis.com/css2?display=swap"+o,a.onload=()=>s({fontFamily:t,success:!0}),a.onerror=()=>{console.error(`Failed to load font: ${t}`),n(new Error(`Failed to load font: ${t}`))},e.head.appendChild(a),this.loadedGoogleFonts.push(a)}));return Promise.allSettled(o)}_generateFontVariations(t){const e=[100,200,300,400,500,600,700,800,900],o=["normal","italic"],s=[];return t.forEach(t=>{e.forEach(e=>{o.forEach(o=>{s.push({family:t,weight:e,style:o,descriptor:`${o} ${e} 1em "${t}"`})})})}),s}_createFontTester(t,e){const o=e.createElement("div");return o.setAttribute("aria-hidden","true"),o.style.cssText="\n position: fixed;\n top: -9999px;\n left: -9999px;\n width: 1px;\n height: 1px;\n overflow: hidden;\n visibility: hidden;\n pointer-events: none;\n user-select: none;\n z-index: -1;\n ",t.forEach(t=>{Object.entries(this.testStrings).forEach(([s,n])=>{const a=e.createElement("span");a.textContent=n,a.style.cssText=`\n font-family: "${t.family}", sans-serif;\n font-weight: ${t.weight};\n font-style: ${t.style};\n font-size: 20px;\n display: inline-block;\n white-space: nowrap;\n `,a.dataset.font=`${t.family}-${t.weight}-${t.style}`,a.dataset.subset=s,o.appendChild(a)})}),o}async _waitForAllFontsToLoad(t,e){const o=[];for(let e=0;e<t.length;e+=10)o.push(t.slice(e,e+10));for(const t of o)await this._waitForFontBatchLoad(t,e)}async _waitForFontBatchLoad(t,e){const o=t.map(async t=>{try{e.fonts.check(t.descriptor)||await e.fonts.load(t.descriptor)}catch(e){console.error(`Failed to load: ${t.descriptor}`,e)}});return Promise.all(o)}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@claspo/document-connector",
3
- "version": "16.0.5",
3
+ "version": "16.0.6",
4
4
  "scripts": {
5
5
  "test": "jest --no-cache --coverage",
6
6
  "build": "rm -rf out && tsc --project tsconfig.json && npm run minify",