Minor refactoring (#437)

This commit is contained in:
ic-768
2022-05-23 23:15:37 +00:00
committed by GitHub
parent c951961a92
commit 8cd5ba6361
3 changed files with 22 additions and 37 deletions

View File

@@ -62,7 +62,6 @@ export class BaseEvalElement extends HTMLElement {
}
async getSourceFromFile(s: string): Promise<string> {
const pyodide = runtime;
const response = await fetch(s);
this.code = await response.text();
return this.code;
@@ -104,11 +103,8 @@ export class BaseEvalElement extends HTMLElement {
let source: string;
let output;
try {
if (this.source) {
source = await this.getSourceFromFile(this.source);
} else {
source = this.getSourceFromElement();
}
source = this.source ? await this.getSourceFromFile(this.source)
: this.getSourceFromElement();
await this._register_esm(pyodide);
@@ -142,7 +138,7 @@ export class BaseEvalElement extends HTMLElement {
if (errorElements.length > 0) {
for (const errorElement of errorElements) {
errorElement.classList.add('hidden');
if(this.hasAttribute('std-err')) {
if (this.hasAttribute('std-err')) {
this.errorElement.hidden = true;
this.errorElement.style.removeProperty('display');
}
@@ -160,7 +156,7 @@ export class BaseEvalElement extends HTMLElement {
addClasses(this.errorElement, ['bg-red-200', 'p-2']);
out.write.callKwargs(err, { append: true });
this.errorElement.children[this.errorElement.children.length - 1].setAttribute('error', '')
this.errorElement.children[this.errorElement.children.length - 1].setAttribute('error', '');
this.errorElement.hidden = false;
this.errorElement.style.display = 'block';
}
@@ -249,7 +245,6 @@ function createWidget(name: string, code: string, klass: string) {
}
}
}
const xPyWidget = customElements.define(name, CustomWidget);
}
export class PyWidget extends HTMLElement {
@@ -290,7 +285,6 @@ export class PyWidget extends HTMLElement {
throw new ReferenceError(
`No id specified for component. Components must have an explicit id. Please use id="" to specify your component id.`,
);
return;
}
const mainDiv = document.createElement('div');

View File

@@ -43,11 +43,7 @@ function createCmdHandler(el: PyRepl): StateCommand {
let initialTheme: string;
function getEditorTheme(el: BaseEvalElement): string {
if (initialTheme) {
return initialTheme;
}
return (initialTheme = el.getAttribute('theme'));
return initialTheme || (initialTheme = el.getAttribute('theme'));
}
export class PyRepl extends BaseEvalElement {
@@ -165,11 +161,9 @@ export class PyRepl extends BaseEvalElement {
mainDiv.appendChild(this.outputElement);
}
if (this.hasAttribute('std-err')) {
this.errorElement = document.getElementById(this.getAttribute('std-err'));
} else {
this.errorElement = this.outputElement;
}
this.errorElement = this.hasAttribute('std-err')
? document.getElementById(this.getAttribute('std-err'))
: this.outputElement;
}
this.appendChild(mainDiv);
@@ -188,7 +182,7 @@ export class PyRepl extends BaseEvalElement {
if (this.hasAttribute('auto-generate')) {
const allPyRepls = document.querySelectorAll(`py-repl[root='${this.getAttribute('root')}'][exec-id]`);
const lastRepl = allPyRepls[allPyRepls.length -1 ];
const lastRepl = allPyRepls[allPyRepls.length - 1];
const lastExecId = lastRepl.getAttribute('exec-id');
const nextExecId = parseInt(lastExecId) + 1;
@@ -198,17 +192,15 @@ export class PyRepl extends BaseEvalElement {
newPyRepl.setAttribute('auto-generate', '');
this.removeAttribute('auto-generate');
if (this.hasAttribute('output')) {
newPyRepl.setAttribute('output', this.getAttribute('output'));
}
const addReplAttribute = (attribute: string) => {
if (this.hasAttribute(attribute)) {
newPyRepl.setAttribute(attribute, this.getAttribute(attribute));
}
};
if (this.hasAttribute('std-out')) {
newPyRepl.setAttribute('std-out', this.getAttribute('std-out'));
}
if (this.hasAttribute('std-err')) {
newPyRepl.setAttribute('std-err', this.getAttribute('std-err'));
}
addReplAttribute('output');
addReplAttribute('std-out');
addReplAttribute('std-err');
newPyRepl.setAttribute('exec-id', nextExecId.toString());
this.parentElement.appendChild(newPyRepl);

View File

@@ -32,8 +32,8 @@ function ltrim(code: string): string {
const k = Math.min(...lengths);
if (k != 0) return lines.map(line => line.substring(k)).join('\n');
else return code;
return k != 0 ? lines.map(line => line.substring(k)).join('\n')
: code;
}
function guidGenerator(): string {
@@ -58,10 +58,10 @@ function showError(msg: string): void {
document.body.prepend(warning);
}
function handleFetchError(e: Error, singleFile: string){
function handleFetchError(e: Error, singleFile: string) {
//Should we still export full error contents to console?
console.warn('Caught an error in loadPaths:\r\n' + e);
let errorContent;
let errorContent: string;
if (e.message.includes('TypeError: Failed to fetch')) {
errorContent = `<p>PyScript: Access to local files
(using "Paths:" in &lt;py-env&gt;)
@@ -75,8 +75,7 @@ function handleFetchError(e: Error, singleFile: string){
singleFile +
`</u> failed with error 404 (File not Found). Are your filename and path are correct?</p>`;
} else {
errorContent =
'<p>PyScript encountered an error while loading from file: ' + e.message + '</p>';
errorContent = '<p>PyScript encountered an error while loading from file: ' + e.message + '</p>';
}
showError(errorContent);
}