@claspo/document-connector 16.7.19-atf1 → 16.7.19-scf2

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.
@@ -3,12 +3,14 @@ import { WidgetInitConfigI } from "@claspo/common/WidgetInitConfig.interface";
3
3
  import WcResolver from '@claspo/renderer/wc-renderer/WcResolver';
4
4
  import { WcRegister } from '@claspo/renderer/wc-renderer/WcRegister';
5
5
  import GoogleFontsLoader from './layouts/GoogleFontsLoader';
6
+ import CustomFontsLoader from './layouts/CustomFontsLoader';
6
7
  import { DocumentConnectorEventName } from './DocumentConnectorEvents.interface';
7
8
  export default class StaticDocumentConnector extends BaseDocumentConnector {
8
9
  componentRegister: WcRegister;
9
10
  componentResolver: WcResolver;
10
11
  googleFontsLoader: GoogleFontsLoader | null;
11
12
  closeSubmitPromise: Promise<boolean> | null;
13
+ customFontsLoader: CustomFontsLoader | null;
12
14
  constructor(params: WidgetInitConfigI);
13
15
  emit(eventName: string, value: any, params?: any): void;
14
16
  connect(containerElement: HTMLElement): Promise<any>;
@@ -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({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}))}}
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 CustomFontsLoader from"./layouts/CustomFontsLoader";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,this.customFontsLoader=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}),this.customFontsLoader=new CustomFontsLoader;const t=e.getRootNode(),o=CustomFontsLoader.getUsedCustomFonts(this.params.documentModel),s=[this.customFontsLoader.load(o,t)];if(!this.params.disableGoogleFonts){const e=GoogleFontsLoader.getUsedGoogleFonts(this.params.documentModel);s.push(this.googleFontsLoader.load(e,t))}return await Promise.all(s),this._initializeRenderer(e,t)}disconnect(){var e,t;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(),null===(t=this.customFontsLoader)||void 0===t||t.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:a=LayoutFactory,layoutType:l,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(l)}),m=a.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}))}}
@@ -0,0 +1,16 @@
1
+ export interface FontsLoaderOptionsI {
2
+ widgetId?: number;
3
+ siteId?: string;
4
+ }
5
+ export default class BaseFontsLoader {
6
+ widgetId?: number;
7
+ siteId?: string;
8
+ ownerId: string;
9
+ protected htmlDocumentObject?: Document;
10
+ protected loadedStylesheetElements: HTMLElement[];
11
+ constructor(options?: FontsLoaderOptionsI);
12
+ protected getOwnerIdPrefix(): string;
13
+ protected setHtmlDocumentObject(htmlDocumentObject?: Document): Document;
14
+ protected addStylesheetLinks(urls: string[], htmlDocumentObject: Document): Promise<void>;
15
+ protected cleanupStylesheetElements(): void;
16
+ }
@@ -0,0 +1 @@
1
+ let nextFontsLoaderOwnerId=1;export default class BaseFontsLoader{constructor(e){this.loadedStylesheetElements=[],e=e||{},this.widgetId=e.widgetId,this.siteId=e.siteId,this.ownerId=`${this.getOwnerIdPrefix()}-${nextFontsLoaderOwnerId++}`}getOwnerIdPrefix(){return"fonts-loader"}setHtmlDocumentObject(e){return this.htmlDocumentObject=e||document,this.htmlDocumentObject}async addStylesheetLinks(e,t){if(!t.head||!(e||[]).length)return;const s=new Set(Array.from(t.querySelectorAll('link[rel="stylesheet"]')).map(e=>e.href)),r=Array.from(new Set(e.filter(e=>e&&!s.has(e)))).map(e=>{const s=document.createElement("link");s.rel="stylesheet",s.href=e;const r=new Promise((t,r)=>{s.onload=()=>t({url:e,success:!0}),s.onerror=()=>r(new Error(`Failed to load font stylesheet: ${e}`))}),n=new Promise((t,s)=>setTimeout(()=>s(new Error(`Timeout while loading font stylesheet: ${e}`)),2e3));return t.head.appendChild(s),this.loadedStylesheetElements.push(s),Promise.race([r,n])});await Promise.allSettled(r)}cleanupStylesheetElements(){this.loadedStylesheetElements.forEach(e=>e.remove()),this.loadedStylesheetElements=[]}}
@@ -0,0 +1,9 @@
1
+ import { ClDocumentCustomFontResourceI } from '@claspo/common/document/Document.interface';
2
+ import BaseFontsLoader, { FontsLoaderOptionsI } from './BaseFontsLoader';
3
+ export default class CustomFontsLoader extends BaseFontsLoader {
4
+ static getUsedCustomFonts(documentModel: any): ClDocumentCustomFontResourceI[];
5
+ constructor(options?: FontsLoaderOptionsI);
6
+ protected getOwnerIdPrefix(): string;
7
+ load(customFonts: ClDocumentCustomFontResourceI[], htmlDocumentObject?: Document): Promise<void>;
8
+ cleanup(): void;
9
+ }
@@ -0,0 +1 @@
1
+ import BaseFontsLoader from"./BaseFontsLoader";import{getUsedFontFamilies}from"./UsedFontFamiliesResolver";import{extractFirstCssFontFamily,normalizeFontFamilyName}from"./google-fonts/GoogleFontsUtils";export default class CustomFontsLoader extends BaseFontsLoader{static getUsedCustomFonts(t){const e=t.shared.customFontResources||[];if(!e.length)return[];const o=getUsedFontFamilies(t),s=new Set(Array.from(o).map(t=>normalizeFontFamilyName(extractFirstCssFontFamily(t))));return e.filter(t=>{if(!(null==t?void 0:t.fontFamily)||!(null==t?void 0:t.url))return!1;const e=normalizeFontFamilyName(extractFirstCssFontFamily(t.fontFamily));return s.has(e)})}constructor(t){super(t)}getOwnerIdPrefix(){return"custom-fonts-loader"}async load(t,e){try{const o=this.setHtmlDocumentObject(e);if(!(t||[]).length)return;const s=Array.from(new Set(t.map(t=>t.url).filter(Boolean)));await this.addStylesheetLinks(s,o)}catch(t){console.error("Error while loading custom fonts",t)}}cleanup(){this.cleanupStylesheetElements(),this.htmlDocumentObject=void 0}}
@@ -1,11 +1,9 @@
1
1
  import { GoogleFontInput, GoogleFontsLoaderOptionsI, ResolvedGoogleFontRequestI } from './google-fonts/GoogleFonts.types';
2
- export default class GoogleFontsLoader {
2
+ import BaseFontsLoader from './BaseFontsLoader';
3
+ export default class GoogleFontsLoader extends BaseFontsLoader {
3
4
  static getUsedGoogleFonts(documentModel: any): ResolvedGoogleFontRequestI[];
4
- widgetId?: number;
5
- siteId?: string;
6
- ownerId: string;
7
- htmlDocumentObject?: Document;
8
5
  constructor(options?: GoogleFontsLoaderOptionsI);
6
+ protected getOwnerIdPrefix(): string;
9
7
  load(fonts: GoogleFontInput[], htmlDocumentObject?: Document): Promise<void>;
10
8
  cleanup(): void;
11
9
  }
@@ -1 +1 @@
1
- import GoogleFontsRegistry from"./google-fonts/GoogleFontsRegistry";import{getUsedGoogleFonts}from"./google-fonts/GoogleFontsUsageExtractor";let nextGoogleFontsLoaderOwnerId=1;export default class GoogleFontsLoader{static getUsedGoogleFonts(t){return getUsedGoogleFonts(t)}constructor(t){t=t||{},this.widgetId=t.widgetId,this.siteId=t.siteId,this.ownerId="google-fonts-loader-"+nextGoogleFontsLoaderOwnerId++}async load(t,o){this.htmlDocumentObject=o||document,await GoogleFontsRegistry.loadFonts({ownerId:this.ownerId,document:this.htmlDocumentObject,fonts:t,context:{widgetId:this.widgetId,siteId:this.siteId}})}cleanup(){GoogleFontsRegistry.releaseOwner({ownerId:this.ownerId,document:this.htmlDocumentObject}),this.htmlDocumentObject=void 0}}
1
+ import GoogleFontsRegistry from"./google-fonts/GoogleFontsRegistry";import{getUsedGoogleFonts}from"./google-fonts/GoogleFontsUsageExtractor";import BaseFontsLoader from"./BaseFontsLoader";export default class GoogleFontsLoader extends BaseFontsLoader{static getUsedGoogleFonts(t){return getUsedGoogleFonts(t)}constructor(t){super(t)}getOwnerIdPrefix(){return"google-fonts-loader"}async load(t,o){this.htmlDocumentObject=this.setHtmlDocumentObject(o),await GoogleFontsRegistry.loadFonts({ownerId:this.ownerId,document:this.htmlDocumentObject,fonts:t,context:{widgetId:this.widgetId,siteId:this.siteId}})}cleanup(){GoogleFontsRegistry.releaseOwner({ownerId:this.ownerId,document:this.htmlDocumentObject}),this.htmlDocumentObject=void 0}}
@@ -0,0 +1 @@
1
+ export declare function getUsedFontFamilies(documentModel: any): Set<string>;
@@ -0,0 +1 @@
1
+ import DocumentUtils from"@claspo/common/document/DocumentUtils";import{getEnabledStyleEntries,splitClassNames}from"./google-fonts/GoogleFontsUsageExtractor";export function getUsedFontFamilies(t){const e=new Set,s=new Set;for(const o of t.views||[])DocumentUtils.iterateDepthFirst(o,t=>{const o=getEnabledStyleEntries(t);for(const t of o){if(t.classes)for(const e of splitClassNames(t.classes))e&&e.startsWith("cl-")&&s.add(e);t.styleAttributes&&t.styleAttributes.fontFamily&&e.add(t.styleAttributes.fontFamily)}});const o=t.shared;for(const t of s){const s=o.textClasses&&o.textClasses[t];s&&(s.styleAttributes&&s.styleAttributes.fontFamily?e.add(s.styleAttributes.fontFamily):(s.isHeader&&o.headerFontFamily&&e.add(o.headerFontFamily),!s.isHeader&&o.textFontFamily&&e.add(o.textFontFamily)))}return o.textFontFamily&&e.add(o.textFontFamily),e}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@claspo/document-connector",
3
- "version": "16.7.19-atf1",
3
+ "version": "16.7.19-scf2",
4
4
  "scripts": {
5
5
  "test": "jest --no-cache --coverage",
6
6
  "build": "rm -rf out && tsc --project tsconfig.json && npm run minify",
@@ -14,8 +14,8 @@
14
14
  "node": ">=18.16.0"
15
15
  },
16
16
  "dependencies": {
17
- "@claspo/common": "7.3.0-atf1",
18
- "@claspo/renderer": "18.7.9-atf1"
17
+ "@claspo/common": "7.3.0-scf2",
18
+ "@claspo/renderer": "18.7.9-scf2"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@babel/core": "^7.28.5",