mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-30 09:42:44 +00:00
Add resource node + improve default mods
This commit is contained in:
@@ -10,18 +10,22 @@ import { BooleanNode } from '../nodes/BooleanNode'
|
||||
import { MapNode } from '../nodes/MapNode'
|
||||
import { NumberNode } from '../nodes/NumberNode'
|
||||
import { RangeNode } from '../nodes/custom/RangeNode'
|
||||
import { ResourceNode } from '../nodes/custom/ResourceNode'
|
||||
|
||||
const EntityCollection = ['sheep', 'pig']
|
||||
|
||||
const predicateTree = new RootNode('predicate', {
|
||||
condition: new EnumNode(['foo', 'bar'], {
|
||||
default: () => 'bar',
|
||||
transform: (s: string) => (s === 'foo') ? {test: 'baz'} : s
|
||||
}),
|
||||
number: new NumberNode({integer: false, min: 0}),
|
||||
range: new RangeNode(),
|
||||
predicate: new ObjectNode({
|
||||
type: new EnumNode(EntityCollection),
|
||||
nbt: new StringNode(),
|
||||
nbt: new ResourceNode({
|
||||
default: (v) => "hahaha"
|
||||
}),
|
||||
test: new BooleanNode(),
|
||||
recipes: new MapNode(
|
||||
new StringNode(),
|
||||
|
||||
@@ -2,9 +2,6 @@ import { DataModel } from "../model/DataModel"
|
||||
import { Path } from "../model/Path"
|
||||
import { TreeView } from "../view/TreeView"
|
||||
|
||||
export type IDefault<T> = (value?: T) => T | undefined
|
||||
export type ITransform<T> = (value: T) => any
|
||||
|
||||
export interface INode<T> {
|
||||
setParent: (parent: INode<any>) => void
|
||||
default: IDefault<T>
|
||||
@@ -26,9 +23,12 @@ export type NodeChildren = {
|
||||
[name: string]: INode<any>
|
||||
}
|
||||
|
||||
export type IDefault<T> = (value?: T) => T | undefined
|
||||
export type ITransform<T> = (value: T) => any
|
||||
|
||||
export interface NodeMods<T> {
|
||||
default?: IDefault<T>
|
||||
transform?: (value: T) => any
|
||||
transform?: ITransform<T>
|
||||
}
|
||||
|
||||
export abstract class AbstractNode<T> implements INode<T> {
|
||||
@@ -36,11 +36,9 @@ export abstract class AbstractNode<T> implements INode<T> {
|
||||
defaultMod: IDefault<T>
|
||||
transformMod: ITransform<T>
|
||||
|
||||
constructor(mods?: NodeMods<T>, defaultMods?: NodeMods<T>, ) {
|
||||
this.defaultMod = mods?.default ? mods.default :
|
||||
defaultMods?.default ? defaultMods.default : (v) => v
|
||||
this.transformMod = mods?.transform ? mods.transform :
|
||||
defaultMods?.transform ? defaultMods.transform : (v) => v
|
||||
constructor(mods?: NodeMods<T>) {
|
||||
this.defaultMod = mods?.default ? mods.default : (v) => v
|
||||
this.transformMod = mods?.transform ? mods.transform : (v) => v
|
||||
}
|
||||
|
||||
setParent(parent: INode<any>) {
|
||||
|
||||
@@ -3,16 +3,16 @@ import { Path } from "../model/Path";
|
||||
import { TreeView } from "../view/TreeView";
|
||||
|
||||
export interface BooleanNodeMods extends NodeMods<boolean> {
|
||||
force: boolean
|
||||
force?: boolean
|
||||
}
|
||||
|
||||
export class BooleanNode extends AbstractNode<boolean> {
|
||||
force: boolean
|
||||
|
||||
constructor(mods?: BooleanNodeMods) {
|
||||
super(mods, {
|
||||
default: () => false
|
||||
})
|
||||
super({
|
||||
default: () => false,
|
||||
...mods})
|
||||
this.force = (mods?.force === true)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ import { Path } from '../model/Path'
|
||||
export class EnumNode extends AbstractNode<string> implements StateNode<string> {
|
||||
protected options: string[]
|
||||
|
||||
constructor(options: string[], mods?: NodeMods<string>) {
|
||||
super(mods)
|
||||
constructor(options: string[], mods?: NodeMods<string> | string) {
|
||||
super(typeof mods === 'string' ? {default: () => mods} : mods)
|
||||
this.options = options
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ export class ListNode extends AbstractNode<IObject[]> {
|
||||
protected children: INode<any>
|
||||
|
||||
constructor(values: INode<any>, mods?: NodeMods<IObject[]>) {
|
||||
super(mods, {
|
||||
default: () => []
|
||||
})
|
||||
super({
|
||||
default: () => [],
|
||||
...mods})
|
||||
this.children = values
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ export class MapNode extends AbstractNode<IMap> {
|
||||
protected values: INode<any>
|
||||
|
||||
constructor(keys: StateNode<string>, values: INode<any>, mods?: NodeMods<IMap>) {
|
||||
super(mods, {
|
||||
default: () => ({})
|
||||
})
|
||||
super({
|
||||
default: () => ({}),
|
||||
...mods})
|
||||
this.keys = keys
|
||||
this.values = values
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ export class NumberNode extends AbstractNode<number> implements StateNode<number
|
||||
max: number
|
||||
|
||||
constructor(mods?: NumberNodeMods) {
|
||||
super(mods, {
|
||||
default: () => 0
|
||||
})
|
||||
super({
|
||||
default: () => 0,
|
||||
...mods})
|
||||
this.integer = mods?.integer ? mods.integer : false
|
||||
this.min = mods?.min !== undefined ? mods.min : -Infinity
|
||||
this.max = mods?.max !== undefined ? mods.max : Infinity
|
||||
|
||||
@@ -10,9 +10,9 @@ export class ObjectNode extends AbstractNode<IObject> {
|
||||
protected fields: NodeChildren
|
||||
|
||||
constructor(fields: NodeChildren, mods?: NodeMods<IObject>) {
|
||||
super(mods, {
|
||||
default: () => ({})
|
||||
})
|
||||
super({
|
||||
default: () => ({}),
|
||||
...mods})
|
||||
this.fields = fields
|
||||
Object.values(fields).forEach(child => {
|
||||
child.setParent(this)
|
||||
|
||||
@@ -3,9 +3,16 @@ import { Path } from '../model/Path'
|
||||
import { DataModel } from '../model/DataModel'
|
||||
import { TreeView } from '../view/TreeView'
|
||||
|
||||
export interface StringNodeMods extends NodeMods<string> {
|
||||
allowEmpty?: boolean
|
||||
}
|
||||
|
||||
export class StringNode extends AbstractNode<string> implements StateNode<string> {
|
||||
constructor(mods?: NodeMods<string>) {
|
||||
allowEmpty: boolean
|
||||
|
||||
constructor(mods?: StringNodeMods) {
|
||||
super(mods)
|
||||
this.allowEmpty = (mods?.allowEmpty === true)
|
||||
}
|
||||
|
||||
getState(el: Element) {
|
||||
@@ -13,7 +20,8 @@ export class StringNode extends AbstractNode<string> implements StateNode<string
|
||||
}
|
||||
|
||||
updateModel(el: Element, path: Path, model: DataModel) {
|
||||
model.set(path, this.getState(el))
|
||||
const value = this.getState(el)
|
||||
model.set(path, this.allowEmpty || value !== '' ? value : undefined)
|
||||
}
|
||||
|
||||
renderRaw(path: Path, value: string, view: TreeView, options?: RenderOptions) {
|
||||
|
||||
12
src/nodes/custom/ResourceNode.ts
Normal file
12
src/nodes/custom/ResourceNode.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { NodeMods, RenderOptions } from '../AbstractNode'
|
||||
import { StringNode } from '../StringNode'
|
||||
|
||||
export class ResourceNode extends StringNode {
|
||||
constructor(mods?: NodeMods<string>) {
|
||||
super({
|
||||
transform: (v) => {
|
||||
if (v === undefined) return undefined
|
||||
return v.startsWith('minecraft:') ? v : 'minecraft:' + v
|
||||
}, ...mods})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user