Cleanup some unnecessary utility (#1453)

This commit is contained in:
Andrea Giammarchi
2023-05-06 08:20:08 +02:00
committed by GitHub
parent 213ced0c7f
commit 6a27c6d9f2
5 changed files with 8 additions and 41 deletions

View File

@@ -8,7 +8,7 @@ import { InterpreterClient } from './interpreter_client';
import { PluginManager, Plugin, PythonPlugin } from './plugin';
import { make_PyScript, initHandlers, mountElements } from './components/pyscript';
import { getLogger } from './logger';
import { showWarning, globalExport, createLock } from './utils';
import { showWarning, createLock } from './utils';
import { calculateFetchPaths } from './plugins/calculateFetchPaths';
import { createCustomElements } from './components/elements';
import { UserError, ErrorCode, _createAlertBanner } from './exceptions';
@@ -450,10 +450,7 @@ modules must contain a "plugin" attribute. For more information check the plugin
}
}
function pyscript_get_config() {
return globalApp.config;
}
globalExport('pyscript_get_config', pyscript_get_config);
globalThis.pyscript_get_config = () => globalApp.config;
// main entry point of execution
const globalApp = new PyScriptApp();

View File

@@ -143,7 +143,7 @@ export class PluginManager {
}
add(...plugins: Plugin[]) {
for (const p of plugins) this._plugins.push(p);
this._plugins.push(...plugins);
}
addPythonPlugin(plugin: PythonPlugin) {

View File

@@ -1,7 +1,7 @@
import toml from '@hoodmane/toml-j0.4';
import { getLogger } from './logger';
import { version } from './version';
import { getAttribute, readTextFromPath, htmlDecode, createDeprecationWarning } from './utils';
import { readTextFromPath, htmlDecode, createDeprecationWarning } from './utils';
import { UserError, ErrorCode } from './exceptions';
const logger = getLogger('py-config');
@@ -74,7 +74,7 @@ export function loadConfigFromElement(el: Element): AppConfig {
srcConfig = {};
inlineConfig = {};
} else {
const configType = getAttribute(el, 'type') || 'toml';
const configType = el.getAttribute('type') || 'toml';
srcConfig = extractFromSrc(el, configType);
inlineConfig = extractFromInline(el, configType);
}
@@ -88,7 +88,7 @@ export function loadConfigFromElement(el: Element): AppConfig {
}
function extractFromSrc(el: Element, configType: string) {
const src = getAttribute(el, 'src');
const src = el.getAttribute('src');
if (src) {
logger.info('loading ', src);
return validateConfig(readTextFromPath(src), configType);

View File

@@ -55,7 +55,6 @@ export async function pyDisplay(interpreter: InterpreterClient, obj: any, kwargs
}
export function displayPyException(err: Error, errElem: HTMLElement) {
//addClasses(errElem, ['py-error'])
const pre = document.createElement('pre');
pre.className = 'py-error';

View File

@@ -2,18 +2,6 @@ import { $$ } from 'basic-devtools';
import { _createAlertBanner } from './exceptions';
export function addClasses(element: HTMLElement, classes: string[]) {
for (const entry of classes) {
element.classList.add(entry);
}
}
export function removeClasses(element: HTMLElement, classes: string[]) {
for (const entry of classes) {
element.classList.remove(entry);
}
}
export function escape(str: string): string {
return str.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
@@ -60,23 +48,6 @@ export function inJest(): boolean {
return typeof process === 'object' && process.env.JEST_WORKER_ID !== undefined;
}
export function globalExport(name: string, obj: object) {
// attach the given object to the global object, so that it is globally
// visible everywhere. Should be used very sparingly!
globalThis[name] = obj;
}
export function getAttribute(el: Element, attr: string): string | null {
if (el.hasAttribute(attr)) {
const value = el.getAttribute(attr);
if (value) {
return value;
}
}
return null;
}
export function joinPaths(parts: string[], separator = '/') {
const res = parts
.map(function (part) {
@@ -102,11 +73,11 @@ export function createDeprecationWarning(msg: string, elementName: string): void
* @param sentinelText {string} [null] The text to match against existing warning banners.
* If null, the full text of 'msg' is used instead.
*/
export function createSingularWarning(msg: string, sentinelText: string | null = null): void {
export function createSingularWarning(msg: string, sentinelText?: string): void {
const banners = $$('.alert-banner, .py-warning', document);
let bannerCount = 0;
for (const banner of banners) {
if (banner.innerHTML.includes(sentinelText ? sentinelText : msg)) {
if (banner.innerHTML.includes(sentinelText || msg)) {
bannerCount++;
}
}