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

[node_editor] fix: add edge create, remove #172

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@stonecrop/node-editor",
"comment": "Add handling for edge creation and removal.",
"type": "patch"
}
],
"packageName": "@stonecrop/node-editor"
}
1 change: 1 addition & 0 deletions node_editor/src/components/EditableEdge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ export default {

.label-input {
text-align: center;
width: 40ch;
}
</style>
33 changes: 31 additions & 2 deletions node_editor/src/components/NodeEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</template>

<script setup lang="ts">
import { type VueFlowStore, Position, VueFlow, useVueFlow, Node } from '@vue-flow/core'
import { type VueFlowStore, Position, VueFlow, useVueFlow, Connection, Node } from '@vue-flow/core'
import { type HTMLAttributes, ref, computed, defineEmits, onBeforeUnmount, onMounted } from 'vue'

import EditableEdge from '@/components/EditableEdge.vue'
Expand Down Expand Up @@ -103,6 +103,7 @@ const elements = computed({
emit('update:modelValue', JSON.parse(JSON.stringify(newValue)))
},
})
const { onConnect, addEdges, onEdgeContextMenu, onPaneReady, removeEdges } = useVueFlow()

onMounted(() => {
document.removeEventListener('keypress', handleKeypress)
Expand All @@ -113,7 +114,6 @@ onBeforeUnmount(() => {
document.removeEventListener('keypress', handleKeypress)
})

const { onPaneReady } = useVueFlow()
onPaneReady(instance => {
vueFlowInstance.value = instance
})
Expand Down Expand Up @@ -222,6 +222,35 @@ const labelChanged = (e, id) => {
}
}
}

const handleConnect = (connection: Connection) => {
const id = vueFlowElements.value.length
const newEdge = {
id: `edge-${id}`,
source: connection.source,
target: connection.target,
type: 'editable',
label: `New Edge`,
interactionWidth: 400,
animated: true,
events: {
click: () => {
activeElementKey.value = newEdge.id
},
},
}
addEdges([newEdge])
}

onConnect(handleConnect)

const handleEdgeRemove = edgeId => {
removeEdges(edgeId)
}

onEdgeContextMenu(({ event, edge }) => {
handleEdgeRemove(edge.id)
})
</script>

<style>
Expand Down
44 changes: 20 additions & 24 deletions node_editor/src/components/StateEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,28 @@ const elements = computed<FlowElements>({
}

stateHash[key] = el
}

for (const [key, value] of Object.entries(states.value)) {
if (value.on) {
for (const [edgeKey, edgeValue] of Object.entries(value.on)) {
if (Array.isArray(edgeValue)) {
for (const edge of edgeValue) {
// TODO: handle typescript errors for both types of states
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
const edgeJson = edge.toJSON()
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
const target = edgeJson.target.toString()
stateElements.push({
id: `${key}-${edgeKey}`,
target: target,
source: key,
label: edgeKey,
animated: true,
})

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
hasInputs[target] = true
}
}
// If the proxy array 'value.on' has more than one edge, 'edgeValue' will contain a proxy object
// where 'target' can be accessed. Otherwise, 'edgeValue' will be available directly.
const target = edgeValue.target || edgeValue
// TODO: handle typescript errors for both types of states
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
stateElements.push({
id: `${key}-${target}`,
source: key,
target: target,
label: edgeKey,
animated: true,
})

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
hasInputs[target] = true
}
}

index++
}

Expand All @@ -78,14 +75,14 @@ const elements = computed<FlowElements>({

return stateElements
},

set: newValue => {
// update modelValue when elements change
onElementsChange(newValue)

// TODO: emit('update:modelValue', modelValue)
},
})

const onElementsChange = (elements: FlowElements) => {
const edges: Record<string, Record<string, any>> = {}
const idToLabel: Record<string, string> = {}
Expand All @@ -104,19 +101,18 @@ const onElementsChange = (elements: FlowElements) => {
states[label] = {
type: 'final',
}
} /* else if (el.source && el.target) {
} else if (el.source && el.target) {
// it's an edge
edges[el.source] = edges[el.source] || {}
edges[el.source][label] = {
target: el.target,
}
} */ else {
} else {
// it's a state
states[label] = {
on: {},
}
}

idToLabel[el.id] = label
}

Expand Down
Loading