@contello/extension 8.21.0 → 8.21.3
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 +123 -0
- package/dist/index.cjs +357 -0
- package/dist/index.d.cts +247 -0
- package/dist/index.d.ts +247 -263
- package/dist/index.js +216 -131
- package/package.json +17 -25
- package/dist/index.umd.cjs +0 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/channel.ts
|
|
2
|
+
var channelIdIterator = 0;
|
|
3
|
+
var requestIdIterator = 0;
|
|
4
|
+
var ExtensionChannel = class {
|
|
5
|
+
constructor(params) {
|
|
6
|
+
this.params = params;
|
|
5
7
|
}
|
|
6
8
|
handlers = /* @__PURE__ */ new Map();
|
|
7
9
|
listeners = /* @__PURE__ */ new Map();
|
|
@@ -10,13 +12,24 @@ class w {
|
|
|
10
12
|
targetOrigin;
|
|
11
13
|
isParent;
|
|
12
14
|
populateChannelId() {
|
|
13
|
-
|
|
15
|
+
if (!this.channelId) {
|
|
16
|
+
this.channelId = this.createChannelId();
|
|
17
|
+
}
|
|
18
|
+
return this.channelId;
|
|
14
19
|
}
|
|
15
|
-
connectParent(
|
|
16
|
-
this.channelId =
|
|
20
|
+
connectParent(targetOrigin, channelId) {
|
|
21
|
+
this.channelId = channelId;
|
|
22
|
+
this.targetOrigin = targetOrigin;
|
|
23
|
+
this.targetWindow = window.parent;
|
|
24
|
+
this.isParent = false;
|
|
25
|
+
this.connect();
|
|
17
26
|
}
|
|
18
|
-
connectChild(
|
|
19
|
-
this.populateChannelId()
|
|
27
|
+
connectChild(targetWindow) {
|
|
28
|
+
this.populateChannelId();
|
|
29
|
+
this.targetWindow = targetWindow;
|
|
30
|
+
this.targetOrigin = "*";
|
|
31
|
+
this.isParent = true;
|
|
32
|
+
this.connect();
|
|
20
33
|
}
|
|
21
34
|
getChannelId() {
|
|
22
35
|
return this.channelId;
|
|
@@ -36,206 +49,278 @@ class w {
|
|
|
36
49
|
disconnect() {
|
|
37
50
|
window.removeEventListener("message", this.handler);
|
|
38
51
|
}
|
|
39
|
-
handler = (
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
handler = (event) => {
|
|
53
|
+
if (event.data.channelId !== this.channelId) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (this.targetOrigin === "*" || event.origin === this.targetOrigin) {
|
|
57
|
+
const { channelId: requestChannelId, requestId, method } = event.data;
|
|
58
|
+
if (requestChannelId === this.channelId) {
|
|
59
|
+
if (this.params.debug) {
|
|
60
|
+
console.log(this.isParent ? "Parent received" : "Child received", event.data);
|
|
61
|
+
}
|
|
62
|
+
if (this.handlers.has(requestId)) {
|
|
63
|
+
this.handlers.get(requestId)(event.data);
|
|
64
|
+
} else if (this.listeners.has(method)) {
|
|
65
|
+
Promise.resolve(this.listeners.get(method)?.(event.data.payload)).then((responsePayload) => this.respond(event.data, responsePayload)).catch((err) => this.respondError(event.data, err));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
43
68
|
}
|
|
44
69
|
};
|
|
45
|
-
respond(
|
|
46
|
-
this.send({ channelId:
|
|
47
|
-
}
|
|
48
|
-
respondError(
|
|
49
|
-
this.send({ channelId:
|
|
50
|
-
}
|
|
51
|
-
on(
|
|
52
|
-
this.listeners.set(
|
|
53
|
-
}
|
|
54
|
-
call(
|
|
55
|
-
return new Promise((
|
|
56
|
-
const
|
|
57
|
-
this.send({ channelId: this.channelId, requestId
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
respond(request, payload) {
|
|
71
|
+
this.send({ channelId: request.channelId, requestId: request.requestId, method: request.method, payload });
|
|
72
|
+
}
|
|
73
|
+
respondError(request, error) {
|
|
74
|
+
this.send({ channelId: request.channelId, requestId: request.requestId, method: request.method, error });
|
|
75
|
+
}
|
|
76
|
+
on(method, handler) {
|
|
77
|
+
this.listeners.set(method, handler);
|
|
78
|
+
}
|
|
79
|
+
call(method, message) {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
const requestId = this.createRequestId();
|
|
82
|
+
this.send({ channelId: this.channelId, requestId, method, payload: message });
|
|
83
|
+
this.handlers.set(requestId, (data) => {
|
|
84
|
+
this.handlers.delete(requestId);
|
|
85
|
+
if (data.error) {
|
|
86
|
+
return reject(data.error);
|
|
87
|
+
}
|
|
88
|
+
resolve(data.payload);
|
|
61
89
|
});
|
|
62
90
|
});
|
|
63
91
|
}
|
|
64
|
-
send(
|
|
65
|
-
this.targetWindow?.postMessage(
|
|
92
|
+
send(data) {
|
|
93
|
+
this.targetWindow?.postMessage(data, this.targetOrigin);
|
|
66
94
|
}
|
|
67
95
|
createChannelId() {
|
|
68
|
-
return `contello-channel-${++
|
|
96
|
+
return `contello-channel-${++channelIdIterator}-${Math.random().toString(36).substring(2)}`;
|
|
69
97
|
}
|
|
70
98
|
createRequestId() {
|
|
71
|
-
return `${this.isParent ? "parent" : "child"}-request-${++
|
|
99
|
+
return `${this.isParent ? "parent" : "child"}-request-${++requestIdIterator}-${Math.random().toString(36).substring(2)}`;
|
|
72
100
|
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
promise = new Promise((e, t) => {
|
|
78
|
-
this.resolve = e, this.reject = t;
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
class m {
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/dialog-ref.ts
|
|
104
|
+
var ContelloDialogRef = class {
|
|
82
105
|
_id;
|
|
83
106
|
open;
|
|
84
107
|
connected;
|
|
85
108
|
ready;
|
|
86
109
|
complete;
|
|
87
110
|
close;
|
|
88
|
-
constructor({ channel
|
|
89
|
-
this.open =
|
|
111
|
+
constructor({ channel, options, controller }) {
|
|
112
|
+
this.open = channel.call("openDialog", options).then(({ id }) => this._id = id);
|
|
113
|
+
this.connected = controller.connected.promise;
|
|
114
|
+
this.ready = controller.ready.promise;
|
|
115
|
+
this.complete = controller.complete.promise;
|
|
116
|
+
this.close = controller.close;
|
|
90
117
|
}
|
|
91
118
|
get id() {
|
|
92
119
|
return this._id;
|
|
93
120
|
}
|
|
94
|
-
}
|
|
95
|
-
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/utils.ts
|
|
124
|
+
var Deferred = class {
|
|
125
|
+
resolve;
|
|
126
|
+
reject;
|
|
127
|
+
promise = new Promise((resolve, reject) => {
|
|
128
|
+
this.resolve = resolve;
|
|
129
|
+
this.reject = reject;
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// src/client.ts
|
|
134
|
+
var ContelloClient = class {
|
|
96
135
|
channel;
|
|
97
136
|
projectId;
|
|
98
137
|
resizeObserver;
|
|
99
138
|
targetOrigin;
|
|
100
139
|
data;
|
|
101
140
|
dialogs = /* @__PURE__ */ new Map();
|
|
102
|
-
constructor(
|
|
103
|
-
this.channel = new
|
|
141
|
+
constructor(targetOrigin, channelId, projectId, debug) {
|
|
142
|
+
this.channel = new ExtensionChannel({ debug });
|
|
143
|
+
this.channel.connectParent(targetOrigin, channelId);
|
|
144
|
+
this.projectId = projectId;
|
|
145
|
+
this.targetOrigin = targetOrigin;
|
|
104
146
|
}
|
|
105
147
|
connect() {
|
|
106
|
-
return this.channel.call("connect").then(({ data
|
|
107
|
-
this.channel.on("dialogConnect", ({ id
|
|
148
|
+
return this.channel.call("connect").then(({ data }) => {
|
|
149
|
+
this.channel.on("dialogConnect", ({ id }) => this.getDialogController(id)?.connected.resolve());
|
|
150
|
+
this.channel.on("dialogReady", ({ id }) => this.getDialogController(id)?.ready.resolve());
|
|
151
|
+
this.channel.on("dialogComplete", ({ id, value }) => this.getDialogController(id)?.complete.resolve(value));
|
|
152
|
+
this.data = data;
|
|
108
153
|
});
|
|
109
154
|
}
|
|
110
155
|
ready() {
|
|
111
|
-
|
|
156
|
+
this.listenForResize();
|
|
157
|
+
return this.channel.call("ready", { height: this.getWindowHeight() });
|
|
112
158
|
}
|
|
113
159
|
getAuthToken() {
|
|
114
|
-
return this.channel.call("getAuthToken").then(({ token
|
|
160
|
+
return this.channel.call("getAuthToken").then(({ token }) => token);
|
|
115
161
|
}
|
|
116
162
|
createProjectUrl() {
|
|
117
163
|
return `${this.targetOrigin}/ui/projects/${this.projectId}`;
|
|
118
164
|
}
|
|
119
|
-
createEntityEntryUrl(
|
|
120
|
-
return `${this.createProjectUrl()}/entities/${
|
|
165
|
+
createEntityEntryUrl(referenceName) {
|
|
166
|
+
return `${this.createProjectUrl()}/entities/${referenceName}`;
|
|
121
167
|
}
|
|
122
|
-
createSingletonEntityUrl(
|
|
123
|
-
return this.createEntityEntryUrl(
|
|
168
|
+
createSingletonEntityUrl(referenceName) {
|
|
169
|
+
return this.createEntityEntryUrl(referenceName);
|
|
124
170
|
}
|
|
125
|
-
createEntityDetailUrl(
|
|
126
|
-
const
|
|
127
|
-
|
|
171
|
+
createEntityDetailUrl(referenceName, params) {
|
|
172
|
+
const base = this.createEntityEntryUrl(referenceName);
|
|
173
|
+
if (params.mode === "create") {
|
|
174
|
+
return `${base}/create`;
|
|
175
|
+
}
|
|
176
|
+
return `${base}/${params.mode}/${params.id}`;
|
|
128
177
|
}
|
|
129
178
|
/**
|
|
130
179
|
* @deprecated Use createEntityDetailUrl instead
|
|
131
180
|
*/
|
|
132
|
-
createEntityUrl(
|
|
133
|
-
return this.createEntityDetailUrl(
|
|
181
|
+
createEntityUrl(referenceName, entityId) {
|
|
182
|
+
return this.createEntityDetailUrl(referenceName, { mode: "edit", id: entityId });
|
|
134
183
|
}
|
|
135
|
-
createExtensionUrl(
|
|
136
|
-
const
|
|
137
|
-
|
|
184
|
+
createExtensionUrl(referenceName, params) {
|
|
185
|
+
const path = params?.path?.join("/") || "";
|
|
186
|
+
const query = new URLSearchParams(params?.query || {}).toString();
|
|
187
|
+
return `${this.createProjectUrl()}/extensions/${referenceName}${path ? `/${path}` : ""}${query ? `?${query}` : ""}`;
|
|
138
188
|
}
|
|
139
|
-
navigate(
|
|
140
|
-
return this.channel.call("navigate", { url
|
|
189
|
+
navigate(url) {
|
|
190
|
+
return this.channel.call("navigate", { url });
|
|
141
191
|
}
|
|
142
|
-
displayNotification(
|
|
143
|
-
return this.channel.call("displayNotification", { type
|
|
192
|
+
displayNotification(type, message) {
|
|
193
|
+
return this.channel.call("displayNotification", { type, message });
|
|
144
194
|
}
|
|
145
|
-
openDialog(
|
|
146
|
-
const
|
|
147
|
-
connected: new
|
|
148
|
-
ready: new
|
|
149
|
-
complete: new
|
|
195
|
+
openDialog(options) {
|
|
196
|
+
const controller = {
|
|
197
|
+
connected: new Deferred(),
|
|
198
|
+
ready: new Deferred(),
|
|
199
|
+
complete: new Deferred(),
|
|
150
200
|
close: () => {
|
|
151
|
-
if (!this.channel)
|
|
201
|
+
if (!this.channel) {
|
|
152
202
|
throw new Error("The channel is not yet initialized");
|
|
153
|
-
|
|
203
|
+
}
|
|
204
|
+
this.channel.call("closeDialog", { id: dialog.id });
|
|
205
|
+
this.dialogs.delete(dialog);
|
|
154
206
|
}
|
|
155
|
-
}
|
|
156
|
-
|
|
207
|
+
};
|
|
208
|
+
const dialog = new ContelloDialogRef({ channel: this.channel, options, controller });
|
|
209
|
+
this.dialogs.set(dialog, controller);
|
|
210
|
+
dialog.complete.then(() => this.dialogs.delete(dialog));
|
|
211
|
+
return dialog;
|
|
157
212
|
}
|
|
158
213
|
listenForResize() {
|
|
159
214
|
this.resizeObserver = new ResizeObserver(() => {
|
|
160
215
|
this.channel.call("resize", { height: this.getWindowHeight() });
|
|
161
|
-
})
|
|
216
|
+
});
|
|
217
|
+
this.resizeObserver.observe(document.documentElement);
|
|
162
218
|
}
|
|
163
219
|
getWindowHeight() {
|
|
164
220
|
return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.body.clientHeight);
|
|
165
221
|
}
|
|
166
|
-
getDialogController(
|
|
167
|
-
const
|
|
168
|
-
if (
|
|
169
|
-
return
|
|
222
|
+
getDialogController(id) {
|
|
223
|
+
const key = Array.from(this.dialogs.keys()).find((dialog) => dialog.id === id);
|
|
224
|
+
if (!key) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
return this.dialogs.get(key);
|
|
170
228
|
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// src/url-parser.ts
|
|
232
|
+
function parseUrl(trustedOrigins) {
|
|
233
|
+
const url = new URL(location.href);
|
|
234
|
+
const channelId = url.searchParams.get("channelId");
|
|
235
|
+
const targetOrigin = url.searchParams.get("origin");
|
|
236
|
+
const applicationId = url.searchParams.get("applicationId");
|
|
237
|
+
const debug = url.searchParams.get("debug") === "true";
|
|
238
|
+
if (!channelId || !targetOrigin || !applicationId) {
|
|
175
239
|
throw new Error("Missing required URL parameters");
|
|
176
|
-
|
|
240
|
+
}
|
|
241
|
+
if (!trustedOrigins?.length) {
|
|
177
242
|
throw new Error("No trusted origins provided");
|
|
178
|
-
if (!s.includes(n))
|
|
179
|
-
throw new Error(`Origin ${n} is not trusted`);
|
|
180
|
-
return { channelId: t, targetOrigin: n, applicationId: r, debug: i };
|
|
181
|
-
}
|
|
182
|
-
class h extends o {
|
|
183
|
-
static connect(e) {
|
|
184
|
-
const { targetOrigin: t, channelId: n, applicationId: r, debug: i } = c(e.trustedOrigins), a = new h(t, n, r, i);
|
|
185
|
-
return a.validate = e.validator || (() => !0), a.newValue = e.newValue || (() => null), a.connect().then(() => a);
|
|
186
243
|
}
|
|
187
|
-
|
|
244
|
+
if (!trustedOrigins.includes(targetOrigin)) {
|
|
245
|
+
throw new Error(`Origin ${targetOrigin} is not trusted`);
|
|
246
|
+
}
|
|
247
|
+
return { channelId, targetOrigin, applicationId, debug };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// src/custom-property.ts
|
|
251
|
+
var ContelloCustomProperty = class _ContelloCustomProperty extends ContelloClient {
|
|
252
|
+
static connect(options) {
|
|
253
|
+
const { targetOrigin, channelId, applicationId, debug } = parseUrl(options.trustedOrigins);
|
|
254
|
+
const customProperty = new _ContelloCustomProperty(targetOrigin, channelId, applicationId, debug);
|
|
255
|
+
customProperty.validate = options.validator || (() => true);
|
|
256
|
+
customProperty.newValue = options.newValue || (() => null);
|
|
257
|
+
return customProperty.connect().then(() => customProperty);
|
|
258
|
+
}
|
|
259
|
+
validate = () => true;
|
|
188
260
|
newValue = () => null;
|
|
189
|
-
constructor(
|
|
190
|
-
super(
|
|
191
|
-
|
|
261
|
+
constructor(targetOrigin, channelId, applicationId, debug) {
|
|
262
|
+
super(targetOrigin, channelId, applicationId, debug);
|
|
263
|
+
this.channel.on("validate", async () => {
|
|
264
|
+
const valid = await Promise.resolve(this.validate());
|
|
265
|
+
return { valid };
|
|
266
|
+
});
|
|
267
|
+
this.channel.on("newValue", async (msg) => {
|
|
268
|
+
await Promise.resolve(this.newValue(msg.value));
|
|
192
269
|
});
|
|
193
270
|
}
|
|
194
271
|
async getValue() {
|
|
195
|
-
|
|
272
|
+
const r = await this.channel.call("getValue");
|
|
273
|
+
return r.value;
|
|
196
274
|
}
|
|
197
|
-
async setValue(
|
|
198
|
-
const
|
|
199
|
-
return await this.channel.call("setValue", { value
|
|
275
|
+
async setValue(value) {
|
|
276
|
+
const valid = await Promise.resolve(this.validate());
|
|
277
|
+
return await this.channel.call("setValue", { value, valid });
|
|
200
278
|
}
|
|
201
|
-
async getValueByPath(
|
|
202
|
-
|
|
279
|
+
async getValueByPath(path) {
|
|
280
|
+
const r = await this.channel.call("getValueByPath", { path });
|
|
281
|
+
return r.value;
|
|
203
282
|
}
|
|
204
|
-
async setValueByPath(
|
|
205
|
-
return this.channel.call("setValueByPath", { value
|
|
283
|
+
async setValueByPath(path, value) {
|
|
284
|
+
return this.channel.call("setValueByPath", { value, path });
|
|
206
285
|
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
// src/dialog.ts
|
|
289
|
+
var ContelloDialog = class _ContelloDialog extends ContelloClient {
|
|
290
|
+
static connect({ trustedOrigins }) {
|
|
291
|
+
const { targetOrigin, channelId, applicationId, debug } = parseUrl(trustedOrigins);
|
|
292
|
+
const dialog = new _ContelloDialog(targetOrigin, channelId, applicationId, debug);
|
|
293
|
+
return dialog.connect().then(() => dialog);
|
|
212
294
|
}
|
|
213
|
-
constructor(
|
|
214
|
-
super(
|
|
295
|
+
constructor(targetOrigin, channelId, applicationId, debug) {
|
|
296
|
+
super(targetOrigin, channelId, applicationId, debug);
|
|
215
297
|
}
|
|
216
|
-
close(
|
|
217
|
-
return this.channel.call("complete", { value
|
|
298
|
+
close(value) {
|
|
299
|
+
return this.channel.call("complete", { value });
|
|
218
300
|
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
// src/extension.ts
|
|
304
|
+
var ContelloExtension = class _ContelloExtension extends ContelloClient {
|
|
305
|
+
static connect({ trustedOrigins }) {
|
|
306
|
+
const { targetOrigin, channelId, applicationId, debug } = parseUrl(trustedOrigins);
|
|
307
|
+
const extension = new _ContelloExtension(targetOrigin, channelId, applicationId, debug);
|
|
308
|
+
return extension.connect().then(() => extension);
|
|
224
309
|
}
|
|
225
|
-
constructor(
|
|
226
|
-
super(
|
|
310
|
+
constructor(targetOrigin, channelId, applicationId, debug) {
|
|
311
|
+
super(targetOrigin, channelId, applicationId, debug);
|
|
227
312
|
}
|
|
228
313
|
getUrlData() {
|
|
229
314
|
return this.channel.call("getUrlData");
|
|
230
315
|
}
|
|
231
|
-
setBreadcrumbs(
|
|
232
|
-
return this.channel.call("setBreadcrumbs", { breadcrumbs
|
|
316
|
+
setBreadcrumbs(breadcrumbs) {
|
|
317
|
+
return this.channel.call("setBreadcrumbs", { breadcrumbs });
|
|
233
318
|
}
|
|
234
|
-
}
|
|
319
|
+
};
|
|
235
320
|
export {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
321
|
+
ContelloCustomProperty,
|
|
322
|
+
ContelloDialog,
|
|
323
|
+
ContelloDialogRef,
|
|
324
|
+
ContelloExtension,
|
|
325
|
+
ExtensionChannel
|
|
241
326
|
};
|
package/package.json
CHANGED
|
@@ -1,28 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contello/extension",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
"version": "8.21.3",
|
|
4
|
+
"description": "Client SDK for building Contello CMS extensions and custom properties",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "admin@entwico.com",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/entwico/contello-js",
|
|
11
|
+
"directory": "packages/extension"
|
|
9
12
|
},
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"eslint-plugin-import": "^2.32.0",
|
|
16
|
-
"eslint-plugin-prettier": "^5.5.1",
|
|
17
|
-
"npm-run-all": "^4.1.5",
|
|
18
|
-
"typescript": "^5.8.3",
|
|
19
|
-
"typescript-eslint": "^8.35.1",
|
|
20
|
-
"vite": "^7.0.2",
|
|
21
|
-
"vite-plugin-dts": "^4.5.4"
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsup",
|
|
15
|
+
"lint": "eslint .",
|
|
16
|
+
"lint:fix": "eslint . --fix",
|
|
17
|
+
"typecheck": "tsc --noEmit"
|
|
22
18
|
},
|
|
23
|
-
"author": "admin@entwico.com",
|
|
24
|
-
"license": "MIT",
|
|
25
|
-
"type": "module",
|
|
26
19
|
"files": [
|
|
27
20
|
"dist"
|
|
28
21
|
],
|
|
@@ -30,12 +23,11 @@
|
|
|
30
23
|
"exports": {
|
|
31
24
|
".": {
|
|
32
25
|
"import": "./dist/index.js",
|
|
33
|
-
"require": "./dist/index.
|
|
26
|
+
"require": "./dist/index.cjs"
|
|
34
27
|
}
|
|
35
28
|
},
|
|
36
29
|
"publishConfig": {
|
|
37
30
|
"access": "public",
|
|
38
31
|
"registry": "https://registry.npmjs.org"
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
}
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/index.umd.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s.ContelloExtension={}))})(this,function(s){"use strict";let o=0,y=0;class m{constructor(e){this.params=e}handlers=new Map;listeners=new Map;targetWindow;channelId;targetOrigin;isParent;populateChannelId(){return this.channelId||(this.channelId=this.createChannelId()),this.channelId}connectParent(e,t){this.channelId=t,this.targetOrigin=e,this.targetWindow=window.parent,this.isParent=!1,this.connect()}connectChild(e){this.populateChannelId(),this.targetWindow=e,this.targetOrigin="*",this.isParent=!0,this.connect()}getChannelId(){return this.channelId}getIsDebug(){return this.params.debug}getTargetOrigin(){return this.targetOrigin}getTargetWindow(){return this.targetWindow}connect(){window.addEventListener("message",this.handler)}disconnect(){window.removeEventListener("message",this.handler)}handler=e=>{if(e.data.channelId===this.channelId&&(this.targetOrigin==="*"||e.origin===this.targetOrigin)){const{channelId:t,requestId:n,method:r}=e.data;t===this.channelId&&(this.params.debug&&console.log(this.isParent?"Parent received":"Child received",e.data),this.handlers.has(n)?this.handlers.get(n)(e.data):this.listeners.has(r)&&Promise.resolve(this.listeners.get(r)?.(e.data.payload)).then(i=>this.respond(e.data,i)).catch(i=>this.respondError(e.data,i)))}};respond(e,t){this.send({channelId:e.channelId,requestId:e.requestId,method:e.method,payload:t})}respondError(e,t){this.send({channelId:e.channelId,requestId:e.requestId,method:e.method,error:t})}on(e,t){this.listeners.set(e,t)}call(e,t){return new Promise((n,r)=>{const i=this.createRequestId();this.send({channelId:this.channelId,requestId:i,method:e,payload:t}),this.handlers.set(i,a=>{if(this.handlers.delete(i),a.error)return r(a.error);n(a.payload)})})}send(e){this.targetWindow?.postMessage(e,this.targetOrigin)}createChannelId(){return`contello-channel-${++o}-${Math.random().toString(36).substring(2)}`}createRequestId(){return`${this.isParent?"parent":"child"}-request-${++y}-${Math.random().toString(36).substring(2)}`}}class c{resolve;reject;promise=new Promise((e,t)=>{this.resolve=e,this.reject=t})}class w{_id;open;connected;ready;complete;close;constructor({channel:e,options:t,controller:n}){this.open=e.call("openDialog",t).then(({id:r})=>this._id=r),this.connected=n.connected.promise,this.ready=n.ready.promise,this.complete=n.complete.promise,this.close=n.close}get id(){return this._id}}class h{channel;projectId;resizeObserver;targetOrigin;data;dialogs=new Map;constructor(e,t,n,r){this.channel=new m({debug:r}),this.channel.connectParent(e,t),this.projectId=n,this.targetOrigin=e}connect(){return this.channel.call("connect").then(({data:e})=>{this.channel.on("dialogConnect",({id:t})=>this.getDialogController(t)?.connected.resolve()),this.channel.on("dialogReady",({id:t})=>this.getDialogController(t)?.ready.resolve()),this.channel.on("dialogComplete",({id:t,value:n})=>this.getDialogController(t)?.complete.resolve(n)),this.data=e})}ready(){return this.listenForResize(),this.channel.call("ready",{height:this.getWindowHeight()})}getAuthToken(){return this.channel.call("getAuthToken").then(({token:e})=>e)}createProjectUrl(){return`${this.targetOrigin}/ui/projects/${this.projectId}`}createEntityEntryUrl(e){return`${this.createProjectUrl()}/entities/${e}`}createSingletonEntityUrl(e){return this.createEntityEntryUrl(e)}createEntityDetailUrl(e,t){const n=this.createEntityEntryUrl(e);return t.mode==="create"?`${n}/create`:`${n}/${t.mode}/${t.id}`}createEntityUrl(e,t){return this.createEntityDetailUrl(e,{mode:"edit",id:t})}createExtensionUrl(e,t){const n=t?.path?.join("/")||"",r=new URLSearchParams(t?.query||{}).toString();return`${this.createProjectUrl()}/extensions/${e}${n?`/${n}`:""}${r?`?${r}`:""}`}navigate(e){return this.channel.call("navigate",{url:e})}displayNotification(e,t){return this.channel.call("displayNotification",{type:e,message:t})}openDialog(e){const t={connected:new c,ready:new c,complete:new c,close:()=>{if(!this.channel)throw new Error("The channel is not yet initialized");this.channel.call("closeDialog",{id:n.id}),this.dialogs.delete(n)}},n=new w({channel:this.channel,options:e,controller:t});return this.dialogs.set(n,t),n.complete.then(()=>this.dialogs.delete(n)),n}listenForResize(){this.resizeObserver=new ResizeObserver(()=>{this.channel.call("resize",{height:this.getWindowHeight()})}),this.resizeObserver.observe(document.documentElement)}getWindowHeight(){return Math.max(document.body.scrollHeight,document.body.offsetHeight,document.body.clientHeight)}getDialogController(e){const t=Array.from(this.dialogs.keys()).find(n=>n.id===e);if(t)return this.dialogs.get(t)}}function d(l){const e=new URL(location.href),t=e.searchParams.get("channelId"),n=e.searchParams.get("origin"),r=e.searchParams.get("applicationId"),i=e.searchParams.get("debug")==="true";if(!t||!n||!r)throw new Error("Missing required URL parameters");if(!l?.length)throw new Error("No trusted origins provided");if(!l.includes(n))throw new Error(`Origin ${n} is not trusted`);return{channelId:t,targetOrigin:n,applicationId:r,debug:i}}class g extends h{static connect(e){const{targetOrigin:t,channelId:n,applicationId:r,debug:i}=d(e.trustedOrigins),a=new g(t,n,r,i);return a.validate=e.validator||(()=>!0),a.newValue=e.newValue||(()=>null),a.connect().then(()=>a)}validate=()=>!0;newValue=()=>null;constructor(e,t,n,r){super(e,t,n,r),this.channel.on("validate",async()=>({valid:await Promise.resolve(this.validate())})),this.channel.on("newValue",async i=>{await Promise.resolve(this.newValue(i.value))})}async getValue(){return(await this.channel.call("getValue")).value}async setValue(e){const t=await Promise.resolve(this.validate());return await this.channel.call("setValue",{value:e,valid:t})}async getValueByPath(e){return(await this.channel.call("getValueByPath",{path:e})).value}async setValueByPath(e,t){return this.channel.call("setValueByPath",{value:t,path:e})}}class u extends h{static connect({trustedOrigins:e}){const{targetOrigin:t,channelId:n,applicationId:r,debug:i}=d(e),a=new u(t,n,r,i);return a.connect().then(()=>a)}constructor(e,t,n,r){super(e,t,n,r)}close(e){return this.channel.call("complete",{value:e})}}class p extends h{static connect({trustedOrigins:e}){const{targetOrigin:t,channelId:n,applicationId:r,debug:i}=d(e),a=new p(t,n,r,i);return a.connect().then(()=>a)}constructor(e,t,n,r){super(e,t,n,r)}getUrlData(){return this.channel.call("getUrlData")}setBreadcrumbs(e){return this.channel.call("setBreadcrumbs",{breadcrumbs:e})}}s.ContelloCustomProperty=g,s.ContelloDialog=u,s.ContelloDialogRef=w,s.ContelloExtension=p,s.ExtensionChannel=m,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|