Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backwards compatibility for edited/deleted components in model view #215

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions app/javascript/projects/model_view.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debounce, every } from 'lodash'
import { debounce } from 'lodash'
import * as React from 'react'
import { Engine, NodeEditor } from 'rete'
import ConnectionPlugin from 'rete-connection-plugin'
Expand Down Expand Up @@ -94,6 +94,7 @@ export function ModelView({ visible, initialTransform, setTransform, initialMode
)

if (initialModel !== null) {
setModel(updateForBackwardsCompatibility(initialModel, editor.components))
editor.fromJSON(initialModel).then(addNodeSyncListeners).then(addWriteModelListener)
}
else {
Expand Down Expand Up @@ -170,5 +171,60 @@ export function ModelView({ visible, initialTransform, setTransform, initialMode
editor?.nodes.forEach(node => editor.view?.updateConnections({ node }))
}, [editor, visible])

return <div className={`model-editor bg-dark flex-grow-1 ${!visible ? "d-none" : ""}`} ref={ref}></div>
return <div className={`model-editor bg-dark flex-grow-1 ${!visible ? "d-none" : ""}`} ref={ref}></div>
}

function updateForBackwardsCompatibility(initialModel: Data, components: any): Data {

let model = initialModel;
let nodeNames = Object.keys(model.nodes)
let activeComponents = Array.from(components.keys())
let inactiveComponents: number[] = []

// remove and log any components that have been deleted/updated
for (let i = 0; i < nodeNames.length; i++) {
let node = model.nodes[nodeNames[i]]
if(!activeComponents.includes(node.name)) {
inactiveComponents.push(node.id)
delete model.nodes[nodeNames[i]]
nodeNames[i] = "deleted"
}
}

// remove any connections that are connected to deleted/updated components
if(inactiveComponents.length > 0) {
for (let i = 0; i < nodeNames.length; i++) {
if(nodeNames[i] !== "deleted") {
let node = model.nodes[nodeNames[i]]
let inputs = Object.keys(node.inputs)
let outputs = Object.keys(node.outputs)

for (let j = 0; j < inputs.length; j++) {
let input = node.inputs[inputs[j]]
input.connections.forEach((connection: any, index: number) => {
if(inactiveComponents.includes(connection.node)) {
input.connections.splice(index, 1)
}
})
model.nodes[nodeNames[i]].inputs[inputs[j]] = input
}


for (let j = 0; j < outputs.length; j++) {
let output = node.outputs[outputs[j]]
output.connections.forEach((connection: any, index: number) => {
if(inactiveComponents.includes(connection.node)) {
output.connections.splice(index, 1)
}
})
model.nodes[nodeNames[i]].outputs[outputs[j]] = output
}


}

}
}

return model
}