Skip to content

Commit 54c37fe

Browse files
loiswells97floehopper
authored andcommitted
Add runCode, stopCode & rerunCode to web component
Based on [1] & #899. [1]: f9086a8
1 parent 76fcc29 commit 54c37fe

5 files changed

Lines changed: 36 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1111
- Add `project_name_editable` attribute to web component (#1009)
1212
- `assets_identifier` attribute for the web component (#901)
1313
- `code` attribute overriding content of `main.py`/`index.html` in the web component (#901)
14+
- Web component methods to run, stop and rerun the code from the host page (#899)
1415

1516
### Changed
1617

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.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
"info",
5454
]),
5555
);
56-
webComp.setAttribute("output_only", "true");
5756
webComp.setAttribute("identifier", "hello-world-solution");
5857

5958
// Pre-set the code attribute with an empty string.

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,
@@ -101,6 +102,34 @@ class WebComponent extends HTMLElement {
101102
this.mountReactApp();
102103
}
103104

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

0 commit comments

Comments
 (0)