Refactoring entities (#23)

* Still WIP

* WIP

* ArrayEntity parsing fixed

* Fix format text entity

* Tests for various entity classes and update entity class implementations

* More tests and fixed

* More entities fixed

* Simple entities serialization fixed

* Entities tests fixed

* Remove serialization bits

* Fix Function reference

* CustomProperties creating fixed

* WIP

* Better typing for grammars

* Decoding code fixes

* Fixing still

* Several fixes

* rename toString to serialize

* Several fixes

* More fixes

* Moving more stuff out of Utility

* Several fixes

* Fixing Linear color entity print

* Serialization fixes

* Fix serialization

* Method to compute grammar

* Renaming fix

* Fix array grammar and equality check

* Fix inlined keys

* Fix type

* Several serialization fixes

* Fix undefined dereference

* Several fixes

* More fixes and cleanup

* Fix keys quoting mechanism

* Fix natural number assignment

* Fix Int64 toString()

* Fix quoted keys for inlined arrays

* Fix PG pins

* Fix several test cases

* Types fixes

* New pin default value empty

* Fix non existing DefaultValue for variadic nodes

* Smaller fixes for crashes

* Fix link color when attached to knot

* Linking test and more reliability operations for adding pins

* Improve issue 18 test

* More tests and fixes

* Fix enum pin entity

* Remove failing test
This commit is contained in:
barsdeveloper
2024-09-08 11:46:36 +02:00
committed by GitHub
parent 31a07b992d
commit 23ee628e28
129 changed files with 8888 additions and 8584 deletions

View File

@@ -1,11 +1,11 @@
export default class ElementFactory {
/** @type {Map<String, AnyConstructor<IElement>>} */
/** @type {Map<String, IElementConstructor>} */
static #elementConstructors = new Map()
/**
* @param {String} tagName
* @param {AnyConstructor<IElement>} entityConstructor
* @param {IElementConstructor} entityConstructor
*/
static registerElement(tagName, entityConstructor) {
ElementFactory.#elementConstructors.set(tagName, entityConstructor)

View File

@@ -1,5 +1,5 @@
import Configuration from "../Configuration.js"
import Utility from "../Utility.js"
import BooleanEntity from "../entity/BooleanEntity.js"
import IDraggableElement from "./IDraggableElement.js"
/**
@@ -15,7 +15,7 @@ export default class ISelectableDraggableElement extends IDraggableElement {
type: Boolean,
attribute: "data-selected",
reflect: true,
converter: Utility.booleanConverter,
converter: BooleanEntity.booleanConverter,
},
}

View File

@@ -1,6 +1,6 @@
import IElement from "./IElement.js"
import BooleanEntity from "../entity/BooleanEntity.js"
import InputTemplate from "../template/pin/InputTemplate.js"
import Utility from "../Utility.js"
import IElement from "./IElement.js"
/** @extends {IElement<Object, InputTemplate>} */
export default class InputElement extends IElement {
@@ -10,19 +10,19 @@ export default class InputElement extends IElement {
singleLine: {
type: Boolean,
attribute: "data-single-line",
converter: Utility.booleanConverter,
converter: BooleanEntity.booleanConverter,
reflect: true,
},
selectOnFocus: {
type: Boolean,
attribute: "data-select-focus",
converter: Utility.booleanConverter,
converter: BooleanEntity.booleanConverter,
reflect: true,
},
blurOnEnter: {
type: Boolean,
attribute: "data-blur-enter",
converter: Utility.booleanConverter,
converter: BooleanEntity.booleanConverter,
reflect: true,
},
}

View File

@@ -1,9 +1,10 @@
import { html, nothing } from "lit"
import Configuration from "../Configuration.js"
import IFromToPositionedElement from "./IFromToPositionedElement.js"
import LinkTemplate from "../template/LinkTemplate.js"
import SVGIcon from "../SVGIcon.js"
import Utility from "../Utility.js"
import BooleanEntity from "../entity/BooleanEntity.js"
import LinkTemplate from "../template/LinkTemplate.js"
import IFromToPositionedElement from "./IFromToPositionedElement.js"
/** @extends {IFromToPositionedElement<Object, LinkTemplate>} */
export default class LinkElement extends IFromToPositionedElement {
@@ -13,7 +14,7 @@ export default class LinkElement extends IFromToPositionedElement {
dragging: {
type: Boolean,
attribute: "data-dragging",
converter: Utility.booleanConverter,
converter: BooleanEntity.booleanConverter,
reflect: true,
},
originatesFromInput: {
@@ -205,11 +206,16 @@ export default class LinkElement extends IFromToPositionedElement {
this.toY = location[1]
}
getInputPin() {
getInputPin(getSomething = false) {
if (this.source?.isInput()) {
return this.source
}
return this.destination
if (this.destination?.isInput()) {
return this.destination
}
if (getSomething) {
return this.source ?? this.destination
}
}
/** @param {PinElement} pin */
@@ -220,11 +226,16 @@ export default class LinkElement extends IFromToPositionedElement {
this.destination = pin
}
getOutputPin() {
getOutputPin(getSomething = false) {
if (this.source?.isOutput()) {
return this.source
}
if (this.destination?.isOutput()) {
return this.destination
}
return this.source
if (getSomething) {
return this.source ?? this.destination
}
}
/** @param {PinElement} pin */

View File

@@ -1,12 +1,11 @@
import Configuration from "../Configuration.js"
import Utility from "../Utility.js"
import nodeTemplateClass from "../decoding/nodeTemplate.js"
import nodeTitle from "../decoding/nodeTitle.js"
import IdentifierEntity from "../entity/IdentifierEntity.js"
import BooleanEntity from "../entity/BooleanEntity.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import PinEntity from "../entity/PinEntity.js"
import PinReferenceEntity from "../entity/PinReferenceEntity.js"
import SerializerFactory from "../serialization/SerializerFactory.js"
import SymbolEntity from "../entity/SymbolEntity.js"
import NodeTemplate from "../template/node/NodeTemplate.js"
import ISelectableDraggableElement from "./ISelectableDraggableElement.js"
@@ -28,7 +27,7 @@ export default class NodeElement extends ISelectableDraggableElement {
advancedPinDisplay: {
type: String,
attribute: "data-advanced-display",
converter: IdentifierEntity.attributeConverter,
converter: SymbolEntity.attributeConverter,
reflect: true,
},
enabledState: {
@@ -42,7 +41,7 @@ export default class NodeElement extends ISelectableDraggableElement {
},
pureFunction: {
type: Boolean,
converter: Utility.booleanConverter,
converter: BooleanEntity.booleanConverter,
attribute: "data-pure-function",
reflect: true,
},
@@ -86,7 +85,7 @@ export default class NodeElement extends ISelectableDraggableElement {
/** @param {String} str */
static fromSerializedObject(str) {
str = str.trim()
let entity = SerializerFactory.getSerializer(ObjectEntity).read(str)
let entity = ObjectEntity.grammar.parse(str)
return NodeElement.newObject(/** @type {ObjectEntity} */(entity))
}
@@ -100,13 +99,17 @@ export default class NodeElement extends ISelectableDraggableElement {
return result
}
/** @param {String} name */
#redirectLinksAfterRename(name) {
for (let sourcePinElement of this.getPinElements()) {
for (let targetPinReference of sourcePinElement.getLinks()) {
this.blueprint.getPin(targetPinReference).redirectLink(sourcePinElement, new PinReferenceEntity({
objectName: name,
pinGuid: sourcePinElement.entity.PinId,
}))
this.blueprint.getPin(targetPinReference).redirectLink(
sourcePinElement,
new PinReferenceEntity(
new SymbolEntity(name),
sourcePinElement.entity.PinId,
)
)
}
}
}
@@ -117,7 +120,7 @@ export default class NodeElement extends ISelectableDraggableElement {
this.advancedPinDisplay = entity.AdvancedPinDisplay?.toString()
this.enabledState = entity.EnabledState
this.nodeDisplayName = nodeTitle(entity)
this.pureFunction = entity.bIsPureFunc
this.pureFunction = entity.bIsPureFunc?.valueOf()
this.dragLinkObjects = []
super.initialize(entity, template)
this.#pins = this.template.createPinElements()
@@ -128,11 +131,15 @@ export default class NodeElement extends ISelectableDraggableElement {
} else {
this.updateComplete.then(() => this.computeSizes())
}
entity.listenAttribute("Name", name => {
this.nodeTitle = entity.Name
this.nodeDisplayName = nodeTitle(entity)
this.#redirectLinksAfterRename(name)
})
entity.listenAttribute(
"Name",
/** @param {InstanceType<typeof ObjectEntity.attributes.Name>} newName */
newName => {
this.nodeTitle = newName.value
this.nodeDisplayName = nodeTitle(entity)
this.#redirectLinksAfterRename(newName.value)
}
)
}
async getUpdateComplete() {
@@ -223,7 +230,7 @@ export default class NodeElement extends ISelectableDraggableElement {
}
setShowAdvancedPinDisplay(value) {
this.entity.AdvancedPinDisplay = new IdentifierEntity(value ? "Shown" : "Hidden")
this.entity.AdvancedPinDisplay = new SymbolEntity(value ? "Shown" : "Hidden")
this.advancedPinDisplay = this.entity.AdvancedPinDisplay
}

View File

@@ -1,15 +1,17 @@
import Utility from "../Utility.js"
import pinTemplate from "../decoding/pinTemplate.js"
import ArrayEntity from "../entity/ArrayEntity.js"
import BooleanEntity from "../entity/BooleanEntity.js"
import GuidEntity from "../entity/GuidEntity.js"
import LinearColorEntity from "../entity/LinearColorEntity.js"
import PinEntity from "../entity/PinEntity.js"
import PinReferenceEntity from "../entity/PinReferenceEntity.js"
import SymbolEntity from "../entity/SymbolEntity.js"
import PinTemplate from "../template/pin/PinTemplate.js"
import ElementFactory from "./ElementFactory.js"
import IElement from "./IElement.js"
/**
* @template {TerminalAttribute} T
* @template {IEntity} T
* @extends {IElement<PinEntity<T>, PinTemplate>}
*/
export default class PinElement extends IElement {
@@ -42,7 +44,7 @@ export default class PinElement extends IElement {
fromAttribute: (value, type) => value
? LinearColorEntity.getLinearColorFromAnyFormat().parse(value)
: null,
toAttribute: (value, type) => value ? Utility.printLinearColor(value) : null,
toAttribute: (value, type) => value ? LinearColorEntity.printLinearColor(value) : null,
},
attribute: "data-color",
reflect: true,
@@ -53,7 +55,7 @@ export default class PinElement extends IElement {
},
isLinked: {
type: Boolean,
converter: Utility.booleanConverter,
converter: BooleanEntity.booleanConverter,
attribute: "data-linked",
reflect: true,
},
@@ -64,7 +66,7 @@ export default class PinElement extends IElement {
},
connectable: {
type: Boolean,
converter: Utility.booleanConverter,
converter: BooleanEntity.booleanConverter,
attribute: "data-connectable",
reflect: true,
}
@@ -89,9 +91,9 @@ export default class PinElement extends IElement {
nodeElement = undefined
) {
this.nodeElement = nodeElement
this.advancedView = entity.bAdvancedView
this.advancedView = entity.bAdvancedView?.valueOf()
this.isLinked = false
this.connectable = !entity.bNotConnectable
this.connectable = !entity.bNotConnectable?.valueOf()
super.initialize(entity, template)
this.pinType = this.entity.getType()
this.defaultValue = this.entity.getDefaultValue()
@@ -105,20 +107,15 @@ export default class PinElement extends IElement {
}
createPinReference() {
return new PinReferenceEntity({
objectName: this.nodeElement.getNodeName(),
pinGuid: this.getPinId(),
})
return new PinReferenceEntity(new SymbolEntity(this.nodeElement.getNodeName()), this.getPinId())
}
/** @return {GuidEntity} */
getPinId() {
return this.entity.PinId
}
/** @returns {String} */
getPinName() {
return this.entity.PinName
return this.entity.PinName?.toString() ?? ""
}
getPinDisplayName() {
@@ -147,7 +144,7 @@ export default class PinElement extends IElement {
}
getLinks() {
return this.entity.LinkedTo ?? []
return this.entity.LinkedTo?.valueOf() ?? []
}
getDefaultValue(maybeCreate = false) {
@@ -165,21 +162,23 @@ export default class PinElement extends IElement {
/** @param {IElement[]} nodesWhitelist */
sanitizeLinks(nodesWhitelist = []) {
this.entity.LinkedTo = this.entity.LinkedTo?.filter(pinReference => {
let pin = this.blueprint.getPin(pinReference)
if (pin) {
if (nodesWhitelist.length && !nodesWhitelist.includes(pin.nodeElement)) {
return false
this.entity.LinkedTo = new (PinEntity.attributes.LinkedTo)(
this.entity.LinkedTo?.valueOf().filter(pinReference => {
let pin = this.blueprint.getPin(pinReference)
if (pin) {
if (nodesWhitelist.length && !nodesWhitelist.includes(pin.nodeElement)) {
return false
}
let link = this.blueprint.getLink(this, pin)
if (!link) {
link = /** @type {LinkElementConstructor} */(ElementFactory.getConstructor("ueb-link"))
.newObject(this, pin)
this.blueprint.addGraphElement(link)
}
}
let link = this.blueprint.getLink(this, pin)
if (!link) {
link = /** @type {LinkElementConstructor} */(ElementFactory.getConstructor("ueb-link"))
.newObject(this, pin)
this.blueprint.addGraphElement(link)
}
}
return pin
})
return pin
})
)
this.isLinked = this.entity.isLinked()
}
@@ -231,7 +230,7 @@ export default class PinElement extends IElement {
redirectLink(originalPinElement, newReference) {
const index = this.getLinks().findIndex(pinReference =>
pinReference.objectName.toString() == originalPinElement.getNodeElement().getNodeName()
&& pinReference.pinGuid.valueOf() == originalPinElement.entity.PinId.valueOf()
&& pinReference.pinGuid.toString() == originalPinElement.entity.PinId.toString()
)
if (index >= 0) {
this.entity.LinkedTo[index] = newReference