ts strict - getAttribute (#863)

* ts strict getAttribute

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* mark xfail

* fix to string

* Remove

* use getAttribute for theme

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Ted Patrick
2022-10-21 07:47:44 -05:00
committed by GitHub
parent 58b4df6b3d
commit c352b502c4
11 changed files with 131 additions and 80 deletions

View File

@@ -5,7 +5,7 @@ import { Compartment, StateCommand } from '@codemirror/state';
import { keymap } from '@codemirror/view';
import { defaultKeymap } from '@codemirror/commands';
import { oneDarkTheme } from '@codemirror/theme-one-dark';
import { addClasses, htmlDecode } from '../utils';
import { getAttribute, addClasses, htmlDecode } from '../utils';
import { BaseEvalElement } from './base';
import type { Runtime } from '../runtime';
import { getLogger } from '../logger';
@@ -25,7 +25,11 @@ export function make_PyRepl(runtime: Runtime) {
let initialTheme: string;
function getEditorTheme(el: BaseEvalElement): string {
return initialTheme || (initialTheme = el.getAttribute('theme'));
const theme = getAttribute(el, 'theme');
if( !initialTheme && theme){
initialTheme = theme;
}
return initialTheme;
}
class PyRepl extends BaseEvalElement {
@@ -120,26 +124,44 @@ export function make_PyRepl(runtime: Runtime) {
this.setAttribute('root', this.id);
}
if (this.hasAttribute('output')) {
this.errorElement = this.outputElement = document.getElementById(this.getAttribute('output'));
const output = getAttribute(this, "output")
if (output) {
const el = document.getElementById(output);
if(el){
this.errorElement = el;
this.outputElement = el
}
} else {
if (this.hasAttribute('std-out')) {
this.outputElement = document.getElementById(this.getAttribute('std-out'));
const stdOut = getAttribute(this, "std-out");
if (stdOut) {
const el = document.getElementById(stdOut);
if(el){
this.outputElement = el
}
} else {
// In this case neither output or std-out have been provided so we need
// to create a new output div to output to
this.outputElement = document.createElement('div');
this.outputElement.classList.add('output');
this.outputElement.hidden = true;
this.outputElement.id = this.id + '-' + this.getAttribute('exec-id');
const stdOut = getAttribute(this, "exec-id") || "";
this.outputElement.id = this.id + '-' + stdOut;
// add the output div id if there's not output pre-defined
mainDiv.appendChild(this.outputElement);
}
this.errorElement = this.hasAttribute('std-err')
? document.getElementById(this.getAttribute('std-err'))
: this.outputElement;
const stdErr = getAttribute(this, "std-err");
if( stdErr ){
const el = document.getElementById(stdErr);
if(el){
this.errorElement = el;
}else{
this.errorElement = this.outputElement
}
}else{
this.errorElement = this.outputElement
}
}
this.appendChild(mainDiv);
@@ -178,13 +200,15 @@ export function make_PyRepl(runtime: Runtime) {
this.removeAttribute('auto-generate');
}
if(this.hasAttribute('output-mode')) {
newPyRepl.setAttribute('output-mode', this.getAttribute('output-mode'));
const outputMode = getAttribute( this, 'output-mode')
if(outputMode) {
newPyRepl.setAttribute('output-mode', outputMode);
}
const addReplAttribute = (attribute: string) => {
if (this.hasAttribute(attribute)) {
newPyRepl.setAttribute(attribute, this.getAttribute(attribute));
const attr = getAttribute( this, attribute)
if(attr) {
newPyRepl.setAttribute(attribute, attr);
}
};
@@ -193,7 +217,9 @@ export function make_PyRepl(runtime: Runtime) {
addReplAttribute('std-err');
newPyRepl.setAttribute('exec-id', nextExecId.toString());
this.parentElement.appendChild(newPyRepl);
if( this.parentElement ){
this.parentElement.appendChild(newPyRepl);
}
}
}