forked from cms/tibi-svelte-starter
Füge Docker- und Babel-Konfigurationen hinzu, aktualisiere Svelte- und Esbuild-Setups, erweitere Typdefinitionen und aktualisiere die README-Datei
This commit is contained in:
45
frontend/src/admin.ts
Normal file
45
frontend/src/admin.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { SvelteComponent } from "svelte"
|
||||
|
||||
function getRenderedElement(
|
||||
component: typeof SvelteComponent,
|
||||
options?: { props: { [key: string]: any }; addCss?: string[] },
|
||||
nestedElements?: { tagName: string; className?: string }[]
|
||||
) {
|
||||
const el = document.createElement("div")
|
||||
el.attachShadow({ mode: "open" })
|
||||
|
||||
const body = document.createElement("body")
|
||||
|
||||
// build nested divs with css classes
|
||||
let target: HTMLElement = body
|
||||
nestedElements?.forEach((e) => {
|
||||
const newElement = document.createElement(e.tagName)
|
||||
if (e.className) {
|
||||
newElement.className = e.className
|
||||
}
|
||||
target.appendChild(newElement)
|
||||
target = newElement
|
||||
})
|
||||
|
||||
el.shadowRoot.appendChild(body)
|
||||
|
||||
options?.addCss?.forEach((css) => {
|
||||
const link = document.createElement("link")
|
||||
link.rel = "stylesheet"
|
||||
link.href = css
|
||||
link.type = "text/css"
|
||||
el.shadowRoot.appendChild(link)
|
||||
})
|
||||
|
||||
new component({
|
||||
target: target,
|
||||
props: options?.props,
|
||||
})
|
||||
|
||||
return el
|
||||
}
|
||||
|
||||
export {
|
||||
getRenderedElement,
|
||||
// pass also required svelte components here
|
||||
}
|
||||
Reference in New Issue
Block a user