@wix/editor-platform-contexts 1.0.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.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ tbd
@@ -0,0 +1,278 @@
1
+ 'use strict';
2
+
3
+ var publicEditorPlatformErrors = require('@wix/public-editor-platform-errors');
4
+ var publicEditorPlatformEvents = require('@wix/public-editor-platform-events');
5
+
6
+ var EditorPlatformContextErrorCode = /* @__PURE__ */ ((EditorPlatformContextErrorCode2) => {
7
+ EditorPlatformContextErrorCode2["IncorrectEnvironment"] = "IncorrectEnvironment";
8
+ return EditorPlatformContextErrorCode2;
9
+ })(EditorPlatformContextErrorCode || {});
10
+ class EditorPlatformContextError extends publicEditorPlatformErrors.BaseError {
11
+ state = {};
12
+ constructor(message, code) {
13
+ super(message, code, "EP Context Error");
14
+ }
15
+ withUrl(url) {
16
+ this.state = { ...this.state, url };
17
+ return this;
18
+ }
19
+ withAppDefinitionId(appDefinitionId) {
20
+ this.state = { ...this.state, appDefinitionId };
21
+ return this;
22
+ }
23
+ }
24
+ const createEditorPlatformContextError = publicEditorPlatformErrors.createErrorBuilder(EditorPlatformContextError);
25
+
26
+ async function transformEventPayload(eventPayload, privateAPI) {
27
+ if (!eventPayload?.type) {
28
+ return eventPayload;
29
+ }
30
+ switch (eventPayload.type) {
31
+ case "componentSelectionChanged":
32
+ const componentRefs = eventPayload.componentRefs || [];
33
+ const components = await Promise.all(
34
+ componentRefs.map((ref) => {
35
+ return privateAPI.components.getComponent(ref);
36
+ })
37
+ );
38
+ return {
39
+ type: eventPayload.type,
40
+ components
41
+ };
42
+ default:
43
+ return eventPayload;
44
+ }
45
+ }
46
+
47
+ class ApplicationBoundEvents {
48
+ constructor(appDefinitionId, events, privateAPI) {
49
+ this.appDefinitionId = appDefinitionId;
50
+ this.privateAPI = privateAPI;
51
+ this.events = events;
52
+ this.subscribe = events.subscribe.bind(events);
53
+ this.commit = events.commit.bind(events);
54
+ this.startTransaction = events.startTransaction.bind(events);
55
+ this.silent = events.silent.bind(events);
56
+ }
57
+ events;
58
+ subscribe;
59
+ commit;
60
+ startTransaction;
61
+ silent;
62
+ notify(event) {
63
+ this.events.notify({
64
+ type: event.type,
65
+ payload: event.payload,
66
+ meta: {
67
+ appDefinitionId: this.appDefinitionId
68
+ }
69
+ });
70
+ }
71
+ notifyCustomEvent(type, payload) {
72
+ this.notify({
73
+ type: publicEditorPlatformEvents.PlatformAppEvent.CustomEvent,
74
+ payload: {
75
+ ...payload,
76
+ type
77
+ }
78
+ });
79
+ }
80
+ /**
81
+ * TODO: we should use same interface for all events
82
+ * (subscribe vs addEventListener)
83
+ */
84
+ addEventListener(eventType, fn) {
85
+ return this.events.subscribe(async (event) => {
86
+ const isAppMatch = event.meta?.appDefinitionId === this.appDefinitionId || event.meta?.appDefinitionId === null;
87
+ const transformPayload = () => transformEventPayload(event.payload, this.privateAPI);
88
+ if (eventType === "*") {
89
+ fn(await transformPayload());
90
+ } else if (event.type === publicEditorPlatformEvents.PlatformAppEvent.CustomEvent) {
91
+ if (eventType === event.payload?.type && !isAppMatch) {
92
+ fn(await transformPayload());
93
+ }
94
+ } else if (event.type === publicEditorPlatformEvents.PlatformAppEvent.HostEvent) {
95
+ if (eventType === event.payload?.type && isAppMatch) {
96
+ fn(await transformPayload());
97
+ }
98
+ }
99
+ });
100
+ }
101
+ }
102
+
103
+ const WAIT_INJECTED_TIMEOUT = 200;
104
+ const WAIT_INJECTED_RETRY_COUNT = 50;
105
+ class ContextInjectionStatus {
106
+ _resolveContextInjected = () => {
107
+ };
108
+ _isInjected = false;
109
+ key;
110
+ constructor(uuid) {
111
+ this.key = `__${uuid}_CONTEXT_INJECTION_STATUS_KEY`;
112
+ if (!globalThis[this.key]) {
113
+ globalThis[this.key] = new Promise((resolve) => {
114
+ this._resolveContextInjected = () => {
115
+ this._isInjected = true;
116
+ resolve();
117
+ };
118
+ });
119
+ }
120
+ }
121
+ isInjected() {
122
+ return !!this._isInjected;
123
+ }
124
+ resolveInjected() {
125
+ this._resolveContextInjected?.();
126
+ }
127
+ waitInjected() {
128
+ return new Promise((resolve, reject) => {
129
+ let injected = false;
130
+ let timeoutId;
131
+ let retryCount = 0;
132
+ const timeout = () => {
133
+ if (injected) {
134
+ return;
135
+ }
136
+ timeoutId = setTimeout(() => {
137
+ retryCount++;
138
+ if (retryCount < WAIT_INJECTED_RETRY_COUNT) {
139
+ if (retryCount % 10 === 0) {
140
+ console.log(
141
+ createEditorPlatformContextError(
142
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
143
+ "contexts are not resolved, still re-trying"
144
+ ).withMessage(`try number ${retryCount}`).message
145
+ );
146
+ }
147
+ timeout();
148
+ return;
149
+ }
150
+ if (!injected) {
151
+ const error = createEditorPlatformContextError(
152
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
153
+ "contexts are not resolved, threw by timeout"
154
+ );
155
+ reject(error);
156
+ }
157
+ }, WAIT_INJECTED_TIMEOUT);
158
+ };
159
+ timeout();
160
+ const _waitContextInjectedPromise = globalThis[this.key];
161
+ _waitContextInjectedPromise.then(() => {
162
+ injected = true;
163
+ clearTimeout(timeoutId);
164
+ resolve();
165
+ });
166
+ });
167
+ }
168
+ }
169
+
170
+ const ENVIRONMENT_CONTEXT_KEY = "__ENVIRONMENT_CONTEXT_KEY";
171
+ var PlatformEnvironment = /* @__PURE__ */ ((PlatformEnvironment2) => {
172
+ PlatformEnvironment2["Worker"] = "Worker";
173
+ PlatformEnvironment2["Frame"] = "Frame";
174
+ PlatformEnvironment2["ComponentPanel"] = "ComponentPanel";
175
+ return PlatformEnvironment2;
176
+ })(PlatformEnvironment || {});
177
+ class EnvironmentContext {
178
+ constructor(environmentContext) {
179
+ this.environmentContext = environmentContext;
180
+ }
181
+ static status = new ContextInjectionStatus("environment");
182
+ static async inject(context) {
183
+ if (globalThis[ENVIRONMENT_CONTEXT_KEY]) {
184
+ throw createEditorPlatformContextError(
185
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
186
+ "Environment context already exists and should not be overridden"
187
+ );
188
+ }
189
+ globalThis[ENVIRONMENT_CONTEXT_KEY] = new EnvironmentContext(context);
190
+ this.status.resolveInjected();
191
+ }
192
+ static async getInstance() {
193
+ await this.status.waitInjected();
194
+ return globalThis[ENVIRONMENT_CONTEXT_KEY];
195
+ }
196
+ getPrivateAPI() {
197
+ return this.environmentContext.privateApi;
198
+ }
199
+ getEvents() {
200
+ return this.environmentContext.events;
201
+ }
202
+ getApplicationAPIs() {
203
+ return this.environmentContext.applicationAPIs ?? {};
204
+ }
205
+ getEnvironment() {
206
+ return this.environmentContext.environment;
207
+ }
208
+ }
209
+
210
+ const APPLICATION_CONTEXT_KEY = "__APPLICATION_CONTEXT_KEY";
211
+ class ApplicationContext {
212
+ constructor(applicationContext, environment) {
213
+ this.applicationContext = applicationContext;
214
+ this.environment = environment;
215
+ this.events = new ApplicationBoundEvents(
216
+ this.applicationContext.appDefinitionId,
217
+ this.environment.getEvents(),
218
+ this.environment.getPrivateAPI()
219
+ );
220
+ }
221
+ static status = new ContextInjectionStatus("application");
222
+ /**
223
+ * TODO: use generics for context type
224
+ * - application
225
+ * - editor
226
+ */
227
+ static async inject(context) {
228
+ if (globalThis[APPLICATION_CONTEXT_KEY]) {
229
+ throw createEditorPlatformContextError(
230
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
231
+ "Application context already exists and should not be overridden"
232
+ );
233
+ }
234
+ globalThis[APPLICATION_CONTEXT_KEY] = new ApplicationContext(
235
+ context,
236
+ await EnvironmentContext.getInstance()
237
+ );
238
+ this.status.resolveInjected();
239
+ }
240
+ static async getInstance() {
241
+ if (typeof __APPLICATION_CONTEXT_KEY !== "undefined") {
242
+ return __APPLICATION_CONTEXT_KEY;
243
+ }
244
+ await this.status.waitInjected();
245
+ return globalThis[APPLICATION_CONTEXT_KEY];
246
+ }
247
+ events;
248
+ getAppDefinitionId() {
249
+ return this.applicationContext.appDefinitionId;
250
+ }
251
+ getBindings() {
252
+ return this.applicationContext;
253
+ }
254
+ getEvents() {
255
+ return this.events;
256
+ }
257
+ getPrivateAPI() {
258
+ return this.environment.getPrivateAPI();
259
+ }
260
+ getPrivateApplicationAPI() {
261
+ const appDefinitionId = this.getAppDefinitionId();
262
+ if (!appDefinitionId) {
263
+ throw createEditorPlatformContextError(
264
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
265
+ "appDefinitionId is not available"
266
+ );
267
+ }
268
+ return this.environment.getApplicationAPIs()[appDefinitionId];
269
+ }
270
+ }
271
+
272
+ exports.APPLICATION_CONTEXT_KEY = APPLICATION_CONTEXT_KEY;
273
+ exports.ApplicationBoundEvents = ApplicationBoundEvents;
274
+ exports.ApplicationContext = ApplicationContext;
275
+ exports.ENVIRONMENT_CONTEXT_KEY = ENVIRONMENT_CONTEXT_KEY;
276
+ exports.EnvironmentContext = EnvironmentContext;
277
+ exports.PlatformEnvironment = PlatformEnvironment;
278
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/errors.ts","../../src/ApplicationBoundEvents/transformEventPayload.ts","../../src/ApplicationBoundEvents/ApplicationBoundEvents.ts","../../src/contexts/ContextInjectionStatus.ts","../../src/contexts/EnvironmentContext.ts","../../src/contexts/ApplicationContext.ts"],"sourcesContent":["import {\n BaseError,\n createErrorBuilder,\n} from '@wix/public-editor-platform-errors';\n\nexport enum EditorPlatformContextErrorCode {\n IncorrectEnvironment = 'IncorrectEnvironment',\n}\n\nclass EditorPlatformContextError extends BaseError<EditorPlatformContextErrorCode> {\n state: Partial<{\n url: string;\n appDefinitionId: string;\n }> = {};\n\n constructor(message: string, code: EditorPlatformContextErrorCode) {\n super(message, code, 'EP Context Error');\n }\n\n withUrl(url: string) {\n this.state = { ...this.state, url };\n return this;\n }\n\n withAppDefinitionId(appDefinitionId: string) {\n this.state = { ...this.state, appDefinitionId };\n return this;\n }\n}\n\nexport const createEditorPlatformContextError = createErrorBuilder<\n EditorPlatformContextErrorCode,\n EditorPlatformContextError\n>(EditorPlatformContextError);\n","import type { ComponentRef } from '@wix/public-editor-platform-interfaces';\nimport {\n AllowedEvents,\n AppEventPayload,\n IComponent,\n PrivateAPIFixMe,\n} from './types';\n\nexport async function transformEventPayload<T extends AllowedEvents>(\n eventPayload: Record<string, string>,\n privateAPI: PrivateAPIFixMe,\n): Promise<AppEventPayload<T>> {\n if (!eventPayload?.type) {\n return eventPayload as AppEventPayload<T>;\n }\n\n switch (eventPayload.type) {\n case 'componentSelectionChanged':\n const componentRefs = (eventPayload.componentRefs ||\n []) as unknown as ComponentRef[];\n\n const components = (await Promise.all(\n componentRefs.map((ref) => {\n return privateAPI.components.getComponent(ref);\n }),\n )) as IComponent[];\n\n return {\n type: eventPayload.type,\n components,\n } as AppEventPayload<T>;\n\n default:\n return eventPayload as AppEventPayload<T>;\n }\n}\n","import {\n PlatformAppEvent,\n PlatformAppEventEmitter,\n IPlatformAppEvent,\n} from '@wix/public-editor-platform-events';\nimport { AllowedEvents, AppEventPayload, PrivateAPIFixMe } from './types';\nimport { transformEventPayload } from './transformEventPayload';\n\nexport type OmitEventMeta<T> = T extends { meta: any } ? Omit<T, 'meta'> : T;\n\n/**\n * TODO: should not be in this package\n */\nexport class ApplicationBoundEvents {\n private events: PlatformAppEventEmitter;\n public subscribe;\n public commit;\n public startTransaction;\n public silent;\n\n constructor(\n private appDefinitionId: string,\n events: PlatformAppEventEmitter,\n private privateAPI: PrivateAPIFixMe,\n ) {\n this.events = events;\n this.subscribe = events.subscribe.bind(events);\n this.commit = events.commit.bind(events);\n this.startTransaction = events.startTransaction.bind(events);\n this.silent = events.silent.bind(events);\n }\n\n public notify(event: OmitEventMeta<IPlatformAppEvent>) {\n this.events.notify({\n type: event.type,\n payload: event.payload,\n meta: {\n appDefinitionId: this.appDefinitionId,\n },\n } as IPlatformAppEvent);\n }\n\n notifyCustomEvent(type: string, payload: Record<string, string>) {\n this.notify({\n type: PlatformAppEvent.CustomEvent,\n payload: {\n ...payload,\n type,\n },\n });\n }\n\n /**\n * TODO: we should use same interface for all events\n * (subscribe vs addEventListener)\n */\n public addEventListener<EventType extends AllowedEvents>(\n eventType: EventType,\n fn: (payload: AppEventPayload<EventType>) => void,\n ) {\n return this.events.subscribe(async (event) => {\n const isAppMatch =\n event.meta?.appDefinitionId === this.appDefinitionId ||\n event.meta?.appDefinitionId === null;\n\n const transformPayload = () =>\n transformEventPayload<EventType>(event.payload, this.privateAPI);\n\n // as an exception to skip autocomplete\n if ((eventType as string) === '*') {\n fn(await transformPayload());\n /**\n * We need to check for app match here to not listen app's own events\n */\n } else if (event.type === PlatformAppEvent.CustomEvent) {\n if (eventType === event.payload?.type && !isAppMatch) {\n fn(await transformPayload());\n }\n } else if (event.type === PlatformAppEvent.HostEvent) {\n if (eventType === event.payload?.type && isAppMatch) {\n fn(await transformPayload());\n }\n }\n });\n }\n}\n","import {\n createEditorPlatformContextError,\n EditorPlatformContextErrorCode,\n} from '../errors';\n\nconst WAIT_INJECTED_TIMEOUT = 200;\nconst WAIT_INJECTED_RETRY_COUNT = 50;\n\nexport class ContextInjectionStatus {\n private _resolveContextInjected = () => {};\n private _isInjected = false;\n private key: string;\n\n constructor(uuid: string) {\n this.key = `__${uuid}_CONTEXT_INJECTION_STATUS_KEY`;\n\n // @ts-expect-error xxx\n if (!globalThis[this.key]) {\n // @ts-expect-error xxx\n globalThis[this.key] = new Promise<void>((resolve) => {\n this._resolveContextInjected = () => {\n this._isInjected = true;\n resolve();\n };\n });\n }\n }\n\n isInjected() {\n return !!this._isInjected;\n }\n\n resolveInjected() {\n this._resolveContextInjected?.();\n }\n\n waitInjected() {\n return new Promise<void>((resolve, reject) => {\n let injected = false;\n let timeoutId: ReturnType<typeof setTimeout>;\n let retryCount = 0;\n\n const timeout = () => {\n if (injected) {\n return;\n }\n\n timeoutId = setTimeout(() => {\n retryCount++;\n\n if (retryCount < WAIT_INJECTED_RETRY_COUNT) {\n /**\n * just log that we are re-trying to connect (each 2 sec+-)\n */\n if (retryCount % 10 === 0) {\n console.log(\n createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'contexts are not resolved, still re-trying',\n ).withMessage(`try number ${retryCount}`).message,\n );\n }\n\n timeout();\n\n return;\n }\n\n if (!injected) {\n const error = createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'contexts are not resolved, threw by timeout',\n );\n\n reject(error);\n }\n }, WAIT_INJECTED_TIMEOUT);\n };\n\n timeout();\n\n // @ts-expect-error xxx\n const _waitContextInjectedPromise = globalThis[this.key];\n _waitContextInjectedPromise.then(() => {\n injected = true;\n clearTimeout(timeoutId);\n resolve();\n });\n });\n }\n}\n","import { PlatformAppEventEmitter } from '@wix/public-editor-platform-events';\nimport { WithPromiseFunctions } from '@wix/editor-platform-transport';\nimport {\n createEditorPlatformContextError,\n EditorPlatformContextErrorCode,\n} from '../errors';\nimport { ContextInjectionStatus } from './ContextInjectionStatus';\n\nexport type IPrivateAPIFixMe = any; // TODO: replace with the real type\n\nexport const ENVIRONMENT_CONTEXT_KEY = '__ENVIRONMENT_CONTEXT_KEY';\n\ndeclare global {\n // eslint-disable-next-line no-var\n var __ENVIRONMENT_CONTEXT_KEY: EnvironmentContext;\n}\n\nexport enum PlatformEnvironment {\n Worker = 'Worker',\n Frame = 'Frame',\n // TODO - rename to MainThread\n ComponentPanel = 'ComponentPanel',\n}\n\nexport interface IEnvironmentContext {\n environment: PlatformEnvironment;\n privateApi: WithPromiseFunctions<IPrivateAPIFixMe>;\n events: PlatformAppEventEmitter;\n /**\n * Probably, we need to rename this prop\n */\n applicationAPIs?: Record<string, any>;\n}\n\nexport class EnvironmentContext {\n private static status = new ContextInjectionStatus('environment');\n\n static async inject(context: IEnvironmentContext) {\n if (globalThis[ENVIRONMENT_CONTEXT_KEY]) {\n throw createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'Environment context already exists and should not be overridden',\n );\n }\n\n globalThis[ENVIRONMENT_CONTEXT_KEY] = new EnvironmentContext(context);\n this.status.resolveInjected();\n }\n\n static async getInstance(): Promise<EnvironmentContext> {\n await this.status.waitInjected();\n return globalThis[ENVIRONMENT_CONTEXT_KEY];\n }\n\n constructor(private environmentContext: IEnvironmentContext) {}\n\n getPrivateAPI() {\n return this.environmentContext.privateApi;\n }\n\n getEvents() {\n return this.environmentContext.events;\n }\n\n getApplicationAPIs() {\n return this.environmentContext.applicationAPIs ?? {};\n }\n\n getEnvironment() {\n return this.environmentContext.environment;\n }\n}\n","import {\n createEditorPlatformContextError,\n EditorPlatformContextErrorCode,\n} from '../errors';\n\nimport { ApplicationBoundEvents } from '../ApplicationBoundEvents';\nimport { EnvironmentContext, PlatformEnvironment } from './EnvironmentContext';\nimport { ContextInjectionStatus } from './ContextInjectionStatus';\n\nexport const APPLICATION_CONTEXT_KEY = '__APPLICATION_CONTEXT_KEY';\ndeclare global {\n // eslint-disable-next-line no-var\n var __APPLICATION_CONTEXT_KEY: ApplicationContext<IApplicationContext>;\n}\n\nexport type IEditorApplicationContext = {\n appDefinitionId: string;\n};\n\nexport type IAddonContext = {\n appDefinitionId: string;\n appDefinitionName: string;\n data: {\n toolPanelConfig: {\n url: string;\n width: string;\n height: string;\n initialPosition: {\n x: string;\n y: string;\n };\n };\n };\n};\n\nexport type IApplicationContext = IEditorApplicationContext | IAddonContext;\nexport class ApplicationContext<TContext extends IApplicationContext> {\n private static status = new ContextInjectionStatus('application');\n\n /**\n * TODO: use generics for context type\n * - application\n * - editor\n */\n static async inject<TContext extends IApplicationContext>(context: TContext) {\n // const environment = await EnvironmentContext.getInstance();\n\n // if (environment.getEnvironment() !== PlatformEnvironment.Frame) {\n // throw createEditorPlatformContextError(\n // EditorPlatformContextErrorCode.IncorrectEnvironment,\n // 'Application context can be injected only in frame environment',\n // );\n // }\n\n if (globalThis[APPLICATION_CONTEXT_KEY]) {\n throw createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'Application context already exists and should not be overridden',\n );\n }\n\n globalThis[APPLICATION_CONTEXT_KEY] = new ApplicationContext(\n context,\n await EnvironmentContext.getInstance(),\n );\n\n this.status.resolveInjected();\n }\n\n static async getInstance<TContext extends IApplicationContext>() {\n if (typeof __APPLICATION_CONTEXT_KEY !== 'undefined') {\n /**\n * NOTE it is taken not from global\n * __APPLICATION_CONTEXT_KEY is the variable which set during\n * application bundle execution using new Function() approach\n */\n return __APPLICATION_CONTEXT_KEY as ApplicationContext<TContext>;\n }\n\n await this.status.waitInjected();\n\n return globalThis[APPLICATION_CONTEXT_KEY] as ApplicationContext<TContext>;\n\n // const environment = await EnvironmentContext.getInstance();\n\n // if (environment.getEnvironment() === PlatformEnvironment.Frame) {\n // await this.status.waitInjected();\n // return globalThis[\n // APPLICATION_CONTEXT_KEY\n // ] as ApplicationContext<TContext>;\n // } else {\n // /**\n // * NOTE it is taken not from global\n // * __APPLICATION_CONTEXT_KEY is the variable which set during\n // * application bundle execution using new Function() approach\n // */\n // return __APPLICATION_CONTEXT_KEY as ApplicationContext<TContext>;\n // }\n }\n\n private events: ApplicationBoundEvents;\n\n constructor(\n private applicationContext: TContext,\n private environment: EnvironmentContext,\n ) {\n this.events = new ApplicationBoundEvents(\n this.applicationContext.appDefinitionId,\n this.environment.getEvents(),\n this.environment.getPrivateAPI(),\n );\n }\n\n getAppDefinitionId() {\n return this.applicationContext.appDefinitionId;\n }\n\n getBindings() {\n return this.applicationContext;\n }\n\n getEvents() {\n return this.events;\n }\n\n getPrivateAPI() {\n return this.environment.getPrivateAPI();\n }\n\n getPrivateApplicationAPI() {\n const appDefinitionId = this.getAppDefinitionId();\n\n if (!appDefinitionId) {\n throw createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'appDefinitionId is not available',\n );\n }\n\n return this.environment.getApplicationAPIs()[appDefinitionId];\n }\n}\n"],"names":["EditorPlatformContextErrorCode","BaseError","createErrorBuilder","PlatformAppEvent","PlatformEnvironment"],"mappings":";;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8BAA+B,CAAA,oBAAA;AAAA,QAC/B,kCAAA;AAAA,OACF,CAAA;AAAA,KACF;AAEA,IAAA,OAAO,IAAK,CAAA,WAAA,CAAY,kBAAmB,EAAA,CAAE,eAAe,CAAA,CAAA;AAAA,GAC9D;AACF;;;;;;;;;"}
@@ -0,0 +1,271 @@
1
+ import { createErrorBuilder, BaseError } from '@wix/public-editor-platform-errors';
2
+ import { PlatformAppEvent } from '@wix/public-editor-platform-events';
3
+
4
+ var EditorPlatformContextErrorCode = /* @__PURE__ */ ((EditorPlatformContextErrorCode2) => {
5
+ EditorPlatformContextErrorCode2["IncorrectEnvironment"] = "IncorrectEnvironment";
6
+ return EditorPlatformContextErrorCode2;
7
+ })(EditorPlatformContextErrorCode || {});
8
+ class EditorPlatformContextError extends BaseError {
9
+ state = {};
10
+ constructor(message, code) {
11
+ super(message, code, "EP Context Error");
12
+ }
13
+ withUrl(url) {
14
+ this.state = { ...this.state, url };
15
+ return this;
16
+ }
17
+ withAppDefinitionId(appDefinitionId) {
18
+ this.state = { ...this.state, appDefinitionId };
19
+ return this;
20
+ }
21
+ }
22
+ const createEditorPlatformContextError = createErrorBuilder(EditorPlatformContextError);
23
+
24
+ async function transformEventPayload(eventPayload, privateAPI) {
25
+ if (!eventPayload?.type) {
26
+ return eventPayload;
27
+ }
28
+ switch (eventPayload.type) {
29
+ case "componentSelectionChanged":
30
+ const componentRefs = eventPayload.componentRefs || [];
31
+ const components = await Promise.all(
32
+ componentRefs.map((ref) => {
33
+ return privateAPI.components.getComponent(ref);
34
+ })
35
+ );
36
+ return {
37
+ type: eventPayload.type,
38
+ components
39
+ };
40
+ default:
41
+ return eventPayload;
42
+ }
43
+ }
44
+
45
+ class ApplicationBoundEvents {
46
+ constructor(appDefinitionId, events, privateAPI) {
47
+ this.appDefinitionId = appDefinitionId;
48
+ this.privateAPI = privateAPI;
49
+ this.events = events;
50
+ this.subscribe = events.subscribe.bind(events);
51
+ this.commit = events.commit.bind(events);
52
+ this.startTransaction = events.startTransaction.bind(events);
53
+ this.silent = events.silent.bind(events);
54
+ }
55
+ events;
56
+ subscribe;
57
+ commit;
58
+ startTransaction;
59
+ silent;
60
+ notify(event) {
61
+ this.events.notify({
62
+ type: event.type,
63
+ payload: event.payload,
64
+ meta: {
65
+ appDefinitionId: this.appDefinitionId
66
+ }
67
+ });
68
+ }
69
+ notifyCustomEvent(type, payload) {
70
+ this.notify({
71
+ type: PlatformAppEvent.CustomEvent,
72
+ payload: {
73
+ ...payload,
74
+ type
75
+ }
76
+ });
77
+ }
78
+ /**
79
+ * TODO: we should use same interface for all events
80
+ * (subscribe vs addEventListener)
81
+ */
82
+ addEventListener(eventType, fn) {
83
+ return this.events.subscribe(async (event) => {
84
+ const isAppMatch = event.meta?.appDefinitionId === this.appDefinitionId || event.meta?.appDefinitionId === null;
85
+ const transformPayload = () => transformEventPayload(event.payload, this.privateAPI);
86
+ if (eventType === "*") {
87
+ fn(await transformPayload());
88
+ } else if (event.type === PlatformAppEvent.CustomEvent) {
89
+ if (eventType === event.payload?.type && !isAppMatch) {
90
+ fn(await transformPayload());
91
+ }
92
+ } else if (event.type === PlatformAppEvent.HostEvent) {
93
+ if (eventType === event.payload?.type && isAppMatch) {
94
+ fn(await transformPayload());
95
+ }
96
+ }
97
+ });
98
+ }
99
+ }
100
+
101
+ const WAIT_INJECTED_TIMEOUT = 200;
102
+ const WAIT_INJECTED_RETRY_COUNT = 50;
103
+ class ContextInjectionStatus {
104
+ _resolveContextInjected = () => {
105
+ };
106
+ _isInjected = false;
107
+ key;
108
+ constructor(uuid) {
109
+ this.key = `__${uuid}_CONTEXT_INJECTION_STATUS_KEY`;
110
+ if (!globalThis[this.key]) {
111
+ globalThis[this.key] = new Promise((resolve) => {
112
+ this._resolveContextInjected = () => {
113
+ this._isInjected = true;
114
+ resolve();
115
+ };
116
+ });
117
+ }
118
+ }
119
+ isInjected() {
120
+ return !!this._isInjected;
121
+ }
122
+ resolveInjected() {
123
+ this._resolveContextInjected?.();
124
+ }
125
+ waitInjected() {
126
+ return new Promise((resolve, reject) => {
127
+ let injected = false;
128
+ let timeoutId;
129
+ let retryCount = 0;
130
+ const timeout = () => {
131
+ if (injected) {
132
+ return;
133
+ }
134
+ timeoutId = setTimeout(() => {
135
+ retryCount++;
136
+ if (retryCount < WAIT_INJECTED_RETRY_COUNT) {
137
+ if (retryCount % 10 === 0) {
138
+ console.log(
139
+ createEditorPlatformContextError(
140
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
141
+ "contexts are not resolved, still re-trying"
142
+ ).withMessage(`try number ${retryCount}`).message
143
+ );
144
+ }
145
+ timeout();
146
+ return;
147
+ }
148
+ if (!injected) {
149
+ const error = createEditorPlatformContextError(
150
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
151
+ "contexts are not resolved, threw by timeout"
152
+ );
153
+ reject(error);
154
+ }
155
+ }, WAIT_INJECTED_TIMEOUT);
156
+ };
157
+ timeout();
158
+ const _waitContextInjectedPromise = globalThis[this.key];
159
+ _waitContextInjectedPromise.then(() => {
160
+ injected = true;
161
+ clearTimeout(timeoutId);
162
+ resolve();
163
+ });
164
+ });
165
+ }
166
+ }
167
+
168
+ const ENVIRONMENT_CONTEXT_KEY = "__ENVIRONMENT_CONTEXT_KEY";
169
+ var PlatformEnvironment = /* @__PURE__ */ ((PlatformEnvironment2) => {
170
+ PlatformEnvironment2["Worker"] = "Worker";
171
+ PlatformEnvironment2["Frame"] = "Frame";
172
+ PlatformEnvironment2["ComponentPanel"] = "ComponentPanel";
173
+ return PlatformEnvironment2;
174
+ })(PlatformEnvironment || {});
175
+ class EnvironmentContext {
176
+ constructor(environmentContext) {
177
+ this.environmentContext = environmentContext;
178
+ }
179
+ static status = new ContextInjectionStatus("environment");
180
+ static async inject(context) {
181
+ if (globalThis[ENVIRONMENT_CONTEXT_KEY]) {
182
+ throw createEditorPlatformContextError(
183
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
184
+ "Environment context already exists and should not be overridden"
185
+ );
186
+ }
187
+ globalThis[ENVIRONMENT_CONTEXT_KEY] = new EnvironmentContext(context);
188
+ this.status.resolveInjected();
189
+ }
190
+ static async getInstance() {
191
+ await this.status.waitInjected();
192
+ return globalThis[ENVIRONMENT_CONTEXT_KEY];
193
+ }
194
+ getPrivateAPI() {
195
+ return this.environmentContext.privateApi;
196
+ }
197
+ getEvents() {
198
+ return this.environmentContext.events;
199
+ }
200
+ getApplicationAPIs() {
201
+ return this.environmentContext.applicationAPIs ?? {};
202
+ }
203
+ getEnvironment() {
204
+ return this.environmentContext.environment;
205
+ }
206
+ }
207
+
208
+ const APPLICATION_CONTEXT_KEY = "__APPLICATION_CONTEXT_KEY";
209
+ class ApplicationContext {
210
+ constructor(applicationContext, environment) {
211
+ this.applicationContext = applicationContext;
212
+ this.environment = environment;
213
+ this.events = new ApplicationBoundEvents(
214
+ this.applicationContext.appDefinitionId,
215
+ this.environment.getEvents(),
216
+ this.environment.getPrivateAPI()
217
+ );
218
+ }
219
+ static status = new ContextInjectionStatus("application");
220
+ /**
221
+ * TODO: use generics for context type
222
+ * - application
223
+ * - editor
224
+ */
225
+ static async inject(context) {
226
+ if (globalThis[APPLICATION_CONTEXT_KEY]) {
227
+ throw createEditorPlatformContextError(
228
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
229
+ "Application context already exists and should not be overridden"
230
+ );
231
+ }
232
+ globalThis[APPLICATION_CONTEXT_KEY] = new ApplicationContext(
233
+ context,
234
+ await EnvironmentContext.getInstance()
235
+ );
236
+ this.status.resolveInjected();
237
+ }
238
+ static async getInstance() {
239
+ if (typeof __APPLICATION_CONTEXT_KEY !== "undefined") {
240
+ return __APPLICATION_CONTEXT_KEY;
241
+ }
242
+ await this.status.waitInjected();
243
+ return globalThis[APPLICATION_CONTEXT_KEY];
244
+ }
245
+ events;
246
+ getAppDefinitionId() {
247
+ return this.applicationContext.appDefinitionId;
248
+ }
249
+ getBindings() {
250
+ return this.applicationContext;
251
+ }
252
+ getEvents() {
253
+ return this.events;
254
+ }
255
+ getPrivateAPI() {
256
+ return this.environment.getPrivateAPI();
257
+ }
258
+ getPrivateApplicationAPI() {
259
+ const appDefinitionId = this.getAppDefinitionId();
260
+ if (!appDefinitionId) {
261
+ throw createEditorPlatformContextError(
262
+ EditorPlatformContextErrorCode.IncorrectEnvironment,
263
+ "appDefinitionId is not available"
264
+ );
265
+ }
266
+ return this.environment.getApplicationAPIs()[appDefinitionId];
267
+ }
268
+ }
269
+
270
+ export { APPLICATION_CONTEXT_KEY, ApplicationBoundEvents, ApplicationContext, ENVIRONMENT_CONTEXT_KEY, EnvironmentContext, PlatformEnvironment };
271
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/errors.ts","../../src/ApplicationBoundEvents/transformEventPayload.ts","../../src/ApplicationBoundEvents/ApplicationBoundEvents.ts","../../src/contexts/ContextInjectionStatus.ts","../../src/contexts/EnvironmentContext.ts","../../src/contexts/ApplicationContext.ts"],"sourcesContent":["import {\n BaseError,\n createErrorBuilder,\n} from '@wix/public-editor-platform-errors';\n\nexport enum EditorPlatformContextErrorCode {\n IncorrectEnvironment = 'IncorrectEnvironment',\n}\n\nclass EditorPlatformContextError extends BaseError<EditorPlatformContextErrorCode> {\n state: Partial<{\n url: string;\n appDefinitionId: string;\n }> = {};\n\n constructor(message: string, code: EditorPlatformContextErrorCode) {\n super(message, code, 'EP Context Error');\n }\n\n withUrl(url: string) {\n this.state = { ...this.state, url };\n return this;\n }\n\n withAppDefinitionId(appDefinitionId: string) {\n this.state = { ...this.state, appDefinitionId };\n return this;\n }\n}\n\nexport const createEditorPlatformContextError = createErrorBuilder<\n EditorPlatformContextErrorCode,\n EditorPlatformContextError\n>(EditorPlatformContextError);\n","import type { ComponentRef } from '@wix/public-editor-platform-interfaces';\nimport {\n AllowedEvents,\n AppEventPayload,\n IComponent,\n PrivateAPIFixMe,\n} from './types';\n\nexport async function transformEventPayload<T extends AllowedEvents>(\n eventPayload: Record<string, string>,\n privateAPI: PrivateAPIFixMe,\n): Promise<AppEventPayload<T>> {\n if (!eventPayload?.type) {\n return eventPayload as AppEventPayload<T>;\n }\n\n switch (eventPayload.type) {\n case 'componentSelectionChanged':\n const componentRefs = (eventPayload.componentRefs ||\n []) as unknown as ComponentRef[];\n\n const components = (await Promise.all(\n componentRefs.map((ref) => {\n return privateAPI.components.getComponent(ref);\n }),\n )) as IComponent[];\n\n return {\n type: eventPayload.type,\n components,\n } as AppEventPayload<T>;\n\n default:\n return eventPayload as AppEventPayload<T>;\n }\n}\n","import {\n PlatformAppEvent,\n PlatformAppEventEmitter,\n IPlatformAppEvent,\n} from '@wix/public-editor-platform-events';\nimport { AllowedEvents, AppEventPayload, PrivateAPIFixMe } from './types';\nimport { transformEventPayload } from './transformEventPayload';\n\nexport type OmitEventMeta<T> = T extends { meta: any } ? Omit<T, 'meta'> : T;\n\n/**\n * TODO: should not be in this package\n */\nexport class ApplicationBoundEvents {\n private events: PlatformAppEventEmitter;\n public subscribe;\n public commit;\n public startTransaction;\n public silent;\n\n constructor(\n private appDefinitionId: string,\n events: PlatformAppEventEmitter,\n private privateAPI: PrivateAPIFixMe,\n ) {\n this.events = events;\n this.subscribe = events.subscribe.bind(events);\n this.commit = events.commit.bind(events);\n this.startTransaction = events.startTransaction.bind(events);\n this.silent = events.silent.bind(events);\n }\n\n public notify(event: OmitEventMeta<IPlatformAppEvent>) {\n this.events.notify({\n type: event.type,\n payload: event.payload,\n meta: {\n appDefinitionId: this.appDefinitionId,\n },\n } as IPlatformAppEvent);\n }\n\n notifyCustomEvent(type: string, payload: Record<string, string>) {\n this.notify({\n type: PlatformAppEvent.CustomEvent,\n payload: {\n ...payload,\n type,\n },\n });\n }\n\n /**\n * TODO: we should use same interface for all events\n * (subscribe vs addEventListener)\n */\n public addEventListener<EventType extends AllowedEvents>(\n eventType: EventType,\n fn: (payload: AppEventPayload<EventType>) => void,\n ) {\n return this.events.subscribe(async (event) => {\n const isAppMatch =\n event.meta?.appDefinitionId === this.appDefinitionId ||\n event.meta?.appDefinitionId === null;\n\n const transformPayload = () =>\n transformEventPayload<EventType>(event.payload, this.privateAPI);\n\n // as an exception to skip autocomplete\n if ((eventType as string) === '*') {\n fn(await transformPayload());\n /**\n * We need to check for app match here to not listen app's own events\n */\n } else if (event.type === PlatformAppEvent.CustomEvent) {\n if (eventType === event.payload?.type && !isAppMatch) {\n fn(await transformPayload());\n }\n } else if (event.type === PlatformAppEvent.HostEvent) {\n if (eventType === event.payload?.type && isAppMatch) {\n fn(await transformPayload());\n }\n }\n });\n }\n}\n","import {\n createEditorPlatformContextError,\n EditorPlatformContextErrorCode,\n} from '../errors';\n\nconst WAIT_INJECTED_TIMEOUT = 200;\nconst WAIT_INJECTED_RETRY_COUNT = 50;\n\nexport class ContextInjectionStatus {\n private _resolveContextInjected = () => {};\n private _isInjected = false;\n private key: string;\n\n constructor(uuid: string) {\n this.key = `__${uuid}_CONTEXT_INJECTION_STATUS_KEY`;\n\n // @ts-expect-error xxx\n if (!globalThis[this.key]) {\n // @ts-expect-error xxx\n globalThis[this.key] = new Promise<void>((resolve) => {\n this._resolveContextInjected = () => {\n this._isInjected = true;\n resolve();\n };\n });\n }\n }\n\n isInjected() {\n return !!this._isInjected;\n }\n\n resolveInjected() {\n this._resolveContextInjected?.();\n }\n\n waitInjected() {\n return new Promise<void>((resolve, reject) => {\n let injected = false;\n let timeoutId: ReturnType<typeof setTimeout>;\n let retryCount = 0;\n\n const timeout = () => {\n if (injected) {\n return;\n }\n\n timeoutId = setTimeout(() => {\n retryCount++;\n\n if (retryCount < WAIT_INJECTED_RETRY_COUNT) {\n /**\n * just log that we are re-trying to connect (each 2 sec+-)\n */\n if (retryCount % 10 === 0) {\n console.log(\n createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'contexts are not resolved, still re-trying',\n ).withMessage(`try number ${retryCount}`).message,\n );\n }\n\n timeout();\n\n return;\n }\n\n if (!injected) {\n const error = createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'contexts are not resolved, threw by timeout',\n );\n\n reject(error);\n }\n }, WAIT_INJECTED_TIMEOUT);\n };\n\n timeout();\n\n // @ts-expect-error xxx\n const _waitContextInjectedPromise = globalThis[this.key];\n _waitContextInjectedPromise.then(() => {\n injected = true;\n clearTimeout(timeoutId);\n resolve();\n });\n });\n }\n}\n","import { PlatformAppEventEmitter } from '@wix/public-editor-platform-events';\nimport { WithPromiseFunctions } from '@wix/editor-platform-transport';\nimport {\n createEditorPlatformContextError,\n EditorPlatformContextErrorCode,\n} from '../errors';\nimport { ContextInjectionStatus } from './ContextInjectionStatus';\n\nexport type IPrivateAPIFixMe = any; // TODO: replace with the real type\n\nexport const ENVIRONMENT_CONTEXT_KEY = '__ENVIRONMENT_CONTEXT_KEY';\n\ndeclare global {\n // eslint-disable-next-line no-var\n var __ENVIRONMENT_CONTEXT_KEY: EnvironmentContext;\n}\n\nexport enum PlatformEnvironment {\n Worker = 'Worker',\n Frame = 'Frame',\n // TODO - rename to MainThread\n ComponentPanel = 'ComponentPanel',\n}\n\nexport interface IEnvironmentContext {\n environment: PlatformEnvironment;\n privateApi: WithPromiseFunctions<IPrivateAPIFixMe>;\n events: PlatformAppEventEmitter;\n /**\n * Probably, we need to rename this prop\n */\n applicationAPIs?: Record<string, any>;\n}\n\nexport class EnvironmentContext {\n private static status = new ContextInjectionStatus('environment');\n\n static async inject(context: IEnvironmentContext) {\n if (globalThis[ENVIRONMENT_CONTEXT_KEY]) {\n throw createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'Environment context already exists and should not be overridden',\n );\n }\n\n globalThis[ENVIRONMENT_CONTEXT_KEY] = new EnvironmentContext(context);\n this.status.resolveInjected();\n }\n\n static async getInstance(): Promise<EnvironmentContext> {\n await this.status.waitInjected();\n return globalThis[ENVIRONMENT_CONTEXT_KEY];\n }\n\n constructor(private environmentContext: IEnvironmentContext) {}\n\n getPrivateAPI() {\n return this.environmentContext.privateApi;\n }\n\n getEvents() {\n return this.environmentContext.events;\n }\n\n getApplicationAPIs() {\n return this.environmentContext.applicationAPIs ?? {};\n }\n\n getEnvironment() {\n return this.environmentContext.environment;\n }\n}\n","import {\n createEditorPlatformContextError,\n EditorPlatformContextErrorCode,\n} from '../errors';\n\nimport { ApplicationBoundEvents } from '../ApplicationBoundEvents';\nimport { EnvironmentContext, PlatformEnvironment } from './EnvironmentContext';\nimport { ContextInjectionStatus } from './ContextInjectionStatus';\n\nexport const APPLICATION_CONTEXT_KEY = '__APPLICATION_CONTEXT_KEY';\ndeclare global {\n // eslint-disable-next-line no-var\n var __APPLICATION_CONTEXT_KEY: ApplicationContext<IApplicationContext>;\n}\n\nexport type IEditorApplicationContext = {\n appDefinitionId: string;\n};\n\nexport type IAddonContext = {\n appDefinitionId: string;\n appDefinitionName: string;\n data: {\n toolPanelConfig: {\n url: string;\n width: string;\n height: string;\n initialPosition: {\n x: string;\n y: string;\n };\n };\n };\n};\n\nexport type IApplicationContext = IEditorApplicationContext | IAddonContext;\nexport class ApplicationContext<TContext extends IApplicationContext> {\n private static status = new ContextInjectionStatus('application');\n\n /**\n * TODO: use generics for context type\n * - application\n * - editor\n */\n static async inject<TContext extends IApplicationContext>(context: TContext) {\n // const environment = await EnvironmentContext.getInstance();\n\n // if (environment.getEnvironment() !== PlatformEnvironment.Frame) {\n // throw createEditorPlatformContextError(\n // EditorPlatformContextErrorCode.IncorrectEnvironment,\n // 'Application context can be injected only in frame environment',\n // );\n // }\n\n if (globalThis[APPLICATION_CONTEXT_KEY]) {\n throw createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'Application context already exists and should not be overridden',\n );\n }\n\n globalThis[APPLICATION_CONTEXT_KEY] = new ApplicationContext(\n context,\n await EnvironmentContext.getInstance(),\n );\n\n this.status.resolveInjected();\n }\n\n static async getInstance<TContext extends IApplicationContext>() {\n if (typeof __APPLICATION_CONTEXT_KEY !== 'undefined') {\n /**\n * NOTE it is taken not from global\n * __APPLICATION_CONTEXT_KEY is the variable which set during\n * application bundle execution using new Function() approach\n */\n return __APPLICATION_CONTEXT_KEY as ApplicationContext<TContext>;\n }\n\n await this.status.waitInjected();\n\n return globalThis[APPLICATION_CONTEXT_KEY] as ApplicationContext<TContext>;\n\n // const environment = await EnvironmentContext.getInstance();\n\n // if (environment.getEnvironment() === PlatformEnvironment.Frame) {\n // await this.status.waitInjected();\n // return globalThis[\n // APPLICATION_CONTEXT_KEY\n // ] as ApplicationContext<TContext>;\n // } else {\n // /**\n // * NOTE it is taken not from global\n // * __APPLICATION_CONTEXT_KEY is the variable which set during\n // * application bundle execution using new Function() approach\n // */\n // return __APPLICATION_CONTEXT_KEY as ApplicationContext<TContext>;\n // }\n }\n\n private events: ApplicationBoundEvents;\n\n constructor(\n private applicationContext: TContext,\n private environment: EnvironmentContext,\n ) {\n this.events = new ApplicationBoundEvents(\n this.applicationContext.appDefinitionId,\n this.environment.getEvents(),\n this.environment.getPrivateAPI(),\n );\n }\n\n getAppDefinitionId() {\n return this.applicationContext.appDefinitionId;\n }\n\n getBindings() {\n return this.applicationContext;\n }\n\n getEvents() {\n return this.events;\n }\n\n getPrivateAPI() {\n return this.environment.getPrivateAPI();\n }\n\n getPrivateApplicationAPI() {\n const appDefinitionId = this.getAppDefinitionId();\n\n if (!appDefinitionId) {\n throw createEditorPlatformContextError(\n EditorPlatformContextErrorCode.IncorrectEnvironment,\n 'appDefinitionId is not available',\n );\n }\n\n return this.environment.getApplicationAPIs()[appDefinitionId];\n }\n}\n"],"names":["EditorPlatformContextErrorCode","PlatformEnvironment"],"mappings":";;;AAKY,IAAA,8BAAA,qBAAAA,+BAAL,KAAA;AACL,EAAAA,gCAAA,sBAAuB,CAAA,GAAA,sBAAA,CAAA;AADb,EAAAA,OAAAA,+BAAAA,CAAAA;AAAA,CAAA,EAAA,8BAAA,IAAA,EAAA,CAAA,CAAA;AAIZ,MAAM,mCAAmC,SAA0C,CAAA;AAAA,EACjF,QAGK,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import { WithPromiseFunctions } from '@wix/editor-platform-transport';
3
+ import { IPlatformAppEvent, PlatformAppEventEmitter } from '@wix/public-editor-platform-events';
4
+ import { ComponentRef } from '@wix/public-editor-platform-interfaces';
5
+
6
+ type PrivateAPIFixMe = any;
7
+ interface Component<TComponentType extends string> {
8
+ compRef: ComponentRef;
9
+ type: TComponentType;
10
+ }
11
+ interface TextComponent extends Component<'text'> {
12
+ text(): Promise<string>;
13
+ text(content: string): Promise<void>;
14
+ }
15
+ interface ImageComponent extends Component<'image'> {
16
+ src(src?: string): Promise<void>;
17
+ }
18
+ interface UnknownComponent extends Component<'unknown'> {
19
+ }
20
+ type IComponent = TextComponent | ImageComponent | UnknownComponent;
21
+ type EventsToAutocomplete = 'componentSelectionChanged';
22
+ type AllowedEvents = Omit<string, EventsToAutocomplete> | EventsToAutocomplete;
23
+ type AppEventPayload<EventType extends AllowedEvents> = EventType extends 'componentSelectionChanged' ? {
24
+ type: EventType;
25
+ components: IComponent[];
26
+ } : Record<string, string>;
27
+
28
+ type OmitEventMeta<T> = T extends {
29
+ meta: any;
30
+ } ? Omit<T, 'meta'> : T;
31
+ /**
32
+ * TODO: should not be in this package
33
+ */
34
+ declare class ApplicationBoundEvents {
35
+ private appDefinitionId;
36
+ private privateAPI;
37
+ private events;
38
+ subscribe: (callback: (event: IPlatformAppEvent) => void) => () => void;
39
+ commit: () => void;
40
+ startTransaction: () => void;
41
+ silent: (isSilent?: boolean) => void;
42
+ constructor(appDefinitionId: string, events: PlatformAppEventEmitter, privateAPI: PrivateAPIFixMe);
43
+ notify(event: OmitEventMeta<IPlatformAppEvent>): void;
44
+ notifyCustomEvent(type: string, payload: Record<string, string>): void;
45
+ /**
46
+ * TODO: we should use same interface for all events
47
+ * (subscribe vs addEventListener)
48
+ */
49
+ addEventListener<EventType extends AllowedEvents>(eventType: EventType, fn: (payload: AppEventPayload<EventType>) => void): () => void;
50
+ }
51
+
52
+ type IPrivateAPIFixMe = any;
53
+ declare const ENVIRONMENT_CONTEXT_KEY = "__ENVIRONMENT_CONTEXT_KEY";
54
+ declare global {
55
+ var __ENVIRONMENT_CONTEXT_KEY: EnvironmentContext;
56
+ }
57
+ declare enum PlatformEnvironment {
58
+ Worker = "Worker",
59
+ Frame = "Frame",
60
+ ComponentPanel = "ComponentPanel"
61
+ }
62
+ interface IEnvironmentContext {
63
+ environment: PlatformEnvironment;
64
+ privateApi: WithPromiseFunctions<IPrivateAPIFixMe>;
65
+ events: PlatformAppEventEmitter;
66
+ /**
67
+ * Probably, we need to rename this prop
68
+ */
69
+ applicationAPIs?: Record<string, any>;
70
+ }
71
+ declare class EnvironmentContext {
72
+ private environmentContext;
73
+ private static status;
74
+ static inject(context: IEnvironmentContext): Promise<void>;
75
+ static getInstance(): Promise<EnvironmentContext>;
76
+ constructor(environmentContext: IEnvironmentContext);
77
+ getPrivateAPI(): WithPromiseFunctions<any>;
78
+ getEvents(): PlatformAppEventEmitter;
79
+ getApplicationAPIs(): Record<string, any>;
80
+ getEnvironment(): PlatformEnvironment;
81
+ }
82
+
83
+ declare const APPLICATION_CONTEXT_KEY = "__APPLICATION_CONTEXT_KEY";
84
+ declare global {
85
+ var __APPLICATION_CONTEXT_KEY: ApplicationContext<IApplicationContext>;
86
+ }
87
+ type IEditorApplicationContext = {
88
+ appDefinitionId: string;
89
+ };
90
+ type IAddonContext = {
91
+ appDefinitionId: string;
92
+ appDefinitionName: string;
93
+ data: {
94
+ toolPanelConfig: {
95
+ url: string;
96
+ width: string;
97
+ height: string;
98
+ initialPosition: {
99
+ x: string;
100
+ y: string;
101
+ };
102
+ };
103
+ };
104
+ };
105
+ type IApplicationContext = IEditorApplicationContext | IAddonContext;
106
+ declare class ApplicationContext<TContext extends IApplicationContext> {
107
+ private applicationContext;
108
+ private environment;
109
+ private static status;
110
+ /**
111
+ * TODO: use generics for context type
112
+ * - application
113
+ * - editor
114
+ */
115
+ static inject<TContext extends IApplicationContext>(context: TContext): Promise<void>;
116
+ static getInstance<TContext extends IApplicationContext>(): Promise<ApplicationContext<TContext>>;
117
+ private events;
118
+ constructor(applicationContext: TContext, environment: EnvironmentContext);
119
+ getAppDefinitionId(): string;
120
+ getBindings(): TContext;
121
+ getEvents(): ApplicationBoundEvents;
122
+ getPrivateAPI(): _wix_editor_platform_transport.WithPromiseFunctions<any>;
123
+ getPrivateApplicationAPI(): any;
124
+ }
125
+
126
+ export { APPLICATION_CONTEXT_KEY, type AllowedEvents, type AppEventPayload, ApplicationBoundEvents, ApplicationContext, ENVIRONMENT_CONTEXT_KEY, EnvironmentContext, type IAddonContext, type IApplicationContext, type IComponent, type IEditorApplicationContext, type IEnvironmentContext, type IPrivateAPIFixMe, type OmitEventMeta, PlatformEnvironment, type PrivateAPIFixMe };
127
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@wix/editor-platform-contexts",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/types/index.d.ts",
8
+ "scripts": {
9
+ "start": "exit 0",
10
+ "build": "yarn typecheck && rollup -c",
11
+ "test": "exit 0",
12
+ "lint": "prettier --check ./src",
13
+ "lint:fix": "prettier --write ./src",
14
+ "typecheck": "tsc --noEmit"
15
+ },
16
+ "keywords": [],
17
+ "files": [
18
+ "dist/cjs",
19
+ "dist/esm",
20
+ "dist/types"
21
+ ],
22
+ "author": {
23
+ "name": "Editor Platform <editor-platform-dev@wix.com>",
24
+ "email": "editor-platform-dev@wix.com"
25
+ },
26
+ "license": "UNLICENSED",
27
+ "devDependencies": {
28
+ "esbuild": "^0.24.2",
29
+ "eslint": "^8.57.1",
30
+ "prettier": "^3.4.2",
31
+ "rollup": "^3.29.5",
32
+ "rollup-plugin-dts": "^6.1.1",
33
+ "rollup-plugin-esbuild": "^5.0.0",
34
+ "typescript": "^5.7.2"
35
+ },
36
+ "dependencies": {
37
+ "@wix/editor-platform-transport": "1.11.0",
38
+ "@wix/public-editor-platform-errors": "1.8.0",
39
+ "@wix/public-editor-platform-events": "1.304.0",
40
+ "@wix/public-editor-platform-interfaces": "1.15.0"
41
+ },
42
+ "publishConfig": {
43
+ "registry": "https://registry.npmjs.org/",
44
+ "access": "public"
45
+ },
46
+ "unpkg": true,
47
+ "lint-staged": {
48
+ "*.{js,ts}": "yarn run lint"
49
+ },
50
+ "wix": {
51
+ "artifact": {
52
+ "groupId": "com.wixpress",
53
+ "artifactId": "editor-platform-contexts",
54
+ "targets": {
55
+ "static": true
56
+ }
57
+ },
58
+ "validations": {
59
+ "postDependenciesBuild": [
60
+ "lint"
61
+ ]
62
+ }
63
+ },
64
+ "falconPackageHash": "e4afa6af68f165bda7414a67c17edf737680f01c344dd59e0ec865b0"
65
+ }