From 43a1c9d2acf8dd7574e23cefbf497b64158b2476 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Tue, 6 Feb 2024 18:24:30 +0100 Subject: [PATCH] examples: animation edge --- docs/examples/index.ts | 12 +- docs/examples/layout/AnimationEdge.vue | 161 ++++++++++++++++++ docs/examples/layout/App.vue | 4 + docs/examples/layout/index.ts | 1 + examples/vite/src/Layouting/AnimationEdge.vue | 117 +++++++++++++ .../vite/src/Layouting/LayoutingExample.vue | 9 +- examples/vite/src/Layouting/ProcessNode.vue | 54 +++++- .../vite/src/Layouting/initial-elements.ts | 20 +-- examples/vite/src/Layouting/useRunProcess.ts | 48 +++--- 9 files changed, 379 insertions(+), 47 deletions(-) create mode 100644 docs/examples/layout/AnimationEdge.vue create mode 100644 examples/vite/src/Layouting/AnimationEdge.vue diff --git a/docs/examples/index.ts b/docs/examples/index.ts index b8836d57..c10f5e85 100644 --- a/docs/examples/index.ts +++ b/docs/examples/index.ts @@ -18,7 +18,7 @@ import { IntersectionApp, IntersectionCSS } from './intersection' import { SnapToHandleApp, SnappableConnectionLine } from './connection-radius' import { NodeResizerApp, ResizableNode } from './node-resizer' import { ToolbarApp, ToolbarNode } from './node-toolbar' -import { LayoutApp, LayoutElements, LayoutNode, LayoutScript } from './layout' +import { LayoutApp, LayoutEdge, LayoutElements, LayoutNode, LayoutScript } from './layout' export const exampleImports = { basic: { @@ -103,8 +103,8 @@ export const exampleImports = { 'TransitionEdge.vue': TransitionEdge, 'style.css': TransitionCSS, 'additionalImports': { - '@vueuse/core': 'https://cdn.jsdelivr.net/npm/@vueuse/core@9.3.0/index.mjs', - '@vueuse/shared': 'https://cdn.jsdelivr.net/npm/@vueuse/shared@9.3.0/index.mjs', + '@vueuse/core': 'https://cdn.jsdelivr.net/npm/@vueuse/core@10.7.0/index.mjs', + '@vueuse/shared': 'https://cdn.jsdelivr.net/npm/@vueuse/shared@10.7.0/index.mjs', 'vue-demi': 'https://cdn.jsdelivr.net/npm/vue-demi@0.13.11/lib/index.mjs', }, }, @@ -128,9 +128,13 @@ export const exampleImports = { 'App.vue': LayoutApp, 'initial-elements.js': LayoutElements, 'ProcessNode.vue': LayoutNode, + 'AnimationEdge.vue': LayoutEdge, 'useRunProcess.js': LayoutScript, 'additionalImports': { - dagre: 'https://cdn.skypack.dev/pin/dagre@v0.8.5-NOlknF82nBdUHQKLJWRC/mode=imports,min/optimized/dagre.js', + '@vueuse/core': 'https://cdn.jsdelivr.net/npm/@vueuse/core@10.7.0/index.mjs', + '@vueuse/shared': 'https://cdn.jsdelivr.net/npm/@vueuse/shared@10.7.0/index.mjs', + 'vue-demi': 'https://cdn.jsdelivr.net/npm/vue-demi@0.13.11/lib/index.mjs', + 'dagre': 'https://cdn.skypack.dev/pin/dagre@v0.8.5-NOlknF82nBdUHQKLJWRC/mode=imports,min/optimized/dagre.js', }, }, } diff --git a/docs/examples/layout/AnimationEdge.vue b/docs/examples/layout/AnimationEdge.vue new file mode 100644 index 00000000..0a2fee97 --- /dev/null +++ b/docs/examples/layout/AnimationEdge.vue @@ -0,0 +1,161 @@ + + + + + diff --git a/docs/examples/layout/App.vue b/docs/examples/layout/App.vue index 1ccc05ac..57ea3529 100644 --- a/docs/examples/layout/App.vue +++ b/docs/examples/layout/App.vue @@ -65,6 +65,10 @@ function handleLayout(direction) { + + diff --git a/docs/examples/layout/index.ts b/docs/examples/layout/index.ts index 8828e7ae..46056a7e 100644 --- a/docs/examples/layout/index.ts +++ b/docs/examples/layout/index.ts @@ -1,4 +1,5 @@ export { default as LayoutApp } from './App.vue?raw' export { default as LayoutElements } from './initial-elements.js?raw' export { default as LayoutNode } from './ProcessNode.vue?raw' +export { default as LayoutEdge } from './AnimationEdge.vue?raw' export { default as LayoutScript } from './useRunProcess.js?raw' diff --git a/examples/vite/src/Layouting/AnimationEdge.vue b/examples/vite/src/Layouting/AnimationEdge.vue new file mode 100644 index 00000000..f099b8fe --- /dev/null +++ b/examples/vite/src/Layouting/AnimationEdge.vue @@ -0,0 +1,117 @@ + + + + + diff --git a/examples/vite/src/Layouting/LayoutingExample.vue b/examples/vite/src/Layouting/LayoutingExample.vue index 32a6f706..df21931a 100644 --- a/examples/vite/src/Layouting/LayoutingExample.vue +++ b/examples/vite/src/Layouting/LayoutingExample.vue @@ -4,6 +4,7 @@ import { ConnectionMode, Panel, Position, VueFlow, useVueFlow } from '@vue-flow/ import { initialEdges, initialNodes } from './initial-elements' import { useRunProcess } from './useRunProcess' +import AnimationEdge from './AnimationEdge.vue' import ProcessNode from './ProcessNode.vue' const nodes = ref(initialNodes) @@ -64,12 +65,16 @@ function handleLayout(direction: 'TB' | 'LR') { - + + + - + diff --git a/examples/vite/src/Layouting/ProcessNode.vue b/examples/vite/src/Layouting/ProcessNode.vue index cc59ff56..445ddf7a 100644 --- a/examples/vite/src/Layouting/ProcessNode.vue +++ b/examples/vite/src/Layouting/ProcessNode.vue @@ -1,10 +1,26 @@ diff --git a/examples/vite/src/Layouting/initial-elements.ts b/examples/vite/src/Layouting/initial-elements.ts index ee7af56b..77042021 100644 --- a/examples/vite/src/Layouting/initial-elements.ts +++ b/examples/vite/src/Layouting/initial-elements.ts @@ -62,14 +62,14 @@ export const initialNodes: Node[] = [ ] export const initialEdges: Edge[] = [ - { id: 'e1-2', source: '1', target: '2', type: 'smoothstep', animated: true }, - { id: 'e1-3', source: '1', target: '3', type: 'smoothstep', animated: true }, - { id: 'e2-2a', source: '2', target: '2a', type: 'smoothstep', animated: true }, - { id: 'e2-2b', source: '2', target: '2b', type: 'smoothstep', animated: true }, - { id: 'e2-2c', source: '2', target: '2c', type: 'smoothstep', animated: true }, - { id: 'e2c-2d', source: '2c', target: '2d', type: 'smoothstep', animated: true }, - { id: 'e3-7', source: '3', target: '4', type: 'smoothstep', animated: true }, - { id: 'e4-5', source: '4', target: '5', type: 'smoothstep', animated: true }, - { id: 'e5-6', source: '5', target: '6', type: 'smoothstep', animated: true }, - { id: 'e5-7', source: '5', target: '7', type: 'smoothstep', animated: true }, + { id: 'e1-2', source: '1', target: '2', type: 'animation', animated: true }, + { id: 'e1-3', source: '1', target: '3', type: 'animation', animated: true }, + { id: 'e2-2a', source: '2', target: '2a', type: 'animation', animated: true }, + { id: 'e2-2b', source: '2', target: '2b', type: 'animation', animated: true }, + { id: 'e2-2c', source: '2', target: '2c', type: 'animation', animated: true }, + { id: 'e2c-2d', source: '2c', target: '2d', type: 'animation', animated: true }, + { id: 'e3-7', source: '3', target: '4', type: 'animation', animated: true }, + { id: 'e4-5', source: '4', target: '5', type: 'animation', animated: true }, + { id: 'e5-6', source: '5', target: '6', type: 'animation', animated: true }, + { id: 'e5-7', source: '5', target: '7', type: 'animation', animated: true }, ] diff --git a/examples/vite/src/Layouting/useRunProcess.ts b/examples/vite/src/Layouting/useRunProcess.ts index 960c8f9c..22e37ac8 100644 --- a/examples/vite/src/Layouting/useRunProcess.ts +++ b/examples/vite/src/Layouting/useRunProcess.ts @@ -20,7 +20,7 @@ export function useRunProcess(dagreGraph: MaybeRefOrGetter const executedNodes = new Set() const runningTasks = new Map() - async function runNode(node: { id: string }) { + async function runNode(node: { id: string }, isStartingNode = false) { if (executedNodes.has(node.id)) { return } @@ -32,33 +32,35 @@ export function useRunProcess(dagreGraph: MaybeRefOrGetter // Simulate an async process with a random timeout between 1 and 3 seconds const delay = Math.floor(Math.random() * 2000) + 1000 return new Promise((resolve) => { - const timeout = setTimeout(async () => { - const children = graph.value.successors(node.id) as unknown as string[] + const timeout = setTimeout( + async () => { + const children = graph.value.successors(node.id) as unknown as string[] - // Randomly decide whether the node will throw an error - const willThrowError = Math.random() < 0.15 + // Randomly decide whether the node will throw an error + const willThrowError = Math.random() < 0.15 - if (willThrowError) { - updateNodeData(node.id, { isRunning: false, hasError: true }) + if (willThrowError) { + updateNodeData(node.id, { isRunning: false, hasError: true }) - await skipDescendants(node.id) + await skipDescendants(node.id) + runningTasks.delete(node.id) + + resolve() + return + } + + updateNodeData(node.id, { isRunning: false, isFinished: true }) runningTasks.delete(node.id) + if (children.length > 0) { + // Run the process on the children in parallel + await Promise.all(children.map((id) => runNode({ id }))) + } + resolve() - return - } - - updateNodeData(node.id, { isRunning: false, isFinished: true }) - console.log(`Node ${node.id} finished`) - runningTasks.delete(node.id) - - if (children.length > 0) { - // Run the process on the children in parallel - await Promise.all(children.map((id) => runNode({ id }))) - } - - resolve() - }, delay) + }, + isStartingNode ? 0 : delay, + ) runningTasks.set(node.id, timeout) }) @@ -77,7 +79,7 @@ export function useRunProcess(dagreGraph: MaybeRefOrGetter const startingNodes = nodes.filter((node) => graph.value.predecessors(node.id)?.length === 0) // Run the process on all starting nodes in parallel - await Promise.all(startingNodes.map(runNode)) + await Promise.all(startingNodes.map((node) => runNode(node, true))) clear() }