-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (49 loc) · 1.46 KB
/
index.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
/**
* Sends data as JSON to custom element
*
* @function
* @param {string} key
* @param {object} obj
* @param {string} customElementId
* @returns {null}
*/
export const sendJSON = (key, obj, customElementId) => {
/**@type {$w.CustomElement} */
const customElement = $w(customElementId);
const jsonData = JSON.stringify(obj);
customElement.setAttribute(key, jsonData);
return null;
}
/**
* Setups styles (CSS) and fonts for you.
*
* @function
* @param {Array<string>} fonts
* @param {Array<string>} styles
* @param {HTMLElement} customElement
*
* @returns {{customElement: HTMLElement, rootDiv: HTMLDivElement}}
*/
export function setupForReact(fonts, styles, customElement) {
customElement.rootDiv.id = 'root';
customElement.attachShadow({ mode: "open" });
let fontsHtmlReady = '';
if (fonts) {
for (const f of fonts) {
if (f.includes('<link')) {
fontsHtmlReady = fontsHtmlReady + f + '\n';
} else {
fontsHtmlReady = fontsHtmlReady + `<link href="${f}" rel="stylesheet">` + '\n';
}
}
}
let stylesHtmlReady = '';
if (styles) {
for (const s of styles) {
stylesHtmlReady = stylesHtmlReady + s + '\n';
}
}
customElement.shadowRoot.innerHTML = `${fonts ? fontsHtmlReady : ''}\n${styles ? stylesHtmlReady : ''}`;
customElement.shadowRoot.appendChild(customElement.rootDiv);
return customElement;
}