From 2059df3ecd9a5c9105e146dc1b8b63225c495962 Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Wed, 13 Apr 2022 14:46:44 -0500 Subject: [PATCH] make pybox with accepting widths attribute to customize box children ration on the page --- pyscriptjs/src/components/pybox.ts | 78 ++++++++++++++++-------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/pyscriptjs/src/components/pybox.ts b/pyscriptjs/src/components/pybox.ts index 5db92e2..b544227 100644 --- a/pyscriptjs/src/components/pybox.ts +++ b/pyscriptjs/src/components/pybox.ts @@ -1,27 +1,5 @@ import { addClasses } from '../utils'; -// Premise used to connect to the first available pyodide interpreter -// let pyodideReadyPromise; -// let environments; -// let currentMode; - -// pyodideLoaded.subscribe(value => { -// pyodideReadyPromise = value; -// }); -// loadedEnvironments.subscribe(value => { -// environments = value; -// }); - -// let propertiesNavOpen; -// componentDetailsNavOpen.subscribe(value => { -// propertiesNavOpen = value; -// }); - -// mode.subscribe(value => { -// currentMode = value; -// }); - - export class PyBox extends HTMLElement { shadow: ShadowRoot; wrapper: HTMLElement; @@ -41,27 +19,53 @@ export class PyBox extends HTMLElement { connectedCallback() { - this.innerHTML = ''; - let mainDiv = document.createElement('div'); addClasses(mainDiv, ["flex"]) - // add Editor to main PyScript div - debugger - // mainDiv.appendChild(eDiv); - // mainDiv.appendChild(this.editorNode); - - if (!this.id){ - console.log("WARNING: define with an id. should always have an id. More than one on a page won't work otherwise!") + // mainDiv.append(...this.childNodes); + + // Ugly hack: for some reason when moving children, the editor box duplicates children + // meaning that we end up with 2 editors, if there's a inside the + // so, if we have more than 2 children with the cm-editor class, we remove one of them + + while (this.childNodes.length > 0) { + console.log(this.firstChild); + if ( this.firstChild.nodeName == "PY-REPL" ){ + // in this case we need to remove the child and craete a new one from scratch + let replDiv = document.createElement('div'); + // we need to put the new repl inside a div so that if the repl has auto-generate true + // it can replicate itself inside that constrained div + replDiv.appendChild(this.firstChild.cloneNode()); + mainDiv.appendChild(replDiv); + this.firstChild.remove(); + } + else{ + if ( this.firstChild.nodeName != "#text" ){ + mainDiv.appendChild(this.firstChild); + }else{ + this.firstChild.remove() + } + } } - if (!this.hasAttribute('widths')) { - this.setAttribute("exec-id", "1"); + // now we need to set widths + this.widths = [] + if (this.hasAttribute('widths')) { + for (let w of this.getAttribute('widths').split(";")) { + this.widths.push(`w-${w}`); + } + }else{ + for (let el of mainDiv.childNodes) { + this.widths.push(`w-1/${mainDiv.childNodes.length}`); + } } - - this.appendChild(mainDiv); - - console.log('connected'); + for (let i in this.widths) { + // @ts-ignore + addClasses(mainDiv.childNodes[parseInt(i)], [this.widths[i]]); + } + + this.appendChild(mainDiv); + console.log('py-box connected'); } }