refactor(react/svelte): simplify ssr usage
This commit is contained in:
@@ -34,8 +34,6 @@ const initialNodes: Node[] = [
|
|||||||
position: Position.Bottom,
|
position: Position.Bottom,
|
||||||
x: nodeSize.width * 0.5,
|
x: nodeSize.width * 0.5,
|
||||||
y: nodeSize.height,
|
y: nodeSize.height,
|
||||||
width: 1,
|
|
||||||
height: 1,
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,35 +1,73 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import { SvelteFlow, Controls, Background, BackgroundVariant, Position } from '@xyflow/svelte';
|
import { SvelteFlow, Controls, Background, BackgroundVariant, Position, type Node, type Edge } from '@xyflow/svelte';
|
||||||
|
|
||||||
import '@xyflow/svelte/dist/style.css';
|
import '@xyflow/svelte/dist/style.css';
|
||||||
|
|
||||||
const nodeDefaults = {
|
const nodes = writable<Node[]>([
|
||||||
sourcePosition: Position.Right,
|
|
||||||
targetPosition: Position.Left,
|
|
||||||
size: {
|
|
||||||
width: 100,
|
|
||||||
height: 40,
|
|
||||||
},
|
|
||||||
handles: [
|
|
||||||
{ type: 'source', x: 100, y: 20, position: Position.Right, width: 1, height: 1 },
|
|
||||||
{ type: 'target', x: 0, y: 20, position: Position.Left, width: 1, height: 1 },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
const nodes = writable([
|
|
||||||
{
|
{
|
||||||
id: '0',
|
id: '0',
|
||||||
position: { x: 0, y: 150 },
|
position: { x: 0, y: 150 },
|
||||||
data: { label: 'Node 0' },
|
data: { label: 'Node 0' },
|
||||||
...nodeDefaults,
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Left,
|
||||||
|
size: {
|
||||||
|
width: 100,
|
||||||
|
height: 40,
|
||||||
|
},
|
||||||
|
handles: [
|
||||||
|
{ type: 'source', x: 100, y: 20, position: Position.Right },
|
||||||
|
{ type: 'target', x: 0, y: 20, position: Position.Left },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'A',
|
||||||
|
position: { x: 250, y: 0 },
|
||||||
|
data: { label: 'A' },
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Left,
|
||||||
|
size: {
|
||||||
|
width: 100,
|
||||||
|
height: 40,
|
||||||
|
},
|
||||||
|
handles: [
|
||||||
|
{ type: 'source', x: 100, y: 20, position: Position.Right },
|
||||||
|
{ type: 'target', x: 0, y: 20, position: Position.Left },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'B',
|
||||||
|
position: { x: 250, y: 150 },
|
||||||
|
data: { label: 'B' },
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Left,
|
||||||
|
size: {
|
||||||
|
width: 100,
|
||||||
|
height: 40,
|
||||||
|
},
|
||||||
|
handles: [
|
||||||
|
{ type: 'source', x: 100, y: 20, position: Position.Right },
|
||||||
|
{ type: 'target', x: 0, y: 20, position: Position.Left },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'C',
|
||||||
|
position: { x: 250, y: 300 },
|
||||||
|
data: { label: 'C' },
|
||||||
|
sourcePosition: Position.Right,
|
||||||
|
targetPosition: Position.Left,
|
||||||
|
size: {
|
||||||
|
width: 100,
|
||||||
|
height: 40,
|
||||||
|
},
|
||||||
|
handles: [
|
||||||
|
{ type: 'source', x: 100, y: 20, position: Position.Right },
|
||||||
|
{ type: 'target', x: 0, y: 20, position: Position.Left },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{ id: 'A', position: { x: 250, y: 0 }, data: { label: 'A' }, ...nodeDefaults },
|
|
||||||
{ id: 'B', position: { x: 250, y: 150 }, data: { label: 'B' }, ...nodeDefaults },
|
|
||||||
{ id: 'C', position: { x: 250, y: 300 }, data: { label: 'C' }, ...nodeDefaults },
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const edges = writable([
|
const edges = writable<Edge[]>([
|
||||||
{ id: '0A', source: '0', target: 'A', animated: true },
|
{ id: '0A', source: '0', target: 'A', animated: true },
|
||||||
{ id: '0B', source: '0', target: 'B', animated: true },
|
{ id: '0B', source: '0', target: 'B', animated: true },
|
||||||
{ id: '0C', source: '0', target: 'C', animated: true },
|
{ id: '0C', source: '0', target: 'C', animated: true },
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import {
|
import {
|
||||||
devWarn,
|
|
||||||
infiniteExtent,
|
infiniteExtent,
|
||||||
ConnectionMode,
|
ConnectionMode,
|
||||||
updateNodes,
|
updateNodes,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
import { internalsSymbol } from '../constants';
|
import { internalsSymbol } from '../constants';
|
||||||
import type { XYPosition, Position, CoordinateExtent, HandleElement } from '.';
|
import type { XYPosition, Position, CoordinateExtent, HandleElement } from '.';
|
||||||
|
import { Optional } from '../utils/types';
|
||||||
|
|
||||||
// this is stuff that all nodes share independent of the framework
|
// this is stuff that all nodes share independent of the framework
|
||||||
export type NodeBase<T = any, U extends string | undefined = string | undefined> = {
|
export type NodeBase<T = any, U extends string | undefined = string | undefined> = {
|
||||||
@@ -28,7 +29,7 @@ export type NodeBase<T = any, U extends string | undefined = string | undefined>
|
|||||||
ariaLabel?: string;
|
ariaLabel?: string;
|
||||||
focusable?: boolean;
|
focusable?: boolean;
|
||||||
origin?: NodeOrigin;
|
origin?: NodeOrigin;
|
||||||
handles?: HandleElement[];
|
handles?: NodeHandle[];
|
||||||
size?: {
|
size?: {
|
||||||
width?: number;
|
width?: number;
|
||||||
height?: number;
|
height?: number;
|
||||||
@@ -94,3 +95,5 @@ export type NodeOrigin = [number, number];
|
|||||||
export type OnNodeDrag = (event: MouseEvent, node: NodeBase, nodes: NodeBase[]) => void;
|
export type OnNodeDrag = (event: MouseEvent, node: NodeBase, nodes: NodeBase[]) => void;
|
||||||
|
|
||||||
export type OnSelectionDrag = (event: MouseEvent, nodes: NodeBase[]) => void;
|
export type OnSelectionDrag = (event: MouseEvent, nodes: NodeBase[]) => void;
|
||||||
|
|
||||||
|
export type NodeHandle = Optional<HandleElement, 'width' | 'height'>;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { EdgePosition } from '../../types/edges';
|
import { EdgePosition } from '../../types/edges';
|
||||||
import { ConnectionMode, OnError } from '../../types/general';
|
import { ConnectionMode, OnError } from '../../types/general';
|
||||||
import { NodeBase, NodeHandleBounds } from '../../types/nodes';
|
import { NodeBase, NodeHandle, NodeHandleBounds } from '../../types/nodes';
|
||||||
import { Position, Rect, XYPosition } from '../../types/utils';
|
import { Position, Rect, XYPosition } from '../../types/utils';
|
||||||
import { errorMessages, internalsSymbol } from '../../constants';
|
import { errorMessages, internalsSymbol } from '../../constants';
|
||||||
import { HandleElement } from '../../types';
|
import { HandleElement } from '../../types';
|
||||||
@@ -59,19 +59,22 @@ export function getEdgePosition(params: GetEdgePositionParams): EdgePosition | n
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function toHandleBounds(handles?: HandleElement[]) {
|
function toHandleBounds(handles?: NodeHandle[]) {
|
||||||
if (!handles) {
|
if (!handles) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return handles.reduce<NodeHandleBounds>(
|
return handles.reduce<NodeHandleBounds>(
|
||||||
(res, item) => {
|
(res, item) => {
|
||||||
|
item.width = item.width || 1;
|
||||||
|
item.height = item.height || 1;
|
||||||
|
|
||||||
if (item.type === 'source') {
|
if (item.type === 'source') {
|
||||||
res.source?.push(item);
|
res.source?.push(item as HandleElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.type === 'target') {
|
if (item.type === 'target') {
|
||||||
res.target?.push(item);
|
res.target?.push(item as HandleElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
||||||
Reference in New Issue
Block a user