@claspo/document-connector 16.7.1 → 16.7.3

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
- import BaseDocumentConnector from"./BaseDocumentConnector";import DocumentConnectorEventType from"./DocumentConnectorEventType";import{getZIndexByLayoutType}from"./layouts/getZindex";import LayoutFactory from"./layouts/LayoutFactory";import StaticEntryModule from"@claspo/renderer/StaticEntryModule";import WcResolver from"@claspo/renderer/wc-renderer/WcResolver";import{RenderMode}from"@claspo/common/RenderMode";import{WcRegister}from"@claspo/renderer/wc-renderer/WcRegister";import GoogleFontsLoader from"./layouts/GoogleFontsLoader";import ThemeCssVars from"@claspo/common/element/ThemeCssVars";const DIRTY_SUBMIT_STATE="dirty",SUBMITTING_SUBMIT_STATE="submitting";export default class StaticDocumentConnector extends BaseDocumentConnector{constructor(e){super(e),this.componentResolver=null,this.googleFontsLoader=null,this.closeSubmitPromise=null;const{documentModel:t,translations:o,trackingService:s,layoutType:n,bundledComponentClasses:r}=e;this.componentRegister=new WcRegister(r,e.productKeyPrefix),this.componentResolver=new WcResolver(RenderMode.STATIC,this.componentRegister,e.productKeyPrefix);const i=new StaticEntryModule(t,this.componentResolver,this.componentRegister,e,o,s,n);this.setEntryModule(i)}emit(e,t,o=null){e!==DocumentConnectorEventType.CLOSE_WIDGET?super.emit(e,t,o):this.handleCloseWidget(t)}async connect(e){this.googleFontsLoader=new GoogleFontsLoader;const t=e.getRootNode();if(!this.params.disableGoogleFonts){const e=GoogleFontsLoader.getUsedGoogleFonts(this.params.documentModel);await this.googleFontsLoader.load(e,t)}return this._initializeRenderer(e,t)}disconnect(){var e;super.disconnect(),this.controllers[0]&&this.controllers[0].disconnect(()=>{this.entryModule&&this.entryModule.viewRouter&&this.entryModule.compositionRoot&&this.entryModule.destroy()}),this.controllers=[],null===(e=this.googleFontsLoader)||void 0===e||e.cleanup()}destroy(){this.disconnect()}on(e,t){return super.on(e,t)}async _initializeRenderer(e,t){var o,s,n,r;await this.componentResolver.loadComponentsIfNecessary(this.params.documentModel,this.params.staticResourcesUrl);const i=this.entryModule.init(t);i.style.display="flex";const{layoutFactoryClass:l=LayoutFactory,layoutType:a,documentModel:c}=this.params,d=Object.assign(Object.assign({},this.params),{zIndex:null!==(s=null===(o=null==c?void 0:c.shared)||void 0===o?void 0:o.zIndex)&&void 0!==s?s:getZIndexByLayoutType(a)}),m=l.create(Object.assign(Object.assign({},d),{layoutType:this.params.layoutType,widgetType:this.params.widgetType,viewIndex:0,renderMode:this.entryModule.renderMode}),this,this.entryModule.viewRouter,this.entryModule.compositionRoot.services.documentModel,this.entryModule.compositionRoot.services.resizeListener);return this.controllers.push(m),super.addPropsRequestEventListener(i),this.params.formVariantId?i.classList.add("cl-widget-"+this.params.formVariantId):i.classList.add("cl-widget"),this.entryModule.listenAction("*",(e,t)=>{this.emit(t,e)}),m.connect(i,e,this.entryModule),(null===(r=null===(n=c.shared)||void 0===n?void 0:n.theme)||void 0===r?void 0:r.schema)&&ThemeCssVars.override(c.shared.theme.schema,m.getConnectedElement()),m.getConnectedElement()}handleCloseWidget(e){this.trySubmitOnClose();try{super.emit(DocumentConnectorEventType.CLOSE_WIDGET,e)}finally{"function"==typeof e&&e()}}trySubmitOnClose(){const e=this.params.submitOnClose||{enabled:!1,skipValidation:!0},t=this.getSubmitState();e.enabled&&!this.closeSubmitPromise&&"submitting"!==t&&"dirty"===t&&this.hasDirtyFormData()&&(this.closeSubmitPromise=Promise.resolve(this.submitOnClose()).catch(e=>(console.error(e),!1)).finally(()=>{this.closeSubmitPromise=null}))}}
1
+ import BaseDocumentConnector from"./BaseDocumentConnector";import DocumentConnectorEventType from"./DocumentConnectorEventType";import{getZIndexByLayoutType}from"./layouts/getZindex";import LayoutFactory from"./layouts/LayoutFactory";import StaticEntryModule from"@claspo/renderer/StaticEntryModule";import WcResolver from"@claspo/renderer/wc-renderer/WcResolver";import{RenderMode}from"@claspo/common/RenderMode";import{WcRegister}from"@claspo/renderer/wc-renderer/WcRegister";import GoogleFontsLoader from"./layouts/GoogleFontsLoader";import ThemeCssVars from"@claspo/common/element/ThemeCssVars";const DIRTY_SUBMIT_STATE="dirty",SUBMITTING_SUBMIT_STATE="submitting";export default class StaticDocumentConnector extends BaseDocumentConnector{constructor(e){super(e),this.componentResolver=null,this.googleFontsLoader=null,this.closeSubmitPromise=null;const{documentModel:t,translations:o,trackingService:s,layoutType:n,bundledComponentClasses:r}=e;this.componentRegister=new WcRegister(r,e.productKeyPrefix),this.componentResolver=new WcResolver(RenderMode.STATIC,this.componentRegister,e.productKeyPrefix);const i=new StaticEntryModule(t,this.componentResolver,this.componentRegister,e,o,s,n);this.setEntryModule(i)}emit(e,t,o=null){e!==DocumentConnectorEventType.CLOSE_WIDGET?super.emit(e,t,o):this.handleCloseWidget(t)}async connect(e){this.googleFontsLoader=new GoogleFontsLoader({widgetId:this.params.widgetId,siteId:this.params.siteId});const t=e.getRootNode();if(!this.params.disableGoogleFonts){const e=GoogleFontsLoader.getUsedGoogleFonts(this.params.documentModel);await this.googleFontsLoader.load(e,t)}return this._initializeRenderer(e,t)}disconnect(){var e;super.disconnect(),this.controllers[0]&&this.controllers[0].disconnect(()=>{this.entryModule&&this.entryModule.viewRouter&&this.entryModule.compositionRoot&&this.entryModule.destroy()}),this.controllers=[],null===(e=this.googleFontsLoader)||void 0===e||e.cleanup()}destroy(){this.disconnect()}on(e,t){return super.on(e,t)}async _initializeRenderer(e,t){var o,s,n,r;await this.componentResolver.loadComponentsIfNecessary(this.params.documentModel,this.params.staticResourcesUrl);const i=this.entryModule.init(t);i.style.display="flex";const{layoutFactoryClass:l=LayoutFactory,layoutType:a,documentModel:d}=this.params,c=Object.assign(Object.assign({},this.params),{zIndex:null!==(s=null===(o=null==d?void 0:d.shared)||void 0===o?void 0:o.zIndex)&&void 0!==s?s:getZIndexByLayoutType(a)}),m=l.create(Object.assign(Object.assign({},c),{layoutType:this.params.layoutType,widgetType:this.params.widgetType,viewIndex:0,renderMode:this.entryModule.renderMode}),this,this.entryModule.viewRouter,this.entryModule.compositionRoot.services.documentModel,this.entryModule.compositionRoot.services.resizeListener);return this.controllers.push(m),super.addPropsRequestEventListener(i),this.params.formVariantId?i.classList.add("cl-widget-"+this.params.formVariantId):i.classList.add("cl-widget"),this.entryModule.listenAction("*",(e,t)=>{this.emit(t,e)}),m.connect(i,e,this.entryModule),(null===(r=null===(n=d.shared)||void 0===n?void 0:n.theme)||void 0===r?void 0:r.schema)&&ThemeCssVars.override(d.shared.theme.schema,m.getConnectedElement()),m.getConnectedElement()}handleCloseWidget(e){this.trySubmitOnClose();try{super.emit(DocumentConnectorEventType.CLOSE_WIDGET,e)}finally{"function"==typeof e&&e()}}trySubmitOnClose(){const e=this.params.submitOnClose||{enabled:!1,skipValidation:!0},t=this.getSubmitState();e.enabled&&!this.closeSubmitPromise&&"submitting"!==t&&"dirty"===t&&this.hasDirtyFormData()&&(this.closeSubmitPromise=Promise.resolve(this.submitOnClose()).catch(e=>(console.error(e),!1)).finally(()=>{this.closeSubmitPromise=null}))}}
@@ -4,10 +4,13 @@ export default class GoogleFontsLoader {
4
4
  static _normalizeFontWeight(fontWeight: any): number;
5
5
  static _normalizeFontFamilyName(fontFamily: any): string;
6
6
  loadedGoogleFonts: any[];
7
+ widgetId?: number;
8
+ siteId?: string;
7
9
  testStrings: {
8
10
  latin: string;
9
11
  cyrillic: string;
10
12
  };
13
+ constructor(options: any);
11
14
  load(fonts: any, htmlDocumentObject: any): Promise<void>;
12
15
  cleanup(): void;
13
16
  _combineUrlDynamicPart(fonts: any): any[];
@@ -30,6 +33,11 @@ export default class GoogleFontsLoader {
30
33
  _getFontFaceDistance(fontFace: any, weight: any): number;
31
34
  _resolveWeightForFontFace(fontFace: any, weight: any): any;
32
35
  _getFontFaceCss(variations: any): string;
36
+ _dedupeFontVariations(variations: any): any[];
37
+ _getVariationResourceKey(variation: any): any;
38
+ _getFontFaceMergeKey(fontFace: any): string;
39
+ _removeFontFaceWeight(css: any): string;
40
+ _replaceFontFaceWeight(css: any, minWeight: any, maxWeight: any): string;
33
41
  _generateFontVariations(fonts: any): any[];
34
42
  _getFontFamily(font: any): any;
35
43
  _getRequestedWeights(font: any): number[];
@@ -37,4 +45,5 @@ export default class GoogleFontsLoader {
37
45
  _createFontTester(variations: any, htmlDocumentObject: any): any;
38
46
  _waitForAllFontsToLoad(variations: any, htmlDocumentObject: any): Promise<void>;
39
47
  _waitForFontBatchLoad(batch: any, htmlDocumentObject: any): Promise<any[]>;
48
+ _logError(message: any): void;
40
49
  }
@@ -1 +1 @@
1
- import DocumentUtils from"@claspo/common/document/DocumentUtils";import ErrorLogger from"@claspo/renderer/error/ErrorLogger";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 Map,n=new Set,s=(t,e=400)=>{t&&(o.has(t)||o.set(t,new Set),o.get(t).add(GoogleFontsLoader._normalizeFontWeight(e)))};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-")&&n.add(e);for(const t of e)t.styleAttributes&&t.styleAttributes.fontFamily&&s(t.styleAttributes.fontFamily,t.styleAttributes.fontWeight)});const r=t.shared;for(const t of n){const e=r.textClasses&&r.textClasses[t];e&&(e.styleAttributes&&e.styleAttributes.fontFamily?s(e.styleAttributes.fontFamily,e.styleAttributes.fontWeight):(e.isHeader&&r.headerFontFamily&&s(r.headerFontFamily,e.styleAttributes&&e.styleAttributes.fontWeight),!e.isHeader&&r.textFontFamily&&s(r.textFontFamily,e.styleAttributes&&e.styleAttributes.fontWeight)))}return r.textFontFamily&&s(r.textFontFamily),e.filter(t=>o.has(t)).map(t=>({fontFamily:t,weights:Array.from(o.get(t)).sort((t,e)=>t-e)}))}static _getEnabledStyleEntries(t){const e=[];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||[]);const o=new Set(e.filter(t=>t.params&&!1===t.params.enabled).map(t=>t.element));return e.filter(t=>!o.has(t.element))}static _normalizeFontWeight(t){if("normal"===t)return 400;if("bold"===t)return 700;const e=parseInt(t,10);return Number.isFinite(e)&&e>0?e:400}static _normalizeFontFamilyName(t){return`${t||""}`.trim().replace(/\s+/g," ").toLowerCase()}async load(t,e){try{if(e=e||document,!(t||[]).length)return;const o=await this._loadResolvedFontVariations(t,e);if(!o.length)return;const n=this._createFontTester(o,e);e.body.appendChild(n);const s=this._waitForAllFontsToLoad(o,e),r=new Promise((t,e)=>setTimeout(()=>e(new Error("Timeout")),2e3));await Promise.race([s,r]),n.remove()}catch(t){ErrorLogger.log(`Error while loading fonts ${t}`)}}cleanup(){this.loadedGoogleFonts.forEach(t=>t.remove()),this.loadedGoogleFonts=[]}_combineUrlDynamicPart(t){const e=[],o=this._makeFontOptions(),n=[{name:"normal",value:0},{name:"italic",value:1}];return t.forEach(t=>{const s=this._getFontFamily(t),r=this._getRequestedWeights(t);if(!s||!r.length)return;const a=[];n.forEach(t=>{o.forEach(e=>{a.push(`${t.value},${e}`)})});const i=a.join(";");e.push({fontFamily:s,weights:r,urlPart:`&family=${this._encodeFontFamily(s)}:ital,wght@${i}`})}),e}_makeFontOptions(){const t=[];for(let e=0;e<9;e++){const o=100*(e+1);t.push(o)}return t}async _loadResolvedFontVariations(t,e){const o=this._combineUrlDynamicPart(t);if(!o||0===o.length)return[];try{const n=await this._fetchFontsMetadata(o),s=this._parseFontFaces(n);if(!s.length)throw new Error("Google Fonts metadata did not contain font-face rules");const r=this._resolveFontVariations(t,s);this._warnAboutUnresolvedReturnedFontFamilies(t,s,r),this._warnAboutItalicOnlyReturnedFontFamilies(t,s);const a=this._getFontFaceCss(r);if(!a)throw new Error("Google Fonts metadata did not contain matching font faces");return this._addFontsStyleToDocument(a,e),r}catch(n){return ErrorLogger.log(`Failed to load optimized Google Fonts CSS. Falling back to stylesheet link. ${n}`),await this._addFontsLinkToDocument(o,e),this._generateFontVariations(t)}}async _fetchFontsMetadata(t){const e=await fetch(this._makeGoogleFontsUrl(t));if(!e.ok)throw new Error(`Google Fonts metadata request failed with status ${e.status}`);return e.text()}_makeGoogleFontsUrl(t){return"https://fonts.googleapis.com/css2?display=swap"+t.map(({urlPart:t})=>t).join("")}_addFontsLinkToDocument(t,e){if(!e.head)return Promise.resolve();const o=[{fontFamily:t.map(({fontFamily:t})=>t).join(", "),urlPart:t.map(({urlPart:t})=>t).join("")}].map(({fontFamily:t,urlPart:o})=>new Promise((n,s)=>{const r=e.createElement("link");r.rel="stylesheet",r.href="https://fonts.googleapis.com/css2?display=swap"+o,r.onload=()=>n({fontFamily:t,success:!0}),r.onerror=()=>{ErrorLogger.log(`Failed to load font: ${t}`),s(new Error(`Failed to load font: ${t}`))},e.head.appendChild(r),this.loadedGoogleFonts.push(r)}));return Promise.allSettled(o)}_addFontsStyleToDocument(t,e){if(!e.head)return;const o=e.createElement("style");o.setAttribute("data-cl-google-fonts","true"),o.textContent=t,e.head.appendChild(o),this.loadedGoogleFonts.push(o)}_parseFontFaces(t){const e=[],o=/@font-face\s*{[^}]*}/gi;let n;for(;null!==(n=o.exec(t||""));){const t=n[0],o=this._getFontFaceProperty(t,"font-family"),s=this._getFontFaceProperty(t,"font-style")||"normal",r=this._parseFontFaceWeight(this._getFontFaceProperty(t,"font-weight"));o&&r?e.push({family:o,style:s,minWeight:r.minWeight,maxWeight:r.maxWeight,css:t}):ErrorLogger.log("Failed to parse font-face")}return e}_getFontFaceProperty(t,e){const o=new RegExp(`${e}\\s*:\\s*([^;]+);`,"i"),n=t.match(o);return n?n[1].trim().replace(/^['"]|['"]$/g,""):""}_parseFontFaceWeight(t){const e=`${t||""}`.match(/\d+(\.\d+)?/g);if(!e||!e.length)return null;const o=Number(e[0]),n=Number(e[e.length-1]);return{minWeight:Math.min(o,n),maxWeight:Math.max(o,n)}}_resolveFontVariations(t,e){const o=["normal","italic"],n=[],s=new Set;return t.forEach(t=>{const r=this._getFontFamily(t);this._getRequestedWeights(t).forEach(t=>{o.forEach(o=>{const a=this._findNearestFontFaces(e,r,o,t);if(!a.length)return;const i=this._resolveWeightForFontFace(a[0],t),l=`${r}|${o}|${i}`;s.has(l)||(s.add(l),n.push({family:r,weight:i,style:o,descriptor:`${o} ${i} 1em "${r}"`,cssBlocks:a.map(t=>t.css)}))})})}),n}_findNearestFontFaces(t,e,o,n){const s=GoogleFontsLoader._normalizeFontFamilyName(e),r=t.filter(t=>GoogleFontsLoader._normalizeFontFamilyName(t.family)===s&&t.style===o);if(!r.length)return[];const a=r.reduce((t,e)=>{if(!t)return e;const o=this._getFontFaceDistance(e,n),s=this._getFontFaceDistance(t,n);if(o<s)return e;if(o>s)return t;const r=this._resolveWeightForFontFace(e,n),a=this._resolveWeightForFontFace(t,n);return n<=500?r<a?e:t:r>a?e:t},null),i=this._resolveWeightForFontFace(a,n);return r.filter(t=>this._resolveWeightForFontFace(t,n)===i)}_warnAboutUnresolvedReturnedFontFamilies(t,e,o){const n=new Set(e.map(t=>GoogleFontsLoader._normalizeFontFamilyName(t.family))),s=new Set(o.map(t=>GoogleFontsLoader._normalizeFontFamilyName(t.family))),r=[];t.forEach(t=>{const e=this._getFontFamily(t),o=GoogleFontsLoader._normalizeFontFamilyName(e);o&&n.has(o)&&!s.has(o)&&r.push(e)}),r.length&&ErrorLogger.log(`Google Fonts metadata returned font faces that were not resolved: ${r.join(", ")}`)}_warnAboutItalicOnlyReturnedFontFamilies(t,e){const o=[];t.forEach(t=>{const n=this._getFontFamily(t),s=GoogleFontsLoader._normalizeFontFamilyName(n);if(!s)return;const r=e.filter(t=>GoogleFontsLoader._normalizeFontFamilyName(t.family)===s);if(!r.length)return;const a=r.some(t=>"normal"===t.style),i=r.some(t=>"italic"===t.style);!a&&i&&o.push(n)}),o.length&&ErrorLogger.log(`Google Fonts metadata returned only italic font faces for: ${o.join(", ")}. Normal text may fall back to a different font.`)}_getFontFaceDistance(t,e){return e>=t.minWeight&&e<=t.maxWeight?0:Math.min(Math.abs(e-t.minWeight),Math.abs(e-t.maxWeight))}_resolveWeightForFontFace(t,e){return e<t.minWeight?t.minWeight:e>t.maxWeight?t.maxWeight:e}_getFontFaceCss(t){const e=[],o=new Set;return t.forEach(t=>{(t.cssBlocks||[]).forEach(t=>{t&&!o.has(t)&&(o.add(t),e.push(t))})}),e.join("\n")}_generateFontVariations(t){const e=[];return t.forEach(t=>{const o=this._getFontFamily(t);this._getRequestedWeights(t).forEach(t=>{["normal","italic"].forEach(n=>{e.push({family:o,weight:t,style:n,descriptor:`${n} ${t} 1em "${o}"`})})})}),e}_getFontFamily(t){return"string"==typeof t?t:t&&t.fontFamily}_getRequestedWeights(t){if("string"==typeof t)return[400];const e=t&&t.weights;if(!e||!e.length)return[400];const o=e.map(t=>GoogleFontsLoader._normalizeFontWeight(t));return Array.from(new Set(o)).sort((t,e)=>t-e)}_encodeFontFamily(t){return encodeURIComponent(t).replace(/%20/g,"+")}_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(([n,s])=>{const r=e.createElement("span");r.textContent=s,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=n,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){ErrorLogger.log(`Failed to load: ${t.descriptor} ${e}`)}});return Promise.all(o)}}
1
+ import DocumentUtils from"@claspo/common/document/DocumentUtils";import ErrorLogger from"@claspo/renderer/error/ErrorLogger";export default class GoogleFontsLoader{static getUsedGoogleFonts(t){const e=t.shared.googleFonts;if(!e||0===e.length)return[];const o=new Map,n=new Set,s=(t,e=400)=>{t&&(o.has(t)||o.set(t,new Set),o.get(t).add(GoogleFontsLoader._normalizeFontWeight(e)))};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-")&&n.add(e);for(const t of e)t.styleAttributes&&t.styleAttributes.fontFamily&&s(t.styleAttributes.fontFamily,t.styleAttributes.fontWeight)});const a=t.shared;for(const t of n){const e=a.textClasses&&a.textClasses[t];e&&(e.styleAttributes&&e.styleAttributes.fontFamily?s(e.styleAttributes.fontFamily,e.styleAttributes.fontWeight):(e.isHeader&&a.headerFontFamily&&s(a.headerFontFamily,e.styleAttributes&&e.styleAttributes.fontWeight),!e.isHeader&&a.textFontFamily&&s(a.textFontFamily,e.styleAttributes&&e.styleAttributes.fontWeight)))}return a.textFontFamily&&s(a.textFontFamily),e.filter(t=>o.has(t)).map(t=>({fontFamily:t,weights:Array.from(o.get(t)).sort((t,e)=>t-e)}))}static _getEnabledStyleEntries(t){const e=[];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||[]);const o=new Set(e.filter(t=>t.params&&!1===t.params.enabled).map(t=>t.element));return e.filter(t=>!o.has(t.element))}static _normalizeFontWeight(t){if("normal"===t)return 400;if("bold"===t)return 700;const e=parseInt(t,10);return Number.isFinite(e)&&e>0?e:400}static _normalizeFontFamilyName(t){return`${t||""}`.trim().replace(/\s+/g," ").toLowerCase()}constructor(t){this.loadedGoogleFonts=[],this.testStrings={latin:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",cyrillic:"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"},t=t||{},this.widgetId=t.widgetId,this.siteId=t.siteId}async load(t,e){try{if(e=e||document,!(t||[]).length)return;const o=await this._loadResolvedFontVariations(t,e);if(!o.length)return;const n=this._createFontTester(o,e);e.body.appendChild(n);const s=this._waitForAllFontsToLoad(o,e),a=new Promise((t,e)=>setTimeout(()=>e(new Error("Timeout")),2e3));await Promise.race([s,a]),n.remove()}catch(t){this._logError(`Error while loading fonts ${t}`)}}cleanup(){this.loadedGoogleFonts.forEach(t=>t.remove()),this.loadedGoogleFonts=[]}_combineUrlDynamicPart(t){const e=[],o=this._makeFontOptions(),n=[{name:"normal",value:0},{name:"italic",value:1}];return t.forEach(t=>{const s=this._getFontFamily(t),a=this._getRequestedWeights(t);if(!s||!a.length)return;const r=[];n.forEach(t=>{o.forEach(e=>{r.push(`${t.value},${e}`)})});const i=r.join(";");e.push({fontFamily:s,weights:a,urlPart:`&family=${this._encodeFontFamily(s)}:ital,wght@${i}`})}),e}_makeFontOptions(){const t=[];for(let e=0;e<9;e++){const o=100*(e+1);t.push(o)}return t}async _loadResolvedFontVariations(t,e){const o=this._combineUrlDynamicPart(t);if(!o||0===o.length)return[];try{const n=await this._fetchFontsMetadata(o),s=this._parseFontFaces(n);if(!s.length)throw new Error("Google Fonts metadata did not contain font-face rules");const a=this._resolveFontVariations(t,s);this._warnAboutUnresolvedReturnedFontFamilies(t,s,a),this._warnAboutItalicOnlyReturnedFontFamilies(t,s);const r=this._getFontFaceCss(a);if(!r)throw new Error("Google Fonts metadata did not contain matching font faces");return this._addFontsStyleToDocument(r,e),this._dedupeFontVariations(a)}catch(n){return this._logError(`Failed to load optimized Google Fonts CSS. Falling back to stylesheet link. ${n}`),await this._addFontsLinkToDocument(o,e),this._generateFontVariations(t)}}async _fetchFontsMetadata(t){const e=this._makeGoogleFontsUrl(t),o=await fetch(e);if(!o.ok)throw new Error(`Google Fonts metadata request failed, url: ${e}, status: ${o.status}`);return o.text()}_makeGoogleFontsUrl(t){return"https://fonts.googleapis.com/css2?display=swap"+t.map(({urlPart:t})=>t).join("")}_addFontsLinkToDocument(t,e){if(!e.head)return Promise.resolve();const o=[{fontFamily:t.map(({fontFamily:t})=>t).join(", "),urlPart:t.map(({urlPart:t})=>t).join("")}].map(({fontFamily:t,urlPart:o})=>new Promise((n,s)=>{const a=e.createElement("link");a.rel="stylesheet",a.href="https://fonts.googleapis.com/css2?display=swap"+o,a.onload=()=>n({fontFamily:t,success:!0}),a.onerror=()=>{this._logError(`Failed to load font: ${t}`),s(new Error(`Failed to load font: ${t}`))},e.head.appendChild(a),this.loadedGoogleFonts.push(a)}));return Promise.allSettled(o)}_addFontsStyleToDocument(t,e){if(!e.head)return;const o=e.createElement("style");o.setAttribute("data-cl-google-fonts","true"),o.textContent=t,e.head.appendChild(o),this.loadedGoogleFonts.push(o)}_parseFontFaces(t){const e=[],o=/@font-face\s*{[^}]*}/gi;let n;for(;null!==(n=o.exec(t||""));){const t=n[0],o=this._getFontFaceProperty(t,"font-family"),s=this._getFontFaceProperty(t,"font-style")||"normal",a=this._parseFontFaceWeight(this._getFontFaceProperty(t,"font-weight"));o&&a?e.push({family:o,style:s,minWeight:a.minWeight,maxWeight:a.maxWeight,src:this._getFontFaceProperty(t,"src"),unicodeRange:this._getFontFaceProperty(t,"unicode-range"),css:t}):this._logError("Failed to parse font-face")}return e}_getFontFaceProperty(t,e){const o=new RegExp(`${e}\\s*:\\s*([^;]+);`,"i"),n=t.match(o);return n?n[1].trim().replace(/^['"]|['"]$/g,""):""}_parseFontFaceWeight(t){const e=`${t||""}`.match(/\d+(\.\d+)?/g);if(!e||!e.length)return null;const o=Number(e[0]),n=Number(e[e.length-1]);return{minWeight:Math.min(o,n),maxWeight:Math.max(o,n)}}_resolveFontVariations(t,e){const o=["normal","italic"],n=[],s=new Set;return t.forEach(t=>{const a=this._getFontFamily(t);this._getRequestedWeights(t).forEach(t=>{o.forEach(o=>{const r=this._findNearestFontFaces(e,a,o,t);if(!r.length)return;const i=this._resolveWeightForFontFace(r[0],t),l=`${a}|${o}|${i}`;s.has(l)||(s.add(l),n.push({family:a,weight:i,style:o,descriptor:`${o} ${i} 1em "${a}"`,cssBlocks:r.map(t=>t.css),fontFaces:r}))})})}),n}_findNearestFontFaces(t,e,o,n){const s=GoogleFontsLoader._normalizeFontFamilyName(e),a=t.filter(t=>GoogleFontsLoader._normalizeFontFamilyName(t.family)===s&&t.style===o);if(!a.length)return[];const r=a.reduce((t,e)=>{if(!t)return e;const o=this._getFontFaceDistance(e,n),s=this._getFontFaceDistance(t,n);if(o<s)return e;if(o>s)return t;const a=this._resolveWeightForFontFace(e,n),r=this._resolveWeightForFontFace(t,n);return n<=500?a<r?e:t:a>r?e:t},null),i=this._resolveWeightForFontFace(r,n);return a.filter(t=>this._resolveWeightForFontFace(t,n)===i)}_warnAboutUnresolvedReturnedFontFamilies(t,e,o){const n=new Set(e.map(t=>GoogleFontsLoader._normalizeFontFamilyName(t.family))),s=new Set(o.map(t=>GoogleFontsLoader._normalizeFontFamilyName(t.family))),a=[];t.forEach(t=>{const e=this._getFontFamily(t),o=GoogleFontsLoader._normalizeFontFamilyName(e);o&&n.has(o)&&!s.has(o)&&a.push(e)}),a.length&&this._logError(`Google Fonts metadata returned font faces that were not resolved: ${a.join(", ")}`)}_warnAboutItalicOnlyReturnedFontFamilies(t,e){const o=[];t.forEach(t=>{const n=this._getFontFamily(t),s=GoogleFontsLoader._normalizeFontFamilyName(n);if(!s)return;const a=e.filter(t=>GoogleFontsLoader._normalizeFontFamilyName(t.family)===s);if(!a.length)return;const r=a.some(t=>"normal"===t.style),i=a.some(t=>"italic"===t.style);!r&&i&&o.push(n)}),o.length&&this._logError(`Google Fonts metadata returned only italic font faces for: ${o.join(", ")}. Normal text may fall back to a different font.`)}_getFontFaceDistance(t,e){return e>=t.minWeight&&e<=t.maxWeight?0:Math.min(Math.abs(e-t.minWeight),Math.abs(e-t.maxWeight))}_resolveWeightForFontFace(t,e){return e<t.minWeight?t.minWeight:e>t.maxWeight?t.maxWeight:e}_getFontFaceCss(t){const e=[],o=new Map,n=new Set;return t.forEach(t=>{t.fontFaces&&t.fontFaces.length?t.fontFaces.forEach(t=>{const e=this._getFontFaceMergeKey(t),n=o.get(e);if(n)return n.minWeight=Math.min(n.minWeight,t.minWeight),void(n.maxWeight=Math.max(n.maxWeight,t.maxWeight));o.set(e,{fontFace:t,minWeight:t.minWeight,maxWeight:t.maxWeight})}):(t.cssBlocks||[]).forEach(t=>{t&&!n.has(t)&&(n.add(t),e.push(t))})}),o.forEach(({fontFace:t,minWeight:o,maxWeight:s})=>{const a=this._replaceFontFaceWeight(t.css,o,s);a&&!n.has(a)&&(n.add(a),e.push(a))}),e.join("\n")}_dedupeFontVariations(t){const e=new Set,o=[];return t.forEach(t=>{const n=this._getVariationResourceKey(t);e.has(n)||(e.add(n),o.push(t))}),o}_getVariationResourceKey(t){if(!t.fontFaces||!t.fontFaces.length)return t.descriptor;const e=Array.from(new Set(t.fontFaces.map(t=>this._getFontFaceMergeKey(t)))).sort();return[GoogleFontsLoader._normalizeFontFamilyName(t.family),t.style,e.join("|")].join("|")}_getFontFaceMergeKey(t){return[GoogleFontsLoader._normalizeFontFamilyName(t.family),t.style,t.src,t.unicodeRange,this._removeFontFaceWeight(t.css)].join("|")}_removeFontFaceWeight(t){return`${t||""}`.replace(/\s*font-weight\s*:\s*[^;]+;/i,"")}_replaceFontFaceWeight(t,e,o){const n=e===o?`${e}`:`${e} ${o}`;return`${t||""}`.replace(/font-weight\s*:\s*[^;]+;/i,`font-weight: ${n};`)}_generateFontVariations(t){const e=[];return t.forEach(t=>{const o=this._getFontFamily(t);this._getRequestedWeights(t).forEach(t=>{["normal","italic"].forEach(n=>{e.push({family:o,weight:t,style:n,descriptor:`${n} ${t} 1em "${o}"`})})})}),e}_getFontFamily(t){return"string"==typeof t?t:t&&t.fontFamily}_getRequestedWeights(t){if("string"==typeof t)return[400];const e=t&&t.weights;if(!e||!e.length)return[400];const o=e.map(t=>GoogleFontsLoader._normalizeFontWeight(t));return Array.from(new Set(o)).sort((t,e)=>t-e)}_encodeFontFamily(t){return encodeURIComponent(t).replace(/%20/g,"+")}_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(([n,s])=>{const a=e.createElement("span");a.textContent=s,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=n,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){this._logError(`Failed to load: ${t.descriptor} ${e}`)}});return Promise.all(o)}_logError(t){const e=[];void 0!==this.widgetId&&null!==this.widgetId&&e.push(`widgetId: ${this.widgetId}`),this.siteId&&e.push(`siteId: ${this.siteId}`),ErrorLogger.log(e.length?`${t} (${e.join(", ")})`:t)}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@claspo/document-connector",
3
- "version": "16.7.1",
3
+ "version": "16.7.3",
4
4
  "scripts": {
5
5
  "test": "jest --no-cache --coverage",
6
6
  "build": "rm -rf out && tsc --project tsconfig.json && npm run minify",