@wix/editor-platform-environment-api 1.38.0 → 1.39.0

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
- {"version":3,"file":"index.d.ts","sources":[],"sourcesContent":[],"names":[],"mappings":""}
1
+ {"version":3,"file":"index.d.ts","sources":["../../src/api/types.ts","../../src/api/AbstractEnvironmentAPI.ts","../../src/api/PlatformFrameAPI/PlatformFrameAPI.ts","../../src/api/PlatformWorkerAPI/PlatformWorkerAPI.ts","../../src/EditorPlatformEnvironmentProxy.ts","../../src/EditorPlatformContextEnvironment.ts"],"sourcesContent":["// TODO: replace with the real type WrappedPrivateAPI\nexport type IPrivateAPIFixMe = any;\n// TODO: fix any\nexport type IApplicationSpec = any;\n","import {\n ApplicationContext,\n EnvironmentContext,\n PlatformEnvironment,\n} from '@wix/editor-platform-contexts';\nimport { PlatformAppEventEmitter } from '@wix/public-editor-platform-events';\nimport { IPrivateAPIFixMe } from './types';\n\nexport enum PlatformConsumerEnvironmentAPIType {\n Frame = 'PLATFORM_FRAME_API',\n Worker = 'PLATFORM_WORKER_API',\n}\n\n/**\n * rename these entities -> API to Env\n */\nexport abstract class AbstractEnvironmentAPI<\n TApiType extends PlatformConsumerEnvironmentAPIType,\n TEnv extends PlatformEnvironment,\n> {\n protected constructor(\n public type: TApiType,\n public envType: TEnv,\n ) {}\n\n abstract initEnvironment(props: unknown): void;\n\n async injectEnvironmentContext({\n events,\n privateApi,\n }: {\n events: PlatformAppEventEmitter;\n privateApi: IPrivateAPIFixMe;\n applicationAPIs?: Record<string, any>;\n }) {\n await EnvironmentContext.inject({\n environment: this.envType,\n events,\n privateApi,\n applicationAPIs: {},\n });\n }\n\n async injectApplicationContext({\n appDefinitionId,\n }: {\n appDefinitionId: string;\n }) {\n await ApplicationContext.inject({\n appDefinitionId,\n // ?\n // anyway, it is not used atm\n appDefinitionName: '',\n });\n }\n}\n","import {\n IPlatformAppEvent,\n IPlatformPrivateEvent,\n PlatformAppEventEmitter,\n} from '@wix/public-editor-platform-events';\nimport { PlatformEnvironment } from '@wix/editor-platform-contexts';\n\nimport { ApplicationEventsBridge } from '../ApplicationEventsBridge';\nimport { IPrivateAPIFixMe } from '../types';\n\nimport {\n AbstractEnvironmentAPI,\n PlatformConsumerEnvironmentAPIType,\n} from '../AbstractEnvironmentAPI';\n\nconst DESIGN_SYSTEM_STYLES_MAP = {\n classic:\n 'https://www.unpkg.com/@wix/design-system/dist/statics/tokens-default.global.css',\n studio:\n 'https://www.unpkg.com/@wix/design-system/dist/statics/tokens-studio.global.css',\n};\n\nexport class PlatformFrameAPI extends AbstractEnvironmentAPI<\n PlatformConsumerEnvironmentAPIType.Frame,\n PlatformEnvironment.Frame\n> {\n #events = new PlatformAppEventEmitter();\n #eventsBridge = new ApplicationEventsBridge(this.#events);\n #privateAPI: IPrivateAPIFixMe;\n #applicationPrivateAPI: any;\n\n constructor() {\n super(PlatformConsumerEnvironmentAPIType.Frame, PlatformEnvironment.Frame);\n\n this.#injectGlobalCSSTokens();\n }\n\n #injectGlobalCSSTokens() {\n if (typeof globalThis?.document?.head?.prepend === 'function') {\n const params = new URL(globalThis.location.href).searchParams;\n\n // TODO: check all possible editorType values\n const host =\n params.get('editorType') === 'CLASSIC' ? 'classic' : 'studio';\n\n const url = DESIGN_SYSTEM_STYLES_MAP[host];\n\n const isAlreadyLoaded =\n url &&\n !!document.querySelectorAll(`link[type=\"text/css\"][href=\"${url}\"]`)\n ?.length;\n\n if (url && !isAlreadyLoaded) {\n const link: HTMLLinkElement = document.createElement('link');\n\n link.setAttribute('rel', 'stylesheet');\n link.setAttribute('type', 'text/css');\n link.setAttribute('href', url);\n\n globalThis.document.head.prepend(link);\n }\n }\n }\n\n async initEnvironment(props: {\n appDefinitionId: string;\n privateAPI: IPrivateAPIFixMe;\n applicationPrivateAPI: any;\n }) {\n const { applicationPrivateAPI, privateAPI, appDefinitionId } = props;\n\n this.#applicationPrivateAPI = applicationPrivateAPI;\n this.#privateAPI = privateAPI;\n\n await this.injectEnvironmentContext({\n privateApi: privateAPI,\n events: this.#events,\n applicationAPIs: {\n [appDefinitionId]: this.#applicationPrivateAPI,\n },\n });\n\n await this.injectApplicationContext({\n appDefinitionId,\n });\n }\n\n notify(event: IPlatformPrivateEvent) {\n this.#eventsBridge.notify(event);\n }\n\n subscribe(cb: (event: IPlatformAppEvent) => void) {\n this.#eventsBridge.subscribe(cb);\n }\n}\n","import { IApplicationSpec, IPrivateAPIFixMe } from '../types';\nimport {\n IPlatformPrivateEvent,\n PlatformAppEventEmitter,\n IPlatformAppEvent,\n} from '@wix/public-editor-platform-events';\nimport { PlatformEnvironment } from '@wix/editor-platform-contexts';\n\nimport { ApplicationEventsBridge } from '../ApplicationEventsBridge';\n\nimport { PlatformApplicationContainer } from './PlatformApplicationContainer';\nimport {\n AbstractEnvironmentAPI,\n PlatformConsumerEnvironmentAPIType,\n} from '../AbstractEnvironmentAPI';\n\nexport class PlatformWorkerAPI extends AbstractEnvironmentAPI<\n PlatformConsumerEnvironmentAPIType.Worker,\n PlatformEnvironment.Worker\n> {\n #events = new PlatformAppEventEmitter();\n\n #eventsBridge = new ApplicationEventsBridge(this.#events);\n #container: PlatformApplicationContainer | null = null;\n #privateAPI: IPrivateAPIFixMe;\n\n #pendingWaiters: ((value: this) => void)[] = [];\n\n constructor() {\n super(\n PlatformConsumerEnvironmentAPIType.Worker,\n PlatformEnvironment.Worker,\n );\n }\n\n create() {}\n\n async initEnvironment(props: {\n // TODO: should be Promise response in types\n buildPrivateAPI: (config: any) => IPrivateAPIFixMe;\n }) {\n const { buildPrivateAPI } = props;\n this.#privateAPI = await buildPrivateAPI({\n // TODO: should be per application (within the container before app execution)\n type: 'EDITOR_ADDON',\n });\n\n await this.injectEnvironmentContext({\n events: this.#events,\n privateApi: this.#privateAPI,\n applicationAPIs: {},\n });\n\n this.#container = new PlatformApplicationContainer(\n this.#privateAPI,\n this.#events,\n );\n\n this.#pendingWaiters.forEach((res) => res(this));\n }\n\n async notify(event: IPlatformPrivateEvent) {\n await this.waitReady();\n\n this.#eventsBridge.notify(event);\n }\n\n subscribe(cb: (event: IPlatformAppEvent) => void) {\n this.#eventsBridge.subscribe(cb);\n }\n\n async runApplication(app: IApplicationSpec) {\n await this.waitReady();\n\n await this.#container!.runApplication(app);\n }\n\n // TODO: should not be any waiters here\n // or should be implemented inside the events instances\n // collect queue until app is not ready\n public waitReady(): Promise<this> {\n return new Promise((res) => {\n if (this.#privateAPI) {\n return res(this);\n }\n\n this.#pendingWaiters.push(res);\n });\n }\n}\n","import { WorkerHostEndpoint } from '@wix/editor-platform-transport';\nimport { EditorPlatformContextEnvironment } from './EditorPlatformContextEnvironment';\nimport {\n createEditorPlatformEnvironmentAPIError,\n EditorPlatformEnvironmentAPIErrorErrorCode,\n} from './errors';\nimport {\n createEditorPlatformInternalError,\n EditorPlatformInternalErrorCode,\n} from '@wix/public-editor-platform-errors';\nimport { PlatformConsumerEnvironmentAPIType } from './api';\n\nexport class EditorPlatformEnvironmentProxy {\n async iframe() {\n throw createEditorPlatformInternalError(\n EditorPlatformInternalErrorCode.UnexpectedError,\n 'not supported',\n );\n }\n\n async worker(worker: Worker) {\n const environment = new EditorPlatformContextEnvironment();\n\n if (!environment.isEnvironmentApiExposed()) {\n environment.expose();\n }\n\n const connectTimeoutId = setTimeout(() => {\n console.info(\n createEditorPlatformEnvironmentAPIError(\n EditorPlatformEnvironmentAPIErrorErrorCode.ProxyEnvironmentConnectTimeout,\n 'probably worker environment does not expose platform API or any SDK function is never used',\n ),\n );\n }, 2_000);\n\n WorkerHostEndpoint.connect(worker, async (transport) => {\n clearTimeout(connectTimeoutId);\n\n const waitAPITimeoutId = setTimeout(() => {\n console.error(\n createEditorPlatformEnvironmentAPIError(\n EditorPlatformEnvironmentAPIErrorErrorCode.ProxyWaitAPITimeout,\n ),\n );\n }, 2_000);\n\n const workerApi = await transport.waitAPI(\n PlatformConsumerEnvironmentAPIType.Worker,\n );\n\n clearTimeout(waitAPITimeoutId);\n\n try {\n const contexts = await environment.getContexts();\n\n await workerApi.injectEnvironmentContext({\n events: contexts.environmentContext.getEvents(),\n privateApi: contexts.environmentContext.getPrivateAPI(),\n applicationAPIs: contexts.environmentContext.getApplicationAPIs(),\n });\n\n await workerApi.injectApplicationContext({\n appDefinitionId: contexts.applicationContext.getAppDefinitionId(),\n });\n } catch (e: any) {\n throw createEditorPlatformEnvironmentAPIError(\n EditorPlatformEnvironmentAPIErrorErrorCode.ProxyContextsError,\n ).withParentError(e);\n }\n });\n }\n}\n","import {\n IFrameConsumerEndpoint,\n WorkerConsumerEndpoint,\n} from '@wix/editor-platform-transport';\n\nimport {\n ApplicationContext,\n EnvironmentContext,\n} from '@wix/editor-platform-contexts';\nimport { PlatformWorkerAPI, PlatformFrameAPI } from './api';\nimport {\n createEditorPlatformEnvironmentAPIError,\n EditorPlatformEnvironmentAPIErrorErrorCode,\n} from './errors';\n\nconst GUARD_PROP = '$_EP_TRANSPORT_ENV_CONTEXT_GUARD';\n\ndeclare global {\n // eslint-disable-next-line no-var\n var $_EP_TRANSPORT_ENV_CONTEXT_GUARD: boolean;\n}\n\nexport class EditorPlatformContextEnvironment {\n isEnvironmentApiExposed() {\n return !!globalThis[GUARD_PROP];\n }\n\n async getContexts() {\n const [environmentContext, applicationContext] = await Promise.all([\n EnvironmentContext.getInstance(),\n ApplicationContext.getInstance(),\n ]);\n\n return {\n environmentContext,\n applicationContext,\n };\n }\n\n expose() {\n if (this.isEnvironmentApiExposed()) {\n return;\n }\n\n const isWorker = typeof importScripts === 'function';\n const isIframe =\n globalThis.self && globalThis.top && globalThis.self !== globalThis.top;\n\n /**\n * TODO: we can try to split this code somehow\n * and load the only API according to the current env.\n */\n if (isWorker) {\n this.#exposeWorkerAPI();\n globalThis[GUARD_PROP] = true;\n } else if (isIframe) {\n this.#exposeFrameAPI();\n globalThis[GUARD_PROP] = true;\n } else {\n /**\n * we expose env APIs only from worker or frame\n * but we have some cases when our code runs in the main thread\n * for example – builder component panels.\n */\n // if (!shouldPreventErrorLogs) {\n // console.error(\n // createEditorPlatformEnvironmentAPIError(\n // EditorPlatformEnvironmentAPIErrorErrorCode.IncorrectExpose,\n // 'seems current code is running in the main thread, while it is expected to be in iframe or worker',\n // ),\n // );\n // }\n }\n }\n\n #exposeWorkerAPI() {\n WorkerConsumerEndpoint.exposeAPI(new PlatformWorkerAPI());\n }\n\n #exposeFrameAPI() {\n IFrameConsumerEndpoint.exposeAPI(new PlatformFrameAPI());\n }\n}\n"],"names":[],"mappings":";;;;AAAO;AACA;;ACEA;AACP;AACA;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;ACnBO;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;ACVO;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;ACfO;AACP;AACA;AACA;;ACFA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/editor-platform-environment-api",
3
- "version": "1.38.0",
3
+ "version": "1.39.0",
4
4
  "description": "",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -29,16 +29,16 @@
29
29
  "eslint": "^8.57.1",
30
30
  "prettier": "^3.5.3",
31
31
  "rollup": "^3.29.5",
32
- "rollup-plugin-dts": "^6.1.1",
32
+ "rollup-plugin-dts": "^6.2.1",
33
33
  "rollup-plugin-esbuild": "^5.0.0",
34
34
  "typescript": "^5.8.2"
35
35
  },
36
36
  "dependencies": {
37
- "@wix/editor-application": "1.38.0",
38
- "@wix/editor-platform-contexts": "1.38.0",
39
- "@wix/editor-platform-transport": "1.13.0",
37
+ "@wix/editor-application": "1.39.0",
38
+ "@wix/editor-platform-contexts": "1.39.0",
39
+ "@wix/editor-platform-transport": "1.14.0",
40
40
  "@wix/public-editor-platform-errors": "1.8.0",
41
- "@wix/public-editor-platform-events": "1.309.0",
41
+ "@wix/public-editor-platform-events": "1.310.0",
42
42
  "@wix/public-editor-platform-interfaces": "1.20.0"
43
43
  },
44
44
  "publishConfig": {
@@ -63,5 +63,5 @@
63
63
  ]
64
64
  }
65
65
  },
66
- "falconPackageHash": "4a279b9e0966b7eb2cd89a9885d8cf8db99def2bae60bde5ad6b2b89"
66
+ "falconPackageHash": "f6a9634495e02a01cdbe2d4808d2e51b1337ca7ef605ac5cc3e42694"
67
67
  }