@nookuio/iframe 1.0.0 → 1.0.1

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/editor.d.ts CHANGED
@@ -2,10 +2,4 @@ import type { CoreIframeContext, CoreIframeEvents, EditorContext, EditorEvents }
2
2
  /**
3
3
  * This function should be called inside the editor
4
4
  */
5
- export declare function createEditorClient<IframeContext extends CoreIframeContext, IframeEvents extends CoreIframeEvents>(iframe: string | HTMLIFrameElement): { [K in keyof IframeContext]: IframeContext[K] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K]>) => Promise<Awaited<ReturnType<IframeContext[K]>>> : IframeContext[K] extends any[] ? Promise<IframeContext[K]> : IframeContext[K] extends object ? IframeContext[K] extends infer T ? { [K_2 in keyof T]: T[K_2] extends (...args: any[]) => any ? (...args: Parameters<T[K_2]>) => Promise<Awaited<ReturnType<T[K_2]>>> : T[K_2] extends any[] ? Promise<T[K_2]> : T[K_2] extends object ? T[K_2] extends infer T_1 ? { [K_3 in keyof T_1]: T_1[K_3] extends (...args: any[]) => any ? (...args: Parameters<T_1[K_3]>) => Promise<Awaited<ReturnType<T_1[K_3]>>> : T_1[K_3] extends any[] ? Promise<T_1[K_3]> : T_1[K_3] extends object ? T_1[K_3] extends infer T_2 ? { [K_4 in keyof T_2]: T_2[K_4] extends (...args: any[]) => any ? (...args: Parameters<T_2[K_4]>) => Promise<Awaited<ReturnType<T_2[K_4]>>> : T_2[K_4] extends any[] ? Promise<T_2[K_4]> : T_2[K_4] extends object ? T_2[K_4] extends infer T_3 ? { [K_5 in keyof T_3]: T_3[K_5] extends (...args: any[]) => any ? (...args: Parameters<T_3[K_5]>) => Promise<Awaited<ReturnType<T_3[K_5]>>> : T_3[K_5] extends any[] ? Promise<T_3[K_5]> : T_3[K_5] extends object ? T_3[K_5] extends infer T_4 ? { [K_6 in keyof T_4]: T_4[K_6] extends (...args: any[]) => any ? (...args: Parameters<T_4[K_6]>) => Promise<Awaited<ReturnType<T_4[K_6]>>> : T_4[K_6] extends any[] ? Promise<T_4[K_6]> : T_4[K_6] extends object ? T_4[K_6] extends infer T_5 ? { [K_7 in keyof T_5]: T_5[K_7] extends (...args: any[]) => any ? (...args: Parameters<T_5[K_7]>) => Promise<Awaited<ReturnType<T_5[K_7]>>> : T_5[K_7] extends any[] ? Promise<T_5[K_7]> : T_5[K_7] extends object ? T_5[K_7] extends infer T_6 ? { [K_8 in keyof T_6]: T_6[K_8] extends (...args: any[]) => any ? (...args: Parameters<T_6[K_8]>) => Promise<Awaited<ReturnType<T_6[K_8]>>> : T_6[K_8] extends any[] ? Promise<T_6[K_8]> : T_6[K_8] extends object ? T_6[K_8] extends infer T_7 ? { [K_9 in keyof T_7]: T_7[K_9] extends (...args: any[]) => any ? (...args: Parameters<T_7[K_9]>) => Promise<Awaited<ReturnType<T_7[K_9]>>> : T_7[K_9] extends any[] ? Promise<T_7[K_9]> : T_7[K_9] extends object ? T_7[K_9] extends infer T_8 ? { [K_10 in keyof T_8]: T_8[K_10] extends (...args: any[]) => any ? (...args: Parameters<T_8[K_10]>) => Promise<Awaited<ReturnType<T_8[K_10]>>> : T_8[K_10] extends any[] ? Promise<T_8[K_10]> : T_8[K_10] extends object ? T_8[K_10] extends infer T_9 ? { [K_11 in keyof T_9]: T_9[K_11] extends (...args: any[]) => any ? (...args: Parameters<T_9[K_11]>) => Promise<Awaited<ReturnType<T_9[K_11]>>> : T_9[K_11] extends any[] ? Promise<T_9[K_11]> : T_9[K_11] extends object ? /*elided*/ any : Promise<T_9[K_11]>; } : never : Promise<T_8[K_10]>; } : never : Promise<T_7[K_9]>; } : never : Promise<T_6[K_8]>; } : never : Promise<T_5[K_7]>; } : never : Promise<T_4[K_6]>; } : never : Promise<T_3[K_5]>; } : never : Promise<T_2[K_4]>; } : never : Promise<T_1[K_3]>; } : never : Promise<T[K_2]>; } : never : Promise<IframeContext[K]>; } & {
6
- $context: EditorContext;
7
- on: <E extends keyof IframeEvents>(eventName: E, listener: (...args: IframeEvents[E] extends infer T ? T extends IframeEvents[E] ? T extends (...args: infer A) => any ? A : never : never : never) => void) => void;
8
- off: <E extends keyof IframeEvents>(eventName: E, listener: (...args: IframeEvents[E] extends infer T ? T extends IframeEvents[E] ? T extends (...args: infer A) => any ? A : never : never : never) => void) => void;
9
- emit: <E extends never>(eventName: E, ...args: EditorEvents[E] extends infer T ? T extends EditorEvents[E] ? T extends (...args: infer A) => any ? A : never : never : never) => void;
10
- removeAllListeners: () => void;
11
- };
5
+ export declare function createEditorClient<IframeContext extends CoreIframeContext, IframeEvents extends CoreIframeEvents>(iframe: string | HTMLIFrameElement): import("@nookuio/rpc").ClientReturn<IframeContext, EditorContext, IframeEvents, EditorEvents>;
package/dist/editor.js CHANGED
@@ -5,60 +5,62 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createEditorClient = createEditorClient;
7
7
  var _constants = require("./constants");
8
- var _createClient = require("./createClient");
8
+ var _rpc = require("@nookuio/rpc");
9
9
  var _telejson = require("telejson");
10
- function y(a) {
11
- const n = (0, _telejson.parse)(a, {
10
+ function l(s) {
11
+ const a = (0, _telejson.parse)(s, {
12
12
  maxDepth: 1 / 0
13
13
  }),
14
- r = e => {
15
- if (Array.isArray(e)) e.forEach((t, s) => {
16
- e[s] = r(t);
17
- });else if (e && typeof e == "object") {
18
- if (e.hasOwnProperty("__v_isRef")) return {
19
- ...e,
14
+ n = r => {
15
+ if (Array.isArray(r)) r.forEach((e, t) => {
16
+ r[t] = n(e);
17
+ });else if (r && typeof r == "object") {
18
+ if (r.hasOwnProperty("__v_isRef")) return {
19
+ ...r,
20
20
  __v_isRef: "true",
21
- value: e.hasOwnProperty("value") && e.value !== void 0 ? e.value : e._value ?? e._rawValue
21
+ value: r.hasOwnProperty("value") && r.value !== void 0 ? r.value : r._value ?? r._rawValue
22
22
  };
23
- Object.keys(e).forEach(t => {
24
- e[t] = r(e[t]);
23
+ Object.keys(r).forEach(e => {
24
+ r[e] = n(r[e]);
25
25
  });
26
26
  }
27
- return e;
27
+ return r;
28
28
  };
29
- return r(n);
29
+ return n(a);
30
30
  }
31
- function createEditorClient(a) {
32
- let n;
33
- function r() {
34
- return (!n || !n.isConnected) && (n = typeof a == "string" ? document.querySelector(`#${a}`) : a), n;
31
+ function createEditorClient(s) {
32
+ let a;
33
+ function n() {
34
+ return (!a || !a.isConnected) && (a = typeof s == "string" ? document.querySelector(`#${s}`) : s), a;
35
35
  }
36
- return (0, _createClient.createClient)({}, {
36
+ const i = new Map();
37
+ return window.addEventListener("message", async e => {
38
+ if (typeof e.data != "object" || e.data.source !== _constants.IFRAME_SOURCE_NAME || e.data.type === "event" || !e.data.response) return;
39
+ const t = i.get(e.data.response.id);
40
+ t && (i.delete(e.data.response.id), t(e.data.response));
41
+ }), (0, _rpc.createClient)({}, {
37
42
  handle(e) {
38
43
  window.addEventListener("message", async t => {
39
44
  if (typeof t.data != "object" || t.data.source !== _constants.IFRAME_SOURCE_NAME || t.data.type === "event" || !t.data.request) return;
40
- const s = r();
41
- if (!s) return;
42
- const o = await e(t.data.request);
43
- s.contentWindow?.postMessage({
45
+ const o = n();
46
+ if (!o) return;
47
+ const d = await e(t.data.request);
48
+ o.contentWindow?.postMessage({
44
49
  source: _constants.EDITOR_SOURCE_NAME,
45
- response: o
50
+ response: d
46
51
  }, "*");
47
52
  });
48
53
  },
49
54
  async invoke(e) {
50
- const t = r();
51
- return t ? new Promise((s, o) => {
52
- const c = setTimeout(() => {
53
- window.removeEventListener("message", d), o(new Error("Request timed out"));
54
- }, 5e3);
55
- function d(i) {
56
- typeof i.data == "object" && (i.data.source !== _constants.IFRAME_SOURCE_NAME || !i.data.response || i.data.response.id === e.id && (clearTimeout(c), window.removeEventListener("message", d), s(i.data.response)));
57
- }
58
- t.contentWindow?.postMessage({
55
+ const t = n();
56
+ return t ? new Promise((o, d) => {
57
+ const c = setTimeout(() => d(new Error("Request timed out")), 5e3);
58
+ i.set(e.id, m => {
59
+ clearTimeout(c), o(m);
60
+ }), t.contentWindow?.postMessage({
59
61
  source: _constants.EDITOR_SOURCE_NAME,
60
62
  request: e
61
- }, "*"), window.addEventListener("message", d);
63
+ }, "*");
62
64
  }) : {
63
65
  type: "response",
64
66
  id: e.id,
@@ -67,7 +69,7 @@ function createEditorClient(a) {
67
69
  };
68
70
  },
69
71
  emit(e) {
70
- const t = r();
72
+ const t = n();
71
73
  t && t.contentWindow?.postMessage({
72
74
  source: _constants.EDITOR_SOURCE_NAME,
73
75
  type: "event",
@@ -79,7 +81,11 @@ function createEditorClient(a) {
79
81
  typeof t.data == "object" && (t.data.source !== _constants.IFRAME_SOURCE_NAME || t.data.type !== "event" || !t.data.request || e(t.data.request));
80
82
  });
81
83
  },
82
- deserialize: y,
84
+ destroy() {
85
+ i.clear();
86
+ },
87
+ timeout: 5e3,
88
+ deserialize: l,
83
89
  serialize: e => (0, _telejson.stringify)(e, {
84
90
  maxDepth: 1 / 0
85
91
  })
package/dist/editor.mjs CHANGED
@@ -1,55 +1,57 @@
1
1
  import { EDITOR_SOURCE_NAME as f, IFRAME_SOURCE_NAME as u } from "./constants.mjs";
2
- import { createClient as E } from "./createClient.mjs";
3
- import { stringify as p, parse as l } from "telejson";
4
- function y(a) {
5
- const n = l(a, { maxDepth: 1 / 0 }), r = (e) => {
6
- if (Array.isArray(e))
7
- e.forEach((t, s) => {
8
- e[s] = r(t);
2
+ import { createClient as p } from "@nookuio/rpc";
3
+ import { stringify as y, parse as E } from "telejson";
4
+ function l(s) {
5
+ const a = E(s, { maxDepth: 1 / 0 }), n = (r) => {
6
+ if (Array.isArray(r))
7
+ r.forEach((e, t) => {
8
+ r[t] = n(e);
9
9
  });
10
- else if (e && typeof e == "object") {
11
- if (e.hasOwnProperty("__v_isRef"))
10
+ else if (r && typeof r == "object") {
11
+ if (r.hasOwnProperty("__v_isRef"))
12
12
  return {
13
- ...e,
13
+ ...r,
14
14
  __v_isRef: "true",
15
15
  // converted to string
16
- value: e.hasOwnProperty("value") && e.value !== void 0 ? e.value : e._value ?? e._rawValue
16
+ value: r.hasOwnProperty("value") && r.value !== void 0 ? r.value : r._value ?? r._rawValue
17
17
  };
18
- Object.keys(e).forEach((t) => {
19
- e[t] = r(e[t]);
18
+ Object.keys(r).forEach((e) => {
19
+ r[e] = n(r[e]);
20
20
  });
21
21
  }
22
- return e;
22
+ return r;
23
23
  };
24
- return r(n);
24
+ return n(a);
25
25
  }
26
- export function createEditorClient(a) {
27
- let n;
28
- function r() {
29
- return (!n || !n.isConnected) && (n = typeof a == "string" ? document.querySelector(`#${a}`) : a), n;
26
+ export function createEditorClient(s) {
27
+ let a;
28
+ function n() {
29
+ return (!a || !a.isConnected) && (a = typeof s == "string" ? document.querySelector(`#${s}`) : s), a;
30
30
  }
31
- return E(
31
+ const i = /* @__PURE__ */ new Map();
32
+ return window.addEventListener("message", async (e) => {
33
+ if (typeof e.data != "object" || e.data.source !== u || e.data.type === "event" || !e.data.response) return;
34
+ const t = i.get(e.data.response.id);
35
+ t && (i.delete(e.data.response.id), t(e.data.response));
36
+ }), p(
32
37
  {},
33
38
  {
34
39
  handle(e) {
35
40
  window.addEventListener("message", async (t) => {
36
41
  if (typeof t.data != "object" || t.data.source !== u || t.data.type === "event" || !t.data.request) return;
37
- const s = r();
38
- if (!s) return;
39
- const o = await e(t.data.request);
40
- s.contentWindow?.postMessage({ source: f, response: o }, "*");
42
+ const o = n();
43
+ if (!o) return;
44
+ const d = await e(t.data.request);
45
+ o.contentWindow?.postMessage({ source: f, response: d }, "*");
41
46
  });
42
47
  },
43
48
  async invoke(e) {
44
- const t = r();
45
- return t ? new Promise((s, o) => {
46
- const c = setTimeout(() => {
47
- window.removeEventListener("message", d), o(new Error("Request timed out"));
48
- }, 5e3);
49
- function d(i) {
50
- typeof i.data == "object" && (i.data.source !== u || !i.data.response || i.data.response.id === e.id && (clearTimeout(c), window.removeEventListener("message", d), s(i.data.response)));
51
- }
52
- t.contentWindow?.postMessage({ source: f, request: e }, "*"), window.addEventListener("message", d);
49
+ const t = n();
50
+ return t ? new Promise((o, d) => {
51
+ const c = setTimeout(() => d(new Error("Request timed out")), 5e3);
52
+ i.set(e.id, (m) => {
53
+ clearTimeout(c), o(m);
54
+ }), t.contentWindow?.postMessage({ source: f, request: e }, "*");
53
55
  }) : {
54
56
  type: "response",
55
57
  id: e.id,
@@ -58,7 +60,7 @@ export function createEditorClient(a) {
58
60
  };
59
61
  },
60
62
  emit(e) {
61
- const t = r();
63
+ const t = n();
62
64
  t && t.contentWindow?.postMessage({ source: f, type: "event", request: e }, "*");
63
65
  },
64
66
  handleEvent(e) {
@@ -66,8 +68,12 @@ export function createEditorClient(a) {
66
68
  typeof t.data == "object" && (t.data.source !== u || t.data.type !== "event" || !t.data.request || e(t.data.request));
67
69
  });
68
70
  },
69
- deserialize: y,
70
- serialize: (e) => p(e, { maxDepth: 1 / 0 })
71
+ destroy() {
72
+ i.clear();
73
+ },
74
+ timeout: 5e3,
75
+ deserialize: l,
76
+ serialize: (e) => y(e, { maxDepth: 1 / 0 })
71
77
  }
72
78
  );
73
79
  }
package/dist/iframe.d.ts CHANGED
@@ -1,18 +1,12 @@
1
- import type { CoreIframeContext, EditorEvents, VueIframeContext, VueIframeEvents } from './types';
1
+ import type { CoreIframeContext, EditorContext, EditorEvents, VueIframeContext, VueIframeEvents } from './types';
2
2
  /**
3
3
  * This function should be called inside the iframe
4
4
  */
5
- export declare function createVueIframeClient(vueCtx: Omit<VueIframeContext, keyof CoreIframeContext>, options?: {
5
+ export declare function createVueIframeClient(vueCtx: Omit<VueIframeContext, Exclude<keyof CoreIframeContext, 'updateCode'>>, options?: {
6
6
  /**
7
7
  * If true, the default console methods will be disabled
8
8
  *
9
9
  * @default false
10
10
  */
11
11
  disableDefaultConsole?: boolean;
12
- }): {} & {
13
- $context: VueIframeContext;
14
- on: <E extends never>(eventName: E, listener: (...args: EditorEvents[E] extends infer T ? T extends EditorEvents[E] ? T extends (...args: infer A) => any ? A : never : never : never) => void) => void;
15
- off: <E extends never>(eventName: E, listener: (...args: EditorEvents[E] extends infer T ? T extends EditorEvents[E] ? T extends (...args: infer A) => any ? A : never : never : never) => void) => void;
16
- emit: <E extends keyof VueIframeEvents>(eventName: E, ...args: VueIframeEvents[E] extends infer T ? T extends VueIframeEvents[E] ? T extends (...args: infer A) => any ? A : never : never : never) => void;
17
- removeAllListeners: () => void;
18
- };
12
+ }): import("@nookuio/rpc").ClientReturn<EditorContext, VueIframeContext, EditorEvents, VueIframeEvents>;
package/dist/iframe.js CHANGED
@@ -5,138 +5,152 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createVueIframeClient = createVueIframeClient;
7
7
  var _constants = require("./constants");
8
- var _createClient = require("./createClient");
8
+ var _rpc = require("@nookuio/rpc");
9
9
  var _telejson = require("telejson");
10
- function b(u, d) {
11
- return `[data-node-id="${d}"][data-node-path="${u}"]`;
10
+ function u(l, t) {
11
+ return `[data-node-id="${t}"][data-node-path="${l}"]`;
12
12
  }
13
- function h(u, d) {
14
- const s = document.querySelectorAll(u);
15
- if (!s?.length) return;
16
- const l = Array.from(s).map(f => {
17
- const c = f.getBoundingClientRect(),
18
- g = {
19
- width: c.width,
20
- height: c.height,
21
- top: c.top,
22
- left: c.left
13
+ function y(l, t) {
14
+ const n = document.querySelectorAll(l);
15
+ if (!n?.length) return;
16
+ const o = Array.from(n).map(r => {
17
+ const s = r.getBoundingClientRect(),
18
+ d = {
19
+ width: s.width,
20
+ height: s.height,
21
+ top: s.top,
22
+ left: s.left
23
23
  };
24
- if (!d) return g;
25
- const m = window.getComputedStyle(f) ?? {};
24
+ if (!t) return d;
25
+ const i = window.getComputedStyle(r) ?? {};
26
26
  return {
27
- ...g,
27
+ ...d,
28
28
  padding: {
29
- top: m.paddingTop,
30
- right: m.paddingRight,
31
- bottom: m.paddingBottom,
32
- left: m.paddingLeft
29
+ top: i.paddingTop,
30
+ right: i.paddingRight,
31
+ bottom: i.paddingBottom,
32
+ left: i.paddingLeft
33
33
  }
34
34
  };
35
35
  });
36
- if (l?.length) return l.length === 1 ? l[0] : l;
36
+ if (o?.length) return o.length === 1 ? o[0] : o;
37
37
  }
38
- function createVueIframeClient(u, d) {
39
- const s = (0, _createClient.createClient)({
40
- ...u,
41
- async editText({
42
- path: e,
43
- id: t
44
- }) {
45
- if (!e || t === void 0) return;
46
- const i = b(e, t),
47
- n = document.querySelector(i);
48
- if (!n) return;
49
- n.style.outline = "none", n.setAttribute("spellcheck", "false"), n.setAttribute("contenteditable", "true");
50
- const o = a => {
51
- const E = a.target;
52
- if (!E) return;
53
- const y = E.innerText;
54
- n.removeAttribute("contenteditable"), s.emit("text-update", {
55
- path: e,
56
- id: t,
57
- content: y
58
- }), n.removeEventListener("blur", o), n.removeEventListener("keydown", r), n.blur();
59
- },
60
- r = a => {
61
- a.key === "Enter" && (a.stopPropagation(), a.preventDefault(), o(a));
62
- };
63
- n.addEventListener("keydown", r), n.addEventListener("blur", o), n.focus();
64
- },
65
- async getHoveredElement({
66
- path: e,
67
- id: t,
68
- x: i,
69
- y: n
70
- }) {
71
- if (e) if (i !== void 0 && n !== void 0) {
72
- let o = document.elementFromPoint(i, n);
73
- if (!o) return;
74
- let r = o.getAttribute("data-node-path");
75
- if (r !== e) {
76
- if (!o.parentElement) return;
77
- for (; o.parentElement && r !== e;) o = o.parentElement, r = o.getAttribute("data-node-path");
78
- }
79
- const a = o.getAttribute("data-node-id");
80
- if (!a) return;
81
- const E = b(e, a),
82
- y = h(E, !1);
83
- return y ? {
84
- id: a,
85
- path: e,
86
- data: y
87
- } : void 0;
88
- } else {
89
- const o = b(e, t),
90
- r = h(o, !1);
91
- return r ? {
92
- id: t,
93
- path: e,
94
- data: r
95
- } : void 0;
96
- }
97
- },
98
- async getPageHeight() {
99
- return document.body.scrollHeight > 0 ? document.body.scrollHeight : document.documentElement.scrollHeight;
100
- },
101
- async getSelectedElement({
102
- path: e,
103
- id: t
104
- }) {
105
- const i = b(e, t),
106
- n = h(i, !0);
107
- if (n) return {
108
- path: e,
109
- id: t,
110
- data: n
38
+ function C(l) {
39
+ return {
40
+ async editText({
41
+ path: t,
42
+ id: n
43
+ }) {
44
+ if (!t || n === void 0) return;
45
+ const o = u(t, n),
46
+ r = document.querySelector(o);
47
+ if (!r) return;
48
+ r.style.outline = "none", r.setAttribute("spellcheck", "false"), r.setAttribute("contenteditable", "true");
49
+ const s = i => {
50
+ const c = i.target;
51
+ if (!c) return;
52
+ const e = c.innerText;
53
+ r.removeAttribute("contenteditable"), l?.onTextUpdate?.({
54
+ path: t,
55
+ id: n,
56
+ content: e
57
+ }), r.removeEventListener("blur", s), r.removeEventListener("keydown", d), r.blur();
58
+ },
59
+ d = i => {
60
+ i.key === "Enter" && (i.stopPropagation(), i.preventDefault(), s(i));
111
61
  };
112
- },
113
- setTheme(e) {
114
- const t = document.querySelector("body");
115
- e === "dark" ? (document.documentElement.classList.remove("light"), document.documentElement.classList.add("dark"), t && (t.classList.remove("light"), t.classList.add("dark"))) : (document.documentElement.classList.remove("dark"), document.documentElement.classList.add("light"), t && (t.classList.remove("dark"), t.classList.add("light")));
62
+ r.addEventListener("keydown", d), r.addEventListener("blur", s), r.focus();
63
+ },
64
+ async getHoveredElement({
65
+ path: t,
66
+ id: n,
67
+ x: o,
68
+ y: r
69
+ }) {
70
+ if (t) if (o !== void 0 && r !== void 0) {
71
+ let s = document.elementFromPoint(o, r);
72
+ if (!s) return;
73
+ let d = s.getAttribute("data-node-path");
74
+ if (d !== t) {
75
+ if (!s.parentElement) return;
76
+ for (; s.parentElement && d !== t;) s = s.parentElement, d = s.getAttribute("data-node-path");
77
+ }
78
+ const i = s.getAttribute("data-node-id");
79
+ if (!i) return;
80
+ const c = u(t, i),
81
+ e = y(c, !1);
82
+ return e ? {
83
+ id: i,
84
+ path: t,
85
+ data: e
86
+ } : void 0;
87
+ } else {
88
+ const s = u(t, n),
89
+ d = y(s, !1);
90
+ return d ? {
91
+ id: n,
92
+ path: t,
93
+ data: d
94
+ } : void 0;
116
95
  }
96
+ },
97
+ async getPageHeight() {
98
+ return document.body.scrollHeight > 0 ? document.body.scrollHeight : document.documentElement.scrollHeight;
99
+ },
100
+ async getSelectedElement({
101
+ path: t,
102
+ id: n
103
+ }) {
104
+ const o = u(t, n),
105
+ r = y(o, !0);
106
+ if (r) return {
107
+ path: t,
108
+ id: n,
109
+ data: r
110
+ };
111
+ },
112
+ setTheme(t) {
113
+ const n = document.querySelector("body");
114
+ t === "dark" ? (document.documentElement.classList.remove("light"), document.documentElement.classList.add("dark"), n && (n.classList.remove("light"), n.classList.add("dark"))) : (document.documentElement.classList.remove("dark"), document.documentElement.classList.add("light"), n && (n.classList.remove("dark"), n.classList.add("light")));
115
+ },
116
+ async updateCode(t, n) {
117
+ typeof n > "u" ? document.body.innerHTML = "" : document.body.innerHTML = n, l?.onResize?.(await this.getPageHeight());
118
+ }
119
+ };
120
+ }
121
+ function createVueIframeClient(l, t) {
122
+ const n = new Map();
123
+ window.addEventListener("message", async e => {
124
+ if (typeof e.data != "object" || e.data.source !== _constants.EDITOR_SOURCE_NAME || e.data.type === "event" || !e.data.response) return;
125
+ const a = n.get(e.data.response.id);
126
+ a && (n.delete(e.data.response.id), a(e.data.response));
127
+ });
128
+ const o = (0, _rpc.createClient)({
129
+ ...l,
130
+ ...C({
131
+ onResize: (...e) => o?.emit("resize", ...e),
132
+ onTextUpdate: (...e) => o?.emit("text-update", ...e)
133
+ })
117
134
  }, {
118
135
  handle(e) {
119
- window.addEventListener("message", async t => {
120
- if (typeof t.data != "object" || t.data.source !== _constants.EDITOR_SOURCE_NAME || t.data.type === "event" || !t.data.request) return;
121
- const i = await e(t.data.request);
136
+ window.addEventListener("message", async a => {
137
+ if (typeof a.data != "object" || a.data.source !== _constants.EDITOR_SOURCE_NAME || a.data.type === "event" || !a.data.request) return;
138
+ const m = await e(a.data.request);
122
139
  window.parent.postMessage({
123
140
  source: _constants.IFRAME_SOURCE_NAME,
124
- response: i
141
+ response: m
125
142
  }, "*");
126
143
  });
127
144
  },
128
145
  invoke(e) {
129
- return new Promise((t, i) => {
130
- const n = setTimeout(() => {
131
- window.removeEventListener("message", o), i(new Error("Request timed out"));
132
- }, 5e3);
133
- function o(r) {
134
- typeof r.data == "object" && (r.data.source !== _constants.EDITOR_SOURCE_NAME || !r.data.response || r.data.response.id === e.id && (clearTimeout(n), window.removeEventListener("message", o), t(r.data.response)));
135
- }
136
- window.parent.postMessage({
146
+ return new Promise((a, m) => {
147
+ const E = setTimeout(() => m(new Error("Request timed out")), 5e3);
148
+ n.set(e.id, p => {
149
+ clearTimeout(E), a(p);
150
+ }), window.parent.postMessage({
137
151
  source: _constants.IFRAME_SOURCE_NAME,
138
152
  request: e
139
- }, "*"), window.addEventListener("message", o);
153
+ }, "*");
140
154
  });
141
155
  },
142
156
  emit(e) {
@@ -147,31 +161,35 @@ function createVueIframeClient(u, d) {
147
161
  }, "*");
148
162
  },
149
163
  handleEvent(e) {
150
- window.addEventListener("message", t => {
151
- typeof t.data == "object" && (t.data.source !== _constants.EDITOR_SOURCE_NAME || t.data.type !== "event" || !t.data.request || e(t.data.request));
164
+ window.addEventListener("message", a => {
165
+ typeof a.data == "object" && (a.data.source !== _constants.EDITOR_SOURCE_NAME || a.data.type !== "event" || !a.data.request || e(a.data.request));
152
166
  });
153
167
  },
168
+ destroy() {
169
+ n.clear();
170
+ },
154
171
  deserialize: e => (0, _telejson.parse)(e),
155
172
  serialize: e => (0, _telejson.stringify)(e, {
156
173
  maxDepth: 1 / 0
157
- })
174
+ }),
175
+ timeout: 5e3
158
176
  }),
159
- l = d?.disableDefaultConsole ? void 0 : console.log.bind(console),
160
- f = d?.disableDefaultConsole ? void 0 : console.warn.bind(console),
161
- c = d?.disableDefaultConsole ? void 0 : console.error.bind(console),
162
- g = d?.disableDefaultConsole ? void 0 : console.info.bind(console);
177
+ r = t?.disableDefaultConsole ? void 0 : console.log.bind(console),
178
+ s = t?.disableDefaultConsole ? void 0 : console.warn.bind(console),
179
+ d = t?.disableDefaultConsole ? void 0 : console.error.bind(console),
180
+ i = t?.disableDefaultConsole ? void 0 : console.info.bind(console);
163
181
  return console.log = (...e) => {
164
- s.emit("console", "log", e), l?.(...e);
182
+ o.emit("console", "log", e), r?.(...e);
165
183
  }, console.warn = (...e) => {
166
- f?.(...e);
184
+ s?.(...e);
167
185
  }, console.error = (...e) => {
168
- s.emit("console", "error", e), c?.(...e);
186
+ o.emit("console", "error", e), d?.(...e);
169
187
  }, console.info = (...e) => {
170
- s.emit("console", "info", e), g?.(...e);
171
- }, window.addEventListener("resize", async () => s.emit("resize", await s.$context.getPageHeight())), new MutationObserver(async () => s.emit("resize", await s.$context.getPageHeight())).observe(document.body, {
188
+ o.emit("console", "info", e), i?.(...e);
189
+ }, window.addEventListener("resize", async () => o.emit("resize", await o.$context.getPageHeight())), new MutationObserver(async () => o.emit("resize", await o.$context.getPageHeight())).observe(document.body, {
172
190
  childList: !0,
173
191
  subtree: !0,
174
192
  attributes: !0,
175
193
  characterData: !0
176
- }), s.emit("ready"), s;
194
+ }), o.emit("ready"), o;
177
195
  }
package/dist/iframe.mjs CHANGED
@@ -1,161 +1,179 @@
1
- import { EDITOR_SOURCE_NAME as p, IFRAME_SOURCE_NAME as w } from "./constants.mjs";
2
- import { createClient as v } from "./createClient.mjs";
3
- import { stringify as C, parse as L } from "telejson";
4
- function b(u, d) {
5
- return `[data-node-id="${d}"][data-node-path="${u}"]`;
1
+ import { EDITOR_SOURCE_NAME as f, IFRAME_SOURCE_NAME as g } from "./constants.mjs";
2
+ import { createClient as b } from "@nookuio/rpc";
3
+ import { stringify as h, parse as w } from "telejson";
4
+ function u(l, t) {
5
+ return `[data-node-id="${t}"][data-node-path="${l}"]`;
6
6
  }
7
- function h(u, d) {
8
- const s = document.querySelectorAll(u);
9
- if (!s?.length) return;
10
- const l = Array.from(s).map((f) => {
11
- const c = f.getBoundingClientRect(), g = {
12
- width: c.width,
13
- height: c.height,
14
- top: c.top,
15
- left: c.left
7
+ function y(l, t) {
8
+ const n = document.querySelectorAll(l);
9
+ if (!n?.length) return;
10
+ const o = Array.from(n).map((r) => {
11
+ const s = r.getBoundingClientRect(), d = {
12
+ width: s.width,
13
+ height: s.height,
14
+ top: s.top,
15
+ left: s.left
16
16
  };
17
- if (!d) return g;
18
- const m = window.getComputedStyle(f) ?? {};
17
+ if (!t) return d;
18
+ const i = window.getComputedStyle(r) ?? {};
19
19
  return {
20
- ...g,
20
+ ...d,
21
21
  padding: {
22
- top: m.paddingTop,
23
- right: m.paddingRight,
24
- bottom: m.paddingBottom,
25
- left: m.paddingLeft
22
+ top: i.paddingTop,
23
+ right: i.paddingRight,
24
+ bottom: i.paddingBottom,
25
+ left: i.paddingLeft
26
26
  }
27
27
  };
28
28
  });
29
- if (l?.length)
30
- return l.length === 1 ? l[0] : l;
29
+ if (o?.length)
30
+ return o.length === 1 ? o[0] : o;
31
31
  }
32
- export function createVueIframeClient(u, d) {
33
- const s = v(
34
- {
35
- ...u,
36
- async editText({ path: e, id: t }) {
37
- if (!e || t === void 0) return;
38
- const i = b(e, t), n = document.querySelector(i);
39
- if (!n) return;
40
- n.style.outline = "none", n.setAttribute("spellcheck", "false"), n.setAttribute("contenteditable", "true");
41
- const o = (a) => {
42
- const E = a.target;
43
- if (!E) return;
44
- const y = E.innerText;
45
- n.removeAttribute("contenteditable"), s.emit("text-update", { path: e, id: t, content: y }), n.removeEventListener("blur", o), n.removeEventListener("keydown", r), n.blur();
46
- }, r = (a) => {
47
- a.key === "Enter" && (a.stopPropagation(), a.preventDefault(), o(a));
48
- };
49
- n.addEventListener("keydown", r), n.addEventListener("blur", o), n.focus();
50
- },
51
- async getHoveredElement({ path: e, id: t, x: i, y: n }) {
52
- if (e)
53
- if (i !== void 0 && n !== void 0) {
54
- let o = document.elementFromPoint(i, n);
55
- if (!o) return;
56
- let r = o.getAttribute("data-node-path");
57
- if (r !== e) {
58
- if (!o.parentElement) return;
59
- for (; o.parentElement && r !== e; )
60
- o = o.parentElement, r = o.getAttribute("data-node-path");
61
- }
62
- const a = o.getAttribute("data-node-id");
63
- if (!a) return;
64
- const E = b(e, a), y = h(E, !1);
65
- return y ? {
66
- id: a,
67
- path: e,
68
- data: y
69
- } : void 0;
70
- } else {
71
- const o = b(e, t), r = h(o, !1);
72
- return r ? {
73
- id: t,
74
- path: e,
75
- data: r
76
- } : void 0;
32
+ function C(l) {
33
+ return {
34
+ async editText({ path: t, id: n }) {
35
+ if (!t || n === void 0) return;
36
+ const o = u(t, n), r = document.querySelector(o);
37
+ if (!r) return;
38
+ r.style.outline = "none", r.setAttribute("spellcheck", "false"), r.setAttribute("contenteditable", "true");
39
+ const s = (i) => {
40
+ const c = i.target;
41
+ if (!c) return;
42
+ const e = c.innerText;
43
+ r.removeAttribute("contenteditable"), l?.onTextUpdate?.({ path: t, id: n, content: e }), r.removeEventListener("blur", s), r.removeEventListener("keydown", d), r.blur();
44
+ }, d = (i) => {
45
+ i.key === "Enter" && (i.stopPropagation(), i.preventDefault(), s(i));
46
+ };
47
+ r.addEventListener("keydown", d), r.addEventListener("blur", s), r.focus();
48
+ },
49
+ async getHoveredElement({ path: t, id: n, x: o, y: r }) {
50
+ if (t)
51
+ if (o !== void 0 && r !== void 0) {
52
+ let s = document.elementFromPoint(o, r);
53
+ if (!s) return;
54
+ let d = s.getAttribute("data-node-path");
55
+ if (d !== t) {
56
+ if (!s.parentElement) return;
57
+ for (; s.parentElement && d !== t; )
58
+ s = s.parentElement, d = s.getAttribute("data-node-path");
77
59
  }
78
- },
79
- async getPageHeight() {
80
- return document.body.scrollHeight > 0 ? document.body.scrollHeight : document.documentElement.scrollHeight;
81
- },
82
- async getSelectedElement({ path: e, id: t }) {
83
- const i = b(e, t), n = h(i, !0);
84
- if (n)
85
- return {
86
- path: e,
87
- id: t,
88
- data: n
89
- };
90
- },
91
- setTheme(e) {
92
- const t = document.querySelector("body");
93
- e === "dark" ? (document.documentElement.classList.remove("light"), document.documentElement.classList.add("dark"), t && (t.classList.remove("light"), t.classList.add("dark"))) : (document.documentElement.classList.remove("dark"), document.documentElement.classList.add("light"), t && (t.classList.remove("dark"), t.classList.add("light")));
94
- }
95
- // updateElementClass({ className, id, path }) {
96
- // const selector = getSelector(path, id);
97
- // const elements = document.querySelectorAll(selector);
98
- // if (!elements?.length) return;
99
- // elements.forEach((elm) => (elm.className = className));
100
- // },
101
- // updateElementAttribute({ attribute, value, id, path }) {
102
- // const selector = getSelector(path, id);
103
- // const elements = document.querySelectorAll(selector);
104
- // if (!elements?.length) return;
105
- // elements.forEach((elm) => elm.setAttribute(attribute, value));
106
- // },
107
- // duplicateElement({ id, path }) {
108
- // const selector = getSelector(path, id);
109
- // const elements = document.querySelectorAll(selector);
110
- // if (!elements?.length) return;
111
- // elements.forEach((elm) => {
112
- // const clone = elm.cloneNode(true) as HTMLElement;
113
- // clone.setAttribute('data-node-id', String(Number(clone.getAttribute('data-node-id')!) + 1));
114
- // elm.parentElement?.insertBefore(clone, elm.nextSibling);
115
- // });
116
- // }
60
+ const i = s.getAttribute("data-node-id");
61
+ if (!i) return;
62
+ const c = u(t, i), e = y(c, !1);
63
+ return e ? {
64
+ id: i,
65
+ path: t,
66
+ data: e
67
+ } : void 0;
68
+ } else {
69
+ const s = u(t, n), d = y(s, !1);
70
+ return d ? {
71
+ id: n,
72
+ path: t,
73
+ data: d
74
+ } : void 0;
75
+ }
76
+ },
77
+ async getPageHeight() {
78
+ return document.body.scrollHeight > 0 ? document.body.scrollHeight : document.documentElement.scrollHeight;
79
+ },
80
+ async getSelectedElement({ path: t, id: n }) {
81
+ const o = u(t, n), r = y(o, !0);
82
+ if (r)
83
+ return {
84
+ path: t,
85
+ id: n,
86
+ data: r
87
+ };
88
+ },
89
+ setTheme(t) {
90
+ const n = document.querySelector("body");
91
+ t === "dark" ? (document.documentElement.classList.remove("light"), document.documentElement.classList.add("dark"), n && (n.classList.remove("light"), n.classList.add("dark"))) : (document.documentElement.classList.remove("dark"), document.documentElement.classList.add("light"), n && (n.classList.remove("dark"), n.classList.add("light")));
92
+ },
93
+ async updateCode(t, n) {
94
+ typeof n > "u" ? document.body.innerHTML = "" : document.body.innerHTML = n, l?.onResize?.(await this.getPageHeight());
95
+ }
96
+ // updateElementClass({ className, id, path }) {
97
+ // const selector = getSelector(path, id);
98
+ // const elements = document.querySelectorAll(selector);
99
+ // if (!elements?.length) return;
100
+ // elements.forEach((elm) => (elm.className = className));
101
+ // },
102
+ // updateElementAttribute({ attribute, value, id, path }) {
103
+ // const selector = getSelector(path, id);
104
+ // const elements = document.querySelectorAll(selector);
105
+ // if (!elements?.length) return;
106
+ // elements.forEach((elm) => elm.setAttribute(attribute, value));
107
+ // },
108
+ // duplicateElement({ id, path }) {
109
+ // const selector = getSelector(path, id);
110
+ // const elements = document.querySelectorAll(selector);
111
+ // if (!elements?.length) return;
112
+ // elements.forEach((elm) => {
113
+ // const clone = elm.cloneNode(true) as HTMLElement;
114
+ // clone.setAttribute('data-node-id', String(Number(clone.getAttribute('data-node-id')!) + 1));
115
+ // elm.parentElement?.insertBefore(clone, elm.nextSibling);
116
+ // });
117
+ // }
118
+ };
119
+ }
120
+ export function createVueIframeClient(l, t) {
121
+ const n = /* @__PURE__ */ new Map();
122
+ window.addEventListener("message", async (e) => {
123
+ if (typeof e.data != "object" || e.data.source !== f || e.data.type === "event" || !e.data.response) return;
124
+ const a = n.get(e.data.response.id);
125
+ a && (n.delete(e.data.response.id), a(e.data.response));
126
+ });
127
+ const o = b(
128
+ {
129
+ ...l,
130
+ ...C({
131
+ onResize: (...e) => o?.emit("resize", ...e),
132
+ onTextUpdate: (...e) => o?.emit("text-update", ...e)
133
+ })
117
134
  },
118
135
  {
119
136
  handle(e) {
120
- window.addEventListener("message", async (t) => {
121
- if (typeof t.data != "object" || t.data.source !== p || t.data.type === "event" || !t.data.request) return;
122
- const i = await e(t.data.request);
123
- window.parent.postMessage({ source: w, response: i }, "*");
137
+ window.addEventListener("message", async (a) => {
138
+ if (typeof a.data != "object" || a.data.source !== f || a.data.type === "event" || !a.data.request) return;
139
+ const m = await e(a.data.request);
140
+ window.parent.postMessage({ source: g, response: m }, "*");
124
141
  });
125
142
  },
126
143
  invoke(e) {
127
- return new Promise((t, i) => {
128
- const n = setTimeout(() => {
129
- window.removeEventListener("message", o), i(new Error("Request timed out"));
130
- }, 5e3);
131
- function o(r) {
132
- typeof r.data == "object" && (r.data.source !== p || !r.data.response || r.data.response.id === e.id && (clearTimeout(n), window.removeEventListener("message", o), t(r.data.response)));
133
- }
134
- window.parent.postMessage({ source: w, request: e }, "*"), window.addEventListener("message", o);
144
+ return new Promise((a, m) => {
145
+ const E = setTimeout(() => m(new Error("Request timed out")), 5e3);
146
+ n.set(e.id, (p) => {
147
+ clearTimeout(E), a(p);
148
+ }), window.parent.postMessage({ source: g, request: e }, "*");
135
149
  });
136
150
  },
137
151
  emit(e) {
138
- window.parent.postMessage({ source: w, type: "event", request: e }, "*");
152
+ window.parent.postMessage({ source: g, type: "event", request: e }, "*");
139
153
  },
140
154
  handleEvent(e) {
141
- window.addEventListener("message", (t) => {
142
- typeof t.data == "object" && (t.data.source !== p || t.data.type !== "event" || !t.data.request || e(t.data.request));
155
+ window.addEventListener("message", (a) => {
156
+ typeof a.data == "object" && (a.data.source !== f || a.data.type !== "event" || !a.data.request || e(a.data.request));
143
157
  });
144
158
  },
145
- deserialize: (e) => L(e),
146
- serialize: (e) => C(e, { maxDepth: 1 / 0 })
159
+ destroy() {
160
+ n.clear();
161
+ },
162
+ deserialize: (e) => w(e),
163
+ serialize: (e) => h(e, { maxDepth: 1 / 0 }),
164
+ timeout: 5e3
147
165
  }
148
- ), l = d?.disableDefaultConsole ? void 0 : console.log.bind(console), f = d?.disableDefaultConsole ? void 0 : console.warn.bind(console), c = d?.disableDefaultConsole ? void 0 : console.error.bind(console), g = d?.disableDefaultConsole ? void 0 : console.info.bind(console);
166
+ ), r = t?.disableDefaultConsole ? void 0 : console.log.bind(console), s = t?.disableDefaultConsole ? void 0 : console.warn.bind(console), d = t?.disableDefaultConsole ? void 0 : console.error.bind(console), i = t?.disableDefaultConsole ? void 0 : console.info.bind(console);
149
167
  return console.log = (...e) => {
150
- s.emit("console", "log", e), l?.(...e);
168
+ o.emit("console", "log", e), r?.(...e);
151
169
  }, console.warn = (...e) => {
152
- f?.(...e);
170
+ s?.(...e);
153
171
  }, console.error = (...e) => {
154
- s.emit("console", "error", e), c?.(...e);
172
+ o.emit("console", "error", e), d?.(...e);
155
173
  }, console.info = (...e) => {
156
- s.emit("console", "info", e), g?.(...e);
157
- }, window.addEventListener("resize", async () => s.emit("resize", await s.$context.getPageHeight())), new MutationObserver(
158
- async () => s.emit("resize", await s.$context.getPageHeight())
174
+ o.emit("console", "info", e), i?.(...e);
175
+ }, window.addEventListener("resize", async () => o.emit("resize", await o.$context.getPageHeight())), new MutationObserver(
176
+ async () => o.emit("resize", await o.$context.getPageHeight())
159
177
  ).observe(document.body, {
160
178
  childList: !0,
161
179
  // observe direct children additions/removals
@@ -165,5 +183,5 @@ export function createVueIframeClient(u, d) {
165
183
  // observe attribute changes (like style, class)
166
184
  characterData: !0
167
185
  // observe text changes
168
- }), s.emit("ready"), s;
186
+ }), o.emit("ready"), o;
169
187
  }
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { RpcEvents } from '@nookuio/rpc';
1
2
  export interface EventRequest {
2
3
  eventName: string;
3
4
  args: any[];
@@ -40,8 +41,10 @@ export interface CoreIframeContext {
40
41
  path: string;
41
42
  id: string;
42
43
  }) => void;
44
+ /** Update the innerHTML of the page */
45
+ updateCode: (path: string, code: string) => void;
43
46
  }
44
- export interface CoreIframeEvents {
47
+ export interface CoreIframeEvents extends RpcEvents {
45
48
  ready: () => void;
46
49
  resize: (height: number) => void;
47
50
  console: (type: 'log' | 'error' | 'info' | 'warn', message: any[]) => void;
@@ -51,15 +54,16 @@ export interface CoreIframeEvents {
51
54
  content: string;
52
55
  }) => void;
53
56
  }
54
- export interface VueIframeContext extends CoreIframeContext {
57
+ export interface VueIframeContext extends Omit<CoreIframeContext, 'updateCode'> {
55
58
  navigateTo: (to: string) => void;
56
59
  getInstanceData: (path: string /** filepath path of the component */) => Record<string, any>;
57
60
  /**
61
+ *
58
62
  * For virtual SFC preview
59
63
  *
60
- * Works only on VIRTUAL_VUE_SFC_RENDERER_PAGE_PATH
64
+ * Works only on `VIRTUAL_VUE_SFC_RENDERER_PAGE_PATH`
61
65
  */
62
- updateVirtualSfcCode: (path: string, code: string) => void;
66
+ updateCode: (path: string, code: string) => void;
63
67
  }
64
68
  export interface VueIframeEvents extends CoreIframeEvents {
65
69
  'route-change': (to: string) => void;
@@ -67,7 +71,7 @@ export interface VueIframeEvents extends CoreIframeEvents {
67
71
  }
68
72
  export interface EditorContext {
69
73
  }
70
- export interface EditorEvents {
74
+ export interface EditorEvents extends RpcEvents {
71
75
  }
72
76
  export interface ElementPosition {
73
77
  top: number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nookuio/iframe",
3
3
  "description": "",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
7
7
  "exports": {
@@ -30,7 +30,8 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "telejson": "7.2.0"
33
+ "telejson": "7.2.0",
34
+ "@nookuio/rpc": "1.0.2"
34
35
  },
35
36
  "devDependencies": {},
36
37
  "keywords": [],
@@ -1,51 +0,0 @@
1
- import type { ContextRequest, ContextResponse, EventRequest } from './types';
2
- type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;
3
- type ClientReturnBase<R, L, RE, LE> = {
4
- [K in keyof R]: R[K] extends (...args: any[]) => any ? (...args: Parameters<R[K]>) => Promise<Awaited<ReturnType<R[K]>>> : R[K] extends any[] ? Promise<R[K]> : R[K] extends object ? ClientReturnBase<R[K], L, RE, LE> : Promise<R[K]>;
5
- };
6
- type ClientReturn<R, L, RE, LE> = ClientReturnBase<R, L, RE, LE> & {
7
- $context: L;
8
- on: <E extends keyof RE>(eventName: E, listener: (...args: ArgumentsType<RE[E]>) => void) => void;
9
- off: <E extends keyof RE>(eventName: E, listener: (...args: ArgumentsType<RE[E]>) => void) => void;
10
- emit: <E extends keyof LE>(eventName: E, ...args: ArgumentsType<LE[E]>) => void;
11
- removeAllListeners: () => void;
12
- };
13
- interface ClientOptions {
14
- /**
15
- * Function to send requests to the remote process
16
- *
17
- * Should return a ContextResponse
18
- */
19
- invoke: (request: ContextRequest) => Promise<ContextResponse>;
20
- /**
21
- * Function to handle incoming requests from the remote process
22
- *
23
- * Will be called only once at the start of the client
24
- */
25
- handle: (handler: (request: ContextRequest) => Promise<ContextResponse>) => void;
26
- /**
27
- * Function to emit events to the remote process
28
- */
29
- emit: (request: EventRequest) => void;
30
- /**
31
- * Function to handle events from the remote process
32
- *
33
- * Will be called only once at the start of the client
34
- */
35
- handleEvent: (handleRequest: (request: EventRequest) => void) => void;
36
- /**
37
- * Custom function to serialize data
38
- *
39
- * by default it uses JSON.stringify
40
- */
41
- serialize?: (data: any) => any;
42
- /**
43
- * Custom function to deserialize data
44
- *
45
- * by default it uses JSON.parse
46
- */
47
- deserialize?: (data: any) => any;
48
- }
49
- type Context = Record<string, any>;
50
- export declare function createClient<R extends Context, L extends Context, RE, LE>(localCtx: L, options: ClientOptions): ClientReturn<R, L, RE, LE>;
51
- export {};
@@ -1,118 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createClient = createClient;
7
- function createClient(y, l) {
8
- const {
9
- invoke: c,
10
- handle: f,
11
- emit: o,
12
- handleEvent: d,
13
- serialize: x,
14
- deserialize: v
15
- } = l;
16
- function g(e) {
17
- try {
18
- return x ? x(e) : JSON.stringify(e);
19
- } catch {
20
- return e;
21
- }
22
- }
23
- function R(e) {
24
- try {
25
- return v ? v(e) : JSON.parse(e);
26
- } catch {
27
- return e;
28
- }
29
- }
30
- const u = new Map();
31
- f(async e => {
32
- const n = p(y, e.key || "");
33
- if (n === void 0) return {
34
- type: "response",
35
- id: e.id,
36
- key: e.key,
37
- result: void 0
38
- };
39
- let r = e.args ? R(e.args) : [];
40
- Array.isArray(r) || (r = [r]);
41
- const s = typeof n == "function" ? await n(...r) : n;
42
- return {
43
- type: "response",
44
- id: e.id,
45
- key: e.key,
46
- result: g(s)
47
- };
48
- }), d(e => {
49
- const n = u.get(e.eventName);
50
- if (!n?.length) return;
51
- let r = e.args ? R(e.args) : [];
52
- Array.isArray(r) || (r = [r]), n.forEach(s => s(...r));
53
- });
54
- const E = new Map();
55
- function m(e) {
56
- const n = e.join(".");
57
- if (E.has(n)) return E.get(n);
58
- const r = async (...i) => {
59
- const t = {
60
- type: "request",
61
- id: `${n}-${Date.now()}-${Math.random()}`,
62
- key: n,
63
- args: g(i)
64
- },
65
- a = await c(t);
66
- return R(a.result);
67
- },
68
- s = new Proxy(r, {
69
- get(i, t) {
70
- if (t === "then") {
71
- const a = i();
72
- return a.then.bind(a);
73
- }
74
- if (t === "catch" || t === "finally") {
75
- const a = i();
76
- return a[t].bind(a);
77
- }
78
- return m([...e, t]);
79
- },
80
- apply(i, t, a) {
81
- return i.apply(t, a);
82
- }
83
- });
84
- return E.set(n, s), s;
85
- }
86
- return new Proxy({}, {
87
- get(e, n) {
88
- if (n === "$context") return y;
89
- if (n === "on") return (s, i) => {
90
- const t = u.get(s);
91
- if (!t) {
92
- u.set(s, [i]);
93
- return;
94
- }
95
- t.push(i);
96
- };
97
- if (n === "off") return (s, i) => {
98
- const t = u.get(s)?.indexOf(i);
99
- t === -1 || t === void 0 || u.get(s)?.splice(t, 1);
100
- };
101
- if (n === "emit") return (s, ...i) => {
102
- o({
103
- eventName: s,
104
- args: g(i)
105
- });
106
- };
107
- if (n === "removeAllListeners") return () => u.clear();
108
- if (typeof n != "symbol") return m([n]);
109
- }
110
- });
111
- }
112
- function p(y, l, c) {
113
- if (!y || typeof y != "object") return c;
114
- const f = Array.isArray(l) ? l : l.replace(/\[(\d+)]/g, ".$1").replace(/\["([^"]+)"\]/g, ".$1").replace(/\['([^']+)'\]/g, ".$1").split(".").filter(Boolean);
115
- let o = y;
116
- for (const d of f) if (o != null && typeof o == "object" && d in o) o = o[d];else return c;
117
- return o;
118
- }
@@ -1,114 +0,0 @@
1
- export function createClient(y, l) {
2
- const { invoke: c, handle: f, emit: o, handleEvent: d, serialize: x, deserialize: v } = l;
3
- function g(e) {
4
- try {
5
- return x ? x(e) : JSON.stringify(e);
6
- } catch {
7
- return e;
8
- }
9
- }
10
- function R(e) {
11
- try {
12
- return v ? v(e) : JSON.parse(e);
13
- } catch {
14
- return e;
15
- }
16
- }
17
- const u = /* @__PURE__ */ new Map();
18
- f(async (e) => {
19
- const n = p(y, e.key || "");
20
- if (n === void 0)
21
- return {
22
- type: "response",
23
- id: e.id,
24
- key: e.key,
25
- result: void 0
26
- };
27
- let r = e.args ? R(e.args) : [];
28
- Array.isArray(r) || (r = [r]);
29
- const s = typeof n == "function" ? await n(...r) : n;
30
- return {
31
- type: "response",
32
- id: e.id,
33
- key: e.key,
34
- result: g(s)
35
- };
36
- }), d((e) => {
37
- const n = u.get(e.eventName);
38
- if (!n?.length) return;
39
- let r = e.args ? R(e.args) : [];
40
- Array.isArray(r) || (r = [r]), n.forEach((s) => s(...r));
41
- });
42
- const E = /* @__PURE__ */ new Map();
43
- function m(e) {
44
- const n = e.join(".");
45
- if (E.has(n))
46
- return E.get(n);
47
- const r = async (...i) => {
48
- const t = {
49
- type: "request",
50
- id: `${n}-${Date.now()}-${Math.random()}`,
51
- // More robust ID
52
- key: n,
53
- args: g(i)
54
- }, a = await c(t);
55
- return R(a.result);
56
- }, s = new Proxy(r, {
57
- get(i, t) {
58
- if (t === "then") {
59
- const a = i();
60
- return a.then.bind(a);
61
- }
62
- if (t === "catch" || t === "finally") {
63
- const a = i();
64
- return a[t].bind(a);
65
- }
66
- return m([...e, t]);
67
- },
68
- apply(i, t, a) {
69
- return i.apply(t, a);
70
- }
71
- });
72
- return E.set(n, s), s;
73
- }
74
- return new Proxy(
75
- {},
76
- {
77
- get(e, n) {
78
- if (n === "$context") return y;
79
- if (n === "on")
80
- return (s, i) => {
81
- const t = u.get(s);
82
- if (!t) {
83
- u.set(s, [i]);
84
- return;
85
- }
86
- t.push(i);
87
- };
88
- if (n === "off")
89
- return (s, i) => {
90
- const t = u.get(s)?.indexOf(i);
91
- t === -1 || t === void 0 || u.get(s)?.splice(t, 1);
92
- };
93
- if (n === "emit")
94
- return (s, ...i) => {
95
- o({ eventName: s, args: g(i) });
96
- };
97
- if (n === "removeAllListeners") return () => u.clear();
98
- if (typeof n != "symbol")
99
- return m([n]);
100
- }
101
- }
102
- );
103
- }
104
- function p(y, l, c) {
105
- if (!y || typeof y != "object") return c;
106
- const f = Array.isArray(l) ? l : l.replace(/\[(\d+)]/g, ".$1").replace(/\["([^"]+)"\]/g, ".$1").replace(/\['([^']+)'\]/g, ".$1").split(".").filter(Boolean);
107
- let o = y;
108
- for (const d of f)
109
- if (o != null && typeof o == "object" && d in o)
110
- o = o[d];
111
- else
112
- return c;
113
- return o;
114
- }