Graflow: better refresh & clear, better EIC example, tension field

This commit is contained in:
STEINNI
2026-01-15 15:12:10 +00:00
parent 3489dfed7f
commit 19b8df4933

View File

@@ -5,6 +5,7 @@ class BZgraflow extends Buildoz{
this.defaultAttrs = { tension: 100 } this.defaultAttrs = { tension: 100 }
this.stagedNodes = { } this.stagedNodes = { }
this.stagedWires = { } this.stagedWires = { }
this.arrowDefs = null
} }
static _loadedNodeStyles = new Set() // Allow multi instances or re-loadNodes, but avoid reinjecting same styles ! static _loadedNodeStyles = new Set() // Allow multi instances or re-loadNodes, but avoid reinjecting same styles !
@@ -71,8 +72,9 @@ class BZgraflow extends Buildoz{
this.nodesRegistry = {} this.nodesRegistry = {}
for(const tpl of doc.querySelectorAll('template')){ for(const tpl of doc.querySelectorAll('template')){
if(tpl.id=='svg-arrows'){ if(tpl.id=='svg-arrows'){
this.wiresContainer.appendChild(tpl.querySelector('defs').cloneNode(true)) this.arrowDefs = tpl.querySelector('defs').cloneNode(true)
this.arrowDefined = true console.log(this.arrowDefs)
this.wiresContainer.appendChild(this.arrowDefs)
} else { } else {
const rootEl = tpl.content.querySelector('.bzgf-node') const rootEl = tpl.content.querySelector('.bzgf-node')
if(!rootEl) continue if(!rootEl) continue
@@ -118,15 +120,23 @@ class BZgraflow extends Buildoz{
this.stagedWires[id] = document.createElementNS('http://www.w3.org/2000/svg', 'path') this.stagedWires[id] = document.createElementNS('http://www.w3.org/2000/svg', 'path')
this.stagedWires[id].setAttribute('d', path) this.stagedWires[id].setAttribute('d', path)
this.stagedWires[id].setAttribute('fill', 'none') this.stagedWires[id].setAttribute('fill', 'none')
if(this.arrowDefined && link.endArrow) this.stagedWires[id].setAttribute('marker-end','url(#arrow)') if(this.arrowDefs && link.endArrow) this.stagedWires[id].setAttribute('marker-end','url(#arrow)')
if(this.arrowDefined && link.startArrow) this.stagedWires[id].setAttribute('marker-start','url(#arrow)') if(this.arrowDefs && link.startArrow) this.stagedWires[id].setAttribute('marker-start','url(#arrow)')
this.stagedWires[id].classList.add('bzgf-wire') this.stagedWires[id].classList.add('bzgf-wire')
this.stagedWires[id].dataset.id = id this.stagedWires[id].dataset.id = id
this.wiresContainer.append(this.stagedWires[id]) this.wiresContainer.append(this.stagedWires[id])
return(this.stagedWires[id]) return(this.stagedWires[id])
} }
clear(){
this.nodesContainer.innerHTML = ''
this.wiresContainer.innerHTML = ''
if(this.arrowDefs) this.wiresContainer.appendChild(this.arrowDefs)
}
refresh(){ refresh(){
this.clear()
let forceAutoplace = false let forceAutoplace = false
for(const node of this.flow.nodes){ for(const node of this.flow.nodes){
if((!node.coords) || (!node.coords.x) ||(!node.coords.y)) forceAutoplace=true if((!node.coords) || (!node.coords.x) ||(!node.coords.y)) forceAutoplace=true
@@ -145,7 +155,6 @@ class BZgraflow extends Buildoz{
} }
bezier(idNode1, idPort1, idNode2, idPort2, tensionMin=60) { bezier(idNode1, idPort1, idNode2, idPort2, tensionMin=60) {
const svgRect = this.wiresContainer.getBoundingClientRect() const svgRect = this.wiresContainer.getBoundingClientRect()
const node1 = this.stagedNodes[idNode1] const node1 = this.stagedNodes[idNode1]
const port1 = node1.ports[idPort1] const port1 = node1.ports[idPort1]
@@ -175,7 +184,7 @@ class BZgraflow extends Buildoz{
let c1y = y1 + (dirVect[port1.direction].y * tension) let c1y = y1 + (dirVect[port1.direction].y * tension)
let c2x = x2 + (dirVect[port2.direction].x * tension) let c2x = x2 + (dirVect[port2.direction].x * tension)
let c2y = y2 + (dirVect[port2.direction].y * tension) let c2y = y2 + (dirVect[port2.direction].y * tension)
if((idNode1==idNode2) && (idPort1==idPort2)){ // Special case of self-loop: correct intermediary points if((idNode1==idNode2) && (idPort1==idPort2)){ // Special case of self-loop: spread a bit intermediary points
if(['n', 's'].includes(port1.direction)) { if(['n', 's'].includes(port1.direction)) {
c1x += tension c1x += tension
c2x -= tension c2x -= tension