8000 fixed REPL bugs (#372) · sarvjeetdev/pyscript@c040158 · GitHub
[go: up one dir, main page]

Skip to content

Commit c040158

Browse files
marianoweberMariano Weberpre-commit-ci[bot]
authored
fixed REPL bugs (pyscript#372)
* fixed REPL play button alignment * fixed REPL auto-generate * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed nextExecId selector * added removeClasses to utils.ts * fixed visual bug after error output * changed play button alignment Co-authored-by: Mariano Weber <info@uiremotely.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6898daf commit c040158

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

pyscriptjs/src/components/base.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { loadedEnvironments, mode, pyodideLoaded } from '../stores';
2-
import { guidGenerator, addClasses } from '../utils';
2+
import { guidGenerator, addClasses, removeClasses } from '../utils';
33
// Premise used to connect to the first available pyodide interpreter
44
let runtime;
55
let environments;
@@ -137,6 +137,19 @@ export class BaseEvalElement extends HTMLElement {
137137
this.outputElement.style.display = 'block';
138138
}
139139

140+
// check if this REPL contains errors, delete them and remove error classes
141+
const errorElements = document.querySelectorAll(`div[id^='${this.errorElement.id}'][error]`);
142+
if (errorElements.length > 0) {
143+
for (const errorElement of errorElements) {
144+
errorElement.classList.add('hidden');
145+
if(this.hasAttribute('std-err')) {
146+
this.errorElement.hidden = true;
147+
this.errorElement.style.removeProperty('display');
148+
}
149+
}
150+
removeClasses(this.errorElement, ['bg-red-200', 'p-2']);
151+
}
152+
140153
this.postEvaluate();
141154
} catch (err) {
142155
if (Element === undefined) {
@@ -146,6 +159,8 @@ export class BaseEvalElement extends HTMLElement {
146159

147160
addClasses(this.errorElement, ['bg-red-200', 'p-2']);
148161
out.write.callKwargs(err, { append: true });
162+
163+
this.errorElement.children[this.errorElement.children.length - 1].setAttribute('error', '')
149164
this.errorElement.hidden = false;
150165
this.errorElement.style.display = 'block';
151166
}

pyscriptjs/src/components/pyrepl.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class PyRepl extends BaseEvalElement {
101101
this.btnRun = document.createElement('button');
102102
this.btnRun.innerHTML =
103103
'<svg id="" class="svelte-fa svelte-ps5qeg" style="height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green" viewBox="0 0 384 512" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg"><g transform="translate(192 256)" transform-origin="96 0"><g transform="translate(0,0) scale(1,1)"><path d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z" fill="currentColor" transform="translate(-192 -256)"></path></g></g></svg>';
104-
addClasses(this.btnRun, ['absolute', 'right-1', 'bottom-3', 'opacity-0', 'group-hover:opacity-100']);
104+
addClasses(this.btnRun, ['absolute', 'right-1', 'bottom-1', 'opacity-0', 'group-hover:opacity-100']);
105105
this.editorNode.appendChild(this.btnRun);
106106

107107
this.btnRun.onclick = wrap(this);
@@ -171,12 +171,16 @@ export class PyRepl extends BaseEvalElement {
171171
this.outputElement.style.display = 'block';
172172

173173
if (this.hasAttribute('auto-generate')) {
174-
const nextExecId = parseInt(this.getAttribute('exec-id')) + 1;
175-
const newPyRepl = document.createElement('py-repl');
174+
const allPyRepls = document.querySelectorAll(`py-repl[root='${this.getAttribute('root')}'][exec-id]`);
175+
const lastRepl = allPyRepls[allPyRepls.length -1 ];
176+
const lastExecId = lastRepl.getAttribute('exec-id');
177+
const nextExecId = parseInt(lastExecId) + 1;
176178

179+
const newPyRepl = document.createElement('py-repl');
177180
newPyRepl.setAttribute('root', this.getAttribute('root'));
178181
newPyRepl.id = this.getAttribute('root') + '-' + nextExecId.toString();
179-
newPyRepl.setAttribute('auto-generate', null);
182+
newPyRepl.setAttribute('auto-generate', '');
183+
this.removeAttribute('auto-generate');
180184

181185
if (this.hasAttribute('output')) {
182186
newPyRepl.setAttribute('output', this.getAttribute('output'));

pyscriptjs/src/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ function addClasses(element: HTMLElement, classes: Array<string>) {
44
}
55
}
66

7+
function removeClasses(element: HTMLElement, classes: Array<string>) {
8+
for (const entry of classes) {
9+
element.classList.remove(entry);
10+
}
11+
}
12+
713
function getLastPath(str: string): string {
814
return str.split('\\').pop().split('/').pop();
915
}
@@ -37,4 +43,4 @@ function guidGenerator(): string {
3743
return S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4();
3844
}
3945

40-
export { addClasses, getLastPath, ltrim, htmlDecode, guidGenerator };
46+
export { addClasses, removeClasses, getLastPath, ltrim, htmlDecode, guidGenerator };

0 commit comments

Comments
 (0)
0