@wix/editor-application 1.308.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.
Files changed (50) hide show
  1. package/README.md +1 -0
  2. package/dist/cjs/environment-api/index.js +584 -0
  3. package/dist/cjs/environment-api/index.js.map +1 -0
  4. package/dist/cjs/index.js +773 -0
  5. package/dist/cjs/index.js.map +1 -0
  6. package/dist/cjs/platform-frame/index.js +368 -0
  7. package/dist/cjs/platform-frame/index.js.map +1 -0
  8. package/dist/cjs/platform-frame-api/index.js +341 -0
  9. package/dist/cjs/platform-frame-api/index.js.map +1 -0
  10. package/dist/cjs/platform-worker/index.js +552 -0
  11. package/dist/cjs/platform-worker/index.js.map +1 -0
  12. package/dist/cjs/platform-worker-api/index.js +550 -0
  13. package/dist/cjs/platform-worker-api/index.js.map +1 -0
  14. package/dist/esm/environment-api/index.js +579 -0
  15. package/dist/esm/environment-api/index.js.map +1 -0
  16. package/dist/esm/index.js +764 -0
  17. package/dist/esm/index.js.map +1 -0
  18. package/dist/esm/platform-frame/index.js +366 -0
  19. package/dist/esm/platform-frame/index.js.map +1 -0
  20. package/dist/esm/platform-frame-api/index.js +339 -0
  21. package/dist/esm/platform-frame-api/index.js.map +1 -0
  22. package/dist/esm/platform-worker/index.js +550 -0
  23. package/dist/esm/platform-worker/index.js.map +1 -0
  24. package/dist/esm/platform-worker-api/index.js +548 -0
  25. package/dist/esm/platform-worker-api/index.js.map +1 -0
  26. package/dist/statics/environment-api/index.js +586 -0
  27. package/dist/statics/environment-api/index.js.map +1 -0
  28. package/dist/statics/index.js +775 -0
  29. package/dist/statics/index.js.map +1 -0
  30. package/dist/statics/platform-frame/index.js +369 -0
  31. package/dist/statics/platform-frame/index.js.map +1 -0
  32. package/dist/statics/platform-frame-api/index.js +344 -0
  33. package/dist/statics/platform-frame-api/index.js.map +1 -0
  34. package/dist/statics/platform-worker/index.js +553 -0
  35. package/dist/statics/platform-worker/index.js.map +1 -0
  36. package/dist/statics/platform-worker-api/index.js +552 -0
  37. package/dist/statics/platform-worker-api/index.js.map +1 -0
  38. package/dist/types/environment-api/index.d.ts +27 -0
  39. package/dist/types/environment-api/index.d.ts.map +1 -0
  40. package/dist/types/index.d.ts +253 -0
  41. package/dist/types/index.d.ts.map +1 -0
  42. package/dist/types/platform-frame/index.d.ts +27 -0
  43. package/dist/types/platform-frame/index.d.ts.map +1 -0
  44. package/dist/types/platform-frame-api/index.d.ts +27 -0
  45. package/dist/types/platform-frame-api/index.d.ts.map +1 -0
  46. package/dist/types/platform-worker/index.d.ts +27 -0
  47. package/dist/types/platform-worker/index.d.ts.map +1 -0
  48. package/dist/types/platform-worker-api/index.d.ts +27 -0
  49. package/dist/types/platform-worker-api/index.d.ts.map +1 -0
  50. package/package.json +96 -0
@@ -0,0 +1,775 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@wix/public-editor-platform-events'), require('@wix/public-editor-platform-interfaces'), require('@wix/public-editor-platform-errors')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@wix/public-editor-platform-events', '@wix/public-editor-platform-interfaces', '@wix/public-editor-platform-errors'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.EditorPlatformApplicationRuntime = {}, global.publicEditorPlatformEvents, global.publicEditorPlatformInterfaces, global.publicEditorPlatformErrors));
5
+ })(this, (function (exports, publicEditorPlatformEvents, publicEditorPlatformInterfaces, publicEditorPlatformErrors) { 'use strict';
6
+
7
+ class ApplicationLifecycle {
8
+ constructor(events) {
9
+ this.events = events;
10
+ this.subscribe();
11
+ }
12
+ callbacks = {};
13
+ subscribe() {
14
+ this.events.subscribe((event) => {
15
+ const { type, payload } = event;
16
+ if (this.callbacks[type]) {
17
+ this.callbacks[type].forEach(({ fn }) => fn(payload));
18
+ }
19
+ });
20
+ }
21
+ addCallback(event, fn) {
22
+ if (!this.callbacks[event]) {
23
+ this.callbacks[event] = [];
24
+ }
25
+ const id = `${performance.now()}`;
26
+ this.callbacks[event].push({
27
+ id,
28
+ fn
29
+ });
30
+ return () => {
31
+ this.callbacks[event] = this.callbacks[event].filter(
32
+ (cb) => cb.id !== id
33
+ );
34
+ };
35
+ }
36
+ /**
37
+ * NOTE: currently, we return function to unsubscribe from the event,
38
+ * probably it is better to return `this` to allow chaining
39
+ * and provide another way to unsubscribe from events.
40
+ */
41
+ onEditorReady(fn) {
42
+ return this.addCallback(publicEditorPlatformEvents.PlatformAppEvent.EditorReady, fn);
43
+ }
44
+ }
45
+
46
+ var EditorPlatformApplicationErrorCode = /* @__PURE__ */ ((EditorPlatformApplicationErrorCode2) => {
47
+ EditorPlatformApplicationErrorCode2["ApplicationRuntimeError"] = "ApplicationRuntimeError";
48
+ EditorPlatformApplicationErrorCode2["ApplicationCreationError"] = "ApplicationCreationError";
49
+ EditorPlatformApplicationErrorCode2["ApplicationLoadError"] = "ApplicationLoadError";
50
+ EditorPlatformApplicationErrorCode2["ApplicationFetchError"] = "ApplicationFetchError";
51
+ EditorPlatformApplicationErrorCode2["ApplicationExecuteError"] = "ApplicationExecuteError";
52
+ EditorPlatformApplicationErrorCode2["ApplicationWasRemoved"] = "ApplicationWasRemoved";
53
+ EditorPlatformApplicationErrorCode2["UndefinedApiMethod"] = "UndefinedApiMethod";
54
+ EditorPlatformApplicationErrorCode2["ApplicationIsNotMutable"] = "ApplicationIsNotMutable";
55
+ EditorPlatformApplicationErrorCode2["FailedToGetPrivateAPI"] = "FailedToGetPrivateAPI";
56
+ return EditorPlatformApplicationErrorCode2;
57
+ })(EditorPlatformApplicationErrorCode || {});
58
+ class EditorPlatformApplicationError extends publicEditorPlatformErrors.BaseError {
59
+ state = {};
60
+ constructor(message, code) {
61
+ super(message, code, "Editor Platform Application Error");
62
+ }
63
+ withUrl(url) {
64
+ this.state = { ...this.state, url };
65
+ return this;
66
+ }
67
+ withAppDefinitionId(appDefinitionId) {
68
+ this.state = { ...this.state, appDefinitionId };
69
+ return this;
70
+ }
71
+ withApiMethod(apiMethod) {
72
+ this.state = { ...this.state, apiMethod };
73
+ return this;
74
+ }
75
+ withApiType(apiType) {
76
+ this.state = { ...this.state, apiType };
77
+ return this;
78
+ }
79
+ }
80
+ const createEditorPlatformApplicationError = publicEditorPlatformErrors.createErrorBuilder(EditorPlatformApplicationError);
81
+
82
+ class ChainAPIConfiguration {
83
+ constructor(api) {
84
+ this.api = api;
85
+ }
86
+ exposePublicAPI(api) {
87
+ this.api.public = api;
88
+ return this;
89
+ }
90
+ exposePrivateAPI(api) {
91
+ this.api.private = api;
92
+ return this;
93
+ }
94
+ }
95
+ class EditorPlatformApplication {
96
+ constructor(type, context) {
97
+ this.type = type;
98
+ this.#context = context;
99
+ const bindings = this.#context.getBindings();
100
+ this.appDefinitionId = bindings.appDefinitionId;
101
+ const events = this.#context.getEvents();
102
+ this.lifecycle = new ApplicationLifecycle(events);
103
+ events.addEventListener(publicEditorPlatformInterfaces.EventType.removeAppCompleted, (e) => {
104
+ if (e.appDefinitionId === this.appDefinitionId) {
105
+ this.state = "REMOVED";
106
+ }
107
+ });
108
+ const registerApplicationInContainer = __APPLICATION_REGISTRY_KEY;
109
+ registerApplicationInContainer(this);
110
+ events.notify({
111
+ type: publicEditorPlatformEvents.PlatformAppEvent.ApplicationInit,
112
+ payload: {}
113
+ });
114
+ }
115
+ appDefinitionId;
116
+ state = "READY";
117
+ lifecycle;
118
+ #applicationAPI = {};
119
+ #chainAPIConfiguration = new ChainAPIConfiguration(
120
+ this.#applicationAPI
121
+ );
122
+ #manifest;
123
+ #context;
124
+ get context() {
125
+ return this.#context;
126
+ }
127
+ get events() {
128
+ return this.#context.getEvents();
129
+ }
130
+ setManifest(manifest) {
131
+ this.#manifest = manifest;
132
+ }
133
+ getManifest() {
134
+ return this.#manifest;
135
+ }
136
+ exposePublicAPI(api) {
137
+ return this.#chainAPIConfiguration.exposePublicAPI(api);
138
+ }
139
+ exposePrivateAPI(api) {
140
+ return this.#chainAPIConfiguration.exposePrivateAPI(api);
141
+ }
142
+ getPublicAPI() {
143
+ return this.#applicationAPI.public;
144
+ }
145
+ getPrivateAPI() {
146
+ return this.#applicationAPI.private;
147
+ }
148
+ ready() {
149
+ }
150
+ buildApplicationError(message) {
151
+ return createEditorPlatformApplicationError(
152
+ EditorPlatformApplicationErrorCode.ApplicationRuntimeError,
153
+ message
154
+ ).withAppDefinitionId(this.appDefinitionId);
155
+ }
156
+ }
157
+
158
+ var EditorPlatformApplicationContextErrorCode = /* @__PURE__ */ ((EditorPlatformApplicationContextErrorCode2) => {
159
+ EditorPlatformApplicationContextErrorCode2["IncorrectEnvironment"] = "IncorrectEnvironment";
160
+ return EditorPlatformApplicationContextErrorCode2;
161
+ })(EditorPlatformApplicationContextErrorCode || {});
162
+ class EditorPlatformApplicationContextError extends publicEditorPlatformErrors.BaseError {
163
+ state = {};
164
+ constructor(message, code) {
165
+ super(message, code, "Editor Platform Application Context Error");
166
+ }
167
+ withUrl(url) {
168
+ this.state = { ...this.state, url };
169
+ return this;
170
+ }
171
+ withAppDefinitionId(appDefinitionId) {
172
+ this.state = { ...this.state, appDefinitionId };
173
+ return this;
174
+ }
175
+ }
176
+ const createEditorPlatformApplicationContextError = publicEditorPlatformErrors.createErrorBuilder(EditorPlatformApplicationContextError);
177
+ async function transformEventPayload(eventPayload, privateAPI) {
178
+ if (!eventPayload?.type) {
179
+ return eventPayload;
180
+ }
181
+ switch (eventPayload.type) {
182
+ case "componentSelectionChanged":
183
+ const componentRefs = eventPayload.componentRefs || [];
184
+ const components = await Promise.all(
185
+ componentRefs.map((ref) => {
186
+ return privateAPI.components.getComponent(ref);
187
+ })
188
+ );
189
+ return {
190
+ type: eventPayload.type,
191
+ components
192
+ };
193
+ default:
194
+ return eventPayload;
195
+ }
196
+ }
197
+ class ApplicationBoundEvents {
198
+ constructor(appDefinitionId, events, privateAPI) {
199
+ this.appDefinitionId = appDefinitionId;
200
+ this.privateAPI = privateAPI;
201
+ this.events = events;
202
+ this.subscribe = events.subscribe.bind(events);
203
+ this.commit = events.commit.bind(events);
204
+ this.startTransaction = events.startTransaction.bind(events);
205
+ this.silent = events.silent.bind(events);
206
+ }
207
+ events;
208
+ subscribe;
209
+ commit;
210
+ startTransaction;
211
+ silent;
212
+ notify(event) {
213
+ this.events.notify({
214
+ type: event.type,
215
+ payload: event.payload,
216
+ meta: {
217
+ appDefinitionId: this.appDefinitionId
218
+ }
219
+ });
220
+ }
221
+ notifyCustomEvent(type, payload) {
222
+ this.notify({
223
+ type: publicEditorPlatformEvents.PlatformAppEvent.CustomEvent,
224
+ payload: {
225
+ ...payload,
226
+ type
227
+ }
228
+ });
229
+ }
230
+ /**
231
+ * TODO: we should use same interface for all events
232
+ * (subscribe vs addEventListener)
233
+ */
234
+ addEventListener(eventType, fn) {
235
+ return this.events.subscribe(async (event) => {
236
+ const isAppMatch = event.meta?.appDefinitionId === this.appDefinitionId || event.meta?.appDefinitionId === null;
237
+ const transformPayload = () => transformEventPayload(event.payload, this.privateAPI);
238
+ if (eventType === "*") {
239
+ fn(await transformPayload());
240
+ } else if (event.type === publicEditorPlatformEvents.PlatformAppEvent.CustomEvent) {
241
+ if (eventType === event.payload?.type && !isAppMatch) {
242
+ fn(await transformPayload());
243
+ }
244
+ } else if (event.type === publicEditorPlatformEvents.PlatformAppEvent.HostEvent) {
245
+ if (eventType === event.payload?.type && isAppMatch) {
246
+ fn(await transformPayload());
247
+ }
248
+ }
249
+ });
250
+ }
251
+ }
252
+ const WAIT_INJECTED_TIMEOUT = 200;
253
+ const WAIT_INJECTED_RETRY_COUNT = 50;
254
+ class ContextInjectionStatus {
255
+ _resolveContextInjected = () => {
256
+ };
257
+ _isInjected = false;
258
+ key;
259
+ constructor(uuid) {
260
+ this.key = `__${uuid}_CONTEXT_INJECTION_STATUS_KEY`;
261
+ if (!globalThis[this.key]) {
262
+ globalThis[this.key] = new Promise((resolve) => {
263
+ this._resolveContextInjected = () => {
264
+ this._isInjected = true;
265
+ resolve();
266
+ };
267
+ });
268
+ }
269
+ }
270
+ isInjected() {
271
+ return !!this._isInjected;
272
+ }
273
+ resolveInjected() {
274
+ this._resolveContextInjected?.();
275
+ }
276
+ waitInjected() {
277
+ return new Promise((resolve, reject) => {
278
+ let injected = false;
279
+ let timeoutId;
280
+ let retryCount = 0;
281
+ const timeout = () => {
282
+ if (injected) {
283
+ return;
284
+ }
285
+ timeoutId = setTimeout(() => {
286
+ retryCount++;
287
+ if (retryCount < WAIT_INJECTED_RETRY_COUNT) {
288
+ if (retryCount % 10 === 0) {
289
+ console.log(
290
+ createEditorPlatformApplicationContextError(
291
+ EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
292
+ "contexts are not resolved, still re-trying"
293
+ ).withMessage(`try number ${retryCount}`).message
294
+ );
295
+ }
296
+ timeout();
297
+ return;
298
+ }
299
+ if (!injected) {
300
+ const error = createEditorPlatformApplicationContextError(
301
+ EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
302
+ "contexts are not resolved, threw by timeout"
303
+ );
304
+ reject(error);
305
+ }
306
+ }, WAIT_INJECTED_TIMEOUT);
307
+ };
308
+ timeout();
309
+ const _waitContextInjectedPromise = globalThis[this.key];
310
+ _waitContextInjectedPromise.then(() => {
311
+ injected = true;
312
+ clearTimeout(timeoutId);
313
+ resolve();
314
+ });
315
+ });
316
+ }
317
+ }
318
+ const ENVIRONMENT_CONTEXT_KEY = "__ENVIRONMENT_CONTEXT_KEY";
319
+ var PlatformEnvironment = /* @__PURE__ */ ((PlatformEnvironment2) => {
320
+ PlatformEnvironment2["Worker"] = "Worker";
321
+ PlatformEnvironment2["Frame"] = "Frame";
322
+ return PlatformEnvironment2;
323
+ })(PlatformEnvironment || {});
324
+ class EnvironmentContext {
325
+ constructor(environmentContext) {
326
+ this.environmentContext = environmentContext;
327
+ }
328
+ static status = new ContextInjectionStatus("environment");
329
+ static async inject(context) {
330
+ if (globalThis[ENVIRONMENT_CONTEXT_KEY]) {
331
+ throw createEditorPlatformApplicationContextError(
332
+ EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
333
+ "Environment context already exists and should not be overridden"
334
+ );
335
+ }
336
+ globalThis[ENVIRONMENT_CONTEXT_KEY] = new EnvironmentContext(context);
337
+ this.status.resolveInjected();
338
+ }
339
+ static async getInstance() {
340
+ await this.status.waitInjected();
341
+ return globalThis[ENVIRONMENT_CONTEXT_KEY];
342
+ }
343
+ getPrivateAPI() {
344
+ return this.environmentContext.privateApi;
345
+ }
346
+ getEvents() {
347
+ return this.environmentContext.events;
348
+ }
349
+ getApplicationAPIs() {
350
+ return this.environmentContext.applicationAPIs ?? {};
351
+ }
352
+ getEnvironment() {
353
+ return this.environmentContext.environment;
354
+ }
355
+ }
356
+ const APPLICATION_CONTEXT_KEY = "__APPLICATION_CONTEXT_KEY";
357
+ class ApplicationContext {
358
+ constructor(applicationContext, environment) {
359
+ this.applicationContext = applicationContext;
360
+ this.environment = environment;
361
+ this.events = new ApplicationBoundEvents(
362
+ this.applicationContext.appDefinitionId,
363
+ this.environment.getEvents(),
364
+ this.environment.getPrivateAPI()
365
+ );
366
+ }
367
+ static status = new ContextInjectionStatus("application");
368
+ /**
369
+ * TODO: use generics for context type
370
+ * - application
371
+ * - editor
372
+ */
373
+ static async inject(context) {
374
+ const environment = await EnvironmentContext.getInstance();
375
+ if (environment.getEnvironment() !== PlatformEnvironment.Frame) {
376
+ throw createEditorPlatformApplicationContextError(
377
+ EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
378
+ "Application context can be injected only in frame environment"
379
+ );
380
+ }
381
+ if (globalThis[APPLICATION_CONTEXT_KEY]) {
382
+ throw createEditorPlatformApplicationContextError(
383
+ EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
384
+ "Application context already exists and should not be overridden"
385
+ );
386
+ }
387
+ globalThis[APPLICATION_CONTEXT_KEY] = new ApplicationContext(
388
+ context,
389
+ await EnvironmentContext.getInstance()
390
+ );
391
+ this.status.resolveInjected();
392
+ }
393
+ static async getInstance() {
394
+ const environment = await EnvironmentContext.getInstance();
395
+ if (environment.getEnvironment() === PlatformEnvironment.Frame) {
396
+ await this.status.waitInjected();
397
+ return globalThis[APPLICATION_CONTEXT_KEY];
398
+ } else {
399
+ return __APPLICATION_CONTEXT_KEY;
400
+ }
401
+ }
402
+ events;
403
+ getAppDefinitionId() {
404
+ return this.applicationContext.appDefinitionId;
405
+ }
406
+ getBindings() {
407
+ return this.applicationContext;
408
+ }
409
+ getEvents() {
410
+ return this.events;
411
+ }
412
+ getPrivateAPI() {
413
+ return this.environment.getPrivateAPI();
414
+ }
415
+ getPrivateApplicationAPI() {
416
+ const appDefinitionId = this.getAppDefinitionId();
417
+ if (!appDefinitionId) {
418
+ throw createEditorPlatformApplicationContextError(
419
+ EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
420
+ "appDefinitionId is not available"
421
+ );
422
+ }
423
+ return this.environment.getApplicationAPIs()[appDefinitionId];
424
+ }
425
+ }
426
+
427
+ var ApplicationType = /* @__PURE__ */ ((ApplicationType2) => {
428
+ ApplicationType2["EditorAddon"] = "EDITOR_ADDON";
429
+ ApplicationType2["Platform"] = "PLATFORM";
430
+ return ApplicationType2;
431
+ })(ApplicationType || {});
432
+
433
+ class WixEditorPlatformApplication extends EditorPlatformApplication {
434
+ static async create() {
435
+ return new WixEditorPlatformApplication(
436
+ await ApplicationContext.getInstance()
437
+ );
438
+ }
439
+ constructor(context) {
440
+ super(ApplicationType.Platform, context);
441
+ }
442
+ }
443
+
444
+ class WixEditorPlatformAddon extends EditorPlatformApplication {
445
+ static async create() {
446
+ const context = await ApplicationContext.getInstance();
447
+ const instance = new WixEditorPlatformAddon(
448
+ context
449
+ );
450
+ try {
451
+ await instance.init();
452
+ return instance;
453
+ } catch (e) {
454
+ const error = createEditorPlatformApplicationError(
455
+ EditorPlatformApplicationErrorCode.ApplicationRuntimeError
456
+ ).withParentError(e).withMessage("Failed to initialize addon");
457
+ console.error(error);
458
+ return instance;
459
+ }
460
+ }
461
+ constructor(context) {
462
+ super(ApplicationType.EditorAddon, context);
463
+ }
464
+ async init() {
465
+ try {
466
+ await this.registerToolsPanel();
467
+ } catch (e) {
468
+ throw createEditorPlatformApplicationError(
469
+ EditorPlatformApplicationErrorCode.ApplicationRuntimeError
470
+ ).withMessage("Failed to register addon tools panel").withParentError(e);
471
+ }
472
+ }
473
+ async registerToolsPanel() {
474
+ const bindings = this.context.getBindings();
475
+ const { url, width, height, initialPosition } = bindings.data?.toolPanelConfig;
476
+ if (url) {
477
+ await this.context.getPrivateAPI().addons.registerToolsPanel(
478
+ { appDefinitionId: bindings.appDefinitionId, applicationId: "111" },
479
+ {
480
+ title: bindings.appDefinitionName
481
+ },
482
+ {
483
+ url,
484
+ // TODO: this can become "200px", "50%" etc in future. For now we simply assume numeric values like "250".
485
+ width: Number(width),
486
+ height: Number(height),
487
+ initialPosition: {
488
+ x: Number(initialPosition.x),
489
+ y: Number(initialPosition.y)
490
+ }
491
+ }
492
+ );
493
+ }
494
+ }
495
+ }
496
+
497
+ class WorkerEventsBridge {
498
+ constructor(platformAppEvents) {
499
+ this.platformAppEvents = platformAppEvents;
500
+ }
501
+ /**
502
+ * Notify by event from Worker Manager (platform infrastructure)
503
+ */
504
+ notify(event) {
505
+ switch (event.type) {
506
+ case publicEditorPlatformEvents.PlatformLifecycleEvent.EditorReady:
507
+ this.platformAppEvents.notify({
508
+ ...event,
509
+ // @ts-expect-error TODO: fix me
510
+ type: publicEditorPlatformEvents.PlatformAppEvent.EditorReady
511
+ });
512
+ break;
513
+ case publicEditorPlatformEvents.PlatformPrivateEvent.HostEvent:
514
+ this.platformAppEvents.notify({
515
+ ...event,
516
+ type: publicEditorPlatformEvents.PlatformAppEvent.HostEvent
517
+ });
518
+ break;
519
+ }
520
+ }
521
+ /**
522
+ * Subscribe to Worker (Application) event
523
+ */
524
+ subscribe(cb) {
525
+ this.platformAppEvents.subscribe((event) => {
526
+ cb(event);
527
+ });
528
+ }
529
+ }
530
+
531
+ const PLATFORM_FRAME_API_TYPE = "PLATFORM_FRAME_API";
532
+ class PlatformFrameAPI {
533
+ type = PLATFORM_FRAME_API_TYPE;
534
+ #events = new publicEditorPlatformEvents.PlatformAppEventEmitter();
535
+ #eventsBridge = new WorkerEventsBridge(this.#events);
536
+ #privateAPI;
537
+ #applicationPrivateAPI;
538
+ async initFrameEnvironment(appDefinitionId, privateAPI, applicationPrivateAPI) {
539
+ this.#applicationPrivateAPI = applicationPrivateAPI;
540
+ this.#privateAPI = privateAPI;
541
+ await EnvironmentContext.inject({
542
+ environment: PlatformEnvironment.Frame,
543
+ privateApi: privateAPI,
544
+ events: this.#events,
545
+ applicationAPIs: {
546
+ [appDefinitionId]: this.#applicationPrivateAPI
547
+ }
548
+ });
549
+ await ApplicationContext.inject({
550
+ appDefinitionId,
551
+ appDefinitionName: ""
552
+ });
553
+ }
554
+ notify(event) {
555
+ this.#eventsBridge.notify(event);
556
+ }
557
+ subscribe(cb) {
558
+ this.#eventsBridge.subscribe(cb);
559
+ }
560
+ }
561
+
562
+ const APPLICATION_REGISTRY_KEY = "__APPLICATION_REGISTRY_KEY";
563
+ async function executeApplication(events, spec, bundle) {
564
+ const executable = new Function(
565
+ APPLICATION_CONTEXT_KEY,
566
+ APPLICATION_REGISTRY_KEY,
567
+ bundle
568
+ );
569
+ let instance;
570
+ const applicationRegistryCallback = (_instance) => {
571
+ if (instance) {
572
+ throw createEditorPlatformApplicationError(
573
+ EditorPlatformApplicationErrorCode.ApplicationExecuteError,
574
+ "Application registry called more than once"
575
+ ).withAppDefinitionId(spec.appDefinitionId);
576
+ }
577
+ if (_instance.type !== spec.type) {
578
+ throw createEditorPlatformApplicationError(
579
+ EditorPlatformApplicationErrorCode.ApplicationExecuteError,
580
+ "Application has different type"
581
+ ).withMessage("expected type", spec.type).withMessage("received type", _instance.type);
582
+ }
583
+ instance = _instance;
584
+ };
585
+ try {
586
+ const context = { ...spec };
587
+ executable.call(
588
+ void 0,
589
+ new ApplicationContext(context, await EnvironmentContext.getInstance()),
590
+ applicationRegistryCallback
591
+ );
592
+ } catch (e) {
593
+ throw createEditorPlatformApplicationError(
594
+ EditorPlatformApplicationErrorCode.ApplicationExecuteError,
595
+ e.message
596
+ ).withAppDefinitionId(spec.appDefinitionId).withParentError(e);
597
+ }
598
+ return new Promise((resolve, reject) => {
599
+ const unsubscribe = events.subscribe((event) => {
600
+ const timeoutId = setTimeout(() => {
601
+ clearTimeout(timeoutId);
602
+ unsubscribe();
603
+ if (!instance) {
604
+ reject(
605
+ createEditorPlatformApplicationError(
606
+ EditorPlatformApplicationErrorCode.ApplicationExecuteError,
607
+ "Application registry was not called, threw by timeout"
608
+ ).withAppDefinitionId(spec.appDefinitionId)
609
+ );
610
+ }
611
+ }, 5e3);
612
+ if (event.type === publicEditorPlatformEvents.PlatformAppEvent.ApplicationInit && event.meta.appDefinitionId === spec.appDefinitionId) {
613
+ clearTimeout(timeoutId);
614
+ unsubscribe();
615
+ if (!instance) {
616
+ reject(
617
+ createEditorPlatformApplicationError(
618
+ EditorPlatformApplicationErrorCode.ApplicationExecuteError,
619
+ "Application registry was not called"
620
+ ).withAppDefinitionId(spec.appDefinitionId)
621
+ );
622
+ }
623
+ resolve({ instance });
624
+ }
625
+ });
626
+ });
627
+ }
628
+
629
+ class PlatformApplicationContainer {
630
+ constructor(privateApi, events) {
631
+ this.privateApi = privateApi;
632
+ this.events = events;
633
+ this.events.subscribe((event) => {
634
+ switch (event.type) {
635
+ case publicEditorPlatformEvents.PlatformAppEvent.HostEvent: {
636
+ if (event.payload.type === publicEditorPlatformInterfaces.EventType.removeAppCompleted) {
637
+ this.events.withEvent(
638
+ this.events.factories.createApplicationRemovedEvent(
639
+ event.payload.appDefinitionId
640
+ ),
641
+ () => {
642
+ return this.removeApplication(event.payload.appDefinitionId);
643
+ }
644
+ );
645
+ }
646
+ break;
647
+ }
648
+ }
649
+ });
650
+ }
651
+ apps = {};
652
+ async runApplication(app) {
653
+ const bundle = await this.loadApplication(app);
654
+ const instance = await this.executeApplication(app, bundle);
655
+ this.setApplication(app.appDefinitionId, instance);
656
+ return instance;
657
+ }
658
+ setApplication(appDefId, instance) {
659
+ this.apps[appDefId] = instance;
660
+ this.events.withEvent(
661
+ this.events.factories.createApplicationApiInitEvent(
662
+ appDefId,
663
+ // TODO: both types are set here...
664
+ // @ts-expect-error TODO: fix me
665
+ instance?.api?.private ? "private" : "public"
666
+ ),
667
+ () => {
668
+ this.privateApi.applicationManager.setApplication(instance);
669
+ }
670
+ );
671
+ }
672
+ getApplication(appDefId) {
673
+ return this.apps[appDefId];
674
+ }
675
+ getAppDefinitionIds() {
676
+ return Object.keys(this.apps);
677
+ }
678
+ removeApplication(appDefinitionId) {
679
+ delete this.apps[appDefinitionId];
680
+ }
681
+ async loadApplication(app) {
682
+ const url = app.url;
683
+ return this.events.withEvent(
684
+ this.events.factories.createApplicationLoadEvent(app, url),
685
+ async () => {
686
+ try {
687
+ return await this.loadApplicationBundle(url);
688
+ } catch (e) {
689
+ throw createEditorPlatformApplicationError(
690
+ EditorPlatformApplicationErrorCode.ApplicationLoadError
691
+ ).withUrl(url).withAppDefinitionId(app.appDefinitionId).withParentError(e);
692
+ }
693
+ }
694
+ );
695
+ }
696
+ async loadApplicationBundle(url) {
697
+ const res = await fetch("url", {
698
+ method: "GET"
699
+ });
700
+ const isSuccessfulResponse = res.status >= 200 && res.status <= 299;
701
+ if (!isSuccessfulResponse) {
702
+ throw createEditorPlatformApplicationError(
703
+ EditorPlatformApplicationErrorCode.ApplicationFetchError
704
+ ).withUrl(url);
705
+ }
706
+ return res.text();
707
+ }
708
+ async executeApplication(app, bundle) {
709
+ return this.events.withEvent(
710
+ this.events.factories.createApplicationExecuteEvent(app, app.url),
711
+ async () => {
712
+ const { instance } = await executeApplication(this.events, app, bundle);
713
+ return instance;
714
+ }
715
+ );
716
+ }
717
+ }
718
+
719
+ const PLATFORM_WORKER_API_TYPE = "PLATFORM_WORKER_API";
720
+ class PlatformWorkerAPI {
721
+ type = PLATFORM_WORKER_API_TYPE;
722
+ #events = new publicEditorPlatformEvents.PlatformAppEventEmitter();
723
+ #eventsBridge = new WorkerEventsBridge(this.#events);
724
+ #container = null;
725
+ #privateAPI;
726
+ #pendingWaiters = [];
727
+ async initWorkerEnvironment(buildPrivateAPI) {
728
+ this.#privateAPI = await buildPrivateAPI({
729
+ // TODO: should be per application (within the container before app execution)
730
+ type: "EDITOR_ADDON"
731
+ });
732
+ await EnvironmentContext.inject({
733
+ environment: PlatformEnvironment.Worker,
734
+ events: this.#events,
735
+ privateApi: this.#privateAPI,
736
+ applicationAPIs: {}
737
+ });
738
+ this.#container = new PlatformApplicationContainer(
739
+ this.#privateAPI,
740
+ this.#events
741
+ );
742
+ this.#pendingWaiters.forEach((res) => res(this));
743
+ }
744
+ async notify(event) {
745
+ await this.waitReady();
746
+ this.#eventsBridge.notify(event);
747
+ }
748
+ subscribe(cb) {
749
+ this.#eventsBridge.subscribe(cb);
750
+ }
751
+ async runApplication(app) {
752
+ await this.waitReady();
753
+ await this.#container.runApplication(app);
754
+ }
755
+ waitReady() {
756
+ return new Promise((res) => {
757
+ if (this.#privateAPI) {
758
+ return res(this);
759
+ }
760
+ this.#pendingWaiters.push(res);
761
+ });
762
+ }
763
+ }
764
+
765
+ exports.ApplicationType = ApplicationType;
766
+ exports.EditorPlatformApplication = EditorPlatformApplication;
767
+ exports.PLATFORM_FRAME_API_TYPE = PLATFORM_FRAME_API_TYPE;
768
+ exports.PLATFORM_WORKER_API_TYPE = PLATFORM_WORKER_API_TYPE;
769
+ exports.PlatformFrameAPI = PlatformFrameAPI;
770
+ exports.PlatformWorkerAPI = PlatformWorkerAPI;
771
+ exports.WixEditorPlatformAddon = WixEditorPlatformAddon;
772
+ exports.WixEditorPlatformApplication = WixEditorPlatformApplication;
773
+
774
+ }));
775
+ //# sourceMappingURL=index.js.map