From 6235617682a6284ad1d0ca49e2d3b29ce3c03c6b Mon Sep 17 00:00:00 2001 From: lideming Date: Tue, 31 Oct 2023 12:56:21 +0800 Subject: [PATCH] feat: injectCss parent option --- src/lib/viewUtils.ts | 5 +++-- src/lib/viewlib.ts | 15 +++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/lib/viewUtils.ts b/src/lib/viewUtils.ts index 6b2890b..75a95eb 100644 --- a/src/lib/viewUtils.ts +++ b/src/lib/viewUtils.ts @@ -196,8 +196,9 @@ export function listenEvents>(element }; } -export function injectCss(css: string, options?: { tag: string; }) { - document.head.appendChild(buildDOM({ tag: options?.tag ?? 'style', text: css })); +export function injectCss(css: string, options?: { parent?: Node, tag?: string; }) { + const parent = options?.parent ?? document.head; + parent.appendChild(buildDOM({ tag: options?.tag ?? 'style', text: css })); } export class TextCompositionWatcher { diff --git a/src/lib/viewlib.ts b/src/lib/viewlib.ts index 3a4c443..9000b3b 100644 --- a/src/lib/viewlib.ts +++ b/src/lib/viewlib.ts @@ -3,13 +3,16 @@ import { injectCss } from "./viewUtils"; import css from "../../style.css"; -export function getWebfxCss() { return css; } +export function getWebfxCss() { + return css; +} + let cssInjected = false; -export function injectWebfxCss() { - if (!cssInjected) { - injectCss(getWebfxCss(), { tag: 'style.webfx-injected-style' }); - cssInjected = true; - } +export function injectWebfxCss(options?: { parent?: Node }) { + if (!cssInjected) { + injectCss(getWebfxCss(), { ...options, tag: "style.webfx-injected-style" }); + cssInjected = true; + } } // Views and helpers are moved to ../views/