@nookuio/iframe 0.9.6 → 0.9.7
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/constants.js +5 -5
- package/dist/constants.mjs +1 -5
- package/dist/createClient.js +90 -114
- package/dist/createClient.mjs +1 -130
- package/dist/editor.d.ts +4 -4
- package/dist/editor.js +55 -79
- package/dist/editor.mjs +1 -95
- package/dist/iframe.js +152 -226
- package/dist/iframe.mjs +1 -222
- package/dist/index.mjs +1 -4
- package/package.json +1 -1
package/dist/iframe.mjs
CHANGED
|
@@ -1,222 +1 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createClient } from "./createClient.mjs";
|
|
3
|
-
import { stringify, parse } from "telejson";
|
|
4
|
-
function getSelector(path, id) {
|
|
5
|
-
return `[data-node-id="${id}"][data-node-path="${path}"]`;
|
|
6
|
-
}
|
|
7
|
-
function getElementStyles(selector, withPadding) {
|
|
8
|
-
const elements = document.querySelectorAll(selector);
|
|
9
|
-
if (!elements?.length) return;
|
|
10
|
-
const styles = Array.from(elements).map((element) => {
|
|
11
|
-
const clientRect = element.getBoundingClientRect();
|
|
12
|
-
const position = {
|
|
13
|
-
width: clientRect.width,
|
|
14
|
-
height: clientRect.height,
|
|
15
|
-
top: clientRect.top,
|
|
16
|
-
left: clientRect.left
|
|
17
|
-
};
|
|
18
|
-
if (!withPadding) return position;
|
|
19
|
-
const computedStyle = window.getComputedStyle(element) ?? {};
|
|
20
|
-
return {
|
|
21
|
-
...position,
|
|
22
|
-
padding: {
|
|
23
|
-
top: computedStyle.paddingTop,
|
|
24
|
-
right: computedStyle.paddingRight,
|
|
25
|
-
bottom: computedStyle.paddingBottom,
|
|
26
|
-
left: computedStyle.paddingLeft
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
});
|
|
30
|
-
if (!styles?.length) return;
|
|
31
|
-
return styles.length === 1 ? styles[0] : styles;
|
|
32
|
-
}
|
|
33
|
-
export function createVueIframeClient(vueCtx, options) {
|
|
34
|
-
const client = createClient(
|
|
35
|
-
{
|
|
36
|
-
...vueCtx,
|
|
37
|
-
async editText({ path, id }) {
|
|
38
|
-
if (!path || id === void 0) return;
|
|
39
|
-
const selector = getSelector(path, id);
|
|
40
|
-
const element = document.querySelector(selector);
|
|
41
|
-
if (!element) return;
|
|
42
|
-
element.style.outline = "none";
|
|
43
|
-
element.setAttribute("spellcheck", "false");
|
|
44
|
-
element.setAttribute("contenteditable", "true");
|
|
45
|
-
const blurEvtHandler = (event) => {
|
|
46
|
-
const elm = event.target;
|
|
47
|
-
if (!elm) return;
|
|
48
|
-
const content = elm.innerText;
|
|
49
|
-
element.removeAttribute("contenteditable");
|
|
50
|
-
client.emit("text-update", { path, id, content });
|
|
51
|
-
element.removeEventListener("blur", blurEvtHandler);
|
|
52
|
-
element.removeEventListener("keydown", keyDownEvtHandler);
|
|
53
|
-
element.blur();
|
|
54
|
-
};
|
|
55
|
-
const keyDownEvtHandler = (event) => {
|
|
56
|
-
if (event.key === "Enter") {
|
|
57
|
-
event.stopPropagation();
|
|
58
|
-
event.preventDefault();
|
|
59
|
-
blurEvtHandler(event);
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
element.addEventListener("keydown", keyDownEvtHandler);
|
|
63
|
-
element.addEventListener("blur", blurEvtHandler);
|
|
64
|
-
element.focus();
|
|
65
|
-
},
|
|
66
|
-
async getHoveredElement({ path, id, x, y }) {
|
|
67
|
-
if (!path) return;
|
|
68
|
-
if (x !== void 0 && y !== void 0) {
|
|
69
|
-
let element = document.elementFromPoint(x, y);
|
|
70
|
-
if (!element) return;
|
|
71
|
-
let elementPath = element.getAttribute("data-node-path");
|
|
72
|
-
if (elementPath !== path) {
|
|
73
|
-
if (!element.parentElement) return;
|
|
74
|
-
while (element.parentElement && elementPath !== path) {
|
|
75
|
-
element = element.parentElement;
|
|
76
|
-
elementPath = element.getAttribute("data-node-path");
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
const elementId = element.getAttribute("data-node-id");
|
|
80
|
-
if (!elementId) return;
|
|
81
|
-
const selector = getSelector(path, elementId);
|
|
82
|
-
const styles = getElementStyles(selector, false);
|
|
83
|
-
if (!styles) return;
|
|
84
|
-
return {
|
|
85
|
-
id: elementId,
|
|
86
|
-
path,
|
|
87
|
-
data: styles
|
|
88
|
-
};
|
|
89
|
-
} else {
|
|
90
|
-
const selector = getSelector(path, id);
|
|
91
|
-
const styles = getElementStyles(selector, false);
|
|
92
|
-
if (!styles) return;
|
|
93
|
-
return {
|
|
94
|
-
id,
|
|
95
|
-
path,
|
|
96
|
-
data: styles
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
async getPageHeight() {
|
|
101
|
-
const height = document.body.scrollHeight > 0 ? document.body.scrollHeight : document.documentElement.scrollHeight;
|
|
102
|
-
return height;
|
|
103
|
-
},
|
|
104
|
-
async getSelectedElement({ path, id }) {
|
|
105
|
-
const selector = getSelector(path, id);
|
|
106
|
-
const styles = getElementStyles(selector, true);
|
|
107
|
-
if (!styles) return;
|
|
108
|
-
return {
|
|
109
|
-
path,
|
|
110
|
-
id,
|
|
111
|
-
data: styles
|
|
112
|
-
};
|
|
113
|
-
},
|
|
114
|
-
toggleTheme(theme) {
|
|
115
|
-
if (theme === "dark") {
|
|
116
|
-
document.documentElement.classList.remove("light");
|
|
117
|
-
document.documentElement.classList.add("dark");
|
|
118
|
-
} else {
|
|
119
|
-
document.documentElement.classList.remove("dark");
|
|
120
|
-
document.documentElement.classList.add("light");
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
// updateElementClass({ className, id, path }) {
|
|
124
|
-
// const selector = getSelector(path, id);
|
|
125
|
-
// const elements = document.querySelectorAll(selector);
|
|
126
|
-
// if (!elements?.length) return;
|
|
127
|
-
// elements.forEach((elm) => (elm.className = className));
|
|
128
|
-
// },
|
|
129
|
-
// updateElementAttribute({ attribute, value, id, path }) {
|
|
130
|
-
// const selector = getSelector(path, id);
|
|
131
|
-
// const elements = document.querySelectorAll(selector);
|
|
132
|
-
// if (!elements?.length) return;
|
|
133
|
-
// elements.forEach((elm) => elm.setAttribute(attribute, value));
|
|
134
|
-
// },
|
|
135
|
-
// duplicateElement({ id, path }) {
|
|
136
|
-
// const selector = getSelector(path, id);
|
|
137
|
-
// const elements = document.querySelectorAll(selector);
|
|
138
|
-
// if (!elements?.length) return;
|
|
139
|
-
// elements.forEach((elm) => {
|
|
140
|
-
// const clone = elm.cloneNode(true) as HTMLElement;
|
|
141
|
-
// clone.setAttribute('data-node-id', String(Number(clone.getAttribute('data-node-id')!) + 1));
|
|
142
|
-
// elm.parentElement?.insertBefore(clone, elm.nextSibling);
|
|
143
|
-
// });
|
|
144
|
-
// }
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
handle(handler) {
|
|
148
|
-
window.addEventListener("message", async (event) => {
|
|
149
|
-
if (typeof event.data !== "object") return;
|
|
150
|
-
if (event.data.source !== EDITOR_SOURCE_NAME || event.data.type === "event" || !event.data.request) return;
|
|
151
|
-
const response = await handler(event.data.request);
|
|
152
|
-
window.parent.postMessage({ source: IFRAME_SOURCE_NAME, response }, "*");
|
|
153
|
-
});
|
|
154
|
-
},
|
|
155
|
-
invoke(request) {
|
|
156
|
-
return new Promise((resolve, reject) => {
|
|
157
|
-
const timeout = setTimeout(() => {
|
|
158
|
-
window.removeEventListener("message", messageHandler);
|
|
159
|
-
reject(new Error("Request timed out"));
|
|
160
|
-
}, 5e3);
|
|
161
|
-
function messageHandler(event) {
|
|
162
|
-
if (typeof event.data !== "object") return;
|
|
163
|
-
if (event.data.source !== EDITOR_SOURCE_NAME || !event.data.response) return;
|
|
164
|
-
if (event.data.response.id !== request.id) return;
|
|
165
|
-
clearTimeout(timeout);
|
|
166
|
-
window.removeEventListener("message", messageHandler);
|
|
167
|
-
resolve(event.data.response);
|
|
168
|
-
}
|
|
169
|
-
window.parent.postMessage({ source: IFRAME_SOURCE_NAME, request }, "*");
|
|
170
|
-
window.addEventListener("message", messageHandler);
|
|
171
|
-
});
|
|
172
|
-
},
|
|
173
|
-
emit(request) {
|
|
174
|
-
window.parent.postMessage({ source: IFRAME_SOURCE_NAME, type: "event", request }, "*");
|
|
175
|
-
},
|
|
176
|
-
handleEvent(handleRequest) {
|
|
177
|
-
window.addEventListener("message", (event) => {
|
|
178
|
-
if (typeof event.data !== "object") return;
|
|
179
|
-
if (event.data.source !== EDITOR_SOURCE_NAME || event.data.type !== "event" || !event.data.request) return;
|
|
180
|
-
handleRequest(event.data.request);
|
|
181
|
-
});
|
|
182
|
-
},
|
|
183
|
-
deserialize: (v) => parse(v),
|
|
184
|
-
serialize: (v) => stringify(v, { maxDepth: Infinity })
|
|
185
|
-
}
|
|
186
|
-
);
|
|
187
|
-
const defaultConsoleLog = options?.disableDefaultConsole ? void 0 : console.log.bind(console);
|
|
188
|
-
const defaultConsoleWarn = options?.disableDefaultConsole ? void 0 : console.warn.bind(console);
|
|
189
|
-
const defaultConsoleError = options?.disableDefaultConsole ? void 0 : console.error.bind(console);
|
|
190
|
-
const defaultConsoleInfo = options?.disableDefaultConsole ? void 0 : console.info.bind(console);
|
|
191
|
-
console.log = (...args) => {
|
|
192
|
-
client.emit("console", "log", args);
|
|
193
|
-
defaultConsoleLog?.(...args);
|
|
194
|
-
};
|
|
195
|
-
console.warn = (...args) => {
|
|
196
|
-
defaultConsoleWarn?.(...args);
|
|
197
|
-
};
|
|
198
|
-
console.error = (...args) => {
|
|
199
|
-
client.emit("console", "error", args);
|
|
200
|
-
defaultConsoleError?.(...args);
|
|
201
|
-
};
|
|
202
|
-
console.info = (...args) => {
|
|
203
|
-
client.emit("console", "info", args);
|
|
204
|
-
defaultConsoleInfo?.(...args);
|
|
205
|
-
};
|
|
206
|
-
window.addEventListener("resize", async () => client.emit("resize", await client.$context.getPageHeight()));
|
|
207
|
-
const mutationObserver = new MutationObserver(
|
|
208
|
-
async () => client.emit("resize", await client.$context.getPageHeight())
|
|
209
|
-
);
|
|
210
|
-
mutationObserver.observe(document.body, {
|
|
211
|
-
childList: true,
|
|
212
|
-
// observe direct children additions/removals
|
|
213
|
-
subtree: true,
|
|
214
|
-
// observe all descendants
|
|
215
|
-
attributes: true,
|
|
216
|
-
// observe attribute changes (like style, class)
|
|
217
|
-
characterData: true
|
|
218
|
-
// observe text changes
|
|
219
|
-
});
|
|
220
|
-
client.emit("ready");
|
|
221
|
-
return client;
|
|
222
|
-
}
|
|
1
|
+
import{EDITOR_SOURCE_NAME as p,IFRAME_SOURCE_NAME as w}from"./constants";import{createClient as C}from"./createClient";import{stringify as v,parse as L}from"telejson";function b(c,l){return`[data-node-id="${l}"][data-node-path="${c}"]`}function h(c,l){const s=document.querySelectorAll(c);if(!s?.length)return;const d=Array.from(s).map(f=>{const u=f.getBoundingClientRect(),g={width:u.width,height:u.height,top:u.top,left:u.left};if(!l)return g;const m=window.getComputedStyle(f)??{};return{...g,padding:{top:m.paddingTop,right:m.paddingRight,bottom:m.paddingBottom,left:m.paddingLeft}}});if(d?.length)return d.length===1?d[0]:d}export function createVueIframeClient(c,l){const s=C({...c,async editText({path:e,id:t}){if(!e||t===void 0)return;const i=b(e,t),n=document.querySelector(i);if(!n)return;n.style.outline="none",n.setAttribute("spellcheck","false"),n.setAttribute("contenteditable","true");const o=a=>{const E=a.target;if(!E)return;const y=E.innerText;n.removeAttribute("contenteditable"),s.emit("text-update",{path:e,id:t,content:y}),n.removeEventListener("blur",o),n.removeEventListener("keydown",r),n.blur()},r=a=>{a.key==="Enter"&&(a.stopPropagation(),a.preventDefault(),o(a))};n.addEventListener("keydown",r),n.addEventListener("blur",o),n.focus()},async getHoveredElement({path:e,id:t,x:i,y:n}){if(e)if(i!==void 0&&n!==void 0){let o=document.elementFromPoint(i,n);if(!o)return;let r=o.getAttribute("data-node-path");if(r!==e){if(!o.parentElement)return;for(;o.parentElement&&r!==e;)o=o.parentElement,r=o.getAttribute("data-node-path")}const a=o.getAttribute("data-node-id");if(!a)return;const E=b(e,a),y=h(E,!1);return y?{id:a,path:e,data:y}:void 0}else{const o=b(e,t),r=h(o,!1);return r?{id:t,path:e,data:r}:void 0}},async getPageHeight(){return document.body.scrollHeight>0?document.body.scrollHeight:document.documentElement.scrollHeight},async getSelectedElement({path:e,id:t}){const i=b(e,t),n=h(i,!0);if(n)return{path:e,id:t,data:n}},toggleTheme(e){e==="dark"?(document.documentElement.classList.remove("light"),document.documentElement.classList.add("dark")):(document.documentElement.classList.remove("dark"),document.documentElement.classList.add("light"))}},{handle(e){window.addEventListener("message",async t=>{if(typeof t.data!="object"||t.data.source!==p||t.data.type==="event"||!t.data.request)return;const i=await e(t.data.request);window.parent.postMessage({source:w,response:i},"*")})},invoke(e){return new Promise((t,i)=>{const n=setTimeout(()=>{window.removeEventListener("message",o),i(new Error("Request timed out"))},5e3);function o(r){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)))}window.parent.postMessage({source:w,request:e},"*"),window.addEventListener("message",o)})},emit(e){window.parent.postMessage({source:w,type:"event",request:e},"*")},handleEvent(e){window.addEventListener("message",t=>{typeof t.data=="object"&&(t.data.source!==p||t.data.type!=="event"||!t.data.request||e(t.data.request))})},deserialize:e=>L(e),serialize:e=>v(e,{maxDepth:1/0})}),d=l?.disableDefaultConsole?void 0:console.log.bind(console),f=l?.disableDefaultConsole?void 0:console.warn.bind(console),u=l?.disableDefaultConsole?void 0:console.error.bind(console),g=l?.disableDefaultConsole?void 0:console.info.bind(console);return console.log=(...e)=>{s.emit("console","log",e),d?.(...e)},console.warn=(...e)=>{f?.(...e)},console.error=(...e)=>{s.emit("console","error",e),u?.(...e)},console.info=(...e)=>{s.emit("console","info",e),g?.(...e)},window.addEventListener("resize",async()=>s.emit("resize",await s.$context.getPageHeight())),new MutationObserver(async()=>s.emit("resize",await s.$context.getPageHeight())).observe(document.body,{childList:!0,subtree:!0,attributes:!0,characterData:!0}),s.emit("ready"),s}
|
package/dist/index.mjs
CHANGED