Spaces:
Running
Running
File size: 20,546 Bytes
9a9d700 d460634 98f8f03 d460634 98f8f03 d460634 98f8f03 d460634 03fbff6 d460634 ddebfca ab53490 d460634 0a23cf5 7194a89 0234ec8 d460634 98f8f03 d460634 98f8f03 d460634 e3b1d5c 3f3cb7b e44317d e3b1d5c 3f3cb7b da1f2d0 d460634 0234ec8 98f8f03 d460634 35313d0 d460634 a112474 303286e 91961e7 502f722 c7f22d1 1107514 d460634 44b3c5d d460634 98f8f03 da1f2d0 03fbff6 d460634 ab53490 7194a89 ddebfca 0234ec8 cd2383d da1f2d0 0a23cf5 a5bf96b da1f2d0 a5bf96b 7c324a6 a112474 ab53490 a5bf96b 6f8e8fe 91961e7 502f722 21c1030 6f8e8fe be60ac3 1f43b58 be60ac3 a5bf96b ddebfca a5bf96b d460634 a5bf96b d460634 a5bf96b d460634 a5bf96b d460634 901487a d460634 98f8f03 d460634 1ae7dde 901487a d460634 bf8f4f1 1107514 502f722 91961e7 d460634 35313d0 f94e9a7 e44317d f94e9a7 7194a89 f94e9a7 e44317d f94e9a7 c83f1c9 f94e9a7 c83f1c9 f94e9a7 7194a89 f94e9a7 901487a ab53490 901487a d460634 3f3cb7b 0a23cf5 3f3cb7b 0a23cf5 3f3cb7b 0a23cf5 3f3cb7b 0a23cf5 b44f5a8 0a23cf5 b44f5a8 0a23cf5 d460634 0a23cf5 d460634 3f3cb7b d460634 ab53490 d460634 3006c30 d460634 0a23cf5 a112474 0a23cf5 3f3cb7b 0a23cf5 a08a3aa c82d8dc 3f3cb7b 0a23cf5 e44317d e2e447f 3f3cb7b fb847c9 3f3cb7b 91961e7 3f3cb7b ef8a15c 3f3cb7b ef8a15c 3f3cb7b bbd3b58 3f3cb7b fb847c9 3f3cb7b ef8a15c 3f3cb7b f563e64 3f3cb7b ef8a15c f563e64 3f3cb7b ef8a15c 3f3cb7b f563e64 3f3cb7b 91961e7 98f8f03 0234ec8 d460634 0234ec8 cd2383d 98f8f03 ddebfca d460634 98f8f03 3f3cb7b 98f8f03 7194a89 dfd9216 03fbff6 ddebfca d460634 35313d0 d460634 ddebfca ab53490 98f8f03 db13d53 2d84389 d7fda19 8fe1d21 d7fda19 86475b8 7194a89 98f8f03 d460634 0a23cf5 d460634 98f8f03 0a23cf5 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 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
// The LynxKite workspace editor.
import { getYjsDoc, syncedStore } from "@syncedstore/core";
import {
type Connection,
Controls,
type Edge,
MarkerType,
type Node,
ReactFlow,
ReactFlowProvider,
type XYPosition,
applyEdgeChanges,
applyNodeChanges,
useReactFlow,
useUpdateNodeInternals,
} from "@xyflow/react";
import axios from "axios";
import { type MouseEvent, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Link } from "react-router";
import useSWR, { type Fetcher } from "swr";
import { WebsocketProvider } from "y-websocket";
// @ts-ignore
import Atom from "~icons/tabler/atom.jsx";
// @ts-ignore
import Backspace from "~icons/tabler/backspace.jsx";
// @ts-ignore
import UngroupIcon from "~icons/tabler/library-minus.jsx";
// @ts-ignore
import GroupIcon from "~icons/tabler/library-plus.jsx";
// @ts-ignore
import Restart from "~icons/tabler/rotate-clockwise.jsx";
// @ts-ignore
import Close from "~icons/tabler/x.jsx";
import Tooltip from "../Tooltip.tsx";
import type { WorkspaceNode, Workspace as WorkspaceType } from "../apiTypes.ts";
import favicon from "../assets/favicon.ico";
import { usePath } from "../common.ts";
// import NodeWithTableView from './NodeWithTableView';
import EnvironmentSelector from "./EnvironmentSelector";
import LynxKiteEdge from "./LynxKiteEdge.tsx";
import { LynxKiteState } from "./LynxKiteState";
import NodeSearch, { type OpsOp, type Catalog, type Catalogs } from "./NodeSearch.tsx";
import NodeWithGraphCreationView from "./nodes/GraphCreationNode.tsx";
import Group from "./nodes/Group.tsx";
import NodeWithComment from "./nodes/NodeWithComment.tsx";
import NodeWithImage from "./nodes/NodeWithImage.tsx";
import NodeWithMolecule from "./nodes/NodeWithMolecule.tsx";
import NodeWithParams from "./nodes/NodeWithParams";
import NodeWithTableView from "./nodes/NodeWithTableView.tsx";
import NodeWithVisualization from "./nodes/NodeWithVisualization.tsx";
export default function Workspace(props: any) {
return (
<ReactFlowProvider>
<LynxKiteFlow {...props} />
</ReactFlowProvider>
);
}
function LynxKiteFlow() {
const updateNodeInternals = useUpdateNodeInternals();
const reactFlow = useReactFlow();
const reactFlowContainer = useRef<HTMLDivElement>(null);
const [nodes, setNodes] = useState([] as Node[]);
const [edges, setEdges] = useState([] as Edge[]);
const path = usePath().replace(/^[/]edit[/]/, "");
const shortPath = path!
.split("/")
.pop()!
.replace(/[.]lynxkite[.]json$/, "");
const [state, setState] = useState({ workspace: {} as WorkspaceType });
const [message, setMessage] = useState(null as string | null);
useEffect(() => {
const state = syncedStore({ workspace: {} as WorkspaceType });
setState(state);
const doc = getYjsDoc(state);
const proto = location.protocol === "https:" ? "wss:" : "ws:";
const wsProvider = new WebsocketProvider(`${proto}//${location.host}/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;
for (const n of state.workspace.nodes) {
if (n.type !== "node_group" && n.dragHandle !== ".drag-handle") {
n.dragHandle = ".drag-handle";
}
}
const nodes = reactFlow.getNodes();
const selection = nodes.filter((n) => n.selected).map((n) => n.id);
const newNodes = state.workspace.nodes.map((n) =>
selection.includes(n.id) ? { ...n, selected: true } : n,
);
setNodes([...newNodes] 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, updateNodeInternals]);
const onNodesChange = useCallback(
(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" &&
!Number.isNaN(ch.position.x) &&
!Number.isNaN(ch.position.y)
) {
getYjsDoc(state).transact(() => {
Object.assign(node.position, ch.position);
});
} else if (ch.type === "select") {
} else if (ch.type === "dimensions") {
getYjsDoc(state).transact(() => Object.assign(node, ch.dimensions));
} else if (ch.type === "remove") {
wnodes.splice(nodeIndex, 1);
} else if (ch.type === "replace") {
// Ideally we would only update the parameter that changed. But ReactFlow does not give us that detail.
const u = {
collapsed: ch.item.data.collapsed,
// The "..." expansion on a Y.map returns an empty object. Copying with fromEntries/entries instead.
params: {
...Object.fromEntries(Object.entries(ch.item.data.params)),
},
__execution_delay: ch.item.data.__execution_delay,
};
getYjsDoc(state).transact(() => Object.assign(node.data, u));
} else {
console.log("Unknown node change", ch);
}
}
},
[state],
);
const onEdgesChange = useCallback(
(changes: any[]) => {
setEdges((eds) => applyEdgeChanges(changes, eds));
const wedges = state.workspace?.edges;
if (!wedges) return;
for (const ch of changes) {
const edgeIndex = wedges.findIndex((e) => e.id === ch.id);
if (ch.type === "remove") {
wedges.splice(edgeIndex, 1);
} else if (ch.type === "select") {
} else {
console.log("Unknown edge change", ch);
}
}
},
[state],
);
const fetcher: Fetcher<Catalogs> = (resource: string, init?: RequestInit) =>
fetch(resource, init).then((res) => res.json());
const catalog = useSWR(`/api/catalog?workspace=${path}`, fetcher);
const [suppressSearchUntil, setSuppressSearchUntil] = useState(0);
const [nodeSearchSettings, setNodeSearchSettings] = useState(
undefined as
| {
pos: XYPosition;
boxes: Catalog;
}
| undefined,
);
const nodeTypes = useMemo(
() => ({
basic: NodeWithParams,
visualization: NodeWithVisualization,
image: NodeWithImage,
table_view: NodeWithTableView,
graph_creation_view: NodeWithGraphCreationView,
molecule: NodeWithMolecule,
comment: NodeWithComment,
node_group: Group,
}),
[],
);
const edgeTypes = useMemo(
() => ({
default: LynxKiteEdge,
}),
[],
);
// Global keyboard shortcuts.
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
// Show the node search dialog on "/".
if (nodeSearchSettings || isTypingInFormElement()) return;
if (event.key === "/") {
event.preventDefault();
setNodeSearchSettings({
pos: getBestPosition(),
boxes: catalog.data![state.workspace.env!],
});
} else if (event.key === "r") {
event.preventDefault();
executeWorkspace();
}
};
// TODO: Switch to keydown once https://github.com/xyflow/xyflow/pull/5055 is merged.
document.addEventListener("keyup", handleKeyDown);
return () => {
document.removeEventListener("keyup", handleKeyDown);
};
}, [catalog.data, nodeSearchSettings, state.workspace.env]);
function getBestPosition() {
const W = reactFlowContainer.current!.clientWidth;
const H = reactFlowContainer.current!.clientHeight;
const w = 200;
const h = 200;
const SPEED = 20;
const GAP = 50;
const pos = { x: 100, y: 100 };
while (pos.y < H) {
// Find a position that is not occupied by a node.
const fpos = reactFlow.screenToFlowPosition(pos);
const occupied = state.workspace.nodes!.some((n) => {
const np = n.position;
return (
np.x < fpos.x + w + GAP &&
np.x + n.width + GAP > fpos.x &&
np.y < fpos.y + h + GAP &&
np.y + n.height + GAP > fpos.y
);
});
if (!occupied) {
return pos;
}
// Move the position to the right and down until we find a free spot.
pos.x += SPEED;
if (pos.x + w > W) {
pos.x = 100;
pos.y += SPEED;
}
}
return { x: 100, y: 100 };
}
function isTypingInFormElement() {
const activeElement = document.activeElement;
return (
activeElement &&
(activeElement.tagName === "INPUT" ||
activeElement.tagName === "TEXTAREA" ||
(activeElement as HTMLElement).isContentEditable)
);
}
const closeNodeSearch = useCallback(() => {
setNodeSearchSettings(undefined);
setSuppressSearchUntil(Date.now() + 200);
}, []);
const toggleNodeSearch = useCallback(
(event: MouseEvent) => {
if (suppressSearchUntil > Date.now()) return;
if (nodeSearchSettings) {
closeNodeSearch();
return;
}
event.preventDefault();
setNodeSearchSettings({
pos: { x: event.clientX, y: event.clientY },
boxes: catalog.data![state.workspace.env!],
});
},
[catalog, state, nodeSearchSettings, suppressSearchUntil, closeNodeSearch],
);
function findFreeId(prefix: string) {
let i = 1;
let id = `${prefix} ${i}`;
const used = new Set(state.workspace.nodes!.map((n) => n.id));
while (used.has(id)) {
i += 1;
id = `${prefix} ${i}`;
}
return id;
}
function addNode(node: Partial<WorkspaceNode>) {
state.workspace.nodes!.push(node as WorkspaceNode);
setNodes([...nodes, node as WorkspaceNode]);
}
function nodeFromMeta(meta: OpsOp): Partial<WorkspaceNode> {
const node: Partial<WorkspaceNode> = {
type: meta.type,
data: {
meta: { value: meta },
title: meta.name,
params: Object.fromEntries(meta.params.map((p) => [p.name, p.default])),
},
};
return node;
}
const addNodeFromSearch = useCallback(
(meta: OpsOp) => {
const node = nodeFromMeta(meta);
const nss = nodeSearchSettings!;
node.position = reactFlow.screenToFlowPosition({
x: nss.pos.x,
y: nss.pos.y,
});
node.id = findFreeId(node.data!.title);
addNode(node);
closeNodeSearch();
},
[nodeSearchSettings, state, reactFlow, nodes, closeNodeSearch],
);
const onConnect = useCallback(
(connection: Connection) => {
setSuppressSearchUntil(Date.now() + 200);
const edge = {
id: `${connection.source} ${connection.sourceHandle} ${connection.target} ${connection.targetHandle}`,
source: connection.source,
sourceHandle: connection.sourceHandle!,
target: connection.target,
targetHandle: connection.targetHandle!,
};
state.workspace.edges!.push(edge);
setEdges((oldEdges) => [...oldEdges, edge]);
},
[state],
);
const parentDir = path!.split("/").slice(0, -1).join("/");
function onDragOver(e: React.DragEvent<HTMLDivElement>) {
e.stopPropagation();
e.preventDefault();
}
async function onDrop(e: React.DragEvent<HTMLDivElement>) {
e.stopPropagation();
e.preventDefault();
const file = e.dataTransfer.files[0];
const formData = new FormData();
formData.append("file", file);
try {
await axios.post("/api/upload", formData, {
onUploadProgress: (progressEvent) => {
const percentCompleted = Math.round((100 * progressEvent.loaded) / progressEvent.total!);
if (percentCompleted === 100) setMessage("Processing file...");
else setMessage(`Uploading ${percentCompleted}%`);
},
});
setMessage(null);
const cat = catalog.data![state.workspace.env!];
const node = nodeFromMeta(cat["Import file"]);
node.id = findFreeId(node.data!.title);
node.position = reactFlow.screenToFlowPosition({
x: e.clientX,
y: e.clientY,
});
node.data!.params.file_path = `uploads/${file.name}`;
if (file.name.includes(".csv")) {
node.data!.params.file_format = "csv";
} else if (file.name.includes(".parquet")) {
node.data!.params.file_format = "parquet";
} else if (file.name.includes(".json")) {
node.data!.params.file_format = "json";
} else if (file.name.includes(".xls")) {
node.data!.params.file_format = "excel";
}
addNode(node);
} catch (error) {
setMessage("File upload failed.");
}
}
async function executeWorkspace() {
const response = await axios.post(`/api/execute_workspace?name=${path}`);
if (response.status !== 200) {
setMessage("Workspace execution failed.");
}
}
function deleteSelection() {
const selectedNodes = nodes.filter((n) => n.selected);
const selectedEdges = edges.filter((e) => e.selected);
reactFlow.deleteElements({ nodes: selectedNodes, edges: selectedEdges });
}
function groupSelection() {
const selectedNodes = nodes.filter((n) => n.selected && !n.parentId);
const groupNode = {
id: findFreeId("Group"),
type: "node_group",
position: { x: 0, y: 0 },
width: 0,
height: 0,
data: { title: "Group", params: {} },
};
let top = Number.POSITIVE_INFINITY;
let left = Number.POSITIVE_INFINITY;
let bottom = Number.NEGATIVE_INFINITY;
let right = Number.NEGATIVE_INFINITY;
const PAD = 10;
for (const node of selectedNodes) {
if (node.position.y - PAD < top) top = node.position.y - PAD;
if (node.position.x - PAD < left) left = node.position.x - PAD;
if (node.position.y + PAD + node.height! > bottom)
bottom = node.position.y + PAD + node.height!;
if (node.position.x + PAD + node.width! > right) right = node.position.x + PAD + node.width!;
}
groupNode.position = {
x: left,
y: top,
};
groupNode.width = right - left;
groupNode.height = bottom - top;
setNodes([
{ ...(groupNode as WorkspaceNode), selected: true },
...nodes.map((n) =>
n.selected
? {
...n,
position: { x: n.position.x - left, y: n.position.y - top },
parentId: groupNode.id,
extent: "parent" as const,
selected: false,
}
: n,
),
]);
getYjsDoc(state).transact(() => {
state.workspace.nodes!.unshift(groupNode as WorkspaceNode);
const selectedNodeIds = new Set(selectedNodes.map((n) => n.id));
for (const node of state.workspace.nodes!) {
if (selectedNodeIds.has(node.id)) {
node.position.x -= left;
node.position.y -= top;
node.parentId = groupNode.id;
node.extent = "parent";
node.selected = false;
}
}
});
}
function ungroupSelection() {
const groups = Object.fromEntries(
nodes
.filter((n) => n.selected && n.type === "node_group" && !n.parentId)
.map((n) => [n.id, n]),
);
setNodes(
nodes
.filter((n) => !groups[n.id])
.map((n) => {
const g = groups[n.parentId!];
if (!g) return n;
return {
...n,
position: { x: n.position.x + g.position.x, y: n.position.y + g.position.y },
parentId: undefined,
extent: undefined,
selected: true,
};
}),
);
getYjsDoc(state).transact(() => {
const wnodes = state.workspace.nodes!;
for (const node of state.workspace.nodes!) {
const g = groups[node.parentId as string];
if (!g) continue;
node.position.x += g.position.x;
node.position.y += g.position.y;
node.parentId = undefined;
node.extent = undefined;
}
for (const groupId in groups) {
const groupIdx = wnodes.findIndex((n) => n.id === groupId);
wnodes.splice(groupIdx, 1);
}
});
}
const areMultipleNodesSelected = nodes.filter((n) => n.selected).length > 1;
const isAnyGroupSelected = nodes.some((n) => n.selected && n.type === "node_group");
return (
<div className="workspace">
<div className="top-bar bg-neutral">
<Link className="logo" to="/">
<img alt="" src={favicon} />
</Link>
<div className="ws-name">{shortPath}</div>
<title>{shortPath}</title>
<EnvironmentSelector
options={Object.keys(catalog.data || {})}
value={state.workspace.env!}
onChange={(env) => {
state.workspace.env = env;
}}
/>
<div className="tools text-secondary">
{areMultipleNodesSelected && (
<Tooltip doc="Group selected nodes">
<button className="btn btn-link" onClick={groupSelection}>
<GroupIcon />
</button>
</Tooltip>
)}
{isAnyGroupSelected && (
<Tooltip doc="Ungroup selected nodes">
<button className="btn btn-link" onClick={ungroupSelection}>
<UngroupIcon />
</button>
</Tooltip>
)}
<Tooltip doc="Delete selected nodes and edges">
<button className="btn btn-link" onClick={deleteSelection}>
<Backspace />
</button>
</Tooltip>
<Tooltip doc="Re-run the workspace">
<button className="btn btn-link" onClick={executeWorkspace}>
<Restart />
</button>
</Tooltip>
<Tooltip doc="Close workspace">
<Link className="btn btn-link" to={`/dir/${parentDir}`} aria-label="close">
<Close />
</Link>
</Tooltip>
</div>
</div>
<div
style={{ height: "100%", width: "100vw" }}
onDragOver={onDragOver}
onDrop={onDrop}
ref={reactFlowContainer}
>
<LynxKiteState.Provider value={state}>
<ReactFlow
nodes={nodes}
edges={edges}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
fitView
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onPaneClick={toggleNodeSearch}
onConnect={onConnect}
proOptions={{ hideAttribution: true }}
maxZoom={10}
minZoom={0.2}
zoomOnScroll={false}
panOnScroll={true}
panOnDrag={false}
selectionOnDrag={true}
panOnScrollSpeed={1}
preventScrolling={false}
defaultEdgeOptions={{
markerEnd: {
type: MarkerType.ArrowClosed,
color: "black",
width: 15,
height: 15,
},
style: {
strokeWidth: 2,
stroke: "black",
},
}}
fitViewOptions={{ maxZoom: 1 }}
>
<Controls />
{nodeSearchSettings && (
<NodeSearch
pos={nodeSearchSettings.pos}
boxes={nodeSearchSettings.boxes}
onCancel={closeNodeSearch}
onAdd={addNodeFromSearch}
/>
)}
</ReactFlow>
</LynxKiteState.Provider>
{message && (
<div className="workspace-message">
<span className="close" onClick={() => setMessage(null)}>
<Close />
</span>
{message}
</div>
)}
</div>
</div>
);
}
|