forked from microBlock-IDE/microBlock-IDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.js
55 lines (44 loc) · 1.72 KB
/
embed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(function() {
let cssScript = document.createElement("style");
cssScript.innerHTML = `
.microBlock-embed {
display: inline-block;
border: #D0D3D4 1px solid;
}
.microBlock-embed > iframe {
border: none;
width: 100%;
height: 100%;
}
`;
document.querySelector("head").appendChild(cssScript);
})();
window.microBlock = {};
window.microBlock.nextId = 1;
window.microBlock.reload = () => {
for (let box of document.querySelectorAll(".microBlock-embed")) {
box.setAttribute("data-id", window.microBlock.nextId);
let optionURL = `&id=${window.microBlock.nextId}`;
for (let attr of box.attributes) {
let name = attr.name;
let value = attr.nodeValue;
if (name === "class") continue;
if (name === "blockonly") name = "blockOnly";
optionURL += `&${name}=${encodeURIComponent(value)}`;
if (name === "width" && value !== "0") box.style.width = value.endsWith("%") ? value : `${value}px`;
if (name === "width" && value === "0") box.style.width = "200px";
if (name === "height" && value !== "0") box.style.height = value.endsWith("%") ? value : `${value}px`;
if (name === "height" && value === "0") box.style.height = "200px";
}
let iframe = document.createElement("iframe");
iframe.setAttribute("src", `https://ide.microblock.app/?embed${optionURL}`);
box.appendChild(iframe);
window.microBlock.nextId++;
}
};
window.microBlock.updateFrameSize = (id, width, height) => {
let iframe = document.querySelector(`.microBlock-embed[data-id='${id}']`);
iframe.style.width = width;
iframe.style.height = height;
};
window.microBlock.reload();