Files
ueblueprint/js/entity/FormatTextEntity.js
barsdeveloper 6f674b284d PCG nodes and other fixes (#14)
* Various fixes

* Fix name from SettingsInterface

* Allow path lookbehind for unknown keys entity

* Subraph object name

* Several fixes

* Various fixes

* Fix colors

* Various pin types
2023-09-14 23:07:09 +02:00

49 lines
1.7 KiB
JavaScript

import IEntity from "./IEntity.js"
import InvariantTextEntity from "./InvariantTextEntity.js"
import LocalizedTextEntity from "./LocalizedTextEntity.js"
import Union from "./Union.js"
export default class FormatTextEntity extends IEntity {
static lookbehind = new Union("LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED")
static attributes = {
...super.attributes,
value: {
type: [new Union(String, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity)],
default: [],
},
}
static {
this.cleanupAttributes(this.attributes)
}
constructor(values) {
super(values)
/** @type {(String | LocalizedTextEntity | InvariantTextEntity | FormatTextEntity)[]} */ this.value
}
toString() {
const pattern = this.value?.[0]?.toString() // The pattern is always the first element of the array
if (!pattern) {
return ""
}
const values = this.value.slice(1).map(v => v.toString())
return this.lookbehind == "LOCGEN_FORMAT_NAMED"
? pattern.replaceAll(/\{([a-zA-Z]\w*)\}/g, (substring, arg) => {
const argLocation = values.indexOf(arg) + 1
return argLocation > 0 && argLocation < values.length
? values[argLocation]
: substring
})
: this.lookbehind == "LOCGEN_FORMAT_ORDERED"
? pattern.replaceAll(/\{(\d+)\}/g, (substring, arg) => {
const argValue = Number(arg)
return argValue < values.length
? values[argValue]
: substring
})
: ""
}
}