File size: 5,090 Bytes
4237e78
af53b62
4237e78
 
 
 
 
 
 
 
 
 
ba4663c
 
4237e78
c1a1d02
3010d5b
a18645a
 
3010d5b
4237e78
 
 
 
af53b62
4237e78
c1a1d02
3010d5b
a18645a
 
3010d5b
4237e78
 
05acf81
a18645a
05acf81
 
 
 
 
 
 
 
 
 
 
 
4237e78
 
3010d5b
4237e78
 
3010d5b
4237e78
 
 
 
3010d5b
 
 
4237e78
 
 
 
 
3010d5b
4237e78
d994c06
 
4237e78
 
 
 
 
 
3010d5b
 
 
 
 
 
4237e78
 
 
 
ca01fa3
 
 
 
 
 
 
4237e78
3010d5b
 
 
 
 
af53b62
 
ba4663c
9e91869
 
 
 
 
 
af53b62
05acf81
 
 
af53b62
 
 
 
 
 
9e91869
af53b62
ba4663c
af53b62
 
 
 
 
 
05acf81
af53b62
 
9e91869
ca01fa3
af53b62
ba4663c
 
 
 
 
 
3010d5b
 
 
 
 
 
 
 
 
 
 
 
 
 
ba4663c
4237e78
 
05acf81
4237e78
 
3010d5b
4237e78
 
 
ba4663c
4237e78
 
 
3010d5b
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<script lang="ts">
  import { writable, derived } from 'svelte/store';
  import {
    SvelteFlow,
    Controls,
    Background,
    MiniMap,
    MarkerType,
    useSvelteFlow,
    type XYPosition,
    type Node,
    type Edge,
    type Connection,
    type NodeTypes,
  } from '@xyflow/svelte';
  import NodeWithParams from './NodeWithParams.svelte';
  import NodeWithParamsVertical from './NodeWithParamsVertical.svelte';
  import NodeWithGraphView from './NodeWithGraphView.svelte';
  import NodeWithTableView from './NodeWithTableView.svelte';
  import NodeWithSubFlow from './NodeWithSubFlow.svelte';
  import NodeSearch from './NodeSearch.svelte';
  import '@xyflow/svelte/dist/style.css';

  const { screenToFlowPosition } = useSvelteFlow();

  const nodeTypes: NodeTypes = {
    basic: NodeWithParams,
    vertical: NodeWithParamsVertical,
    graph_view: NodeWithGraphView,
    table_view: NodeWithTableView,
    sub_flow: NodeWithSubFlow,
  };

  export let path = '';
  const nodes = writable<Node[]>([]);
  const edges = writable<Edge[]>([]);
  let workspaceLoaded = false;
  async function fetchWorkspace(path) {
    if (!path) return;
    const res = await fetch(`/api/load?path=${path}`);
    const j = await res.json();
    nodes.set(j.nodes);
    edges.set(j.edges);
    backendWorkspace = orderedJSON(j);
    workspaceLoaded = true;
  }
  $: fetchWorkspace(path);

  function closeNodeSearch() {
    nodeSearchSettings = undefined;
  }
  function toggleNodeSearch({ detail: { event } }) {
    if (nodeSearchSettings) {
      closeNodeSearch();
      return;
    }
    event.preventDefault();
    nodeSearchSettings = {
      pos: { x: event.clientX, y: event.clientY },
      boxes: $boxes,
    };
  }
  function addNode(e) {
    const node = {...e.detail};
    nodes.update((n) => {
      node.position = screenToFlowPosition({x: nodeSearchSettings.pos.x, y: nodeSearchSettings.pos.y});
      const title = node.data.title;
      node.data.params = Object.fromEntries(
        node.data.params.map((p) => [p.name, p.default]));
      let i = 1;
      node.id = `${title} ${i}`;
      while (n.find((x) => x.id === node.id)) {
        i += 1;
        node.id = `${title} ${i}`;
      }
      node.parentNode = nodeSearchSettings.parentNode;
      if (node.parentNode) {
        node.extent = 'parent';
        const parent = n.find((x) => x.id === node.parentNode);
        node.position = { x: node.position.x - parent.position.x, y: node.position.y - parent.position.y };
      }
      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 nodeSearchSettings: {
    pos: XYPosition,
    boxes: any[],
    parentNode: string,
  };

  const graph = derived([nodes, edges], ([nodes, edges]) => ({ nodes, edges }));
  let backendWorkspace: string;
  // Like JSON.stringify, but with keys sorted.
  function orderedJSON(obj: any) {
    const allKeys = new Set();
    JSON.stringify(obj, (key, value) => (allKeys.add(key), value));
    return JSON.stringify(obj, Array.from(allKeys).sort());
  }
  graph.subscribe(async (g) => {
    if (!workspaceLoaded) {
      return;
    }
    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 = orderedJSON(g);
    if (ws === backendWorkspace) return;
    // console.log('save', '\n' + ws, '\n' + backendWorkspace);
    backendWorkspace = ws;
    const res = await fetch('/api/save', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ path, ws: g }),
    });
    const j = await res.json();
    backendWorkspace = orderedJSON(j);
    nodes.set(j.nodes);
  });
  function onconnect(connection: Connection) {
    edges.update((edges) => {
      // Only one source can connect to a given target.
      return edges.filter((e) => e.source === connection.source || e.target !== connection.target);
    });
  }
  function nodeClick(e) {
    const node = e.detail.node;
    const meta = $boxes.find(m => m.data.title === node.data.title);
    if (!meta) return;
    const sub_nodes = meta.sub_nodes;
    if (!sub_nodes) return;
    const event = e.detail.event;
    if (event.target.classList.contains('title')) return;
    nodeSearchSettings = {
      pos: { x: event.clientX, y: event.clientY },
      boxes: sub_nodes,
      parentNode: node.id,
    };
  }

</script>

<div style:height="100%">
  <SvelteFlow {nodes} {edges} {nodeTypes} fitView
    on:paneclick={toggleNodeSearch}
    on:nodeclick={nodeClick}
    proOptions={{ hideAttribution: true }}
    maxZoom={1.5}
    minZoom={0.3}
    onconnect={onconnect}
    >
    <Controls />
    <MiniMap />
    {#if nodeSearchSettings}
      <NodeSearch pos={nodeSearchSettings.pos} boxes={nodeSearchSettings.boxes} on:cancel={closeNodeSearch} on:add={addNode} />
    {/if}
  </SvelteFlow>
</div>