mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-20 02:37:41 -05:00
load code from the attr src of py-repl (#1292)
* load code from the attr src of py-repl * load code from the attr src of py-repl * load code from the attr src of py-repl * load code from the attr src of py-repl * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -12,6 +12,8 @@ import { getLogger } from '../logger';
|
||||
import { InterpreterClient } from '../interpreter_client';
|
||||
import type { PyScriptApp } from '../main';
|
||||
import { Stdio } from '../stdio';
|
||||
import { robustFetch } from '../fetch';
|
||||
import { _createAlertBanner } from '../exceptions';
|
||||
|
||||
const logger = getLogger('py-repl');
|
||||
const RUNBUTTON = `<svg 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>`;
|
||||
@@ -31,7 +33,7 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {
|
||||
editor: EditorView;
|
||||
stdout_manager: Stdio | null;
|
||||
stderr_manager: Stdio | null;
|
||||
|
||||
static observedAttributes = ['src'];
|
||||
connectedCallback() {
|
||||
ensureUniqueId(this);
|
||||
|
||||
@@ -51,6 +53,42 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {
|
||||
logger.debug(`element ${this.id} successfully connected`);
|
||||
}
|
||||
|
||||
get src() {
|
||||
return this.getAttribute('src');
|
||||
}
|
||||
|
||||
set src(value) {
|
||||
this.setAttribute('src', value);
|
||||
}
|
||||
|
||||
attributeChangedCallback(name: string, oldVal: string, newVal: string) {
|
||||
if (name === 'src' && newVal !== oldVal) {
|
||||
void this.loadReplSrc();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch url from src attribute of py-repl tags and
|
||||
* preload the code from fetch response into the Corresponding py-repl tag,
|
||||
* but please note that they will not be pre-run unless you click the runbotton.
|
||||
*/
|
||||
async loadReplSrc() {
|
||||
try {
|
||||
const response = await robustFetch(this.src);
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
const cmcontentElement = this.querySelector("div[class='cm-content']");
|
||||
const { lastElementChild } = cmcontentElement;
|
||||
cmcontentElement.replaceChildren(lastElementChild);
|
||||
lastElementChild.textContent = await response.text();
|
||||
logger.info(`loading code from ${this.src} to repl...success`);
|
||||
} catch (err) {
|
||||
const e = err as Error;
|
||||
_createAlertBanner(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/** Create and configure the codemirror editor
|
||||
*/
|
||||
makeEditor(pySrc: string): EditorView {
|
||||
|
||||
Reference in New Issue
Block a user