Spaces:
Running
Running
File size: 4,158 Bytes
4237e78 af53b62 4237e78 c1a1d02 4237e78 af53b62 4237e78 c1a1d02 4237e78 ca01fa3 4237e78 c1a1d02 4237e78 ca01fa3 4237e78 c1a1d02 ca01fa3 c1a1d02 4237e78 ca01fa3 4237e78 af53b62 ca01fa3 af53b62 ca01fa3 af53b62 4237e78 ca01fa3 4237e78 ca01fa3 4237e78 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
<script lang="ts">
import { writable, derived } from 'svelte/store';
import {
SvelteFlow,
Controls,
Background,
MiniMap,
MarkerType,
Position,
useSvelteFlow,
type XYPosition,
type Node,
type Edge,
} from '@xyflow/svelte';
import NodeWithParams from './NodeWithParams.svelte';
import NodeWithGraphVisualization from './NodeWithGraphVisualization.svelte';
import NodeSearch from './NodeSearch.svelte';
import '@xyflow/svelte/dist/style.css';
const { screenToFlowPosition } = useSvelteFlow();
const nodeTypes: NodeTypes = {
basic: NodeWithParams,
graphviz: NodeWithGraphVisualization,
};
const nodes = writable<Node[]>([
{
id: '1',
type: 'basic',
data: { title: 'Compute PageRank', params: { damping: 0.85, iterations: 3 } },
position: { x: 0, y: 0 },
sourcePosition: Position.Right,
targetPosition: Position.Left,
},
{
id: '3',
type: 'basic',
data: { title: 'Import Parquet', params: { filename: '/tmp/x.parquet' } },
position: { x: -400, y: 0 },
sourcePosition: Position.Right,
},
{
id: '4',
type: 'graphviz',
data: { title: 'Visualize graph', params: {} },
position: { x: 300, y: 0 },
targetPosition: Position.Left,
},
]);
const edges = writable<Edge[]>([
{
id: '3-1',
source: '3',
target: '1',
// markerEnd: { type: MarkerType.ArrowClosed },
},
{
id: '3-4',
source: '1',
target: '4',
// markerEnd: { type: MarkerType.ArrowClosed },
},
]);
function closeNodeSearch() {
nodeSearchPos = undefined;
}
function toggleNodeSearch({ detail: { event } }) {
if (nodeSearchPos) {
closeNodeSearch();
return;
}
event.preventDefault();
const width = 500;
const height = 200;
nodeSearchPos = {
top: event.clientY < height - 200 ? event.clientY : undefined,
left: event.clientX < width - 200 ? event.clientX : undefined,
right: event.clientX >= width - 200 ? width - event.clientX : undefined,
bottom: event.clientY >= height - 200 ? height - event.clientY : undefined
};
nodeSearchPos = {
top: event.clientY,
left: event.clientX - 150,
};
}
function addNode(e) {
const node = {...e.detail};
nodes.update((n) => {
node.position = screenToFlowPosition({x: nodeSearchPos.left, y: nodeSearchPos.top});
const title = node.data.title;
let i = 1;
node.id = `${title} ${i}`;
while (n.find((x) => x.id === node.id)) {
i += 1;
node.id = `${title} ${i}`;
}
return [...n, node]
});
closeNodeSearch();
}
const boxes = writable([]);
async function getBoxes() {
const res = await fetch('/api/catalog');
const j = await res.json();
boxes.set(j);
}
getBoxes();
let nodeSearchPos: XYPosition | undefined = undefined;
const graph = derived([nodes, edges], ([nodes, edges]) => ({ nodes, edges }));
let backendWorkspace;
graph.subscribe(async (g) => {
const dragging = g.nodes.find((n) => n.dragging);
if (dragging) return;
g = JSON.parse(JSON.stringify(g));
for (const node of g.nodes) {
delete node.computed;
}
const ws = JSON.stringify(g);
if (ws === backendWorkspace) return;
console.log('current vs backend', '\n' + ws, '\n' + backendWorkspace);
backendWorkspace = ws;
const res = await fetch('/api/save', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(g),
});
const j = await res.json();
backendWorkspace = JSON.stringify(j);
nodes.set(j.nodes);
});
</script>
<div style:height="100vh">
<SvelteFlow {nodes} {edges} {nodeTypes} fitView
on:paneclick={toggleNodeSearch}
proOptions={{ hideAttribution: true }}
maxZoom={1.5}
minZoom={0.3}
>
<Background patternColor="#39bcf3" />
<Controls />
<MiniMap />
{#if nodeSearchPos}<NodeSearch boxes={$boxes} on:cancel={closeNodeSearch} on:add={addNode} pos={nodeSearchPos} />{/if}
</SvelteFlow>
</div>
|