forked from RaspberryPiFoundation/editor-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-component.js
127 lines (111 loc) · 3.15 KB
/
web-component.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React from "react";
import ReactDOM from "react-dom";
import * as ReactDOMClient from "react-dom/client";
import * as Sentry from "@sentry/react";
import { BrowserTracing } from "@sentry/tracing";
import WebComponentLoader from "./containers/WebComponentLoader";
import store from "./redux/stores/WebComponentStore";
import { Provider } from "react-redux";
import "./utils/i18n";
import camelCase from "camelcase";
Sentry.init({
dsn: process.env.REACT_APP_SENTRY_DSN,
integrations: [new BrowserTracing()],
environment: process.env.REACT_APP_SENTRY_ENV,
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
class WebComponent extends HTMLElement {
root;
mountPoint;
componentAttributes = {};
componentProperties = {};
connectedCallback() {
this.mountReactApp();
}
disconnectedCallback() {
ReactDOM.unmountComponentAtNode(this.mountPoint);
}
static get observedAttributes() {
return [
"host_styles",
"auth_key",
"identifier",
"code",
"sense_hat_always_enabled",
"instructions",
"with_projectbar",
"project_name_editable",
"with_sidebar",
"sidebar_options",
"theme",
"embedded",
"show_save_prompt",
"load_remix_disabled",
];
}
attributeChangedCallback(name, _oldVal, newVal) {
let value;
if (
[
"sense_hat_always_enabled",
"with_sidebar",
"with_projectbar",
"project_name_editable",
"show_save_prompt",
"load_remix_disabled",
].includes(name)
) {
value = newVal !== "false";
} else if (
["instructions", "sidebar_options", "host_styles"].includes(name)
) {
value = JSON.parse(newVal);
} else {
value = newVal;
}
this.componentAttributes[camelCase(name)] = value;
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 component 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("part", "editor-root");
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);