This commit is contained in:
Asaki Yuki 2026-01-23 15:15:48 +07:00
parent 6817734ed9
commit 99c294a9ba
14 changed files with 19528 additions and 19413 deletions

View file

@ -1,27 +1,29 @@
import { isCompileBinding } from "../compilers/bindings/Checker.js"
import { Parser } from "../compilers/bindings/Parser.js"
import { FormatProperties } from "../compilers/FormatProperties.js"
import { Memory } from "../compilers/Memory.js"
import { BindingType } from "../types/enums/BindingType.js"
import { Renderer } from "../types/enums/Renderer.js"
import { Type } from "../types/enums/Type.js"
import { Properties } from "../types/properties/components.js"
import { BindingItem } from "../types/properties/value.js"
import { Class } from "./Class.js"
import { RandomString } from "./Utils.js"
import { ExtendsOf, RandomString } from "./Utils.js"
import util from "node:util"
export class UI<T extends Type, K extends Renderer | null = null> extends Class {
path: string
readonly path: string
name: string
namespace: string
extend?: UI<Type, Renderer | null>
readonly name: string
readonly namespace: string
readonly extend?: UI<Type, Renderer | null>
extendable: boolean
readonly extendable: boolean
controls = new Map<string, [UI<Type, Renderer | null>, Properties<Type, Renderer | null>]>()
bindings: BindingItem[] = []
readonly controls = new Map<string, [UI<Type, Renderer | null>, Properties<Type, Renderer | null>]>()
readonly bindings: BindingItem[] = []
readonly extendType?: Type
properties: Properties<T, K> = <any>{}
constructor(
@ -37,6 +39,11 @@ export class UI<T extends Type, K extends Renderer | null = null> extends Class
process.exit(1)
}
if (namespace && !/^\w+$/.test(namespace)) {
console.error(`The '${namespace}' cannot be used as a namespace`)
process.exit(1)
}
this.name = name?.match(/^(\w|\/)+/)?.[0] || RandomString(16)
this.namespace = namespace || RandomString(16)
@ -44,6 +51,29 @@ export class UI<T extends Type, K extends Renderer | null = null> extends Class
else this.path = path
this.extendable = this.name.search("/") === -1
Memory.add(this)
}
protected UI_JSON() {
const obj: any = {
...FormatProperties(this.properties),
}
if (this.type) {
obj.type = this.type
}
if (this.bindings.length) {
obj.bindings = this.bindings
}
if (this.controls.size) {
obj.controls = []
this.controls.forEach((e, key) => obj.controls.push({ [key + e[0]]: e[1] }))
}
return obj
}
setProperties(properties: Properties<T, K>) {
@ -77,32 +107,19 @@ export class UI<T extends Type, K extends Renderer | null = null> extends Class
return this
}
toString() {
clone(properties?: Properties<T, K>, name?: string, namespace?: string) {
return ExtendsOf(this, properties, name, namespace)
}
protected toString() {
return `@${this.namespace}.${this.name}`
}
toJSON() {
const obj: any = {
...FormatProperties(this.properties),
}
if (this.type) {
obj.type = this.type
}
if (this.bindings.length) {
obj.bindings = this.bindings
}
if (this.controls.size) {
obj.controls = []
this.controls.forEach((e, key) => obj.controls.push({ [key + e[0]]: e[1] }))
}
return obj
protected toJSON() {
return this.UI_JSON()
}
[util.inspect.custom]($: any, opts: any) {
protected [util.inspect.custom]($: any, opts: any) {
const obj: any = {
...FormatProperties(this.properties),
}
@ -116,8 +133,10 @@ export class UI<T extends Type, K extends Renderer | null = null> extends Class
this.controls.forEach((e, key) => obj.controls.push({ [key + e[0]]: e[1] }))
}
const elementType = this.type || (this.extend ? `${this.extendType || "unknown"}:${this.extend}` : "unknown")
return `\x1b[33mUI\x1b[0m<\x1b[92m${
this.type || this.extend ? `${this.extend}` : "ANY"
elementType
}\x1b[0m> \x1b[92m"${this}\x1b[92m"\x1b[0m ${util.inspect(obj, opts)}\n`
}
}
@ -127,4 +146,9 @@ export class ModifyUI<T extends Type> extends UI<T, null> {
if (!path) throw new Error("ModifyUI cannot have a path")
super(undefined, name, namespace, path)
}
protected toJSON() {
const obj = this.UI_JSON()
return obj
}
}