Spaces:
Running
Running
File size: 5,660 Bytes
5fe7b6c 98f8f03 a5bf96b 5fe7b6c 98f8f03 03fbff6 ddebfca a5bf96b 98f8f03 ddebfca 98f8f03 03fbff6 98f8f03 03fbff6 a5bf96b ddebfca 5fe7b6c a5bf96b ddebfca a5bf96b ddebfca a5bf96b ddebfca a5bf96b 98f8f03 5fe7b6c 98f8f03 5fe7b6c 98f8f03 ddebfca 98f8f03 5fe7b6c 98f8f03 dfd9216 03fbff6 ddebfca 03fbff6 ddebfca 98f8f03 |
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 |
// The LynxKite workspace editor.
import { useParams } from "react-router";
import useSWR from 'swr';
import { useEffect, useMemo, useCallback, useState } from "react";
import favicon from '../assets/favicon.ico';
import {
ReactFlow,
Controls,
MarkerType,
ReactFlowProvider,
applyEdgeChanges,
applyNodeChanges,
useUpdateNodeInternals,
type XYPosition,
type Node,
type Edge,
type Connection,
type NodeTypes,
} from '@xyflow/react';
// @ts-ignore
import ArrowBack from '~icons/tabler/arrow-back.jsx';
// @ts-ignore
import Backspace from '~icons/tabler/backspace.jsx';
// @ts-ignore
import Atom from '~icons/tabler/atom.jsx';
import { syncedStore, getYjsDoc } from "@syncedstore/core";
import { WebsocketProvider } from "y-websocket";
import NodeWithParams from './nodes/NodeWithParams';
// import NodeWithVisualization from './NodeWithVisualization';
// import NodeWithImage from './NodeWithImage';
// import NodeWithTableView from './NodeWithTableView';
// import NodeWithSubFlow from './NodeWithSubFlow';
// import NodeWithArea from './NodeWithArea';
// import NodeSearch from './NodeSearch';
import EnvironmentSelector from './EnvironmentSelector';
import { LynxKiteState } from './LynxKiteState';
import '@xyflow/react/dist/style.css';
import { Workspace } from "../apiTypes.ts";
export default function (props: any) {
return (
<ReactFlowProvider>
<LynxKiteFlow {...props} />
</ReactFlowProvider>
);
}
function LynxKiteFlow() {
const updateNodeInternals = useUpdateNodeInternals()
const [nodes, setNodes] = useState([] as Node[]);
const [edges, setEdges] = useState([] as Edge[]);
const { path } = useParams();
const [state, setState] = useState({ workspace: {} as Workspace });
useEffect(() => {
const state = syncedStore({ workspace: {} as Workspace });
setState(state);
const doc = getYjsDoc(state);
const wsProvider = new WebsocketProvider("ws://localhost:8000/ws/crdt", path!, doc);
const onChange = (update: any, origin: any, doc: any, tr: any) => {
if (origin === wsProvider) {
// An update from the CRDT. Apply it to the local state.
// This is only necessary because ReactFlow keeps secret internal copies of our stuff.
if (!state.workspace) return;
if (!state.workspace.nodes) return;
if (!state.workspace.edges) return;
setNodes([...state.workspace.nodes] as Node[]);
setEdges([...state.workspace.edges] as Edge[]);
for (const node of state.workspace.nodes) {
// Make sure the internal copies are updated.
updateNodeInternals(node.id);
}
}
};
doc.on('update', onChange);
return () => {
doc.destroy();
wsProvider.destroy();
}
}, [path]);
const onNodesChange = (changes: any[]) => {
// An update from the UI. Apply it to the local state...
setNodes((nds) => applyNodeChanges(changes, nds));
// ...and to the CRDT state. (Which could be the same, except for ReactFlow's internal copies.)
const wnodes = state.workspace?.nodes;
if (!wnodes) return;
for (const ch of changes) {
const nodeIndex = wnodes.findIndex((n) => n.id === ch.id);
if (nodeIndex === -1) continue;
const node = wnodes[nodeIndex];
if (!node) continue;
// Position events sometimes come with NaN values. Ignore them.
if (ch.type === 'position' && !isNaN(ch.position.x) && !isNaN(ch.position.y)) {
Object.assign(node.position, ch.position);
} else if (ch.type === 'select') {
} else if (ch.type === 'dimensions') {
} else if (ch.type === 'replace') {
node.data.collapsed = ch.item.data.collapsed;
node.data.params = { ...ch.item.data.params };
} else {
console.log('Unknown node change', ch);
}
}
};
const onEdgesChange = (changes: any[]) => {
setEdges((eds) => applyEdgeChanges(changes, eds));
};
const fetcher = (resource: string, init?: RequestInit) => fetch(resource, init).then(res => res.json());
const catalog = useSWR('/api/catalog', fetcher);
const nodeTypes = useMemo(() => ({
basic: NodeWithParams,
table_view: NodeWithParams,
}), []);
const parentDir = path!.split('/').slice(0, -1).join('/');
return (
<div className="workspace">
<div className="top-bar bg-neutral">
<a className="logo" href=""><img src={favicon} /></a>
<div className="ws-name">
{path}
</div>
<EnvironmentSelector
options={Object.keys(catalog.data || {})}
value={state.workspace.env!}
onChange={(env) => { state.workspace.env = env; }}
/>
<div className="tools text-secondary">
<a href=""><Atom /></a>
<a href=""><Backspace /></a>
<a href={'/dir/' + parentDir}><ArrowBack /></a>
</div>
</div>
<div style={{ height: "100%", width: '100vw' }}>
<LynxKiteState.Provider value={state}>
<ReactFlow
nodes={nodes}
edges={edges}
nodeTypes={nodeTypes} fitView
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
proOptions={{ hideAttribution: true }}
maxZoom={3}
minZoom={0.3}
defaultEdgeOptions={{ markerEnd: { type: MarkerType.Arrow } }}
>
<Controls />
{/* {#if nodeSearchSettings}
<NodeSearch pos={nodeSearchSettings.pos} boxes={nodeSearchSettings.boxes} on:cancel={closeNodeSearch} on:add={addNode} />
{/if} */}
</ReactFlow>
</LynxKiteState.Provider>
</div>
</div>
);
}
|