Fix #1834 - Throw an error if more than a terminal exists (#1837)

This commit is contained in:
Andrea Giammarchi
2023-11-03 15:19:30 +01:00
committed by GitHub
parent c8ec29a3d8
commit aef028be6e
4 changed files with 71 additions and 12 deletions

View File

@@ -1,27 +1,35 @@
// PyScript py-terminal plugin
import { TYPES, hooks } from "../core.js";
import { notify } from "./error.js";
const SELECTOR = [...TYPES.keys()]
.map((type) => `script[type="${type}"][terminal],${type}-script[terminal]`)
.join(",");
// show the error on main and
// stops the module from keep executing
const notifyAndThrow = (message) => {
notify(message);
throw new Error(message);
};
const pyTerminal = async () => {
const terminals = document.querySelectorAll(SELECTOR);
// no results will look further for runtime nodes
if (!terminals.length) return;
// we currently support only one terminal as in "classic"
if (terminals.length > 1)
console.warn("Unable to satisfy multiple terminals");
// if we arrived this far, let's drop the MutationObserver
// as we only support one terminal per page (right now).
mo.disconnect();
// we currently support only one terminal as in "classic"
if (terminals.length > 1) notifyAndThrow("You can use at most 1 terminal.");
const [element] = terminals;
// hopefully to be removed in the near future!
if (element.matches('script[type="mpy"],mpy-script'))
throw new Error("Unsupported terminal");
notifyAndThrow("Unsupported terminal.");
// import styles lazily
document.head.append(