better anon names
This commit is contained in:
parent
f643f8b2eb
commit
c04300802d
|
|
@ -197,9 +197,6 @@ function registerStyles(name: string, def: TagDef) {
|
||||||
injectStylesInBrowser()
|
injectStylesInBrowser()
|
||||||
}
|
}
|
||||||
|
|
||||||
// automatic names
|
|
||||||
let anonComponents = 1
|
|
||||||
|
|
||||||
// module-level scoping
|
// module-level scoping
|
||||||
export function createScope(scope: string) {
|
export function createScope(scope: string) {
|
||||||
return {
|
return {
|
||||||
|
|
@ -207,15 +204,15 @@ export function createScope(scope: string) {
|
||||||
if (typeof nameOrDef === 'string')
|
if (typeof nameOrDef === 'string')
|
||||||
return define(`${scope}${nameOrDef === 'Root' ? '' : nameOrDef}`, defIfNamed)
|
return define(`${scope}${nameOrDef === 'Root' ? '' : nameOrDef}`, defIfNamed)
|
||||||
else
|
else
|
||||||
return define(`${scope}Def${anonComponents++}`, nameOrDef as TagDef)
|
return define(`${scope}${anonName(nameOrDef)}`, nameOrDef as TagDef)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// the main event
|
// the main event
|
||||||
export function define(nameOrDef: string | TagDef, defIfNamed?: TagDef) {
|
export function define(nameOrDef: string | TagDef, defIfNamed?: TagDef) {
|
||||||
const name = defIfNamed ? (nameOrDef as string) : `Def${anonComponents++}`
|
|
||||||
const def = defIfNamed ?? nameOrDef as TagDef
|
const def = defIfNamed ?? nameOrDef as TagDef
|
||||||
|
const name = defIfNamed ? (nameOrDef as string) : anonName(def)
|
||||||
|
|
||||||
if (styles[name]) throw `${name} is already defined! Must use unique names.`
|
if (styles[name]) throw `${name} is already defined! Must use unique names.`
|
||||||
registerStyles(name, def)
|
registerStyles(name, def)
|
||||||
|
|
@ -231,5 +228,22 @@ export function define(nameOrDef: string | TagDef, defIfNamed?: TagDef) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// automatic names
|
||||||
|
const anonComponents: Record<string, number> = {}
|
||||||
|
|
||||||
|
// div tag -> Div1
|
||||||
|
function anonName(def: TagDef): string {
|
||||||
|
const base = (def.base ?? 'div')
|
||||||
|
const count = (anonComponents[base] ??= 1)
|
||||||
|
anonComponents[base] += 1
|
||||||
|
return tagName(base) + String(count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// a => Anchor, nav => Nav
|
||||||
|
function tagName(base: string): string {
|
||||||
|
const capitalized = base.slice(0, 1).toUpperCase() + base.slice(1)
|
||||||
|
return capitalized === 'A' ? 'Anchor' : capitalized
|
||||||
|
}
|
||||||
|
|
||||||
// shortcut so you only have to import one thing, if you want
|
// shortcut so you only have to import one thing, if you want
|
||||||
define.Styles = Styles
|
define.Styles = Styles
|
||||||
Loading…
Reference in New Issue
Block a user