Skip to content

Commit 33adb92

Browse files
loiswells97floehopper
authored andcommitted
Add runCode, stopCode & rerunCode to web component
Based on [1] & #899. [1]: f9086a8
1 parent 5afe531 commit 33adb92

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1313
- Add `assets_identifier` attribute to web component (#1014 & originally #901)
1414
- Enhance `code` attribute on web component to override project main component content (#1014 & originally #901)
1515
- Add `embedded` attribute to web component (#1014 & originally #901)
16+
- Add `runCode`, `stopCode` & `rerunCode` methods to web component (#1014 & originally #899)
1617

1718
### Changed
1819

src/components/Editor/Output/Output.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Output = ({ embedded = false, browserPreview = false }) => {
1212
const isBrowserPreview =
1313
searchParams.get("browserPreview") === "true" || browserPreview;
1414
const usePyodide = searchParams.get("pyodide") === "true";
15+
const webComponent = useSelector((state) => state.editor.webComponent);
1516

1617
return (
1718
<>
@@ -21,7 +22,9 @@ const Output = ({ embedded = false, browserPreview = false }) => {
2122
projectType={project.project_type}
2223
usePyodide={usePyodide}
2324
/>
24-
{isEmbedded && !isBrowserPreview && <RunBar embedded />}
25+
{!webComponent && isEmbedded && !isBrowserPreview && (
26+
<RunBar embedded />
27+
)}
2528
</div>
2629
</>
2730
);

src/components/WebComponentProject/WebComponentProject.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ const WebComponentProject = ({
4444
const [codeHasRun, setCodeHasRun] = useState(codeHasBeenRun);
4545
const dispatch = useDispatch();
4646

47-
useEffect(() => {
48-
dispatch(setIsSplitView(false));
49-
dispatch(setWebComponent(true));
50-
}, [dispatch]);
47+
dispatch(setIsSplitView(false));
48+
dispatch(setWebComponent(true));
5149

5250
useEffect(() => {
5351
setCodeHasRun(false);

src/web-component.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import store from "./redux/stores/WebComponentStore";
88
import { Provider } from "react-redux";
99
import "./utils/i18n";
1010
import camelCase from "camelcase";
11+
import { stopCodeRun, stopDraw, triggerCodeRun } from "./redux/EditorSlice";
1112

1213
Sentry.init({
1314
dsn: process.env.REACT_APP_SENTRY_DSN,
@@ -102,6 +103,34 @@ class WebComponent extends HTMLElement {
102103
this.mountReactApp();
103104
}
104105

106+
stopCode() {
107+
const state = store.getState();
108+
if (state.editor.codeRunTriggered || state.editor.drawTriggered) {
109+
store.dispatch(stopCodeRun());
110+
store.dispatch(stopDraw());
111+
}
112+
}
113+
114+
runCode() {
115+
store.dispatch(triggerCodeRun());
116+
}
117+
118+
rerunCode() {
119+
this.stopCode();
120+
121+
new Promise((resolve) => {
122+
let checkInterval = setInterval(() => {
123+
let state = store.getState();
124+
if (!state.codeRunTriggered && !state.drawTriggered) {
125+
clearInterval(checkInterval);
126+
resolve();
127+
}
128+
}, 50);
129+
}).then(() => {
130+
this.runCode();
131+
});
132+
}
133+
105134
reactProps() {
106135
return {
107136
...this.componentAttributes,

0 commit comments

Comments
 (0)