@webiny/website-builder-sdk 0.0.0-unstable.df7a8bb475 → 0.0.0-unstable.e1999482d0
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/BindingsApi.js +29 -36
- package/BindingsApi.js.map +1 -1
- package/BindingsProcessor.js +34 -43
- package/BindingsProcessor.js.map +1 -1
- package/BindingsProcessor.test.js +82 -78
- package/BindingsProcessor.test.js.map +1 -1
- package/BindingsResolver.d.ts +4 -1
- package/BindingsResolver.js +92 -122
- package/BindingsResolver.js.map +1 -1
- package/BindingsResolver.test.js +399 -363
- package/BindingsResolver.test.js.map +1 -1
- package/ComponentInputTraverser.js +29 -49
- package/ComponentInputTraverser.js.map +1 -1
- package/ComponentManifestToAstConverter.d.ts +33 -1
- package/ComponentManifestToAstConverter.js +164 -21
- package/ComponentManifestToAstConverter.js.map +1 -1
- package/ComponentRegistry.d.ts +2 -1
- package/ComponentRegistry.js +40 -22
- package/ComponentRegistry.js.map +1 -1
- package/ComponentResolver.d.ts +6 -3
- package/ComponentResolver.js +30 -29
- package/ComponentResolver.js.map +1 -1
- package/ConstraintEvaluator.d.ts +41 -0
- package/ConstraintEvaluator.js +304 -0
- package/ConstraintEvaluator.js.map +1 -0
- package/ConstraintEvaluator.test.d.ts +1 -0
- package/ConstraintEvaluator.test.js +1771 -0
- package/ConstraintEvaluator.test.js.map +1 -0
- package/ContentSdk.d.ts +80 -4
- package/ContentSdk.js +190 -89
- package/ContentSdk.js.map +1 -1
- package/DocumentStore.d.ts +5 -0
- package/DocumentStore.js +49 -59
- package/DocumentStore.js.map +1 -1
- package/DocumentStore.test.d.ts +1 -0
- package/DocumentStore.test.js +126 -0
- package/DocumentStore.test.js.map +1 -0
- package/DocumentStoreManager.d.ts +9 -2
- package/DocumentStoreManager.js +36 -16
- package/DocumentStoreManager.js.map +1 -1
- package/EditingSdk.d.ts +19 -3
- package/EditingSdk.js +112 -121
- package/EditingSdk.js.map +1 -1
- package/ElementBindings.d.ts +28 -0
- package/ElementBindings.js +88 -0
- package/ElementBindings.js.map +1 -0
- package/ElementBindings.test.d.ts +1 -0
- package/ElementBindings.test.js +127 -0
- package/ElementBindings.test.js.map +1 -0
- package/ElementFactory.d.ts +13 -4
- package/ElementFactory.js +170 -164
- package/ElementFactory.js.map +1 -1
- package/ElementFactory.test.d.ts +1 -0
- package/ElementFactory.test.js +511 -0
- package/ElementFactory.test.js.map +1 -0
- package/ElementSelection.d.ts +20 -0
- package/ElementSelection.js +32 -0
- package/ElementSelection.js.map +1 -0
- package/ElementSelection.test.d.ts +1 -0
- package/ElementSelection.test.js +73 -0
- package/ElementSelection.test.js.map +1 -0
- package/Environment.d.ts +7 -0
- package/Environment.js +19 -19
- package/Environment.js.map +1 -1
- package/Environment.test.d.ts +1 -0
- package/Environment.test.js +39 -0
- package/Environment.test.js.map +1 -0
- package/FunctionConverter.js +8 -7
- package/FunctionConverter.js.map +1 -1
- package/GlobalComponentLanguage.d.ts +31 -0
- package/GlobalComponentLanguage.js +37 -0
- package/GlobalComponentLanguage.js.map +1 -0
- package/HashObject.js +11 -12
- package/HashObject.js.map +1 -1
- package/HotkeyManager.js +40 -47
- package/HotkeyManager.js.map +1 -1
- package/IBindingsUpdater.js +0 -3
- package/IRedirects.js +0 -3
- package/InheritanceProcessor.js +99 -139
- package/InheritanceProcessor.js.map +1 -1
- package/InheritanceProcessor.test.js +184 -177
- package/InheritanceProcessor.test.js.map +1 -1
- package/InheritedValueResolver.d.ts +6 -0
- package/InheritedValueResolver.js +22 -20
- package/InheritedValueResolver.js.map +1 -1
- package/InputBindingsProcessor.js +193 -289
- package/InputBindingsProcessor.js.map +1 -1
- package/InputRenderer.test.d.ts +1 -0
- package/InputRenderer.test.js +186 -0
- package/InputRenderer.test.js.map +1 -0
- package/InputsBindingsProcessor.test.js +464 -314
- package/InputsBindingsProcessor.test.js.map +1 -1
- package/InputsUpdater.d.ts +1 -1
- package/InputsUpdater.js +24 -26
- package/InputsUpdater.js.map +1 -1
- package/LayoutContentArea.d.ts +68 -0
- package/LayoutContentArea.js +78 -0
- package/LayoutContentArea.js.map +1 -0
- package/LayoutContentArea.test.d.ts +1 -0
- package/LayoutContentArea.test.js +111 -0
- package/LayoutContentArea.test.js.map +1 -0
- package/LiveSdk.d.ts +6 -3
- package/LiveSdk.js +16 -13
- package/LiveSdk.js.map +1 -1
- package/Logger.js +9 -8
- package/Logger.js.map +1 -1
- package/MouseTracker.js +77 -83
- package/MouseTracker.js.map +1 -1
- package/NullSdk.d.ts +5 -3
- package/NullSdk.js +25 -14
- package/NullSdk.js.map +1 -1
- package/PreviewDocument.d.ts +3 -1
- package/PreviewDocument.js +30 -30
- package/PreviewDocument.js.map +1 -1
- package/PreviewSdk.d.ts +7 -3
- package/PreviewSdk.js +23 -17
- package/PreviewSdk.js.map +1 -1
- package/PreviewViewport.d.ts +5 -0
- package/PreviewViewport.js +60 -63
- package/PreviewViewport.js.map +1 -1
- package/PreviewViewport.test.d.ts +1 -0
- package/PreviewViewport.test.js +27 -0
- package/PreviewViewport.test.js.map +1 -0
- package/RadioOptionInputs.test.d.ts +1 -0
- package/RadioOptionInputs.test.js +521 -0
- package/RadioOptionInputs.test.js.map +1 -0
- package/ResizeObserver.js +24 -31
- package/ResizeObserver.js.map +1 -1
- package/StylesBindingsProcessor.js +41 -79
- package/StylesBindingsProcessor.js.map +1 -1
- package/StylesUpdater.d.ts +1 -1
- package/StylesUpdater.js +23 -25
- package/StylesUpdater.js.map +1 -1
- package/Theme.js +30 -25
- package/Theme.js.map +1 -1
- package/ViewportManager.js +89 -101
- package/ViewportManager.js.map +1 -1
- package/asset/deliveryUrl.d.ts +41 -0
- package/asset/deliveryUrl.js +34 -0
- package/asset/deliveryUrl.js.map +1 -0
- package/asset/deliveryUrl.test.d.ts +1 -0
- package/asset/deliveryUrl.test.js +127 -0
- package/asset/deliveryUrl.test.js.map +1 -0
- package/asset/geometry.d.ts +7 -0
- package/asset/geometry.js +10 -0
- package/asset/geometry.js.map +1 -0
- package/asset/imageSrcSet.d.ts +63 -0
- package/asset/imageSrcSet.js +98 -0
- package/asset/imageSrcSet.js.map +1 -0
- package/asset/imageSrcSet.test.d.ts +1 -0
- package/asset/imageSrcSet.test.js +103 -0
- package/asset/imageSrcSet.test.js.map +1 -0
- package/asset/index.d.ts +5 -0
- package/asset/index.js +5 -0
- package/asset/normalize.d.ts +24 -0
- package/asset/normalize.js +106 -0
- package/asset/normalize.js.map +1 -0
- package/asset/normalize.test.d.ts +1 -0
- package/asset/normalize.test.js +263 -0
- package/asset/normalize.test.js.map +1 -0
- package/asset/types.d.ts +2 -0
- package/asset/types.js +0 -0
- package/componentLoader/ComponentLoader.d.ts +16 -0
- package/componentLoader/ComponentLoader.js +0 -0
- package/componentLoader/RegistryOnlyComponentLoader.d.ts +9 -0
- package/componentLoader/RegistryOnlyComponentLoader.js +8 -0
- package/componentLoader/RegistryOnlyComponentLoader.js.map +1 -0
- package/componentLoader/createComponentLoader.d.ts +7 -0
- package/componentLoader/createComponentLoader.js +10 -0
- package/componentLoader/createComponentLoader.js.map +1 -0
- package/componentLoader/defaultComponentLoader.d.ts +7 -0
- package/componentLoader/defaultComponentLoader.js +8 -0
- package/componentLoader/defaultComponentLoader.js.map +1 -0
- package/componentLoader/index.d.ts +3 -0
- package/componentLoader/index.js +3 -0
- package/constants.d.ts +1 -0
- package/constants.js +7 -5
- package/constants.js.map +1 -1
- package/contentClientState.test.d.ts +1 -0
- package/contentClientState.test.js +404 -0
- package/contentClientState.test.js.map +1 -0
- package/contentEntry/ContentEntryBindingsResolver.d.ts +33 -0
- package/contentEntry/ContentEntryBindingsResolver.js +52 -0
- package/contentEntry/ContentEntryBindingsResolver.js.map +1 -0
- package/contentEntry/ContentEntryCache.d.ts +49 -0
- package/contentEntry/ContentEntryCache.js +51 -0
- package/contentEntry/ContentEntryCache.js.map +1 -0
- package/contentEntry/resolveContentEntry.d.ts +74 -0
- package/contentEntry/resolveContentEntry.js +53 -0
- package/contentEntry/resolveContentEntry.js.map +1 -0
- package/contentEntry/resolveContentEntry.test.d.ts +1 -0
- package/contentEntry/resolveContentEntry.test.js +213 -0
- package/contentEntry/resolveContentEntry.test.js.map +1 -0
- package/createComponent.d.ts +5 -0
- package/createComponent.js +32 -0
- package/createComponent.js.map +1 -0
- package/createContentClient.d.ts +17 -0
- package/createContentClient.js +16 -0
- package/createContentClient.js.map +1 -0
- package/createContentClient.test.d.ts +1 -0
- package/createContentClient.test.js +165 -0
- package/createContentClient.test.js.map +1 -0
- package/createElement.js +5 -6
- package/createElement.js.map +1 -1
- package/createGlobalElementDocument.d.ts +6 -0
- package/createGlobalElementDocument.js +23 -0
- package/createGlobalElementDocument.js.map +1 -0
- package/createInput.d.ts +10 -2
- package/createInput.js +92 -143
- package/createInput.js.map +1 -1
- package/createTheme.js +2 -3
- package/createTheme.js.map +1 -1
- package/dataProviders/ApiClient.d.ts +13 -5
- package/dataProviders/ApiClient.js +58 -49
- package/dataProviders/ApiClient.js.map +1 -1
- package/dataProviders/ApiClient.test.d.ts +1 -0
- package/dataProviders/ApiClient.test.js +116 -0
- package/dataProviders/ApiClient.test.js.map +1 -0
- package/dataProviders/DefaultDataProvider.d.ts +7 -2
- package/dataProviders/DefaultDataProvider.js +103 -45
- package/dataProviders/DefaultDataProvider.js.map +1 -1
- package/dataProviders/DefaultDataProvider.test.d.ts +1 -0
- package/dataProviders/DefaultDataProvider.test.js +81 -0
- package/dataProviders/DefaultDataProvider.test.js.map +1 -0
- package/dataProviders/GET_EXPERIMENT_PAUSED.d.ts +1 -0
- package/dataProviders/GET_EXPERIMENT_PAUSED.js +17 -0
- package/dataProviders/GET_EXPERIMENT_PAUSED.js.map +1 -0
- package/dataProviders/GET_LAYOUT_BY_SLUG.d.ts +1 -0
- package/dataProviders/GET_LAYOUT_BY_SLUG.js +27 -0
- package/dataProviders/GET_LAYOUT_BY_SLUG.js.map +1 -0
- package/dataProviders/GET_PAGE_BY_ID.d.ts +1 -1
- package/dataProviders/GET_PAGE_BY_ID.js +10 -1
- package/dataProviders/GET_PAGE_BY_ID.js.map +1 -1
- package/dataProviders/GET_PAGE_BY_PATH.d.ts +1 -1
- package/dataProviders/GET_PAGE_BY_PATH.js +10 -1
- package/dataProviders/GET_PAGE_BY_PATH.js.map +1 -1
- package/dataProviders/GET_PAGE_EXPERIMENT.d.ts +1 -0
- package/dataProviders/GET_PAGE_EXPERIMENT.js +32 -0
- package/dataProviders/GET_PAGE_EXPERIMENT.js.map +1 -0
- package/dataProviders/GET_VARIANT_CONTENT.d.ts +1 -0
- package/dataProviders/GET_VARIANT_CONTENT.js +24 -0
- package/dataProviders/GET_VARIANT_CONTENT.js.map +1 -0
- package/dataProviders/LIST_PUBLISHED_PAGES.d.ts +1 -1
- package/dataProviders/LIST_PUBLISHED_PAGES.js +16 -5
- package/dataProviders/LIST_PUBLISHED_PAGES.js.map +1 -1
- package/dataProviders/NullDataProvider.d.ts +17 -4
- package/dataProviders/NullDataProvider.js +33 -13
- package/dataProviders/NullDataProvider.js.map +1 -1
- package/dataProviders/RedirectsProvider.js +24 -27
- package/dataProviders/RedirectsProvider.js.map +1 -1
- package/dataProviders/queries.test.d.ts +1 -0
- package/dataProviders/queries.test.js +53 -0
- package/dataProviders/queries.test.js.map +1 -0
- package/defaultBreakpoints.js +43 -22
- package/defaultBreakpoints.js.map +1 -1
- package/documentOperations/$addElementReferenceToParent.d.ts +1 -1
- package/documentOperations/$addElementReferenceToParent.js +30 -26
- package/documentOperations/$addElementReferenceToParent.js.map +1 -1
- package/documentOperations/AddElement.js +8 -7
- package/documentOperations/AddElement.js.map +1 -1
- package/documentOperations/AddToParent.d.ts +2 -2
- package/documentOperations/AddToParent.js +14 -13
- package/documentOperations/AddToParent.js.map +1 -1
- package/documentOperations/IDocumentOperation.js +0 -3
- package/documentOperations/RemoveElement.js +10 -7
- package/documentOperations/RemoveElement.js.map +1 -1
- package/documentOperations/SetGlobalInputBinding.js +23 -22
- package/documentOperations/SetGlobalInputBinding.js.map +1 -1
- package/documentOperations/SetGlobalStyleBinding.js +23 -23
- package/documentOperations/SetGlobalStyleBinding.js.map +1 -1
- package/documentOperations/SetInputBindingOverride.js +30 -29
- package/documentOperations/SetInputBindingOverride.js.map +1 -1
- package/documentOperations/SetStyleBindingOverride.js +30 -31
- package/documentOperations/SetStyleBindingOverride.js.map +1 -1
- package/documentOperations/index.js +9 -8
- package/documentOperations/index.js.map +1 -1
- package/experiments/PostHogAnalyticsProvider.d.ts +31 -0
- package/experiments/PostHogAnalyticsProvider.js +28 -0
- package/experiments/PostHogAnalyticsProvider.js.map +1 -0
- package/experiments/analyticsProvider.d.ts +4 -0
- package/experiments/analyticsProvider.js +11 -0
- package/experiments/analyticsProvider.js.map +1 -0
- package/experiments/bucketing.d.ts +27 -0
- package/experiments/bucketing.js +91 -0
- package/experiments/bucketing.js.map +1 -0
- package/experiments/bucketing.test.d.ts +1 -0
- package/experiments/bucketing.test.js +183 -0
- package/experiments/bucketing.test.js.map +1 -0
- package/experiments/index.d.ts +5 -0
- package/experiments/index.js +5 -0
- package/experiments/render.d.ts +82 -0
- package/experiments/render.js +149 -0
- package/experiments/render.js.map +1 -0
- package/experiments/types.d.ts +108 -0
- package/experiments/types.js +4 -0
- package/experiments/types.js.map +1 -0
- package/findMatchingAstNode.js +11 -13
- package/findMatchingAstNode.js.map +1 -1
- package/generateElementId.js +2 -1
- package/generateElementId.js.map +1 -1
- package/globalElements.d.ts +43 -0
- package/globalElements.js +79 -0
- package/globalElements.js.map +1 -0
- package/globalElements.test.d.ts +1 -0
- package/globalElements.test.js +343 -0
- package/globalElements.test.js.map +1 -0
- package/headersProvider.d.ts +1 -2
- package/headersProvider.js +4 -3
- package/headersProvider.js.map +1 -1
- package/image/geometry.d.ts +54 -0
- package/image/geometry.js +112 -0
- package/image/geometry.js.map +1 -0
- package/image/geometry.test.d.ts +1 -0
- package/image/geometry.test.js +190 -0
- package/image/geometry.test.js.map +1 -0
- package/image/index.d.ts +3 -0
- package/image/index.js +3 -0
- package/image/presets.d.ts +6 -0
- package/image/presets.js +20 -0
- package/image/presets.js.map +1 -0
- package/image/types.d.ts +10 -0
- package/image/types.js +0 -0
- package/index.d.ts +21 -0
- package/index.js +21 -2
- package/jsonPatch.js +5 -9
- package/jsonPatch.js.map +1 -1
- package/listItemId.d.ts +17 -0
- package/listItemId.js +14 -0
- package/listItemId.js.map +1 -0
- package/listItemId.test.d.ts +1 -0
- package/listItemId.test.js +265 -0
- package/listItemId.test.js.map +1 -0
- package/messages.d.ts +3 -0
- package/messages.js +15 -11
- package/messages.js.map +1 -1
- package/messenger/MessageOrigin.js +12 -11
- package/messenger/MessageOrigin.js.map +1 -1
- package/messenger/Messenger.js +58 -71
- package/messenger/Messenger.js.map +1 -1
- package/messenger/index.js +0 -2
- package/package.json +17 -16
- package/radioOptionInputs.d.ts +24 -0
- package/radioOptionInputs.js +26 -0
- package/radioOptionInputs.js.map +1 -0
- package/registerComponentGroup.js +5 -6
- package/registerComponentGroup.js.map +1 -1
- package/types/ShorthandCssProperties.js +0 -3
- package/types/WebsiteBuilderTheme.d.ts +12 -0
- package/types/WebsiteBuilderTheme.js +0 -3
- package/types.d.ts +338 -18
- package/types.js +0 -3
- package/withGlobalElementDefaults.d.ts +8 -0
- package/withGlobalElementDefaults.js +60 -0
- package/withGlobalElementDefaults.js.map +1 -0
- package/IBindingsUpdater.js.map +0 -1
- package/IRedirects.js.map +0 -1
- package/documentOperations/IDocumentOperation.js.map +0 -1
- package/index.js.map +0 -1
- package/messenger/index.js.map +0 -1
- package/types/ShorthandCssProperties.js.map +0 -1
- package/types/WebsiteBuilderTheme.js.map +0 -1
- package/types.js.map +0 -1
package/ContentSdk.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["environment","LiveSdk","EditingSdk","ComponentResolver","PreviewSdk","componentRegistry","ApiClient","DefaultDataProvider","Theme","viewportManager","RedirectsProvider","InternalContentSdk","constructor","redirectsProvider","liveSdk","editingSdk","activeSdk","getEditingSdk","getPage","path","listPages","getAllRedirects","getRedirectByPath","ContentSdk","isPreview","init","config","afterInit","configHash","JSON","stringify","lastConfig","apiClient","apiHost","apiKey","apiTenant","dataProvider","preview","isEditing","theme","from","isClient","setBreakpoints","breakpoints","sdk","assertInitialized","registerComponent","blueprint","register","resolveElement","params","resolve","isPreviewing","Error","contentSdk"],"sources":["ContentSdk.ts"],"sourcesContent":["import type {\n Component,\n IContentSdk,\n PublicPage,\n PublicRedirect,\n ResolvedComponent\n} from \"~/types.js\";\nimport { environment } from \"./Environment.js\";\nimport { LiveSdk } from \"./LiveSdk.js\";\nimport { EditingSdk } from \"./EditingSdk.js\";\nimport { ComponentResolver, type ResolveElementParams } from \"~/ComponentResolver.js\";\nimport { PreviewSdk } from \"./PreviewSdk.js\";\nimport { componentRegistry } from \"~/ComponentRegistry.js\";\nimport { ApiClient } from \"~/dataProviders/ApiClient.js\";\nimport { DefaultDataProvider } from \"~/dataProviders/DefaultDataProvider.js\";\nimport type { WebsiteBuilderThemeInput } from \"./types/WebsiteBuilderTheme.js\";\nimport { Theme } from \"./Theme.js\";\nimport { viewportManager } from \"./ViewportManager.js\";\nimport type { IRedirects } from \"~/IRedirects.js\";\nimport { RedirectsProvider } from \"~/dataProviders/RedirectsProvider.js\";\n\nexport type ApiConfig = {\n apiKey: string;\n apiHost: string;\n apiTenant: string;\n};\n\nexport type ContentSDKConfig = ApiConfig & {\n preview?: boolean;\n theme?: WebsiteBuilderThemeInput;\n};\n\nclass InternalContentSdk implements IContentSdk, IRedirects {\n private activeSdk: IContentSdk;\n private editingSdk: EditingSdk | undefined;\n private redirectsProvider: IRedirects;\n\n constructor(redirectsProvider: IRedirects, liveSdk: LiveSdk, editingSdk?: EditingSdk) {\n this.redirectsProvider = redirectsProvider;\n this.activeSdk = editingSdk ?? liveSdk;\n this.editingSdk = editingSdk;\n }\n\n getEditingSdk() {\n return this.editingSdk;\n }\n\n async getPage(path: string): Promise<PublicPage | null> {\n return this.activeSdk.getPage(path);\n }\n\n listPages(): Promise<PublicPage[]> {\n return this.activeSdk.listPages();\n }\n\n getAllRedirects(): Promise<Map<string, PublicRedirect>> {\n return this.redirectsProvider.getAllRedirects();\n }\n\n getRedirectByPath(path: string): Promise<PublicRedirect | undefined> {\n return this.redirectsProvider.getRedirectByPath(path);\n }\n}\n\nexport class ContentSdk implements IContentSdk, IRedirects {\n protected sdk?: InternalContentSdk;\n private isPreview = false;\n private lastConfig: any;\n\n public init(config: ContentSDKConfig, afterInit?: () => void): void {\n const configHash = JSON.stringify(config);\n if (this.lastConfig && this.lastConfig === configHash) {\n return;\n }\n\n this.lastConfig = configHash;\n const apiClient = new ApiClient(config.apiHost, config.apiKey, config.apiTenant);\n\n const dataProvider = new DefaultDataProvider({ apiClient });\n\n let liveSdk: IContentSdk = new LiveSdk(dataProvider);\n\n if (config.preview && !environment.isEditing()) {\n this.isPreview = true;\n liveSdk = new PreviewSdk(dataProvider, liveSdk);\n }\n\n const theme = Theme.from(config.theme ?? {});\n\n if (environment.isClient()) {\n viewportManager.setBreakpoints(theme.breakpoints);\n }\n\n let editingSdk;\n if (environment.isEditing()) {\n editingSdk = new EditingSdk(liveSdk, theme);\n }\n\n this.sdk = new InternalContentSdk(\n new RedirectsProvider(apiClient),\n liveSdk as LiveSdk,\n editingSdk\n );\n\n if (typeof afterInit === \"function\") {\n afterInit();\n }\n }\n\n public getEditingSdk() {\n this.assertInitialized();\n return this.sdk.getEditingSdk();\n }\n\n public getPage(path: string) {\n this.assertInitialized();\n return this.sdk.getPage(path);\n }\n\n public listPages() {\n this.assertInitialized();\n return this.sdk.listPages();\n }\n\n public async getAllRedirects() {\n this.assertInitialized();\n return this.sdk.getAllRedirects();\n }\n\n getRedirectByPath(path: string): Promise<PublicRedirect | undefined> {\n this.assertInitialized();\n return this.sdk.getRedirectByPath(path);\n }\n\n registerComponent(blueprint: Component): void {\n this.assertInitialized();\n componentRegistry.register(blueprint);\n }\n\n resolveElement(params: ResolveElementParams): ResolvedComponent[] | null {\n this.assertInitialized();\n return new ComponentResolver(componentRegistry).resolve(params);\n }\n\n isEditing() {\n return environment.isEditing();\n }\n\n isPreviewing() {\n return this.isPreview;\n }\n\n private assertInitialized(): asserts this is this & {\n sdk: IContentSdk;\n } {\n if (!this.sdk) {\n throw new Error(`ContentSdk has not been initialized!`);\n }\n }\n}\n\nexport const contentSdk = new ContentSdk();\n"],"mappings":"AAOA,SAASA,WAAW;AACpB,SAASC,OAAO;AAChB,SAASC,UAAU;AACnB,SAASC,iBAAiB;AAC1B,SAASC,UAAU;AACnB,SAASC,iBAAiB;AAC1B,SAASC,SAAS;AAClB,SAASC,mBAAmB;AAE5B,SAASC,KAAK;AACd,SAASC,eAAe;AAExB,SAASC,iBAAiB;AAa1B,MAAMC,kBAAkB,CAAoC;EAKxDC,WAAWA,CAACC,iBAA6B,EAAEC,OAAgB,EAAEC,UAAuB,EAAE;IAClF,IAAI,CAACF,iBAAiB,GAAGA,iBAAiB;IAC1C,IAAI,CAACG,SAAS,GAAGD,UAAU,IAAID,OAAO;IACtC,IAAI,CAACC,UAAU,GAAGA,UAAU;EAChC;EAEAE,aAAaA,CAAA,EAAG;IACZ,OAAO,IAAI,CAACF,UAAU;EAC1B;EAEA,MAAMG,OAAOA,CAACC,IAAY,EAA8B;IACpD,OAAO,IAAI,CAACH,SAAS,CAACE,OAAO,CAACC,IAAI,CAAC;EACvC;EAEAC,SAASA,CAAA,EAA0B;IAC/B,OAAO,IAAI,CAACJ,SAAS,CAACI,SAAS,CAAC,CAAC;EACrC;EAEAC,eAAeA,CAAA,EAAyC;IACpD,OAAO,IAAI,CAACR,iBAAiB,CAACQ,eAAe,CAAC,CAAC;EACnD;EAEAC,iBAAiBA,CAACH,IAAY,EAAuC;IACjE,OAAO,IAAI,CAACN,iBAAiB,CAACS,iBAAiB,CAACH,IAAI,CAAC;EACzD;AACJ;AAEA,OAAO,MAAMI,UAAU,CAAoC;EAE/CC,SAAS,GAAG,KAAK;EAGlBC,IAAIA,CAACC,MAAwB,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ACJ;AAEA,OAAO,MAAMC,UAAU,GAAG,IAAI/B,UAAU,CAAC,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"ContentSdk.js","sources":["../src/ContentSdk.ts"],"sourcesContent":["import type {\n Component,\n Document,\n GetLayoutParams,\n IContentSdk,\n IDataProvider,\n ListPagesOptions,\n ListPagesResult,\n PublicLayout,\n PublicPage,\n PublicRedirect,\n ResolvedComponent\n} from \"~/types.js\";\nimport type { ActiveExperiment, VariantContent } from \"~/experiments/types.js\";\nimport {\n getPageWithExperiment,\n type ExperimentRenderResult,\n type GetPageWithExperimentOptions\n} from \"~/experiments/render.js\";\nimport { environment } from \"./Environment.js\";\nimport { LiveSdk } from \"./LiveSdk.js\";\nimport { EditingSdk } from \"./EditingSdk.js\";\nimport { ComponentResolver, type ResolveElementParams } from \"~/ComponentResolver.js\";\nimport { PreviewSdk } from \"./PreviewSdk.js\";\nimport type { ComponentRegistry } from \"~/ComponentRegistry.js\";\nimport { componentRegistry } from \"~/ComponentRegistry.js\";\nimport type { ContentEntryCache } from \"~/contentEntry/ContentEntryCache.js\";\nimport { contentEntryCache } from \"~/contentEntry/ContentEntryCache.js\";\nimport type { DocumentStoreManager } from \"~/DocumentStoreManager.js\";\nimport { documentStoreManager } from \"~/DocumentStoreManager.js\";\nimport { ApiClient } from \"~/dataProviders/ApiClient.js\";\nimport { DefaultDataProvider } from \"~/dataProviders/DefaultDataProvider.js\";\nimport type { WebsiteBuilderThemeInput } from \"./types/WebsiteBuilderTheme.js\";\nimport { Theme } from \"./Theme.js\";\nimport { viewportManager } from \"./ViewportManager.js\";\nimport type { IRedirects } from \"~/IRedirects.js\";\nimport { RedirectsProvider } from \"~/dataProviders/RedirectsProvider.js\";\nimport type { HeadersProvider } from \"~/headersProvider.js\";\nimport type { HeadersConfig } from \"@webiny/sdk\";\nimport type { ContentEntryLoader } from \"~/contentEntry/resolveContentEntry.js\";\nimport type { ComponentLoader, ComponentLoaderFactory } from \"~/componentLoader/ComponentLoader.js\";\nimport type { ComponentLoaderFactoryParams } from \"~/componentLoader/ComponentLoader.js\";\nimport { createComponentLoader } from \"~/componentLoader/createComponentLoader.js\";\n\nexport type ApiConfig = {\n apiKey: string;\n apiHost: string;\n apiTenant: string;\n /** Extra headers sent with every API request; a function is called before each request. */\n headers?: HeadersConfig;\n};\n\nexport type ContentSDKConfig = ApiConfig & {\n preview?: boolean;\n previewParams?: string;\n theme?: WebsiteBuilderThemeInput;\n /**\n * Request headers for server-side preview and experiments. When omitted, the provider set\n * with `setHeadersProvider` is used. Meant for apps that serve requests outside the\n * framework hook; it never carries the tenant.\n */\n headersProvider?: HeadersProvider;\n /**\n * Loads the CMS entries that `contentEntry` inputs reference, for this SDK's content\n * entry cache. Pass one bound to the same tenant as this SDK. When omitted, the cache's\n * loader is left as it is.\n */\n contentEntryLoader?: ContentEntryLoader;\n /**\n * Creates the loader for components a document references but nobody registered. It gets\n * this SDK's API client and API config. When omitted, the default set with\n * `setDefaultComponentLoader` is used, and without one nothing is loaded.\n */\n componentLoader?: ComponentLoaderFactory;\n};\n\n/**\n * The state a `ContentSdk` renders with. Each piece defaults to its module-level singleton, so\n * `new ContentSdk()` shares state with `contentSdk`. `createContentClient` passes new instances.\n */\nexport type ContentSdkState = {\n componentRegistry?: ComponentRegistry;\n contentEntryCache?: ContentEntryCache;\n documentStoreManager?: DocumentStoreManager;\n};\n\nclass InternalContentSdk implements IContentSdk, IRedirects {\n private activeSdk: IContentSdk;\n private editingSdk: EditingSdk | undefined;\n private redirectsProvider: IRedirects;\n\n constructor(redirectsProvider: IRedirects, liveSdk: LiveSdk, editingSdk?: EditingSdk) {\n this.redirectsProvider = redirectsProvider;\n this.activeSdk = editingSdk ?? liveSdk;\n this.editingSdk = editingSdk;\n }\n\n getEditingSdk() {\n return this.editingSdk;\n }\n\n async getPage(path: string): Promise<PublicPage | null> {\n return this.activeSdk.getPage(path);\n }\n\n getLayout(params: GetLayoutParams): Promise<PublicLayout | null> {\n return this.activeSdk.getLayout(params);\n }\n\n listPages(options?: ListPagesOptions): Promise<ListPagesResult> {\n return this.activeSdk.listPages(options);\n }\n\n getAllRedirects(): Promise<Map<string, PublicRedirect>> {\n return this.redirectsProvider.getAllRedirects();\n }\n\n getRedirectByPath(path: string): Promise<PublicRedirect | undefined> {\n return this.redirectsProvider.getRedirectByPath(path);\n }\n}\n\n/**\n * The config's top-level values that its JSON doesn't capture (functions and class instances), in key order.\n */\nfunction getReferences(config: ContentSDKConfig): unknown[] {\n return Object.keys(config)\n .sort()\n .map(key => (config as Record<string, unknown>)[key])\n .filter(value => {\n if (typeof value === \"function\") {\n return true;\n }\n if (!value || typeof value !== \"object\" || Array.isArray(value)) {\n return false;\n }\n const prototype = Object.getPrototypeOf(value);\n return prototype !== Object.prototype && prototype !== null;\n });\n}\n\nexport class ContentSdk implements IContentSdk, IRedirects {\n protected sdk?: InternalContentSdk;\n private isPreview = false;\n private lastConfig?: { hash: string; references: unknown[] };\n private dataProvider?: IDataProvider;\n private headersProvider?: HeadersProvider;\n private componentLoaderFactory?: ComponentLoaderFactory;\n private componentLoaderParams?: ComponentLoaderFactoryParams;\n private componentLoader?: ComponentLoader;\n private readonly componentRegistry: ComponentRegistry;\n private readonly contentEntryCache: ContentEntryCache;\n private readonly documentStoreManager: DocumentStoreManager;\n\n constructor(state: ContentSdkState = {}) {\n this.componentRegistry = state.componentRegistry ?? componentRegistry;\n this.contentEntryCache = state.contentEntryCache ?? contentEntryCache;\n this.documentStoreManager = state.documentStoreManager ?? documentStoreManager;\n }\n\n public init(config: ContentSDKConfig, afterInit?: () => void): void {\n // `JSON.stringify` leaves out functions (`headers`, `headersProvider`, the loaders) and keeps only the\n // fields of class instances, so those are compared by identity.\n const lastConfig = {\n hash: JSON.stringify(config),\n references: getReferences(config)\n };\n if (\n this.lastConfig &&\n this.lastConfig.hash === lastConfig.hash &&\n this.lastConfig.references.length === lastConfig.references.length &&\n this.lastConfig.references.every(\n (value, index) => value === lastConfig.references[index]\n )\n ) {\n return;\n }\n\n this.lastConfig = lastConfig;\n this.headersProvider = config.headersProvider;\n if (config.contentEntryLoader) {\n this.contentEntryCache.setLoader(config.contentEntryLoader);\n }\n const apiClient = new ApiClient({\n apiHost: config.apiHost,\n apiKey: config.apiKey,\n apiTenant: config.apiTenant,\n headers: config.headers,\n preview: config.preview\n });\n\n const dataProvider = new DefaultDataProvider({ apiClient });\n\n // The loader is created on first use, so a default set after this call still applies.\n this.componentLoaderFactory = config.componentLoader;\n this.componentLoaderParams = {\n apiClient,\n config: {\n apiHost: config.apiHost,\n apiKey: config.apiKey,\n apiTenant: config.apiTenant,\n headers: config.headers\n }\n };\n this.componentLoader = undefined;\n this.dataProvider = dataProvider;\n\n let liveSdk: IContentSdk = new LiveSdk(dataProvider, this.documentStoreManager);\n\n if (config.preview && !environment.isEditing()) {\n this.isPreview = true;\n liveSdk = new PreviewSdk(\n dataProvider,\n liveSdk,\n config.previewParams,\n config.headersProvider\n );\n }\n\n const theme = Theme.from(config.theme ?? {});\n\n if (environment.isClient()) {\n viewportManager.setBreakpoints(theme.breakpoints);\n }\n\n let editingSdk;\n if (environment.isEditing()) {\n editingSdk = new EditingSdk(liveSdk, theme, {\n componentRegistry: this.componentRegistry,\n documentStoreManager: this.documentStoreManager\n });\n }\n\n this.sdk = new InternalContentSdk(\n new RedirectsProvider(apiClient),\n liveSdk as LiveSdk,\n editingSdk\n );\n\n if (typeof afterInit === \"function\") {\n afterInit();\n }\n }\n\n public getEditingSdk() {\n this.assertInitialized();\n return this.sdk.getEditingSdk();\n }\n\n public getPage(path: string) {\n this.assertInitialized();\n return this.sdk.getPage(path);\n }\n\n /**\n * The published layout with the slug, composed with the global components of `language`, to render\n * content in (e.g. a CMS entry whose model names the layout). `null` when no such layout is published.\n */\n public getLayout(params: GetLayoutParams) {\n this.assertInitialized();\n return this.sdk.getLayout(params);\n }\n\n public getPageExperiment(path: string): Promise<ActiveExperiment | null> {\n return this.requireDataProvider().getPageExperiment(path);\n }\n\n public getVariantContent(variantId: string): Promise<VariantContent | null> {\n return this.requireDataProvider().getVariantContent(variantId);\n }\n\n public getExperimentPaused(experimentId: string): Promise<boolean> {\n return this.requireDataProvider().getExperimentPaused(experimentId);\n }\n\n /**\n * Resolve and render the right page for the current visitor (control or a variant),\n * server-side. Bucketing, targeting, exposure emission, and cache-key handling are all\n * encapsulated here so projects do not reimplement them. See {@link getPageWithExperiment}.\n */\n public getPageWithExperiment(\n path: string,\n options?: GetPageWithExperimentOptions\n ): Promise<ExperimentRenderResult> {\n this.assertInitialized();\n this.requireDataProvider();\n const experimentOptions: GetPageWithExperimentOptions = { ...options };\n if (this.headersProvider && !experimentOptions.headersProvider) {\n experimentOptions.headersProvider = this.headersProvider;\n }\n return getPageWithExperiment(\n {\n getPage: p => this.getPage(p),\n getPageExperiment: p => this.getPageExperiment(p),\n getVariantContent: id => this.getVariantContent(id),\n getExperimentPaused: id => this.getExperimentPaused(id)\n },\n path,\n experimentOptions\n );\n }\n\n public listPages(options?: ListPagesOptions) {\n this.assertInitialized();\n return this.sdk.listPages(options);\n }\n\n public async getAllRedirects() {\n this.assertInitialized();\n return this.sdk.getAllRedirects();\n }\n\n getRedirectByPath(path: string): Promise<PublicRedirect | undefined> {\n this.assertInitialized();\n return this.sdk.getRedirectByPath(path);\n }\n\n registerComponent(blueprint: Component): void {\n this.assertInitialized();\n this.componentRegistry.register(blueprint);\n }\n\n getComponent(name: string): Component | undefined {\n return componentRegistry.get(name);\n }\n\n getComponentManifests() {\n return componentRegistry.getManifests();\n }\n\n resolveElement(params: ResolveElementParams): ResolvedComponent[] | null {\n this.assertInitialized();\n return new ComponentResolver(this.componentRegistry, this.contentEntryCache).resolve(\n params\n );\n }\n\n /**\n * Load every component the document references that isn't registered yet, through the\n * component loader, and register what it returns. Call it before rendering the document.\n * Nothing is cached here: an unregistered component goes to the loader on every call.\n */\n public async loadDocumentComponents(document: Document): Promise<void> {\n this.assertInitialized();\n const names = new Set<string>();\n for (const element of Object.values(document.elements)) {\n const name = element.component?.name;\n if (name && !this.componentRegistry.get(name)) {\n names.add(name);\n }\n }\n\n const loader = this.getComponentLoader();\n const loads = Array.from(names).map(name => loader.loadComponent({ name }));\n const components = await Promise.all(loads);\n\n for (const component of components) {\n if (component) {\n this.componentRegistry.register(component);\n }\n }\n }\n\n private getComponentLoader(): ComponentLoader {\n if (!this.componentLoader) {\n this.componentLoader = createComponentLoader(\n this.componentLoaderFactory,\n this.componentLoaderParams!\n );\n }\n return this.componentLoader;\n }\n\n public getComponentRegistry(): ComponentRegistry {\n return this.componentRegistry;\n }\n\n public getContentEntryCache(): ContentEntryCache {\n return this.contentEntryCache;\n }\n\n public getDocumentStoreManager(): DocumentStoreManager {\n return this.documentStoreManager;\n }\n\n isEditing() {\n return environment.isEditing();\n }\n\n isPreviewing() {\n return this.isPreview;\n }\n\n private assertInitialized(): asserts this is this & {\n sdk: IContentSdk;\n } {\n if (!this.sdk) {\n throw new Error(`ContentSdk has not been initialized!`);\n }\n }\n\n private requireDataProvider(): IDataProvider {\n if (!this.dataProvider) {\n throw new Error(`ContentSdk has not been initialized!`);\n }\n return this.dataProvider;\n }\n}\n\nexport const contentSdk = new ContentSdk();\n"],"names":["InternalContentSdk","redirectsProvider","liveSdk","editingSdk","path","params","options","getReferences","config","Object","key","value","Array","prototype","ContentSdk","state","componentRegistry","contentEntryCache","documentStoreManager","afterInit","lastConfig","JSON","index","apiClient","ApiClient","dataProvider","DefaultDataProvider","undefined","LiveSdk","environment","PreviewSdk","theme","Theme","viewportManager","EditingSdk","RedirectsProvider","variantId","experimentId","experimentOptions","getPageWithExperiment","p","id","blueprint","name","ComponentResolver","document","names","Set","element","loader","loads","components","Promise","component","createComponentLoader","Error","contentSdk"],"mappings":";;;;;;;;;;;;;;;AAsFA,MAAMA;IAKF,YAAYC,iBAA6B,EAAEC,OAAgB,EAAEC,UAAuB,CAAE;QAClF,IAAI,CAAC,iBAAiB,GAAGF;QACzB,IAAI,CAAC,SAAS,GAAGE,cAAcD;QAC/B,IA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IAAI,CAAC,kBAAkB;QACtC,MAAMC,QAAQtC,MAAM,IAAI,CAACkC,OAAO,GAAG,CAACH,CAAAA,OAAQM,OAAO,aAAa,CAAC;gBAAEN;YAAK;QACxE,MAAMQ,aAAa,MAAMC,QAAQ,GAAG,CAACF;QAErC,KAAK,MAAMG,aAAaF,WACpB,IAAIE,WACA,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAACA;IAG5C;IAEQ,qBAAsC;QAC1C,IAAI,CAAC,IAAI,CAAC,eAAe,EACrB,IAAI,CAAC,eAAe,GAAGC,sBACnB,IAAI,CAAC,sBAAsB,EAC3B,IAAI,CAAC,qBAAqB;QAGlC,OAAO,IAAI,CAAC,eAAe;IAC/B;IAEO,uBAA0C;QAC7C,OAAO,IAAI,CAAC,iBAAiB;IACjC;IAEO,uBAA0C;QAC7C,OAAO,IAAI,CAAC,iBAAiB;IACjC;IAEO,0BAAgD;QACnD,OAAO,IAAI,CAAC,oBAAoB;IACpC;IAEA,YAAY;QACR,OAAOzB,YAAY,SAAS;IAChC;IAEA,eAAe;QACX,OAAO,IAAI,CAAC,SAAS;IACzB;IAEQ,oBAEN;QACE,IAAI,CAAC,IAAI,CAAC,GAAG,EACT,MAAM,IAAI0B,MAAM;IAExB;IAEQ,sBAAqC;QACzC,IAAI,CAAC,IAAI,CAAC,YAAY,EAClB,MAAM,IAAIA,MAAM;QAEpB,OAAO,IAAI,CAAC,YAAY;IAC5B;AACJ;AAEO,MAAMC,aAAa,IAAI1C"}
|
package/DocumentStore.d.ts
CHANGED
|
@@ -5,6 +5,11 @@ export declare class DocumentStore<TDocument extends Document = Document> {
|
|
|
5
5
|
private documentReady;
|
|
6
6
|
private readyResolvers;
|
|
7
7
|
constructor();
|
|
8
|
+
/**
|
|
9
|
+
* Puts `doc` in the store. A document already in it takes only the differences, value by
|
|
10
|
+
* value, so a subscriber (an element's bindings) is notified only when its own part changed:
|
|
11
|
+
* replacing whole objects would re-run every subscriber for any change.
|
|
12
|
+
*/
|
|
8
13
|
setDocument(doc: TDocument): void;
|
|
9
14
|
updateDocument(cb: (document: TDocument) => void): void;
|
|
10
15
|
getDocument(): TDocument | null;
|
package/DocumentStore.js
CHANGED
|
@@ -1,67 +1,57 @@
|
|
|
1
1
|
import { jsonPatch } from "./jsonPatch.js";
|
|
2
|
-
import { makeAutoObservable, runInAction,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
setDocument(doc) {
|
|
11
|
-
runInAction(() => {
|
|
12
|
-
if (this.document) {
|
|
13
|
-
Object.assign(this.document, doc);
|
|
14
|
-
} else {
|
|
15
|
-
this.document = observable(doc);
|
|
16
|
-
}
|
|
17
|
-
this.documentReady = true;
|
|
18
|
-
this.readyResolvers.forEach(fn => fn());
|
|
19
|
-
this.readyResolvers = [];
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
updateDocument(cb) {
|
|
23
|
-
runInAction(() => {
|
|
24
|
-
if (this.document) {
|
|
25
|
-
cb(this.document);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
getDocument() {
|
|
30
|
-
return this.document;
|
|
31
|
-
}
|
|
32
|
-
getElement(id) {
|
|
33
|
-
if (!this.document) {
|
|
34
|
-
return null;
|
|
2
|
+
import { makeAutoObservable, observable, runInAction, toJS } from "mobx";
|
|
3
|
+
class DocumentStore {
|
|
4
|
+
constructor(){
|
|
5
|
+
this.document = null;
|
|
6
|
+
this.documentReady = false;
|
|
7
|
+
this.readyResolvers = [];
|
|
8
|
+
makeAutoObservable(this);
|
|
35
9
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
10
|
+
setDocument(doc) {
|
|
11
|
+
runInAction(()=>{
|
|
12
|
+
if (this.document) {
|
|
13
|
+
const changes = jsonPatch.compare(toJS(this.document), doc);
|
|
14
|
+
if (changes.length) jsonPatch.applyPatch(this.document, changes, false, true);
|
|
15
|
+
} else this.document = observable(doc);
|
|
16
|
+
this.documentReady = true;
|
|
17
|
+
this.readyResolvers.forEach((fn)=>fn());
|
|
18
|
+
this.readyResolvers = [];
|
|
19
|
+
});
|
|
41
20
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
...patch
|
|
47
|
-
};
|
|
21
|
+
updateDocument(cb) {
|
|
22
|
+
runInAction(()=>{
|
|
23
|
+
if (this.document) cb(this.document);
|
|
24
|
+
});
|
|
48
25
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
26
|
+
getDocument() {
|
|
27
|
+
return this.document;
|
|
28
|
+
}
|
|
29
|
+
getElement(id) {
|
|
30
|
+
if (!this.document) return null;
|
|
31
|
+
return this.document.elements[id];
|
|
32
|
+
}
|
|
33
|
+
updateElement(id, patch) {
|
|
34
|
+
if (!this.document) return;
|
|
35
|
+
const current = this.document.elements[id];
|
|
36
|
+
if (current) this.document.elements[id] = {
|
|
37
|
+
...current,
|
|
38
|
+
...patch
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
applyPatch(patch) {
|
|
42
|
+
runInAction(()=>{
|
|
43
|
+
jsonPatch.applyPatch(this.document, patch, false, true);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async waitForDocument() {
|
|
47
|
+
if (this.documentReady) return this.document;
|
|
48
|
+
return new Promise((resolve)=>{
|
|
49
|
+
this.readyResolvers.push(()=>{
|
|
50
|
+
resolve(this.document);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
58
53
|
}
|
|
59
|
-
return new Promise(resolve => {
|
|
60
|
-
this.readyResolvers.push(() => {
|
|
61
|
-
resolve(this.document);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
54
|
}
|
|
55
|
+
export { DocumentStore };
|
|
66
56
|
|
|
67
57
|
//# sourceMappingURL=DocumentStore.js.map
|
package/DocumentStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"DocumentStore.js","sources":["../src/DocumentStore.ts"],"sourcesContent":["import { jsonPatch, type JsonPatchOperation } from \"~/jsonPatch.js\";\nimport type { Document } from \"~/types.js\";\nimport { makeAutoObservable, runInAction, observable, toJS } from \"mobx\";\n\nexport class DocumentStore<TDocument extends Document = Document> {\n private document: TDocument | null = null;\n private documentReady = false;\n private readyResolvers: (() => void)[] = [];\n\n constructor() {\n makeAutoObservable(this);\n }\n\n /**\n * Puts `doc` in the store. A document already in it takes only the differences, value by\n * value, so a subscriber (an element's bindings) is notified only when its own part changed:\n * replacing whole objects would re-run every subscriber for any change.\n */\n setDocument(doc: TDocument) {\n runInAction(() => {\n if (this.document) {\n const changes = jsonPatch.compare(toJS(this.document), doc);\n if (changes.length) {\n jsonPatch.applyPatch(this.document, changes, false, true);\n }\n } else {\n this.document = observable(doc);\n }\n this.documentReady = true;\n this.readyResolvers.forEach(fn => fn());\n this.readyResolvers = [];\n });\n }\n\n updateDocument(cb: (document: TDocument) => void) {\n runInAction(() => {\n if (this.document) {\n cb(this.document);\n }\n });\n }\n\n getDocument() {\n return this.document;\n }\n\n getElement(id: string) {\n if (!this.document) {\n return null;\n }\n\n return this.document.elements[id];\n }\n\n updateElement(id: string, patch: Partial<any>) {\n if (!this.document) {\n return;\n }\n\n const current = this.document.elements[id];\n if (current) {\n this.document.elements[id] = { ...current, ...patch };\n }\n }\n\n applyPatch(patch: JsonPatchOperation[]) {\n runInAction(() => {\n jsonPatch.applyPatch(this.document!, patch, false, true);\n });\n }\n\n async waitForDocument(): Promise<TDocument> {\n if (this.documentReady) {\n return this.document as TDocument;\n }\n\n return new Promise(resolve => {\n this.readyResolvers.push(() => {\n resolve(this.document as TDocument);\n });\n });\n }\n}\n"],"names":["DocumentStore","makeAutoObservable","doc","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IAAI,CAAC,QAAQ,EAAGM,OAAO,OAAO;QACvD;IACJ;IAEA,MAAM,kBAAsC;QACxC,IAAI,IAAI,CAAC,aAAa,EAClB,OAAO,IAAI,CAAC,QAAQ;QAGxB,OAAO,IAAIE,QAAQC,CAAAA;YACf,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC;gBACrBA,QAAQ,IAAI,CAAC,QAAQ;YACzB;QACJ;IACJ;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { reaction, toJS } from "mobx";
|
|
3
|
+
import { DocumentStore } from "./DocumentStore.js";
|
|
4
|
+
function page(titles, extra = {}) {
|
|
5
|
+
return {
|
|
6
|
+
id: "page#0001",
|
|
7
|
+
properties: {
|
|
8
|
+
title: "Home"
|
|
9
|
+
},
|
|
10
|
+
metadata: {},
|
|
11
|
+
elements: Object.fromEntries(Object.keys(titles).map((id)=>[
|
|
12
|
+
id,
|
|
13
|
+
{
|
|
14
|
+
type: "Webiny/Element",
|
|
15
|
+
id,
|
|
16
|
+
component: {
|
|
17
|
+
name: "Text"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
])),
|
|
21
|
+
bindings: Object.fromEntries(Object.entries(titles).map(([id, title])=>[
|
|
22
|
+
id,
|
|
23
|
+
{
|
|
24
|
+
inputs: {
|
|
25
|
+
title: {
|
|
26
|
+
id: `${id}-title`,
|
|
27
|
+
type: "text",
|
|
28
|
+
static: title
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
])),
|
|
33
|
+
...extra
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function watchBindings(store, ids) {
|
|
37
|
+
const runs = Object.fromEntries(ids.map((id)=>[
|
|
38
|
+
id,
|
|
39
|
+
0
|
|
40
|
+
]));
|
|
41
|
+
const disposers = ids.map((id)=>reaction(()=>toJS(store.getDocument().bindings[id]), ()=>{
|
|
42
|
+
runs[id]++;
|
|
43
|
+
}));
|
|
44
|
+
return {
|
|
45
|
+
runs,
|
|
46
|
+
dispose: ()=>disposers.forEach((dispose)=>dispose())
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
describe("DocumentStore.setDocument", ()=>{
|
|
50
|
+
it("updates only the bindings that changed when a new document comes in", ()=>{
|
|
51
|
+
const store = new DocumentStore();
|
|
52
|
+
store.setDocument(page({
|
|
53
|
+
a: "Hello",
|
|
54
|
+
b: "World"
|
|
55
|
+
}));
|
|
56
|
+
const watched = watchBindings(store, [
|
|
57
|
+
"a",
|
|
58
|
+
"b"
|
|
59
|
+
]);
|
|
60
|
+
store.setDocument(page({
|
|
61
|
+
a: "Hallo",
|
|
62
|
+
b: "World"
|
|
63
|
+
}));
|
|
64
|
+
expect(watched.runs).toEqual({
|
|
65
|
+
a: 1,
|
|
66
|
+
b: 0
|
|
67
|
+
});
|
|
68
|
+
expect(toJS(store.getDocument())).toEqual(page({
|
|
69
|
+
a: "Hallo",
|
|
70
|
+
b: "World"
|
|
71
|
+
}));
|
|
72
|
+
watched.dispose();
|
|
73
|
+
});
|
|
74
|
+
it("changes nothing when the new document is equal", ()=>{
|
|
75
|
+
const store = new DocumentStore();
|
|
76
|
+
store.setDocument(page({
|
|
77
|
+
a: "Hello",
|
|
78
|
+
b: "World"
|
|
79
|
+
}));
|
|
80
|
+
const watched = watchBindings(store, [
|
|
81
|
+
"a",
|
|
82
|
+
"b"
|
|
83
|
+
]);
|
|
84
|
+
store.setDocument(page({
|
|
85
|
+
a: "Hello",
|
|
86
|
+
b: "World"
|
|
87
|
+
}));
|
|
88
|
+
expect(watched.runs).toEqual({
|
|
89
|
+
a: 0,
|
|
90
|
+
b: 0
|
|
91
|
+
});
|
|
92
|
+
watched.dispose();
|
|
93
|
+
});
|
|
94
|
+
it("adds and removes elements and fields as the new document has them", ()=>{
|
|
95
|
+
const store = new DocumentStore();
|
|
96
|
+
store.setDocument(page({
|
|
97
|
+
a: "Hello",
|
|
98
|
+
b: "World"
|
|
99
|
+
}, {
|
|
100
|
+
state: {
|
|
101
|
+
open: true
|
|
102
|
+
}
|
|
103
|
+
}));
|
|
104
|
+
store.setDocument(page({
|
|
105
|
+
a: "Hello",
|
|
106
|
+
c: "New"
|
|
107
|
+
}));
|
|
108
|
+
expect(toJS(store.getDocument())).toEqual(page({
|
|
109
|
+
a: "Hello",
|
|
110
|
+
c: "New"
|
|
111
|
+
}));
|
|
112
|
+
});
|
|
113
|
+
it("keeps the store's document object, so subscribers keep reading the same store", ()=>{
|
|
114
|
+
const store = new DocumentStore();
|
|
115
|
+
store.setDocument(page({
|
|
116
|
+
a: "Hello"
|
|
117
|
+
}));
|
|
118
|
+
const first = store.getDocument();
|
|
119
|
+
store.setDocument(page({
|
|
120
|
+
a: "Hallo"
|
|
121
|
+
}));
|
|
122
|
+
expect(store.getDocument()).toBe(first);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
//# sourceMappingURL=DocumentStore.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DocumentStore.test.js","sources":["../src/DocumentStore.test.ts"],"sourcesContent":["import { describe, expect, it } from \"vitest\";\nimport { reaction, toJS } from \"mobx\";\nimport { DocumentStore } from \"./DocumentStore.js\";\nimport type { Document } from \"./types.js\";\n\nfunction page(titles: Record<string, string>, extra: Record<string, unknown> = {}): Document {\n return {\n id: \"page#0001\",\n properties: { title: \"Home\" },\n metadata: {},\n elements: Object.fromEntries(\n Object.keys(titles).map(id => [\n id,\n { type: \"Webiny/Element\", id, component: { name: \"Text\" } }\n ])\n ),\n bindings: Object.fromEntries(\n Object.entries(titles).map(([id, title]) => [\n id,\n { inputs: { title: { id: `${id}-title`, type: \"text\", static: title } } }\n ])\n ),\n ...extra\n } as unknown as Document;\n}\n\n/** How often each element's bindings were seen to change, as an element renderer subscribes. */\nfunction watchBindings(store: DocumentStore, ids: string[]) {\n const runs: Record<string, number> = Object.fromEntries(ids.map(id => [id, 0]));\n const disposers = ids.map(id =>\n reaction(\n () => toJS(store.getDocument()!.bindings[id]),\n () => {\n runs[id]++;\n }\n )\n );\n return { runs, dispose: () => disposers.forEach(dispose => dispose()) };\n}\n\ndescribe(\"DocumentStore.setDocument\", () => {\n it(\"updates only the bindings that changed when a new document comes in\", () => {\n const store = new DocumentStore();\n store.setDocument(page({ a: \"Hello\", b: \"World\" }));\n const watched = watchBindings(store, [\"a\", \"b\"]);\n\n store.setDocument(page({ a: \"Hallo\", b: \"World\" }));\n\n expect(watched.runs).toEqual({ a: 1, b: 0 });\n expect(toJS(store.getDocument())).toEqual(page({ a: \"Hallo\", b: \"World\" }));\n watched.dispose();\n });\n\n it(\"changes nothing when the new document is equal\", () => {\n const store = new DocumentStore();\n store.setDocument(page({ a: \"Hello\", b: \"World\" }));\n const watched = watchBindings(store, [\"a\", \"b\"]);\n\n store.setDocument(page({ a: \"Hello\", b: \"World\" }));\n\n expect(watched.runs).toEqual({ a: 0, b: 0 });\n watched.dispose();\n });\n\n it(\"adds and removes elements and fields as the new document has them\", () => {\n const store = new DocumentStore();\n store.setDocument(page({ a: \"Hello\", b: \"World\" }, { state: { open: true } }));\n\n store.setDocument(page({ a: \"Hello\", c: \"New\" }));\n\n expect(toJS(store.getDocument())).toEqual(page({ a: \"Hello\", c: \"New\" }));\n });\n\n it(\"keeps the store's document object, so subscribers keep reading the same store\", () => {\n const store = new DocumentStore();\n store.setDocument(page({ a: \"Hello\" }));\n const first = store.getDocument();\n\n store.setDocument(page({ a: \"Hallo\" }));\n\n expect(store.getDocument()).toBe(first);\n });\n});\n"],"names":["page","titles","extra","Object","id","title","watchBindings","store","ids","runs","disposers","reaction","toJS","dispose","describe","it","DocumentStore","watched","expect","first"],"mappings":";;;AAKA,SAASA,KAAKC,MAA8B,EAAEC,QAAiC,CAAC,CAAC;IAC7E,OAAO;QACH,IAAI;QACJ,YAAY;YAAE,OAAO;QAAO;QAC5B,U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ACxEiB,QAAQ,OAAO;IACnB;IAEAF,GAAG,kDAAkD;QACjD,MAAMR,QAAQ,IAAIS;QAClBT,MAAM,WAAW,CAACP,KAAK;YAAE,GAAG;YAAS,GAAG;QAAQ;QAChD,MAAMiB,UAAUX,cAAcC,OAAO;YAAC;YAAK;SAAI;QAE/CA,MAAM,WAAW,CAACP,KAAK;YAAE,GAAG;YAAS,GAAG;QAAQ;QAEhDkB,OAAOD,QAAQ,IAAI,EAAE,OAAO,CAAC;YAAE,GAAG;YAAG,GAAG;QAAE;QAC1CA,QAAQ,OAAO;IACnB;IAEAF,GAAG,qEAAqE;QACpE,MAAMR,QAAQ,IAAIS;QAClBT,MAAM,WAAW,CAACP,KAAK;YAAE,GAAG;YAAS,GAAG;QAAQ,GAAG;YAAE,OAAO;gBAAE,MAAM;YAAK;QAAE;QAE3EO,MAAM,WAAW,CAACP,KAAK;YAAE,GAAG;YAAS,GAAG;QAAM;QAE9CkB,OAAON,KAAKL,MAAM,WAAW,KAAK,OAAO,CAACP,KAAK;YAAE,GAAG;YAAS,GAAG;QAAM;IAC1E;IAEAe,GAAG,iFAAiF;QAChF,MAAMR,QAAQ,IAAIS;QAClBT,MAAM,WAAW,CAACP,KAAK;YAAE,GAAG;QAAQ;QACpC,MAAMmB,QAAQZ,MAAM,WAAW;QAE/BA,MAAM,WAAW,CAACP,KAAK;YAAE,GAAG;QAAQ;QAEpCkB,OAAOX,MAAM,WAAW,IAAI,IAAI,CAACY;IACrC;AACJ"}
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import type { Document } from "./types.js";
|
|
2
2
|
import { DocumentStore } from "./DocumentStore.js";
|
|
3
|
-
declare
|
|
3
|
+
export declare const DEFAULT_MAX_DOCUMENT_STORES = 10000;
|
|
4
|
+
export type DocumentStoreManagerOptions = {
|
|
5
|
+
maxEntries?: number;
|
|
6
|
+
};
|
|
7
|
+
export declare class DocumentStoreManager {
|
|
4
8
|
private stores;
|
|
9
|
+
private readonly maxEntries;
|
|
10
|
+
constructor(options?: DocumentStoreManagerOptions);
|
|
5
11
|
getStores(): Map<string, DocumentStore<Document>>;
|
|
6
12
|
getStore<TDocument extends Document>(id: string): DocumentStore<TDocument>;
|
|
7
13
|
removeStore(id: string): void;
|
|
8
14
|
hasStore(id: string): boolean;
|
|
15
|
+
clear(): void;
|
|
16
|
+
private evict;
|
|
9
17
|
}
|
|
10
18
|
export declare const documentStoreManager: DocumentStoreManager;
|
|
11
|
-
export {};
|
package/DocumentStoreManager.js
CHANGED
|
@@ -1,22 +1,42 @@
|
|
|
1
1
|
import { DocumentStore } from "./DocumentStore.js";
|
|
2
|
+
const DEFAULT_MAX_DOCUMENT_STORES = 10000;
|
|
2
3
|
class DocumentStoreManager {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
constructor(options = {}){
|
|
5
|
+
this.stores = new Map();
|
|
6
|
+
this.maxEntries = options.maxEntries ?? DEFAULT_MAX_DOCUMENT_STORES;
|
|
7
|
+
}
|
|
8
|
+
getStores() {
|
|
9
|
+
return this.stores;
|
|
10
|
+
}
|
|
11
|
+
getStore(id) {
|
|
12
|
+
const existing = this.stores.get(id);
|
|
13
|
+
if (existing) {
|
|
14
|
+
this.stores.delete(id);
|
|
15
|
+
this.stores.set(id, existing);
|
|
16
|
+
return existing;
|
|
17
|
+
}
|
|
18
|
+
const store = new DocumentStore();
|
|
19
|
+
this.stores.set(id, store);
|
|
20
|
+
this.evict();
|
|
21
|
+
return store;
|
|
22
|
+
}
|
|
23
|
+
removeStore(id) {
|
|
24
|
+
this.stores.delete(id);
|
|
25
|
+
}
|
|
26
|
+
hasStore(id) {
|
|
27
|
+
return this.stores.has(id);
|
|
28
|
+
}
|
|
29
|
+
clear() {
|
|
30
|
+
this.stores.clear();
|
|
31
|
+
}
|
|
32
|
+
evict() {
|
|
33
|
+
while(this.stores.size > this.maxEntries){
|
|
34
|
+
const oldest = this.stores.keys().next().value;
|
|
35
|
+
this.stores.delete(oldest);
|
|
36
|
+
}
|
|
10
37
|
}
|
|
11
|
-
return this.stores.get(id);
|
|
12
|
-
}
|
|
13
|
-
removeStore(id) {
|
|
14
|
-
this.stores.delete(id);
|
|
15
|
-
}
|
|
16
|
-
hasStore(id) {
|
|
17
|
-
return this.stores.has(id);
|
|
18
|
-
}
|
|
19
38
|
}
|
|
20
|
-
|
|
39
|
+
const documentStoreManager = new DocumentStoreManager();
|
|
40
|
+
export { DEFAULT_MAX_DOCUMENT_STORES, DocumentStoreManager, documentStoreManager };
|
|
21
41
|
|
|
22
42
|
//# sourceMappingURL=DocumentStoreManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"DocumentStoreManager.js","sources":["../src/DocumentStoreManager.ts"],"sourcesContent":["import type { Document } from \"./types.js\";\nimport { DocumentStore } from \"./DocumentStore.js\";\n\n// High enough that an app serving one site never reaches it.\nexport const DEFAULT_MAX_DOCUMENT_STORES = 10_000;\n\nexport type DocumentStoreManagerOptions = {\n // Stores kept before the least recently used one is dropped.\n maxEntries?: number;\n};\n\nexport class DocumentStoreManager {\n private stores = new Map<string, DocumentStore>();\n private readonly maxEntries: number;\n\n constructor(options: DocumentStoreManagerOptions = {}) {\n this.maxEntries = options.maxEntries ?? DEFAULT_MAX_DOCUMENT_STORES;\n }\n\n getStores() {\n return this.stores;\n }\n\n getStore<TDocument extends Document>(id: string): DocumentStore<TDocument> {\n const existing = this.stores.get(id);\n if (existing) {\n // Move it to the end, so the map stays ordered from least to most recently used.\n this.stores.delete(id);\n this.stores.set(id, existing);\n return existing as DocumentStore<TDocument>;\n }\n\n const store = new DocumentStore<TDocument>();\n this.stores.set(id, store);\n this.evict();\n return store;\n }\n\n removeStore(id: string) {\n this.stores.delete(id);\n }\n\n hasStore(id: string): boolean {\n return this.stores.has(id);\n }\n\n clear() {\n this.stores.clear();\n }\n\n private evict() {\n while (this.stores.size > this.maxEntries) {\n const oldest = this.stores.keys().next().value!;\n this.stores.delete(oldest);\n }\n }\n}\n\nexport const documentStoreManager = new DocumentStoreManager();\n"],"names":["DEFAULT_MAX_DOCUMENT_STORES","DocumentStoreManager","options","Map","id","existing","store","DocumentStore","oldest","documentStoreManager"],"mappings":";AAIO,MAAMA,8BAA8B;AAOpC,MAAMC;IAIT,YAAYC,UAAuC,CAAC,CAAC,CAAE;aAH/C,MAAM,GAAG,IAAIC;QAIjB,IAAI,CAAC,UAAU,GAAGD,QAAQ,UAAU,IAAIF;IAC5C;IAEA,YAAY;QACR,OAAO,IAAI,CAAC,MAAM;IACtB;IAEA,SAAqCI,EAAU,EAA4B;QACvE,MAAMC,WAAW,IAAI,CAAC,MAAM,CAAC,GAAG,CAACD;QACjC,IAAIC,UAAU;YAEV,IAAI,CAAC,MAAM,CAAC,MAAM,CAACD;YACnB,IAAI,CAAC,MAAM,CAAC,GAAG,CAACA,IAAIC;YACpB,OAAOA;QACX;QAEA,MAAMC,QAAQ,IAAIC;QAClB,IAAI,CAAC,MAAM,CAAC,GAAG,CAACH,IAAIE;QACpB,IAAI,CAAC,KAAK;QACV,OAAOA;IACX;IAEA,YAAYF,EAAU,EAAE;QACpB,IAAI,CAAC,MAAM,CAAC,MAAM,CAACA;IACvB;IAEA,SAASA,EAAU,EAAW;QAC1B,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAACA;IAC3B;IAEA,QAAQ;QACJ,IAAI,CAAC,MAAM,CAAC,KAAK;IACrB;IAEQ,QAAQ;QACZ,MAAO,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,UAAU,CAAE;YACvC,MAAMI,SAAS,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,IAAI,GAAG,KAAK;YAC9C,IAAI,CAAC,MAAM,CAAC,MAAM,CAACA;QACvB;IACJ;AACJ;AAEO,MAAMC,uBAAuB,IAAIR"}
|
package/EditingSdk.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import type { ComponentGroup, IContentSdk, PublicPage } from "./types.js";
|
|
1
|
+
import type { ComponentGroup, Document, GetLayoutParams, IContentSdk, ListPagesOptions, ListPagesResult, PublicLayout, PublicPage } from "./types.js";
|
|
2
2
|
import { Messenger } from "./messenger/index.js";
|
|
3
|
+
import type { ComponentRegistry } from "./ComponentRegistry.js";
|
|
4
|
+
import type { DocumentStoreManager } from "./DocumentStoreManager.js";
|
|
3
5
|
import type { WebsiteBuilderTheme } from "./types/WebsiteBuilderTheme.js";
|
|
6
|
+
export type EditingSdkState = {
|
|
7
|
+
componentRegistry?: ComponentRegistry;
|
|
8
|
+
documentStoreManager?: DocumentStoreManager;
|
|
9
|
+
};
|
|
4
10
|
export declare class EditingSdk implements IContentSdk {
|
|
5
11
|
readonly messenger: Messenger;
|
|
6
12
|
private positionReportingEnabled;
|
|
@@ -8,10 +14,20 @@ export declare class EditingSdk implements IContentSdk {
|
|
|
8
14
|
private liveSdk;
|
|
9
15
|
private documentStore;
|
|
10
16
|
private previewDocument;
|
|
17
|
+
private elementSelection;
|
|
11
18
|
private lastBoxesHash;
|
|
12
|
-
constructor(liveSdk: IContentSdk, theme: WebsiteBuilderTheme);
|
|
19
|
+
constructor(liveSdk: IContentSdk, theme: WebsiteBuilderTheme, state?: EditingSdkState);
|
|
13
20
|
getPage(path: string): Promise<PublicPage | null>;
|
|
14
|
-
|
|
21
|
+
/**
|
|
22
|
+
* The document open in the editor, whatever its type (a page, a layout, ...).
|
|
23
|
+
*/
|
|
24
|
+
getEditedDocument(): Promise<Document | null>;
|
|
25
|
+
getLayout(params: GetLayoutParams): Promise<PublicLayout | null>;
|
|
26
|
+
listPages(options?: ListPagesOptions): Promise<ListPagesResult>;
|
|
27
|
+
/**
|
|
28
|
+
* Whether the element, or an element inside it, is selected in the editor. Observable.
|
|
29
|
+
*/
|
|
30
|
+
isElementSelected(id: string): boolean;
|
|
15
31
|
registerComponentGroup(group: ComponentGroup): void;
|
|
16
32
|
private getReferrerOrigin;
|
|
17
33
|
private setupListeners;
|