@contello/extension 8.21.0 → 8.21.2

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/index.js CHANGED
@@ -1,7 +1,9 @@
1
- let u = 0, p = 0;
2
- class w {
3
- constructor(e) {
4
- this.params = e;
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
- return this.channelId || (this.channelId = this.createChannelId()), this.channelId;
15
+ if (!this.channelId) {
16
+ this.channelId = this.createChannelId();
17
+ }
18
+ return this.channelId;
14
19
  }
15
- connectParent(e, t) {
16
- this.channelId = t, this.targetOrigin = e, this.targetWindow = window.parent, this.isParent = !1, this.connect();
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(e) {
19
- this.populateChannelId(), this.targetWindow = e, this.targetOrigin = "*", this.isParent = !0, this.connect();
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 = (e) => {
40
- if (e.data.channelId === this.channelId && (this.targetOrigin === "*" || e.origin === this.targetOrigin)) {
41
- const { channelId: t, requestId: n, method: r } = e.data;
42
- 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)));
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(e, t) {
46
- this.send({ channelId: e.channelId, requestId: e.requestId, method: e.method, payload: t });
47
- }
48
- respondError(e, t) {
49
- this.send({ channelId: e.channelId, requestId: e.requestId, method: e.method, error: t });
50
- }
51
- on(e, t) {
52
- this.listeners.set(e, t);
53
- }
54
- call(e, t) {
55
- return new Promise((n, r) => {
56
- const i = this.createRequestId();
57
- this.send({ channelId: this.channelId, requestId: i, method: e, payload: t }), this.handlers.set(i, (a) => {
58
- if (this.handlers.delete(i), a.error)
59
- return r(a.error);
60
- n(a.payload);
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(e) {
65
- this.targetWindow?.postMessage(e, this.targetOrigin);
92
+ send(data) {
93
+ this.targetWindow?.postMessage(data, this.targetOrigin);
66
94
  }
67
95
  createChannelId() {
68
- return `contello-channel-${++u}-${Math.random().toString(36).substring(2)}`;
96
+ return `contello-channel-${++channelIdIterator}-${Math.random().toString(36).substring(2)}`;
69
97
  }
70
98
  createRequestId() {
71
- return `${this.isParent ? "parent" : "child"}-request-${++p}-${Math.random().toString(36).substring(2)}`;
99
+ return `${this.isParent ? "parent" : "child"}-request-${++requestIdIterator}-${Math.random().toString(36).substring(2)}`;
72
100
  }
73
- }
74
- class l {
75
- resolve;
76
- reject;
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: e, options: t, controller: n }) {
89
- 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;
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
- class o {
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(e, t, n, r) {
103
- this.channel = new w({ debug: r }), this.channel.connectParent(e, t), this.projectId = n, this.targetOrigin = e;
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: e }) => {
107
- 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;
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
- return this.listenForResize(), this.channel.call("ready", { height: this.getWindowHeight() });
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: e }) => e);
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(e) {
120
- return `${this.createProjectUrl()}/entities/${e}`;
165
+ createEntityEntryUrl(referenceName) {
166
+ return `${this.createProjectUrl()}/entities/${referenceName}`;
121
167
  }
122
- createSingletonEntityUrl(e) {
123
- return this.createEntityEntryUrl(e);
168
+ createSingletonEntityUrl(referenceName) {
169
+ return this.createEntityEntryUrl(referenceName);
124
170
  }
125
- createEntityDetailUrl(e, t) {
126
- const n = this.createEntityEntryUrl(e);
127
- return t.mode === "create" ? `${n}/create` : `${n}/${t.mode}/${t.id}`;
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(e, t) {
133
- return this.createEntityDetailUrl(e, { mode: "edit", id: t });
181
+ createEntityUrl(referenceName, entityId) {
182
+ return this.createEntityDetailUrl(referenceName, { mode: "edit", id: entityId });
134
183
  }
135
- createExtensionUrl(e, t) {
136
- const n = t?.path?.join("/") || "", r = new URLSearchParams(t?.query || {}).toString();
137
- return `${this.createProjectUrl()}/extensions/${e}${n ? `/${n}` : ""}${r ? `?${r}` : ""}`;
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(e) {
140
- return this.channel.call("navigate", { url: e });
189
+ navigate(url) {
190
+ return this.channel.call("navigate", { url });
141
191
  }
142
- displayNotification(e, t) {
143
- return this.channel.call("displayNotification", { type: e, message: t });
192
+ displayNotification(type, message) {
193
+ return this.channel.call("displayNotification", { type, message });
144
194
  }
145
- openDialog(e) {
146
- const t = {
147
- connected: new l(),
148
- ready: new l(),
149
- complete: new l(),
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
- this.channel.call("closeDialog", { id: n.id }), this.dialogs.delete(n);
203
+ }
204
+ this.channel.call("closeDialog", { id: dialog.id });
205
+ this.dialogs.delete(dialog);
154
206
  }
155
- }, n = new m({ channel: this.channel, options: e, controller: t });
156
- return this.dialogs.set(n, t), n.complete.then(() => this.dialogs.delete(n)), n;
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
- }), this.resizeObserver.observe(document.documentElement);
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(e) {
167
- const t = Array.from(this.dialogs.keys()).find((n) => n.id === e);
168
- if (t)
169
- return this.dialogs.get(t);
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
- function c(s) {
173
- 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";
174
- if (!t || !n || !r)
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
- if (!s?.length)
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
- validate = () => !0;
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(e, t, n, r) {
190
- super(e, t, n, r), this.channel.on("validate", async () => ({ valid: await Promise.resolve(this.validate()) })), this.channel.on("newValue", async (i) => {
191
- await Promise.resolve(this.newValue(i.value));
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
- return (await this.channel.call("getValue")).value;
272
+ const r = await this.channel.call("getValue");
273
+ return r.value;
196
274
  }
197
- async setValue(e) {
198
- const t = await Promise.resolve(this.validate());
199
- return await this.channel.call("setValue", { value: e, valid: t });
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(e) {
202
- return (await this.channel.call("getValueByPath", { path: e })).value;
279
+ async getValueByPath(path) {
280
+ const r = await this.channel.call("getValueByPath", { path });
281
+ return r.value;
203
282
  }
204
- async setValueByPath(e, t) {
205
- return this.channel.call("setValueByPath", { value: t, path: e });
283
+ async setValueByPath(path, value) {
284
+ return this.channel.call("setValueByPath", { value, path });
206
285
  }
207
- }
208
- class d extends o {
209
- static connect({ trustedOrigins: e }) {
210
- const { targetOrigin: t, channelId: n, applicationId: r, debug: i } = c(e), a = new d(t, n, r, i);
211
- return a.connect().then(() => a);
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(e, t, n, r) {
214
- super(e, t, n, r);
295
+ constructor(targetOrigin, channelId, applicationId, debug) {
296
+ super(targetOrigin, channelId, applicationId, debug);
215
297
  }
216
- close(e) {
217
- return this.channel.call("complete", { value: e });
298
+ close(value) {
299
+ return this.channel.call("complete", { value });
218
300
  }
219
- }
220
- class g extends o {
221
- static connect({ trustedOrigins: e }) {
222
- const { targetOrigin: t, channelId: n, applicationId: r, debug: i } = c(e), a = new g(t, n, r, i);
223
- return a.connect().then(() => a);
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(e, t, n, r) {
226
- super(e, t, n, r);
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(e) {
232
- return this.channel.call("setBreadcrumbs", { breadcrumbs: e });
316
+ setBreadcrumbs(breadcrumbs) {
317
+ return this.channel.call("setBreadcrumbs", { breadcrumbs });
233
318
  }
234
- }
319
+ };
235
320
  export {
236
- h as ContelloCustomProperty,
237
- d as ContelloDialog,
238
- m as ContelloDialogRef,
239
- g as ContelloExtension,
240
- w as ExtensionChannel
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
- "description": "Contello extension library",
4
- "scripts": {
5
- "start": "vite build --watch",
6
- "build": "rm -rf dist/ && vite build",
7
- "lint": "eslint . --report-unused-disable-directives --max-warnings 0",
8
- "patch": "node ci/patcher.js"
3
+ "version": "8.21.2",
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
- "devDependencies": {
11
- "@types/node": "^24.0.10",
12
- "@typescript-eslint/eslint-plugin": "^8.35.1",
13
- "eslint": "^9.30.1",
14
- "eslint-config-prettier": "^10.1.5",
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.umd.cjs"
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
- "version": "8.21.0"
41
- }
32
+ }
33
+ }
@@ -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"})});