-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathweb-component.js
More file actions
76 lines (62 loc) · 1.97 KB
/
Copy pathweb-component.js
File metadata and controls
76 lines (62 loc) · 1.97 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React from 'react';
import ReactDOM from 'react-dom';
import * as ReactDOMClient from 'react-dom/client';
import WebComponentLoader from './components/WebComponent/WebComponentLoader/WebComponentLoader';
import store from './app/store'
import { Provider } from 'react-redux'
class WebComponent extends HTMLElement {
root;
mountPoint;
componentAttributes = {};
componentProperties = {};
connectedCallback() {
this.mountReactApp();
}
disconnectedCallback() {
ReactDOM.unmountComponentAtNode(this.mountPoint);
}
static get observedAttributes() {
return ['code', 'visualoutput'];
}
attributeChangedCallback(name, _oldVal, newVal) {
// console.log(name, newVal)
this.componentAttributes[name] = newVal;
this.mountReactApp();
}
get editorCode() {
// console.log('getting editor code');
const state = store.getState();
// console.log(state.editor.foo);
return state.editor.project.components[0].content;
}
get menuItems() {
return this.componentProperties.menuItems;
}
set menuItems(newValue) {
// update properties in the web componet via js calls from host app
// see public/web-component/index.html
console.log('menu items set')
this.componentProperties.menuItems = newValue;
this.mountReactApp();
}
reactProps() {
return { ...this.componentAttributes, ...this.componentProperties };
}
mountReactApp() {
if (!this.mountPoint) {
this.mountPoint = document.createElement('div');
this.mountPoint.setAttribute("id", "root");
this.mountPoint.setAttribute("style", "height: 100%");
this.attachShadow({ mode: 'open' }).appendChild(this.mountPoint);
this.root = ReactDOMClient.createRoot(this.mountPoint);
}
this.root.render(
<React.StrictMode>
<Provider store={store}>
<WebComponentLoader { ...this.reactProps() }/>
</Provider>
</React.StrictMode>
);
}
}
window.customElements.define('editor-wc', WebComponent);