File size: 6,670 Bytes
4237e78
c470835
 
4237e78
 
 
 
 
 
c470835
4237e78
 
 
ba4663c
 
4237e78
e7fa7ee
 
 
a180fd2
c1a1d02
b5a8a95
4d72daa
a18645a
3010d5b
b7a4f8b
4237e78
e7fa7ee
4237e78
a180fd2
 
 
c470835
a180fd2
 
 
 
 
728c1b0
a180fd2
 
 
c470835
 
 
 
 
 
 
4237e78
5882a26
 
4237e78
af53b62
4237e78
c1a1d02
b5a8a95
4d72daa
a18645a
3010d5b
b7a4f8b
4237e78
 
c470835
 
728c1b0
4237e78
3010d5b
4237e78
 
3010d5b
4237e78
 
 
 
3010d5b
 
728c1b0
4237e78
 
 
942065e
c470835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4237e78
c470835
 
 
 
 
 
 
 
4237e78
 
e7fa7ee
ca01fa3
e7fa7ee
 
4237e78
3010d5b
 
 
83cc307
3010d5b
af53b62
3010d5b
 
aa0792f
3010d5b
 
 
 
 
 
 
 
83cc307
3010d5b
 
c470835
 
 
 
d43f961
c470835
d43f961
c470835
 
 
 
 
 
 
 
 
 
 
 
 
 
e7fa7ee
4237e78
 
e7fa7ee
728c1b0
e7fa7ee
 
 
 
 
 
 
 
a180fd2
 
 
 
e7fa7ee
 
 
 
 
 
 
728c1b0
e7fa7ee
 
c470835
 
e7fa7ee
 
 
 
 
 
 
 
3010d5b
e7fa7ee
 
 
a180fd2
4237e78
e7fa7ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<script lang="ts">
  import { setContext } from 'svelte';
  import { writable } from 'svelte/store';
  import {
    SvelteFlow,
    Controls,
    MiniMap,
    MarkerType,
    useSvelteFlow,
    useUpdateNodeInternals,
    type XYPosition,
    type Node,
    type Edge,
    type Connection,
    type NodeTypes,
  } from '@xyflow/svelte';
  import ArrowBack from 'virtual:icons/tabler/arrow-back'
  import Backspace from 'virtual:icons/tabler/backspace'
  import Atom from 'virtual:icons/tabler/Atom'
  import { useQuery } from '@sveltestack/svelte-query';
  import NodeWithParams from './NodeWithParams.svelte';
  import NodeWithVisualization from './NodeWithVisualization.svelte';
  import NodeWithImage from './NodeWithImage.svelte';
  import NodeWithTableView from './NodeWithTableView.svelte';
  import NodeWithSubFlow from './NodeWithSubFlow.svelte';
  import NodeWithArea from './NodeWithArea.svelte';
  import NodeSearch from './NodeSearch.svelte';
  import EnvironmentSelector from './EnvironmentSelector.svelte';
  import '@xyflow/svelte/dist/style.css';
  import { syncedStore, getYjsDoc } from "@syncedstore/core";
  import { svelteSyncedStore } from "@syncedstore/svelte";
  import { WebsocketProvider } from "y-websocket";
  const updateNodeInternals = useUpdateNodeInternals();

  function getCRDTStore(path) {
    const sstore = syncedStore({ workspace: {} });
    const doc = getYjsDoc(sstore);
    const wsProvider = new WebsocketProvider("ws://localhost:8000/ws/crdt", path, doc);
    return {store: svelteSyncedStore(sstore), sstore, doc};
  }
  $: connection = getCRDTStore(path);
  $: store = connection.store;
  $: store.subscribe((value) => {
    if (!value?.workspace?.edges) return;
    $nodes = [...value.workspace.nodes];
    $edges = [...value.workspace.edges];
    updateNodeInternals();
  });
  $: setContext('LynxKite store', store);

  export let path = '';

  const { screenToFlowPosition } = useSvelteFlow();

  const nodeTypes: NodeTypes = {
    basic: NodeWithParams,
    visualization: NodeWithVisualization,
    image: NodeWithImage,
    table_view: NodeWithTableView,
    sub_flow: NodeWithSubFlow,
    area: NodeWithArea,
  };

  const nodes = writable<Node[]>([]);
  const edges = writable<Edge[]>([]);

  function closeNodeSearch() {
    nodeSearchSettings = undefined;
  }
  function toggleNodeSearch({ detail: { event } }) {
    if (nodeSearchSettings) {
      closeNodeSearch();
      return;
    }
    event.preventDefault();
    nodeSearchSettings = {
      pos: { x: event.clientX, y: event.clientY },
      boxes: $catalog.data[$store.workspace.env],
    };
  }
  function addNode(e) {
    const meta = {...e.detail};
    const node = {
      type: meta.type,
      data: {
        meta: meta,
        title: meta.name,
        params: Object.fromEntries(
          Object.values(meta.params).map((p) => [p.name, p.default])),
      },
    };
    node.position = screenToFlowPosition({x: nodeSearchSettings.pos.x, y: nodeSearchSettings.pos.y});
    const title = node.data.title;
    let i = 1;
    node.id = `${title} ${i}`;
    const nodes = $store.workspace.nodes;
    while (nodes.find((x) => x.id === node.id)) {
      i += 1;
      node.id = `${title} ${i}`;
    }
    node.parentId = nodeSearchSettings.parentId;
    if (node.parentId) {
      node.extent = 'parent';
      const parent = nodes.find((x) => x.id === node.parentId);
      node.position = { x: node.position.x - parent.position.x, y: node.position.y - parent.position.y };
    }
    nodes.push(node);
    closeNodeSearch();
  }
  const catalog = useQuery(['catalog'], async () => {
    const res = await fetch('/api/catalog');
    return res.json();
  }, {staleTime: 60000, retry: false});

  let nodeSearchSettings: {
    pos: XYPosition,
    boxes: any[],
    parentId: string,
  };

  function nodeClick(e) {
    const node = e.detail.node;
    const meta = node.data.meta;
    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,
      parentId: node.id,
    };
  }
  function onConnect(params: Connection) {
    const edge = {
      id: `${params.source} ${params.target}`,
      source: params.source,
      sourceHandle: params.sourceHandle,
      target: params.target,
      targetHandle: params.targetHandle,
    };
    $store.workspace.edges.push(edge);
  }
  function onDelete(params) {
    const { nodes, edges } = params;
    for (const node of nodes) {
      const index = $store.workspace.nodes.findIndex((x) => x.id === node.id);
      if (index !== -1) $store.workspace.nodes.splice(index, 1);
    }
    for (const edge of edges) {
      const index = $store.workspace.edges.findIndex((x) => x.id === edge.id);
      if (index !== -1) $store.workspace.edges.splice(index, 1);
    }
  }
  $: parentDir = path.split('/').slice(0, -1).join('/');
</script>

<div class="page">
  {#if $store.workspace !== undefined}
  <div class="top-bar">
    <div class="ws-name">
      <a href><img src="/favicon.ico"></a>
      {path}
    </div>
    <div class="tools">
      <EnvironmentSelector
        options={Object.keys($catalog.data || {})}
        value={$store.workspace.env}
        onChange={(env) => {
          $store.workspace.env = env;
        }}
        />
      <a href><Atom /></a>
      <a href><Backspace /></a>
      <a href="#dir?path={parentDir}"><ArrowBack /></a>
    </div>
  </div>
  <div style:height="100%">
    <SvelteFlow {nodes} {edges} {nodeTypes} fitView
      on:paneclick={toggleNodeSearch}
      on:nodeclick={nodeClick}
      onconnect={onConnect}
      ondelete={onDelete}
      proOptions={{ hideAttribution: true }}
      maxZoom={3}
      minZoom={0.3}
      defaultEdgeOptions={{ markerEnd: { type: MarkerType.Arrow } }}
      >
      <Controls />
      <MiniMap />
      {#if nodeSearchSettings}
      <NodeSearch pos={nodeSearchSettings.pos} boxes={nodeSearchSettings.boxes} on:cancel={closeNodeSearch} on:add={addNode} />
      {/if}
    </SvelteFlow>
  </div>
  {/if}
</div>

<style>
  .top-bar {
    display: flex;
    justify-content: space-between;
    background: oklch(30% 0.13 230);
    color: white;
  }
  .ws-name {
    font-size: 1.5em;
  }
  .ws-name img {
    height: 1.5em;
    vertical-align: middle;
    margin: 4px;
  }
  .page {
    display: flex;
    flex-direction: column;
    height: 100vh;
  }

  .tools {
    display: flex;
    align-items: center;
  }
  .tools a {
    color: oklch(75% 0.13 230);
    font-size: 1.5em;
    padding: 0 10px;
  }
</style>