@nookuio/iframe 1.0.2 → 1.0.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/dist/iframe.js +55 -42
- package/dist/iframe.mjs +54 -42
- package/dist/types.d.ts +11 -0
- package/package.json +1 -1
package/dist/iframe.js
CHANGED
|
@@ -7,19 +7,19 @@ exports.createVueIframeClient = createVueIframeClient;
|
|
|
7
7
|
var _constants = require("./constants");
|
|
8
8
|
var _rpc = require("@nookuio/rpc");
|
|
9
9
|
var _telejson = require("telejson");
|
|
10
|
-
function
|
|
10
|
+
function m(c, a) {
|
|
11
11
|
return `[data-node-id="${a}"][data-node-path="${c}"]`;
|
|
12
12
|
}
|
|
13
|
-
function
|
|
13
|
+
function g(c, a) {
|
|
14
14
|
const n = document.querySelectorAll(c);
|
|
15
15
|
if (!n?.length) return;
|
|
16
16
|
const t = Array.from(n).map(d => {
|
|
17
|
-
const
|
|
17
|
+
const r = d.getBoundingClientRect(),
|
|
18
18
|
s = {
|
|
19
|
-
width:
|
|
20
|
-
height:
|
|
21
|
-
top:
|
|
22
|
-
left:
|
|
19
|
+
width: r.width,
|
|
20
|
+
height: r.height,
|
|
21
|
+
top: r.top,
|
|
22
|
+
left: r.left
|
|
23
23
|
};
|
|
24
24
|
if (!a) return s;
|
|
25
25
|
const i = window.getComputedStyle(d) ?? {};
|
|
@@ -45,33 +45,33 @@ function C(c) {
|
|
|
45
45
|
id: t
|
|
46
46
|
}) {
|
|
47
47
|
if (!n || t === void 0) return;
|
|
48
|
-
const d =
|
|
49
|
-
|
|
50
|
-
if (!
|
|
51
|
-
|
|
48
|
+
const d = m(n, t),
|
|
49
|
+
r = document.querySelector(d);
|
|
50
|
+
if (!r) return;
|
|
51
|
+
r.style.outline = "none", r.setAttribute("spellcheck", "false"), r.setAttribute("contenteditable", "true");
|
|
52
52
|
const s = l => {
|
|
53
53
|
const e = l.target;
|
|
54
54
|
if (!e) return;
|
|
55
|
-
const
|
|
56
|
-
|
|
55
|
+
const o = e.innerText;
|
|
56
|
+
r.removeAttribute("contenteditable"), c?.onTextUpdate?.({
|
|
57
57
|
path: n,
|
|
58
58
|
id: t,
|
|
59
|
-
content:
|
|
60
|
-
}),
|
|
59
|
+
content: o
|
|
60
|
+
}), r.removeEventListener("blur", s), r.removeEventListener("keydown", i), r.blur();
|
|
61
61
|
},
|
|
62
62
|
i = l => {
|
|
63
63
|
l.key === "Enter" && (l.stopPropagation(), l.preventDefault(), s(l));
|
|
64
64
|
};
|
|
65
|
-
|
|
65
|
+
r.addEventListener("keydown", i), r.addEventListener("blur", s), r.focus();
|
|
66
66
|
},
|
|
67
67
|
async getHoveredElement({
|
|
68
68
|
path: n,
|
|
69
69
|
id: t,
|
|
70
70
|
x: d,
|
|
71
|
-
y:
|
|
71
|
+
y: r
|
|
72
72
|
}) {
|
|
73
|
-
if (n) if (d !== void 0 &&
|
|
74
|
-
let s = document.elementFromPoint(d,
|
|
73
|
+
if (n) if (d !== void 0 && r !== void 0) {
|
|
74
|
+
let s = document.elementFromPoint(d, r);
|
|
75
75
|
if (!s) return;
|
|
76
76
|
let i = s.getAttribute("data-node-path");
|
|
77
77
|
if (i !== n) {
|
|
@@ -80,16 +80,16 @@ function C(c) {
|
|
|
80
80
|
}
|
|
81
81
|
const l = s.getAttribute("data-node-id");
|
|
82
82
|
if (!l) return;
|
|
83
|
-
const e =
|
|
84
|
-
|
|
85
|
-
return
|
|
83
|
+
const e = m(n, l),
|
|
84
|
+
o = g(e, !1);
|
|
85
|
+
return o ? {
|
|
86
86
|
id: l,
|
|
87
87
|
path: n,
|
|
88
|
-
data:
|
|
88
|
+
data: o
|
|
89
89
|
} : void 0;
|
|
90
90
|
} else {
|
|
91
|
-
const s =
|
|
92
|
-
i =
|
|
91
|
+
const s = m(n, t),
|
|
92
|
+
i = g(s, !1);
|
|
93
93
|
return i ? {
|
|
94
94
|
id: t,
|
|
95
95
|
path: n,
|
|
@@ -102,12 +102,12 @@ function C(c) {
|
|
|
102
102
|
path: n,
|
|
103
103
|
id: t
|
|
104
104
|
}) {
|
|
105
|
-
const d =
|
|
106
|
-
|
|
107
|
-
if (
|
|
105
|
+
const d = m(n, t),
|
|
106
|
+
r = g(d, !0);
|
|
107
|
+
if (r) return {
|
|
108
108
|
path: n,
|
|
109
109
|
id: t,
|
|
110
|
-
data:
|
|
110
|
+
data: r
|
|
111
111
|
};
|
|
112
112
|
},
|
|
113
113
|
setTheme(n) {
|
|
@@ -123,8 +123,21 @@ function createVueIframeClient(c, a) {
|
|
|
123
123
|
const n = new Map();
|
|
124
124
|
window.addEventListener("message", async e => {
|
|
125
125
|
if (typeof e.data != "object" || e.data.source !== _constants.EDITOR_SOURCE_NAME || e.data.type === "event" || !e.data.response) return;
|
|
126
|
-
const
|
|
127
|
-
|
|
126
|
+
const o = n.get(e.data.response.id);
|
|
127
|
+
o && (n.delete(e.data.response.id), o(e.data.response));
|
|
128
|
+
}), window.addEventListener("keydown", e => {
|
|
129
|
+
if (!t) return;
|
|
130
|
+
const o = document.activeElement,
|
|
131
|
+
u = o?.isContentEditable || o?.tagName === "INPUT" || o?.tagName === "TEXTAREA";
|
|
132
|
+
t.emit("keydown", {
|
|
133
|
+
key: e.key,
|
|
134
|
+
ctrlKey: e.ctrlKey,
|
|
135
|
+
shiftKey: e.shiftKey,
|
|
136
|
+
altKey: e.altKey,
|
|
137
|
+
metaKey: e.metaKey,
|
|
138
|
+
repeat: e.repeat,
|
|
139
|
+
inputFocused: u
|
|
140
|
+
});
|
|
128
141
|
});
|
|
129
142
|
const t = (0, _rpc.createClient)({
|
|
130
143
|
...C({
|
|
@@ -134,20 +147,20 @@ function createVueIframeClient(c, a) {
|
|
|
134
147
|
...c
|
|
135
148
|
}, {
|
|
136
149
|
handle(e) {
|
|
137
|
-
window.addEventListener("message", async
|
|
138
|
-
if (typeof
|
|
139
|
-
const
|
|
150
|
+
window.addEventListener("message", async o => {
|
|
151
|
+
if (typeof o.data != "object" || o.data.source !== _constants.EDITOR_SOURCE_NAME || o.data.type === "event" || !o.data.request) return;
|
|
152
|
+
const u = await e(o.data.request);
|
|
140
153
|
window.parent.postMessage({
|
|
141
154
|
source: _constants.IFRAME_SOURCE_NAME,
|
|
142
|
-
response:
|
|
155
|
+
response: u
|
|
143
156
|
}, "*");
|
|
144
157
|
});
|
|
145
158
|
},
|
|
146
159
|
invoke(e) {
|
|
147
|
-
return new Promise((
|
|
148
|
-
const E = setTimeout(() =>
|
|
160
|
+
return new Promise((o, u) => {
|
|
161
|
+
const E = setTimeout(() => u(new Error("Request timed out")), 5e3);
|
|
149
162
|
n.set(e.id, p => {
|
|
150
|
-
clearTimeout(E),
|
|
163
|
+
clearTimeout(E), o(p);
|
|
151
164
|
}), window.parent.postMessage({
|
|
152
165
|
source: _constants.IFRAME_SOURCE_NAME,
|
|
153
166
|
request: e
|
|
@@ -162,8 +175,8 @@ function createVueIframeClient(c, a) {
|
|
|
162
175
|
}, "*");
|
|
163
176
|
},
|
|
164
177
|
handleEvent(e) {
|
|
165
|
-
window.addEventListener("message",
|
|
166
|
-
typeof
|
|
178
|
+
window.addEventListener("message", o => {
|
|
179
|
+
typeof o.data == "object" && (o.data.source !== _constants.EDITOR_SOURCE_NAME || o.data.type !== "event" || !o.data.request || e(o.data.request));
|
|
167
180
|
});
|
|
168
181
|
},
|
|
169
182
|
destroy() {
|
|
@@ -176,13 +189,13 @@ function createVueIframeClient(c, a) {
|
|
|
176
189
|
timeout: 5e3
|
|
177
190
|
}),
|
|
178
191
|
d = a?.disableDefaultConsole ? void 0 : console.log.bind(console),
|
|
179
|
-
|
|
192
|
+
r = a?.disableDefaultConsole ? void 0 : console.warn.bind(console),
|
|
180
193
|
s = a?.disableDefaultConsole ? void 0 : console.error.bind(console),
|
|
181
194
|
i = a?.disableDefaultConsole ? void 0 : console.info.bind(console);
|
|
182
195
|
return console.log = (...e) => {
|
|
183
196
|
t.emit("console", "log", e), d?.(...e);
|
|
184
197
|
}, console.warn = (...e) => {
|
|
185
|
-
|
|
198
|
+
r?.(...e);
|
|
186
199
|
}, console.error = (...e) => {
|
|
187
200
|
t.emit("console", "error", e), s?.(...e);
|
|
188
201
|
}, console.info = (...e) => {
|
package/dist/iframe.mjs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { EDITOR_SOURCE_NAME as f, IFRAME_SOURCE_NAME as
|
|
1
|
+
import { EDITOR_SOURCE_NAME as f, IFRAME_SOURCE_NAME as y } from "./constants.mjs";
|
|
2
2
|
import { createClient as b } from "@nookuio/rpc";
|
|
3
|
-
import { stringify as
|
|
4
|
-
function
|
|
3
|
+
import { stringify as w, parse as h } from "telejson";
|
|
4
|
+
function m(c, a) {
|
|
5
5
|
return `[data-node-id="${a}"][data-node-path="${c}"]`;
|
|
6
6
|
}
|
|
7
|
-
function
|
|
7
|
+
function g(c, a) {
|
|
8
8
|
const n = document.querySelectorAll(c);
|
|
9
9
|
if (!n?.length) return;
|
|
10
10
|
const t = Array.from(n).map((d) => {
|
|
11
|
-
const
|
|
12
|
-
width:
|
|
13
|
-
height:
|
|
14
|
-
top:
|
|
15
|
-
left:
|
|
11
|
+
const r = d.getBoundingClientRect(), s = {
|
|
12
|
+
width: r.width,
|
|
13
|
+
height: r.height,
|
|
14
|
+
top: r.top,
|
|
15
|
+
left: r.left
|
|
16
16
|
};
|
|
17
17
|
if (!a) return s;
|
|
18
18
|
const i = window.getComputedStyle(d) ?? {};
|
|
@@ -36,23 +36,23 @@ function C(c) {
|
|
|
36
36
|
return {
|
|
37
37
|
async editText({ path: n, id: t }) {
|
|
38
38
|
if (!n || t === void 0) return;
|
|
39
|
-
const d =
|
|
40
|
-
if (!
|
|
41
|
-
|
|
39
|
+
const d = m(n, t), r = document.querySelector(d);
|
|
40
|
+
if (!r) return;
|
|
41
|
+
r.style.outline = "none", r.setAttribute("spellcheck", "false"), r.setAttribute("contenteditable", "true");
|
|
42
42
|
const s = (l) => {
|
|
43
43
|
const e = l.target;
|
|
44
44
|
if (!e) return;
|
|
45
|
-
const
|
|
46
|
-
|
|
45
|
+
const o = e.innerText;
|
|
46
|
+
r.removeAttribute("contenteditable"), c?.onTextUpdate?.({ path: n, id: t, content: o }), r.removeEventListener("blur", s), r.removeEventListener("keydown", i), r.blur();
|
|
47
47
|
}, i = (l) => {
|
|
48
48
|
l.key === "Enter" && (l.stopPropagation(), l.preventDefault(), s(l));
|
|
49
49
|
};
|
|
50
|
-
|
|
50
|
+
r.addEventListener("keydown", i), r.addEventListener("blur", s), r.focus();
|
|
51
51
|
},
|
|
52
|
-
async getHoveredElement({ path: n, id: t, x: d, y:
|
|
52
|
+
async getHoveredElement({ path: n, id: t, x: d, y: r }) {
|
|
53
53
|
if (n)
|
|
54
|
-
if (d !== void 0 &&
|
|
55
|
-
let s = document.elementFromPoint(d,
|
|
54
|
+
if (d !== void 0 && r !== void 0) {
|
|
55
|
+
let s = document.elementFromPoint(d, r);
|
|
56
56
|
if (!s) return;
|
|
57
57
|
let i = s.getAttribute("data-node-path");
|
|
58
58
|
if (i !== n) {
|
|
@@ -62,14 +62,14 @@ function C(c) {
|
|
|
62
62
|
}
|
|
63
63
|
const l = s.getAttribute("data-node-id");
|
|
64
64
|
if (!l) return;
|
|
65
|
-
const e =
|
|
66
|
-
return
|
|
65
|
+
const e = m(n, l), o = g(e, !1);
|
|
66
|
+
return o ? {
|
|
67
67
|
id: l,
|
|
68
68
|
path: n,
|
|
69
|
-
data:
|
|
69
|
+
data: o
|
|
70
70
|
} : void 0;
|
|
71
71
|
} else {
|
|
72
|
-
const s =
|
|
72
|
+
const s = m(n, t), i = g(s, !1);
|
|
73
73
|
return i ? {
|
|
74
74
|
id: t,
|
|
75
75
|
path: n,
|
|
@@ -79,12 +79,12 @@ function C(c) {
|
|
|
79
79
|
},
|
|
80
80
|
getPageHeight: a,
|
|
81
81
|
async getSelectedElement({ path: n, id: t }) {
|
|
82
|
-
const d =
|
|
83
|
-
if (
|
|
82
|
+
const d = m(n, t), r = g(d, !0);
|
|
83
|
+
if (r)
|
|
84
84
|
return {
|
|
85
85
|
path: n,
|
|
86
86
|
id: t,
|
|
87
|
-
data:
|
|
87
|
+
data: r
|
|
88
88
|
};
|
|
89
89
|
},
|
|
90
90
|
setTheme(n) {
|
|
@@ -122,8 +122,20 @@ export function createVueIframeClient(c, a) {
|
|
|
122
122
|
const n = /* @__PURE__ */ new Map();
|
|
123
123
|
window.addEventListener("message", async (e) => {
|
|
124
124
|
if (typeof e.data != "object" || e.data.source !== f || e.data.type === "event" || !e.data.response) return;
|
|
125
|
-
const
|
|
126
|
-
|
|
125
|
+
const o = n.get(e.data.response.id);
|
|
126
|
+
o && (n.delete(e.data.response.id), o(e.data.response));
|
|
127
|
+
}), window.addEventListener("keydown", (e) => {
|
|
128
|
+
if (!t) return;
|
|
129
|
+
const o = document.activeElement, u = o?.isContentEditable || o?.tagName === "INPUT" || o?.tagName === "TEXTAREA";
|
|
130
|
+
t.emit("keydown", {
|
|
131
|
+
key: e.key,
|
|
132
|
+
ctrlKey: e.ctrlKey,
|
|
133
|
+
shiftKey: e.shiftKey,
|
|
134
|
+
altKey: e.altKey,
|
|
135
|
+
metaKey: e.metaKey,
|
|
136
|
+
repeat: e.repeat,
|
|
137
|
+
inputFocused: u
|
|
138
|
+
});
|
|
127
139
|
});
|
|
128
140
|
const t = b(
|
|
129
141
|
{
|
|
@@ -135,40 +147,40 @@ export function createVueIframeClient(c, a) {
|
|
|
135
147
|
},
|
|
136
148
|
{
|
|
137
149
|
handle(e) {
|
|
138
|
-
window.addEventListener("message", async (
|
|
139
|
-
if (typeof
|
|
140
|
-
const
|
|
141
|
-
window.parent.postMessage({ source:
|
|
150
|
+
window.addEventListener("message", async (o) => {
|
|
151
|
+
if (typeof o.data != "object" || o.data.source !== f || o.data.type === "event" || !o.data.request) return;
|
|
152
|
+
const u = await e(o.data.request);
|
|
153
|
+
window.parent.postMessage({ source: y, response: u }, "*");
|
|
142
154
|
});
|
|
143
155
|
},
|
|
144
156
|
invoke(e) {
|
|
145
|
-
return new Promise((
|
|
146
|
-
const E = setTimeout(() =>
|
|
157
|
+
return new Promise((o, u) => {
|
|
158
|
+
const E = setTimeout(() => u(new Error("Request timed out")), 5e3);
|
|
147
159
|
n.set(e.id, (p) => {
|
|
148
|
-
clearTimeout(E),
|
|
149
|
-
}), window.parent.postMessage({ source:
|
|
160
|
+
clearTimeout(E), o(p);
|
|
161
|
+
}), window.parent.postMessage({ source: y, request: e }, "*");
|
|
150
162
|
});
|
|
151
163
|
},
|
|
152
164
|
emit(e) {
|
|
153
|
-
window.parent.postMessage({ source:
|
|
165
|
+
window.parent.postMessage({ source: y, type: "event", request: e }, "*");
|
|
154
166
|
},
|
|
155
167
|
handleEvent(e) {
|
|
156
|
-
window.addEventListener("message", (
|
|
157
|
-
typeof
|
|
168
|
+
window.addEventListener("message", (o) => {
|
|
169
|
+
typeof o.data == "object" && (o.data.source !== f || o.data.type !== "event" || !o.data.request || e(o.data.request));
|
|
158
170
|
});
|
|
159
171
|
},
|
|
160
172
|
destroy() {
|
|
161
173
|
n.clear();
|
|
162
174
|
},
|
|
163
|
-
deserialize: (e) =>
|
|
164
|
-
serialize: (e) =>
|
|
175
|
+
deserialize: (e) => h(e),
|
|
176
|
+
serialize: (e) => w(e, { maxDepth: 1 / 0 }),
|
|
165
177
|
timeout: 5e3
|
|
166
178
|
}
|
|
167
|
-
), d = a?.disableDefaultConsole ? void 0 : console.log.bind(console),
|
|
179
|
+
), d = a?.disableDefaultConsole ? void 0 : console.log.bind(console), r = a?.disableDefaultConsole ? void 0 : console.warn.bind(console), s = a?.disableDefaultConsole ? void 0 : console.error.bind(console), i = a?.disableDefaultConsole ? void 0 : console.info.bind(console);
|
|
168
180
|
return console.log = (...e) => {
|
|
169
181
|
t.emit("console", "log", e), d?.(...e);
|
|
170
182
|
}, console.warn = (...e) => {
|
|
171
|
-
|
|
183
|
+
r?.(...e);
|
|
172
184
|
}, console.error = (...e) => {
|
|
173
185
|
t.emit("console", "error", e), s?.(...e);
|
|
174
186
|
}, console.info = (...e) => {
|
package/dist/types.d.ts
CHANGED
|
@@ -53,6 +53,17 @@ export interface CoreIframeEvents extends RpcEvents {
|
|
|
53
53
|
id: string;
|
|
54
54
|
content: string;
|
|
55
55
|
}) => void;
|
|
56
|
+
'exit-preview-mode': () => void;
|
|
57
|
+
keydown: (event: {
|
|
58
|
+
key: string;
|
|
59
|
+
ctrlKey: boolean;
|
|
60
|
+
shiftKey: boolean;
|
|
61
|
+
altKey: boolean;
|
|
62
|
+
metaKey: boolean;
|
|
63
|
+
repeat: boolean;
|
|
64
|
+
/** Is `true` if the focused element is an input, textarea, or contenteditable element */
|
|
65
|
+
inputFocused: boolean;
|
|
66
|
+
}) => void;
|
|
56
67
|
}
|
|
57
68
|
export interface VueIframeContext extends Omit<CoreIframeContext, 'updateCode'> {
|
|
58
69
|
navigateTo: (to: string) => void;
|