Skip to content

Commit fe31a91

Browse files
committed
fixes and improvements
1 parent 2cbd615 commit fe31a91

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideRunner.jsx

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
7575
const showVisualTab = queryParams.get("show_visual_tab") === "true";
7676
const [hasVisual, setHasVisual] = useState(showVisualTab || senseHatAlways);
7777
const [visuals, setVisuals] = useState([]);
78-
const [showRunner, setShowRunner] = useState(active);
7978
const [inputStack, setInputStack] = useState([]);
8079
const [indentationLevel, setIndentationLevel] = useState(0);
8180
const [awaitingInput, setAwaitingInput] = useState(false);
@@ -88,30 +87,41 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
8887
const prependToInputStack = (input) => {
8988
setInputStack((prevInputStack) => {
9089
if (prevInputStack[0] === "") {
91-
console.log("overwriting...");
9290
const newStack = [...prevInputStack];
9391
newStack[0] = input;
9492
return newStack;
9593
} else {
96-
console.log("prepending...");
97-
console.log(prevInputStack);
9894
return [input, ...prevInputStack];
9995
}
10096
});
10197
};
10298
const [inputStackIndex, setInputStackIndex] = useState(0);
10399

104-
const incrementIndentationLevel = () => {
105-
setIndentationLevel((prevIndentationLevel) => prevIndentationLevel + 1);
100+
const incrementIndentationLevel = (prevLine) => {
101+
// console.log("prevLine", prevLine);
102+
// console.log(prevLine.match(/^\s*/)[0].length);
103+
const prevLevel = prevLine
104+
? Math.floor(prevLine.match(/^\s*/)[0].length / 4)
105+
: 0;
106+
setIndentationLevel(prevLevel + 1);
106107
};
107108

108-
useEffect(() => {
109-
console.log("indentationLevel", indentationLevel);
110-
}, [indentationLevel]);
109+
const keepSameIndentationLevel = (prevLine) => {
110+
const prevLevel = prevLine
111+
? Math.floor(prevLine.match(/^\s*/)[0].length / 4)
112+
: 0;
113+
setIndentationLevel(prevLevel);
114+
};
111115

112-
useEffect(() => {
113-
console.log("isOutputOnly", isOutputOnly);
114-
});
116+
const handleIndentationLevel = (prevLine) => {
117+
if (prevLine.trimEnd().slice(-1) === ":") {
118+
incrementIndentationLevel(prevLine);
119+
} else if (prevLine.trimEnd() === "") {
120+
setIndentationLevel(0);
121+
} else {
122+
keepSameIndentationLevel(prevLine);
123+
}
124+
};
115125

116126
useEffect(() => {
117127
const handleKeyDown = (event) => {
@@ -149,14 +159,16 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
149159
}, [awaitingInput, indentationLevel, consoleMode]);
150160

151161
useEffect(() => {
152-
console.log("inputStack", inputStack);
153-
}, [inputStack]);
154-
155-
useEffect(() => {
156-
console.log("inputStackIndex", inputStackIndex);
157162
const inputElement = getInputElement();
158163
if (inputElement) {
159164
inputElement.innerText = inputStack[inputStackIndex];
165+
// move cursor to end of text
166+
const range = document.createRange();
167+
const selection = window.getSelection();
168+
range.selectNodeContents(inputElement);
169+
range.collapse(false);
170+
selection.removeAllRanges();
171+
selection.addRange(range);
160172
}
161173
}, [inputStackIndex]);
162174

@@ -199,10 +211,10 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
199211
}, [pyodideWorker]);
200212

201213
useEffect(() => {
202-
if (autoRun) {
214+
if (autoRun && active && pyodideWorker) {
203215
dispatch(triggerCodeRun());
204216
}
205-
}, []);
217+
}, [active, pyodideWorker]);
206218

207219
useEffect(() => {
208220
if (codeRunTriggered && active && output.current) {
@@ -254,13 +266,19 @@ const PyodideRunner = ({ active, consoleMode = false, autoRun = false }) => {
254266
setAwaitingInput(false);
255267

256268
prependToInputStack(content);
257-
if (content.trimEnd().slice(-1) === ":") {
258-
incrementIndentationLevel();
259-
} else if (content.trimEnd() === "") {
260-
console.log("the content is");
261-
console.log(content);
262-
setIndentationLevel(0);
263-
}
269+
// if (content.trimEnd().slice(-1) === ":") {
270+
// incrementIndentationLevel(content);
271+
// } else if (content.trimEnd() === "") {
272+
// // console.log("the content is");
273+
// // console.log(content);
274+
// setIndentationLevel(0);
275+
// }
276+
277+
console.log(
278+
"indentedLine",
279+
getInputElement().parentElement.innerText.matches(/...:/),
280+
);
281+
handleIndentationLevel(content);
264282

265283
const encoder = new TextEncoder();
266284
const bytes = encoder.encode(content + "\n");

0 commit comments

Comments
 (0)