@wix/editor-application 1.393.0 → 1.394.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/dist/cjs/index.js +23 -605
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +23 -602
- package/dist/esm/index.js.map +1 -1
- package/dist/statics/index.js +26 -609
- package/dist/statics/index.js.map +1 -1
- package/dist/types/index.d.ts +18 -192
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +10 -35
- package/dist/cjs/environment-api/index.js +0 -634
- package/dist/cjs/environment-api/index.js.map +0 -1
- package/dist/cjs/platform-frame/index.js +0 -403
- package/dist/cjs/platform-frame/index.js.map +0 -1
- package/dist/cjs/platform-frame-api/index.js +0 -377
- package/dist/cjs/platform-frame-api/index.js.map +0 -1
- package/dist/cjs/platform-worker/index.js +0 -580
- package/dist/cjs/platform-worker/index.js.map +0 -1
- package/dist/cjs/platform-worker-api/index.js +0 -579
- package/dist/cjs/platform-worker-api/index.js.map +0 -1
- package/dist/esm/environment-api/index.js +0 -630
- package/dist/esm/environment-api/index.js.map +0 -1
- package/dist/esm/platform-frame/index.js +0 -401
- package/dist/esm/platform-frame/index.js.map +0 -1
- package/dist/esm/platform-frame-api/index.js +0 -375
- package/dist/esm/platform-frame-api/index.js.map +0 -1
- package/dist/esm/platform-worker/index.js +0 -578
- package/dist/esm/platform-worker/index.js.map +0 -1
- package/dist/esm/platform-worker-api/index.js +0 -577
- package/dist/esm/platform-worker-api/index.js.map +0 -1
- package/dist/statics/environment-api/index.js +0 -636
- package/dist/statics/environment-api/index.js.map +0 -1
- package/dist/statics/platform-frame/index.js +0 -404
- package/dist/statics/platform-frame/index.js.map +0 -1
- package/dist/statics/platform-frame-api/index.js +0 -380
- package/dist/statics/platform-frame-api/index.js.map +0 -1
- package/dist/statics/platform-worker/index.js +0 -581
- package/dist/statics/platform-worker/index.js.map +0 -1
- package/dist/statics/platform-worker-api/index.js +0 -581
- package/dist/statics/platform-worker-api/index.js.map +0 -1
- package/dist/types/environment-api/index.d.ts +0 -59
- package/dist/types/environment-api/index.d.ts.map +0 -1
- package/dist/types/platform-frame/index.d.ts +0 -59
- package/dist/types/platform-frame/index.d.ts.map +0 -1
- package/dist/types/platform-frame-api/index.d.ts +0 -59
- package/dist/types/platform-frame-api/index.d.ts.map +0 -1
- package/dist/types/platform-worker/index.d.ts +0 -59
- package/dist/types/platform-worker/index.d.ts.map +0 -1
- package/dist/types/platform-worker-api/index.d.ts +0 -59
- package/dist/types/platform-worker-api/index.d.ts.map +0 -1
|
@@ -1,634 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var publicEditorPlatformEvents = require('@wix/public-editor-platform-events');
|
|
4
|
-
var publicEditorPlatformErrors = require('@wix/public-editor-platform-errors');
|
|
5
|
-
var publicEditorPlatformInterfaces = require('@wix/public-editor-platform-interfaces');
|
|
6
|
-
|
|
7
|
-
class WorkerEventsBridge {
|
|
8
|
-
constructor(platformAppEvents) {
|
|
9
|
-
this.platformAppEvents = platformAppEvents;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Notify by event from Worker Manager (platform infrastructure)
|
|
13
|
-
*/
|
|
14
|
-
notify(event) {
|
|
15
|
-
switch (event.type) {
|
|
16
|
-
case publicEditorPlatformEvents.PlatformLifecycleEvent.EditorReady:
|
|
17
|
-
this.platformAppEvents.notify({
|
|
18
|
-
...event,
|
|
19
|
-
// @ts-expect-error TODO: fix me
|
|
20
|
-
type: publicEditorPlatformEvents.PlatformAppEvent.EditorReady
|
|
21
|
-
});
|
|
22
|
-
break;
|
|
23
|
-
case publicEditorPlatformEvents.PlatformPrivateEvent.HostEvent:
|
|
24
|
-
this.platformAppEvents.notify({
|
|
25
|
-
...event,
|
|
26
|
-
type: publicEditorPlatformEvents.PlatformAppEvent.HostEvent
|
|
27
|
-
});
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Subscribe to Worker (Application) event
|
|
33
|
-
*/
|
|
34
|
-
subscribe(cb) {
|
|
35
|
-
this.platformAppEvents.subscribe((event) => {
|
|
36
|
-
cb(event);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
var PlatformConsumerEnvironmentAPIType = /* @__PURE__ */ ((PlatformConsumerEnvironmentAPIType2) => {
|
|
42
|
-
PlatformConsumerEnvironmentAPIType2["Frame"] = "PLATFORM_FRAME_API";
|
|
43
|
-
PlatformConsumerEnvironmentAPIType2["Worker"] = "PLATFORM_WORKER_API";
|
|
44
|
-
return PlatformConsumerEnvironmentAPIType2;
|
|
45
|
-
})(PlatformConsumerEnvironmentAPIType || {});
|
|
46
|
-
class AbstractEnvironmentAPI {
|
|
47
|
-
constructor(env) {
|
|
48
|
-
this.env = env;
|
|
49
|
-
this.create();
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
var EditorPlatformApplicationContextErrorCode = /* @__PURE__ */ ((EditorPlatformApplicationContextErrorCode2) => {
|
|
54
|
-
EditorPlatformApplicationContextErrorCode2["IncorrectEnvironment"] = "IncorrectEnvironment";
|
|
55
|
-
EditorPlatformApplicationContextErrorCode2["ClientAuthError"] = "ClientAuthError";
|
|
56
|
-
return EditorPlatformApplicationContextErrorCode2;
|
|
57
|
-
})(EditorPlatformApplicationContextErrorCode || {});
|
|
58
|
-
class EditorPlatformApplicationContextError extends publicEditorPlatformErrors.BaseError {
|
|
59
|
-
state = {};
|
|
60
|
-
constructor(message, code) {
|
|
61
|
-
super(message, code, "Editor Platform Application Context 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
|
-
}
|
|
72
|
-
const createEditorPlatformApplicationContextError = publicEditorPlatformErrors.createErrorBuilder(EditorPlatformApplicationContextError);
|
|
73
|
-
async function transformEventPayload(eventPayload, privateAPI) {
|
|
74
|
-
if (!eventPayload?.type) {
|
|
75
|
-
return eventPayload;
|
|
76
|
-
}
|
|
77
|
-
switch (eventPayload.type) {
|
|
78
|
-
case "componentSelectionChanged":
|
|
79
|
-
const componentRefs = eventPayload.componentRefs || [];
|
|
80
|
-
const components = await Promise.all(
|
|
81
|
-
componentRefs.map((ref) => {
|
|
82
|
-
return privateAPI.components.getComponent(ref);
|
|
83
|
-
})
|
|
84
|
-
);
|
|
85
|
-
return {
|
|
86
|
-
type: eventPayload.type,
|
|
87
|
-
components
|
|
88
|
-
};
|
|
89
|
-
default:
|
|
90
|
-
return eventPayload;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
class ApplicationBoundEvents {
|
|
94
|
-
constructor(appDefinitionId, events, privateAPI) {
|
|
95
|
-
this.appDefinitionId = appDefinitionId;
|
|
96
|
-
this.privateAPI = privateAPI;
|
|
97
|
-
this.events = events;
|
|
98
|
-
this.subscribe = events.subscribe.bind(events);
|
|
99
|
-
this.commit = events.commit.bind(events);
|
|
100
|
-
this.startTransaction = events.startTransaction.bind(events);
|
|
101
|
-
this.silent = events.silent.bind(events);
|
|
102
|
-
}
|
|
103
|
-
events;
|
|
104
|
-
subscribe;
|
|
105
|
-
commit;
|
|
106
|
-
startTransaction;
|
|
107
|
-
silent;
|
|
108
|
-
notify(event) {
|
|
109
|
-
this.events.notify({
|
|
110
|
-
type: event.type,
|
|
111
|
-
payload: event.payload,
|
|
112
|
-
meta: {
|
|
113
|
-
appDefinitionId: this.appDefinitionId
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
notifyCustomEvent(type, payload) {
|
|
118
|
-
this.notify({
|
|
119
|
-
type: publicEditorPlatformEvents.PlatformAppEvent.CustomEvent,
|
|
120
|
-
payload: {
|
|
121
|
-
...payload,
|
|
122
|
-
type
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* TODO: we should use same interface for all events
|
|
128
|
-
* (subscribe vs addEventListener)
|
|
129
|
-
*/
|
|
130
|
-
addEventListener(eventType, fn) {
|
|
131
|
-
return this.events.subscribe(async (event) => {
|
|
132
|
-
const isAppMatch = event.meta?.appDefinitionId === this.appDefinitionId || event.meta?.appDefinitionId === null;
|
|
133
|
-
const transformPayload = () => transformEventPayload(event.payload, this.privateAPI);
|
|
134
|
-
if (eventType === "*") {
|
|
135
|
-
fn(await transformPayload());
|
|
136
|
-
} else if (event.type === publicEditorPlatformEvents.PlatformAppEvent.CustomEvent) {
|
|
137
|
-
if (eventType === event.payload?.type && !isAppMatch) {
|
|
138
|
-
fn(await transformPayload());
|
|
139
|
-
}
|
|
140
|
-
} else if (event.type === publicEditorPlatformEvents.PlatformAppEvent.HostEvent) {
|
|
141
|
-
if (eventType === event.payload?.type && isAppMatch) {
|
|
142
|
-
fn(await transformPayload());
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
const WAIT_INJECTED_TIMEOUT = 200;
|
|
149
|
-
const WAIT_INJECTED_RETRY_COUNT = 50;
|
|
150
|
-
class ContextInjectionStatus {
|
|
151
|
-
_resolveContextInjected = () => {
|
|
152
|
-
};
|
|
153
|
-
_isInjected = false;
|
|
154
|
-
key;
|
|
155
|
-
constructor(uuid) {
|
|
156
|
-
this.key = `__${uuid}_CONTEXT_INJECTION_STATUS_KEY`;
|
|
157
|
-
if (!globalThis[this.key]) {
|
|
158
|
-
globalThis[this.key] = new Promise((resolve) => {
|
|
159
|
-
this._resolveContextInjected = () => {
|
|
160
|
-
this._isInjected = true;
|
|
161
|
-
resolve();
|
|
162
|
-
};
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
isInjected() {
|
|
167
|
-
return !!this._isInjected;
|
|
168
|
-
}
|
|
169
|
-
resolveInjected() {
|
|
170
|
-
this._resolveContextInjected?.();
|
|
171
|
-
}
|
|
172
|
-
waitInjected() {
|
|
173
|
-
return new Promise((resolve, reject) => {
|
|
174
|
-
let injected = false;
|
|
175
|
-
let timeoutId;
|
|
176
|
-
let retryCount = 0;
|
|
177
|
-
const timeout = () => {
|
|
178
|
-
if (injected) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
timeoutId = setTimeout(() => {
|
|
182
|
-
retryCount++;
|
|
183
|
-
if (retryCount < WAIT_INJECTED_RETRY_COUNT) {
|
|
184
|
-
if (retryCount % 10 === 0) {
|
|
185
|
-
console.log(
|
|
186
|
-
createEditorPlatformApplicationContextError(
|
|
187
|
-
EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
|
|
188
|
-
"contexts are not resolved, still re-trying"
|
|
189
|
-
).withMessage(`try number ${retryCount}`).message
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
timeout();
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
if (!injected) {
|
|
196
|
-
const error = createEditorPlatformApplicationContextError(
|
|
197
|
-
EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
|
|
198
|
-
"contexts are not resolved, threw by timeout"
|
|
199
|
-
);
|
|
200
|
-
reject(error);
|
|
201
|
-
}
|
|
202
|
-
}, WAIT_INJECTED_TIMEOUT);
|
|
203
|
-
};
|
|
204
|
-
timeout();
|
|
205
|
-
const _waitContextInjectedPromise = globalThis[this.key];
|
|
206
|
-
_waitContextInjectedPromise.then(() => {
|
|
207
|
-
injected = true;
|
|
208
|
-
clearTimeout(timeoutId);
|
|
209
|
-
resolve();
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
const ENVIRONMENT_CONTEXT_KEY = "__ENVIRONMENT_CONTEXT_KEY";
|
|
215
|
-
var PlatformEnvironment = /* @__PURE__ */ ((PlatformEnvironment2) => {
|
|
216
|
-
PlatformEnvironment2["Worker"] = "Worker";
|
|
217
|
-
PlatformEnvironment2["Frame"] = "Frame";
|
|
218
|
-
PlatformEnvironment2["ComponentPanel"] = "ComponentPanel";
|
|
219
|
-
return PlatformEnvironment2;
|
|
220
|
-
})(PlatformEnvironment || {});
|
|
221
|
-
class EnvironmentContext {
|
|
222
|
-
constructor(environmentContext) {
|
|
223
|
-
this.environmentContext = environmentContext;
|
|
224
|
-
}
|
|
225
|
-
static status = new ContextInjectionStatus("environment");
|
|
226
|
-
static async inject(context) {
|
|
227
|
-
if (globalThis[ENVIRONMENT_CONTEXT_KEY]) {
|
|
228
|
-
throw createEditorPlatformApplicationContextError(
|
|
229
|
-
EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
|
|
230
|
-
"Environment context already exists and should not be overridden"
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
globalThis[ENVIRONMENT_CONTEXT_KEY] = new EnvironmentContext(context);
|
|
234
|
-
this.status.resolveInjected();
|
|
235
|
-
}
|
|
236
|
-
static async getInstance() {
|
|
237
|
-
await this.status.waitInjected();
|
|
238
|
-
return globalThis[ENVIRONMENT_CONTEXT_KEY];
|
|
239
|
-
}
|
|
240
|
-
getPrivateAPI() {
|
|
241
|
-
return this.environmentContext.privateApi;
|
|
242
|
-
}
|
|
243
|
-
getEvents() {
|
|
244
|
-
return this.environmentContext.events;
|
|
245
|
-
}
|
|
246
|
-
getApplicationAPIs() {
|
|
247
|
-
return this.environmentContext.applicationAPIs ?? {};
|
|
248
|
-
}
|
|
249
|
-
getEnvironment() {
|
|
250
|
-
return this.environmentContext.environment;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
const APPLICATION_CONTEXT_KEY = "__APPLICATION_CONTEXT_KEY";
|
|
254
|
-
class ApplicationContext {
|
|
255
|
-
constructor(applicationContext, environment) {
|
|
256
|
-
this.applicationContext = applicationContext;
|
|
257
|
-
this.environment = environment;
|
|
258
|
-
this.events = new ApplicationBoundEvents(
|
|
259
|
-
this.applicationContext.appDefinitionId,
|
|
260
|
-
this.environment.getEvents(),
|
|
261
|
-
this.environment.getPrivateAPI()
|
|
262
|
-
);
|
|
263
|
-
}
|
|
264
|
-
static status = new ContextInjectionStatus("application");
|
|
265
|
-
/**
|
|
266
|
-
* TODO: use generics for context type
|
|
267
|
-
* - application
|
|
268
|
-
* - editor
|
|
269
|
-
*/
|
|
270
|
-
static async inject(context) {
|
|
271
|
-
const environment = await EnvironmentContext.getInstance();
|
|
272
|
-
if (environment.getEnvironment() !== PlatformEnvironment.Frame) {
|
|
273
|
-
throw createEditorPlatformApplicationContextError(
|
|
274
|
-
EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
|
|
275
|
-
"Application context can be injected only in frame environment"
|
|
276
|
-
);
|
|
277
|
-
}
|
|
278
|
-
if (globalThis[APPLICATION_CONTEXT_KEY]) {
|
|
279
|
-
throw createEditorPlatformApplicationContextError(
|
|
280
|
-
EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
|
|
281
|
-
"Application context already exists and should not be overridden"
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
globalThis[APPLICATION_CONTEXT_KEY] = new ApplicationContext(
|
|
285
|
-
context,
|
|
286
|
-
await EnvironmentContext.getInstance()
|
|
287
|
-
);
|
|
288
|
-
this.status.resolveInjected();
|
|
289
|
-
}
|
|
290
|
-
static async getInstance() {
|
|
291
|
-
const environment = await EnvironmentContext.getInstance();
|
|
292
|
-
if (environment.getEnvironment() === PlatformEnvironment.Frame) {
|
|
293
|
-
await this.status.waitInjected();
|
|
294
|
-
return globalThis[APPLICATION_CONTEXT_KEY];
|
|
295
|
-
} else {
|
|
296
|
-
return __APPLICATION_CONTEXT_KEY;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
events;
|
|
300
|
-
getAppDefinitionId() {
|
|
301
|
-
return this.applicationContext.appDefinitionId;
|
|
302
|
-
}
|
|
303
|
-
getBindings() {
|
|
304
|
-
return this.applicationContext;
|
|
305
|
-
}
|
|
306
|
-
getEvents() {
|
|
307
|
-
return this.events;
|
|
308
|
-
}
|
|
309
|
-
getPrivateAPI() {
|
|
310
|
-
return this.environment.getPrivateAPI();
|
|
311
|
-
}
|
|
312
|
-
getPrivateApplicationAPI() {
|
|
313
|
-
const appDefinitionId = this.getAppDefinitionId();
|
|
314
|
-
if (!appDefinitionId) {
|
|
315
|
-
throw createEditorPlatformApplicationContextError(
|
|
316
|
-
EditorPlatformApplicationContextErrorCode.IncorrectEnvironment,
|
|
317
|
-
"appDefinitionId is not available"
|
|
318
|
-
);
|
|
319
|
-
}
|
|
320
|
-
return this.environment.getApplicationAPIs()[appDefinitionId];
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
const DESIGN_SYSTEM_STYLES_MAP = {
|
|
325
|
-
classic: "https://www.unpkg.com/@wix/design-system/dist/statics/tokens-default.global.css",
|
|
326
|
-
studio: "https://www.unpkg.com/@wix/design-system/dist/statics/tokens-studio.global.css"
|
|
327
|
-
};
|
|
328
|
-
class PlatformFrameAPI extends AbstractEnvironmentAPI {
|
|
329
|
-
type = PlatformConsumerEnvironmentAPIType.Frame;
|
|
330
|
-
#events = new publicEditorPlatformEvents.PlatformAppEventEmitter();
|
|
331
|
-
#eventsBridge = new WorkerEventsBridge(this.#events);
|
|
332
|
-
#privateAPI;
|
|
333
|
-
#applicationPrivateAPI;
|
|
334
|
-
constructor() {
|
|
335
|
-
super(PlatformConsumerEnvironmentAPIType.Frame);
|
|
336
|
-
}
|
|
337
|
-
create() {
|
|
338
|
-
if (typeof globalThis?.document?.head?.prepend === "function") {
|
|
339
|
-
const params = new URL(globalThis.location.href).searchParams;
|
|
340
|
-
const host = params.get("editorType") === "CLASSIC" ? "classic" : "studio";
|
|
341
|
-
const url = DESIGN_SYSTEM_STYLES_MAP[host];
|
|
342
|
-
const isAlreadyLoaded = url && !!document.querySelectorAll(`link[type="text/css"][href="${url}"]`)?.length;
|
|
343
|
-
if (url && !isAlreadyLoaded) {
|
|
344
|
-
const link = document.createElement("link");
|
|
345
|
-
link.setAttribute("rel", "stylesheet");
|
|
346
|
-
link.setAttribute("type", "text/css");
|
|
347
|
-
link.setAttribute("href", url);
|
|
348
|
-
globalThis.document.head.prepend(link);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
async initEnvironment(props) {
|
|
353
|
-
const { applicationPrivateAPI, privateAPI, appDefinitionId } = props;
|
|
354
|
-
this.#applicationPrivateAPI = applicationPrivateAPI;
|
|
355
|
-
this.#privateAPI = privateAPI;
|
|
356
|
-
await EnvironmentContext.inject({
|
|
357
|
-
environment: PlatformEnvironment.Frame,
|
|
358
|
-
privateApi: privateAPI,
|
|
359
|
-
events: this.#events,
|
|
360
|
-
applicationAPIs: {
|
|
361
|
-
[appDefinitionId]: this.#applicationPrivateAPI
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
await ApplicationContext.inject({
|
|
365
|
-
appDefinitionId,
|
|
366
|
-
appDefinitionName: ""
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
notify(event) {
|
|
370
|
-
this.#eventsBridge.notify(event);
|
|
371
|
-
}
|
|
372
|
-
subscribe(cb) {
|
|
373
|
-
this.#eventsBridge.subscribe(cb);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
var EditorPlatformApplicationErrorCode = /* @__PURE__ */ ((EditorPlatformApplicationErrorCode2) => {
|
|
378
|
-
EditorPlatformApplicationErrorCode2["ApplicationRuntimeError"] = "ApplicationRuntimeError";
|
|
379
|
-
EditorPlatformApplicationErrorCode2["ApplicationCreationError"] = "ApplicationCreationError";
|
|
380
|
-
EditorPlatformApplicationErrorCode2["ApplicationLoadError"] = "ApplicationLoadError";
|
|
381
|
-
EditorPlatformApplicationErrorCode2["ApplicationFetchError"] = "ApplicationFetchError";
|
|
382
|
-
EditorPlatformApplicationErrorCode2["ApplicationExecuteError"] = "ApplicationExecuteError";
|
|
383
|
-
EditorPlatformApplicationErrorCode2["ApplicationWasRemoved"] = "ApplicationWasRemoved";
|
|
384
|
-
EditorPlatformApplicationErrorCode2["UndefinedApiMethod"] = "UndefinedApiMethod";
|
|
385
|
-
EditorPlatformApplicationErrorCode2["ApplicationIsNotMutable"] = "ApplicationIsNotMutable";
|
|
386
|
-
EditorPlatformApplicationErrorCode2["FailedToGetPrivateAPI"] = "FailedToGetPrivateAPI";
|
|
387
|
-
EditorPlatformApplicationErrorCode2["ClientAuthError"] = "ClientAuthError";
|
|
388
|
-
return EditorPlatformApplicationErrorCode2;
|
|
389
|
-
})(EditorPlatformApplicationErrorCode || {});
|
|
390
|
-
class EditorPlatformApplicationError extends publicEditorPlatformErrors.BaseError {
|
|
391
|
-
state = {};
|
|
392
|
-
constructor(message, code) {
|
|
393
|
-
super(message, code, "Editor Platform Application Error");
|
|
394
|
-
}
|
|
395
|
-
withUrl(url) {
|
|
396
|
-
this.state = { ...this.state, url };
|
|
397
|
-
return this;
|
|
398
|
-
}
|
|
399
|
-
withAppDefinitionId(appDefinitionId) {
|
|
400
|
-
this.state = { ...this.state, appDefinitionId };
|
|
401
|
-
return this;
|
|
402
|
-
}
|
|
403
|
-
withApiMethod(apiMethod) {
|
|
404
|
-
this.state = { ...this.state, apiMethod };
|
|
405
|
-
return this;
|
|
406
|
-
}
|
|
407
|
-
withApiType(apiType) {
|
|
408
|
-
this.state = { ...this.state, apiType };
|
|
409
|
-
return this;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
const createEditorPlatformApplicationError = publicEditorPlatformErrors.createErrorBuilder(EditorPlatformApplicationError);
|
|
413
|
-
|
|
414
|
-
const APPLICATION_REGISTRY_KEY = "__APPLICATION_REGISTRY_KEY";
|
|
415
|
-
async function executeApplication(events, spec, bundle) {
|
|
416
|
-
const executable = new Function(
|
|
417
|
-
APPLICATION_CONTEXT_KEY,
|
|
418
|
-
APPLICATION_REGISTRY_KEY,
|
|
419
|
-
bundle
|
|
420
|
-
);
|
|
421
|
-
let instance;
|
|
422
|
-
const applicationRegistryCallback = (_instance) => {
|
|
423
|
-
if (instance) {
|
|
424
|
-
throw createEditorPlatformApplicationError(
|
|
425
|
-
EditorPlatformApplicationErrorCode.ApplicationExecuteError,
|
|
426
|
-
"Application registry called more than once"
|
|
427
|
-
).withAppDefinitionId(spec.appDefinitionId);
|
|
428
|
-
}
|
|
429
|
-
if (_instance.type !== spec.type) {
|
|
430
|
-
throw createEditorPlatformApplicationError(
|
|
431
|
-
EditorPlatformApplicationErrorCode.ApplicationExecuteError,
|
|
432
|
-
"Application has different type"
|
|
433
|
-
).withMessage("expected type", spec.type).withMessage("received type", _instance.type);
|
|
434
|
-
}
|
|
435
|
-
instance = _instance;
|
|
436
|
-
};
|
|
437
|
-
try {
|
|
438
|
-
const context = { ...spec };
|
|
439
|
-
executable.call(
|
|
440
|
-
null,
|
|
441
|
-
new ApplicationContext(context, await EnvironmentContext.getInstance()),
|
|
442
|
-
applicationRegistryCallback
|
|
443
|
-
);
|
|
444
|
-
} catch (e) {
|
|
445
|
-
throw createEditorPlatformApplicationError(
|
|
446
|
-
EditorPlatformApplicationErrorCode.ApplicationExecuteError,
|
|
447
|
-
e.message
|
|
448
|
-
).withAppDefinitionId(spec.appDefinitionId).withParentError(e);
|
|
449
|
-
}
|
|
450
|
-
return new Promise((resolve, reject) => {
|
|
451
|
-
const unsubscribe = events.subscribe((event) => {
|
|
452
|
-
const timeoutId = setTimeout(() => {
|
|
453
|
-
clearTimeout(timeoutId);
|
|
454
|
-
unsubscribe();
|
|
455
|
-
if (!instance) {
|
|
456
|
-
reject(
|
|
457
|
-
createEditorPlatformApplicationError(
|
|
458
|
-
EditorPlatformApplicationErrorCode.ApplicationExecuteError,
|
|
459
|
-
"Application registry was not called, threw by timeout"
|
|
460
|
-
).withAppDefinitionId(spec.appDefinitionId)
|
|
461
|
-
);
|
|
462
|
-
}
|
|
463
|
-
}, 5e3);
|
|
464
|
-
if (event.type === publicEditorPlatformEvents.PlatformAppEvent.ApplicationInit && event.meta.appDefinitionId === spec.appDefinitionId) {
|
|
465
|
-
clearTimeout(timeoutId);
|
|
466
|
-
unsubscribe();
|
|
467
|
-
if (!instance) {
|
|
468
|
-
reject(
|
|
469
|
-
createEditorPlatformApplicationError(
|
|
470
|
-
EditorPlatformApplicationErrorCode.ApplicationExecuteError,
|
|
471
|
-
"Application registry was not called"
|
|
472
|
-
).withAppDefinitionId(spec.appDefinitionId)
|
|
473
|
-
);
|
|
474
|
-
}
|
|
475
|
-
resolve({ instance });
|
|
476
|
-
}
|
|
477
|
-
});
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
class PlatformApplicationContainer {
|
|
482
|
-
#apps = {};
|
|
483
|
-
#privateAPI;
|
|
484
|
-
#events;
|
|
485
|
-
constructor(privateApi, events) {
|
|
486
|
-
this.#privateAPI = privateApi;
|
|
487
|
-
this.#events = events;
|
|
488
|
-
this.#events.subscribe((event) => {
|
|
489
|
-
switch (event.type) {
|
|
490
|
-
case publicEditorPlatformEvents.PlatformAppEvent.HostEvent: {
|
|
491
|
-
if (event.payload.type === publicEditorPlatformInterfaces.EventType.removeAppCompleted) {
|
|
492
|
-
void this.#events.withEvent(
|
|
493
|
-
this.#events.factories.createApplicationRemovedEvent(
|
|
494
|
-
event.payload.appDefinitionId
|
|
495
|
-
),
|
|
496
|
-
() => {
|
|
497
|
-
return this.removeApplication(event.payload.appDefinitionId);
|
|
498
|
-
}
|
|
499
|
-
);
|
|
500
|
-
}
|
|
501
|
-
break;
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
async runApplication(appSpec) {
|
|
507
|
-
const bundle = await this.loadApplication(appSpec);
|
|
508
|
-
const instance = await this.executeApplication(appSpec, bundle);
|
|
509
|
-
this.setApplication(appSpec.appDefinitionId, instance);
|
|
510
|
-
return instance;
|
|
511
|
-
}
|
|
512
|
-
setApplication(appDefId, instance) {
|
|
513
|
-
this.#apps[appDefId] = instance;
|
|
514
|
-
void this.#events.withEvent(
|
|
515
|
-
this.#events.factories.createApplicationApiInitEvent(
|
|
516
|
-
appDefId,
|
|
517
|
-
// TODO: both types are set here...
|
|
518
|
-
// @ts-expect-error TODO: fix me
|
|
519
|
-
instance?.api?.private ? "private" : "public"
|
|
520
|
-
),
|
|
521
|
-
() => {
|
|
522
|
-
this.#privateAPI.applicationManager.setApplication(instance);
|
|
523
|
-
}
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
getApplication(appDefId) {
|
|
527
|
-
return this.#apps[appDefId];
|
|
528
|
-
}
|
|
529
|
-
getAppDefinitionIds() {
|
|
530
|
-
return Object.keys(this.#apps);
|
|
531
|
-
}
|
|
532
|
-
removeApplication(appDefinitionId) {
|
|
533
|
-
delete this.#apps[appDefinitionId];
|
|
534
|
-
}
|
|
535
|
-
async loadApplication(appSpec) {
|
|
536
|
-
const url = appSpec.url;
|
|
537
|
-
return this.#events.withEvent(
|
|
538
|
-
this.#events.factories.createApplicationLoadEvent(appSpec, url),
|
|
539
|
-
async () => {
|
|
540
|
-
try {
|
|
541
|
-
return await this.loadApplicationBundle(url);
|
|
542
|
-
} catch (e) {
|
|
543
|
-
throw createEditorPlatformApplicationError(
|
|
544
|
-
EditorPlatformApplicationErrorCode.ApplicationLoadError
|
|
545
|
-
).withUrl(url).withAppDefinitionId(appSpec.appDefinitionId).withParentError(e);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
);
|
|
549
|
-
}
|
|
550
|
-
async loadApplicationBundle(url) {
|
|
551
|
-
const res = await fetch(url, {
|
|
552
|
-
method: "GET"
|
|
553
|
-
});
|
|
554
|
-
const isSuccessfulResponse = res.status >= 200 && res.status <= 299;
|
|
555
|
-
if (!isSuccessfulResponse) {
|
|
556
|
-
throw createEditorPlatformApplicationError(
|
|
557
|
-
EditorPlatformApplicationErrorCode.ApplicationFetchError
|
|
558
|
-
).withUrl(url);
|
|
559
|
-
}
|
|
560
|
-
return res.text();
|
|
561
|
-
}
|
|
562
|
-
async executeApplication(appSpec, bundle) {
|
|
563
|
-
return this.#events.withEvent(
|
|
564
|
-
this.#events.factories.createApplicationExecuteEvent(
|
|
565
|
-
appSpec,
|
|
566
|
-
appSpec.url
|
|
567
|
-
),
|
|
568
|
-
async () => {
|
|
569
|
-
const { instance } = await executeApplication(
|
|
570
|
-
this.#events,
|
|
571
|
-
appSpec,
|
|
572
|
-
bundle
|
|
573
|
-
);
|
|
574
|
-
return instance;
|
|
575
|
-
}
|
|
576
|
-
);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
class PlatformWorkerAPI extends AbstractEnvironmentAPI {
|
|
581
|
-
type = PlatformConsumerEnvironmentAPIType.Worker;
|
|
582
|
-
#events = new publicEditorPlatformEvents.PlatformAppEventEmitter();
|
|
583
|
-
#eventsBridge = new WorkerEventsBridge(this.#events);
|
|
584
|
-
#container = null;
|
|
585
|
-
#privateAPI;
|
|
586
|
-
#pendingWaiters = [];
|
|
587
|
-
constructor() {
|
|
588
|
-
super(PlatformConsumerEnvironmentAPIType.Worker);
|
|
589
|
-
}
|
|
590
|
-
create() {
|
|
591
|
-
}
|
|
592
|
-
async initEnvironment(props) {
|
|
593
|
-
const { buildPrivateAPI } = props;
|
|
594
|
-
this.#privateAPI = await buildPrivateAPI({
|
|
595
|
-
// TODO: should be per application (within the container before app execution)
|
|
596
|
-
type: "EDITOR_ADDON"
|
|
597
|
-
});
|
|
598
|
-
await EnvironmentContext.inject({
|
|
599
|
-
environment: PlatformEnvironment.Worker,
|
|
600
|
-
events: this.#events,
|
|
601
|
-
privateApi: this.#privateAPI,
|
|
602
|
-
applicationAPIs: {}
|
|
603
|
-
});
|
|
604
|
-
this.#container = new PlatformApplicationContainer(
|
|
605
|
-
this.#privateAPI,
|
|
606
|
-
this.#events
|
|
607
|
-
);
|
|
608
|
-
this.#pendingWaiters.forEach((res) => res(this));
|
|
609
|
-
}
|
|
610
|
-
async notify(event) {
|
|
611
|
-
await this.waitReady();
|
|
612
|
-
this.#eventsBridge.notify(event);
|
|
613
|
-
}
|
|
614
|
-
subscribe(cb) {
|
|
615
|
-
this.#eventsBridge.subscribe(cb);
|
|
616
|
-
}
|
|
617
|
-
async runApplication(app) {
|
|
618
|
-
await this.waitReady();
|
|
619
|
-
await this.#container.runApplication(app);
|
|
620
|
-
}
|
|
621
|
-
waitReady() {
|
|
622
|
-
return new Promise((res) => {
|
|
623
|
-
if (this.#privateAPI) {
|
|
624
|
-
return res(this);
|
|
625
|
-
}
|
|
626
|
-
this.#pendingWaiters.push(res);
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
exports.PlatformConsumerEnvironmentAPIType = PlatformConsumerEnvironmentAPIType;
|
|
632
|
-
exports.PlatformFrameAPI = PlatformFrameAPI;
|
|
633
|
-
exports.PlatformWorkerAPI = PlatformWorkerAPI;
|
|
634
|
-
//# sourceMappingURL=index.js.map
|