From b33285c634984d9270ab05e35c94f1abe3963899 Mon Sep 17 00:00:00 2001 From: somebody1234 Date: Fri, 24 Nov 2023 20:49:15 +1000 Subject: [PATCH] Fix auto layout (#8345) So it turns out auto-layout was broken if there were existing, already positioned nodes # Important Notes None --- app/gui2/src/components/ComponentBrowser.vue | 2 - app/gui2/src/components/GraphEditor.vue | 70 +++++++------- .../graph/__tests__/graphDatabase.test.ts | 8 +- app/gui2/src/stores/graph/graphDatabase.ts | 56 +---------- app/gui2/src/stores/graph/index.ts | 95 +++++++++++-------- app/gui2/src/util/ast/extended.ts | 8 ++ 6 files changed, 101 insertions(+), 138 deletions(-) diff --git a/app/gui2/src/components/ComponentBrowser.vue b/app/gui2/src/components/ComponentBrowser.vue index f73b965d123..494958a262f 100644 --- a/app/gui2/src/components/ComponentBrowser.vue +++ b/app/gui2/src/components/ComponentBrowser.vue @@ -45,9 +45,7 @@ const emit = defineEmits<{ function getInitialContent(): string { if (props.sourceNode == null) return props.initialContent const sourceNode = props.sourceNode - console.log(sourceNode) const sourceNodeName = graphStore.db.getNodeMainOutputPortIdentifier(sourceNode) - console.log(sourceNodeName) const sourceNodeNameWithDot = sourceNodeName ? sourceNodeName + '.' : '' return sourceNodeNameWithDot + props.initialContent } diff --git a/app/gui2/src/components/GraphEditor.vue b/app/gui2/src/components/GraphEditor.vue index 4ec79c41c6a..f48c039da25 100644 --- a/app/gui2/src/components/GraphEditor.vue +++ b/app/gui2/src/components/GraphEditor.vue @@ -288,37 +288,37 @@ async function handleFileDrop(event: DragEvent) { } } -function onComponentBrowserCommit(content: string) { - if (content != null && graphStore.editedNodeInfo != null) { - // We finish editing a node. - graphStore.setNodeContent(graphStore.editedNodeInfo.id, content) - } else if (content != null) { - // We finish creating a new node. - const nodePosition = componentBrowserPosition.value - graphStore.createNode(nodePosition.sub(COMPONENT_BROWSER_TO_NODE_OFFSET), content) - } +function resetComponentBrowserState() { componentBrowserVisible.value = false graphStore.editedNodeInfo = undefined interaction.setCurrent(undefined) } -function onComponentBrowserCancel() { - componentBrowserVisible.value = false - graphStore.editedNodeInfo = undefined - interaction.setCurrent(undefined) +function onComponentBrowserCommit(content: string) { + if (content != null) { + if (graphStore.editedNodeInfo) { + // We finish editing a node. + graphStore.setNodeContent(graphStore.editedNodeInfo.id, content) + } else { + // We finish creating a new node. + const nodePosition = componentBrowserPosition.value + graphStore.createNode(nodePosition.sub(COMPONENT_BROWSER_TO_NODE_OFFSET), content) + } + } + resetComponentBrowserState() } +const onComponentBrowserCancel = resetComponentBrowserState + function getNodeContent(id: ExprId): string { - const node = graphStore.db.nodeIdToNode.get(id) - if (node == null) return '' - return node.rootSpan.repr() + return graphStore.db.nodeIdToNode.get(id)?.rootSpan.repr() ?? '' } // Watch the `editedNode` in the graph store watch( () => graphStore.editedNodeInfo, (editedInfo) => { - if (editedInfo != null) { + if (editedInfo) { componentBrowserPosition.value = targetComponentBrowserPosition() componentBrowserInputContent.value = getNodeContent(editedInfo.id) componentBrowserVisible.value = true @@ -328,16 +328,16 @@ watch( }, ) -const breadcrumbs = computed(() => { - return projectStore.executionContext.desiredStack.map((frame) => { +const breadcrumbs = computed(() => + projectStore.executionContext.desiredStack.map((frame) => { switch (frame.type) { case 'ExplicitCall': return frame.methodPointer.name case 'LocalCall': return frame.expressionId } - }) -}) + }), +) // === Clipboard === @@ -358,7 +358,7 @@ interface CopiedNode { function copyNodeContent() { const id = nodeSelection.selected.values().next().value const node = graphStore.db.nodeIdToNode.get(id) - if (node == null) return + if (!node) return const content = node.rootSpan.repr() const metadata = projectStore.module?.getNodeMetadata(id) ?? undefined const copiedNode: CopiedNode = { expression: content, metadata } @@ -392,24 +392,23 @@ async function retrieveDataFromClipboard(): Promise { /// Read the clipboard and if it contains valid data, create a node from the content. async function readNodeFromClipboard() { let clipboardData = await retrieveDataFromClipboard() - if (clipboardData == undefined) { + if (!clipboardData) { console.warn('No valid data in clipboard.') return } const copiedNode = clipboardData.nodes[0] - if (copiedNode == undefined) { + if (!copiedNode) { console.warn('No valid node in clipboard.') return } - if (copiedNode.expression != null) { - graphStore.createNode( - graphNavigator.sceneMousePos ?? Vec2.Zero, - copiedNode.expression, - copiedNode.metadata, - ) - } else { + if (copiedNode.expression == null) { console.warn('No valid expression in clipboard.') } + graphStore.createNode( + graphNavigator.sceneMousePos ?? Vec2.Zero, + copiedNode.expression, + copiedNode.metadata, + ) } function handleNodeOutputPortDoubleClick(id: ExprId) { @@ -428,15 +427,14 @@ function handleNodeOutputPortDoubleClick(id: ExprId) {