port more examples
This commit is contained in:
@@ -1,6 +1,4 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { useSvelteFlow } from '@xyflow/svelte';
|
|
||||||
|
|
||||||
const onDragStart = (event: DragEvent, nodeType: string) => {
|
const onDragStart = (event: DragEvent, nodeType: string) => {
|
||||||
if (!event.dataTransfer) {
|
if (!event.dataTransfer) {
|
||||||
return null;
|
return null;
|
||||||
@@ -9,8 +7,6 @@
|
|||||||
event.dataTransfer.setData('application/svelteflow', nodeType);
|
event.dataTransfer.setData('application/svelteflow', nodeType);
|
||||||
event.dataTransfer.effectAllowed = 'move';
|
event.dataTransfer.effectAllowed = 'move';
|
||||||
};
|
};
|
||||||
|
|
||||||
// const { zoomIn, zoomOut, fitView, viewport, nodes } = $derived(useSvelteFlow());
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<aside>
|
<aside>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { writable } from 'svelte/store';
|
|
||||||
import {
|
import {
|
||||||
SvelteFlow,
|
SvelteFlow,
|
||||||
Controls,
|
Controls,
|
||||||
@@ -13,7 +12,7 @@
|
|||||||
|
|
||||||
import '@xyflow/svelte/dist/style.css';
|
import '@xyflow/svelte/dist/style.css';
|
||||||
|
|
||||||
const nodes = writable([
|
let nodes = $state.raw([
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
@@ -25,7 +24,7 @@
|
|||||||
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 } }
|
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 } }
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const edges = writable([
|
let edges = $state.raw([
|
||||||
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
{ id: 'e1-3', source: '1', target: '3' }
|
{ id: 'e1-3', source: '1', target: '3' }
|
||||||
]);
|
]);
|
||||||
@@ -54,8 +53,8 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlow
|
<SvelteFlow
|
||||||
{nodes}
|
bind:nodes
|
||||||
{edges}
|
bind:edges
|
||||||
elementsSelectable={isSelectable}
|
elementsSelectable={isSelectable}
|
||||||
nodesConnectable={isConnectable}
|
nodesConnectable={isConnectable}
|
||||||
nodesDraggable={isDraggable}
|
nodesDraggable={isDraggable}
|
||||||
@@ -66,6 +65,7 @@
|
|||||||
{zoomOnDoubleClick}
|
{zoomOnDoubleClick}
|
||||||
{panOnDrag}
|
{panOnDrag}
|
||||||
{onMoveEnd}
|
{onMoveEnd}
|
||||||
|
fitView
|
||||||
>
|
>
|
||||||
<MiniMap />
|
<MiniMap />
|
||||||
<Controls />
|
<Controls />
|
||||||
|
|||||||
@@ -1,31 +1,30 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { SvelteFlow, Background, Controls, useSvelteFlow } from '@xyflow/svelte';
|
import { SvelteFlow, Background, Controls, useSvelteFlow } from '@xyflow/svelte';
|
||||||
import type { Edge, Node } from '@xyflow/svelte';
|
import type { Edge, Node } from '@xyflow/svelte';
|
||||||
import { writable } from 'svelte/store';
|
|
||||||
|
|
||||||
import '@xyflow/svelte/dist/style.css';
|
import '@xyflow/svelte/dist/style.css';
|
||||||
|
|
||||||
import { initialNodes, initialEdges } from './nodes-and-edges';
|
import { initialNodes, initialEdges } from './nodes-and-edges';
|
||||||
|
|
||||||
const nodes = writable<Node[]>(initialNodes);
|
let nodes = $state.raw<Node[]>(initialNodes);
|
||||||
const edges = writable<Edge[]>(initialEdges);
|
let edges = $state.raw<Edge[]>(initialEdges);
|
||||||
|
|
||||||
const { getIntersectingNodes } = $derived(useSvelteFlow());
|
const { getIntersectingNodes } = $derived(useSvelteFlow());
|
||||||
|
|
||||||
function onNodeDrag({ targetNode }) {
|
const onNodeDrag = ({ targetNode }) => {
|
||||||
if (targetNode) {
|
if (targetNode) {
|
||||||
const intersections = getIntersectingNodes(targetNode).map((n) => n.id);
|
const intersections = getIntersectingNodes(targetNode).map((n) => n.id);
|
||||||
|
|
||||||
$nodes.forEach((n) => {
|
nodes = nodes.map((node) => ({
|
||||||
n.class = intersections.includes(n.id) ? 'highlight' : '';
|
...node,
|
||||||
});
|
class: intersections.includes(node.id) ? 'highlight' : ''
|
||||||
$nodes = $nodes;
|
}));
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div style="height:100vh;">
|
<div style="height:100vh;">
|
||||||
<SvelteFlow {nodes} {edges} fitView class="intersection-flow" onnodedrag={onNodeDrag}>
|
<SvelteFlow bind:nodes bind:edges fitView class="intersection-flow" onnodedrag={onNodeDrag}>
|
||||||
<Background />
|
<Background />
|
||||||
<Controls />
|
<Controls />
|
||||||
</SvelteFlow>
|
</SvelteFlow>
|
||||||
@@ -48,10 +47,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
:global(
|
:global(
|
||||||
.intersection-flow .svelte-flow__node.selected,
|
.intersection-flow .svelte-flow__node.selected,
|
||||||
.intersection-flow .svelte-flow__node:hover,
|
.intersection-flow .svelte-flow__node:hover,
|
||||||
.intersection-flow .svelte-flow__node:focus
|
.intersection-flow .svelte-flow__node:focus
|
||||||
) {
|
) {
|
||||||
box-shadow: 6px 6px 0 1px rgba(0, 0, 0, 0.7);
|
box-shadow: 6px 6px 0 1px rgba(0, 0, 0, 0.7);
|
||||||
background-color: #eee;
|
background-color: #eee;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { writable } from 'svelte/store';
|
|
||||||
import { SvelteFlow, Controls, Panel, type Edge } from '@xyflow/svelte';
|
import { SvelteFlow, Controls, Panel, type Edge } from '@xyflow/svelte';
|
||||||
|
|
||||||
import DefaultResizer from './DefaultResizer.svelte';
|
import DefaultResizer from './DefaultResizer.svelte';
|
||||||
@@ -19,9 +18,9 @@
|
|||||||
|
|
||||||
const nodeStyle = 'border: 1px solid #222; font-size: 10px; background-color: #ddd;';
|
const nodeStyle = 'border: 1px solid #222; font-size: 10px; background-color: #ddd;';
|
||||||
|
|
||||||
const edges = writable<Edge[]>([]);
|
let edges = $state.raw<Edge[]>([]);
|
||||||
|
|
||||||
const nodes = writable<ResizeNode[]>([
|
let nodes = $state.raw<ResizeNode[]>([
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
type: 'defaultResizer',
|
type: 'defaultResizer',
|
||||||
@@ -132,8 +131,8 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlow
|
<SvelteFlow
|
||||||
{nodes}
|
bind:nodes
|
||||||
{edges}
|
bind:edges
|
||||||
{nodeTypes}
|
{nodeTypes}
|
||||||
minZoom={0.2}
|
minZoom={0.2}
|
||||||
maxZoom={5}
|
maxZoom={5}
|
||||||
|
|||||||
@@ -1,14 +1,8 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Handle, Position, type NodeProps, NodeResizeControl } from '@xyflow/svelte';
|
import { Handle, Position, type NodeProps, NodeResizeControl, type Node } from '@xyflow/svelte';
|
||||||
import type { ResizeNode } from './types';
|
import type { ResizeNode } from './types';
|
||||||
|
|
||||||
type $$Props = NodeProps<ResizeNode>;
|
let { data }: NodeProps<ResizeNode> = $props();
|
||||||
|
|
||||||
interface Props {
|
|
||||||
data: $$Props['data'];
|
|
||||||
}
|
|
||||||
|
|
||||||
let { data }: Props = $props();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<NodeResizeControl
|
<NodeResizeControl
|
||||||
|
|||||||
@@ -2,14 +2,7 @@
|
|||||||
import { Handle, NodeResizer, Position, type NodeProps } from '@xyflow/svelte';
|
import { Handle, NodeResizer, Position, type NodeProps } from '@xyflow/svelte';
|
||||||
import type { ResizeNode } from './types';
|
import type { ResizeNode } from './types';
|
||||||
|
|
||||||
type $$Props = NodeProps<ResizeNode>;
|
let { data, selected = undefined }: NodeProps<ResizeNode> = $props();
|
||||||
|
|
||||||
interface Props {
|
|
||||||
data: $$Props['data'];
|
|
||||||
selected?: $$Props['selected'];
|
|
||||||
}
|
|
||||||
|
|
||||||
let { data, selected = undefined }: Props = $props();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<NodeResizer
|
<NodeResizer
|
||||||
|
|||||||
@@ -2,13 +2,7 @@
|
|||||||
import { Handle, NodeResizeControl, Position, type NodeProps } from '@xyflow/svelte';
|
import { Handle, NodeResizeControl, Position, type NodeProps } from '@xyflow/svelte';
|
||||||
import type { ResizeNode } from './types';
|
import type { ResizeNode } from './types';
|
||||||
|
|
||||||
type $$Props = NodeProps<ResizeNode>;
|
let { data }: NodeProps<ResizeNode> = $props();
|
||||||
|
|
||||||
interface Props {
|
|
||||||
data: $$Props['data'];
|
|
||||||
}
|
|
||||||
|
|
||||||
let { data }: Props = $props();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<NodeResizeControl
|
<NodeResizeControl
|
||||||
|
|||||||
@@ -2,13 +2,7 @@
|
|||||||
import { Handle, NodeResizeControl, Position, type NodeProps } from '@xyflow/svelte';
|
import { Handle, NodeResizeControl, Position, type NodeProps } from '@xyflow/svelte';
|
||||||
import type { ResizeNode } from './types';
|
import type { ResizeNode } from './types';
|
||||||
|
|
||||||
type $$Props = NodeProps<ResizeNode>;
|
let { data }: NodeProps<ResizeNode> = $props();
|
||||||
|
|
||||||
interface Props {
|
|
||||||
data: $$Props['data'];
|
|
||||||
}
|
|
||||||
|
|
||||||
let { data }: Props = $props();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<NodeResizeControl
|
<NodeResizeControl
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { writable } from 'svelte/store';
|
|
||||||
import {
|
import {
|
||||||
SvelteFlow,
|
SvelteFlow,
|
||||||
Background,
|
Background,
|
||||||
@@ -11,7 +10,7 @@
|
|||||||
|
|
||||||
import '@xyflow/svelte/dist/style.css';
|
import '@xyflow/svelte/dist/style.css';
|
||||||
|
|
||||||
import CustomNode from './CustomNode.svelte';
|
import CustomNode, { type CustomNodeType } from './CustomNode.svelte';
|
||||||
import SelectedNodesToolbar from './SelectedNodesToolbar.svelte';
|
import SelectedNodesToolbar from './SelectedNodesToolbar.svelte';
|
||||||
|
|
||||||
const nodeTypes: NodeTypes = {
|
const nodeTypes: NodeTypes = {
|
||||||
@@ -19,9 +18,9 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const positions = ['top', 'right', 'bottom', 'left'];
|
const positions = ['top', 'right', 'bottom', 'left'];
|
||||||
const alignments = ['start', 'center', 'end'];
|
const alignments: ('start' | 'center' | 'end')[] = ['start', 'center', 'end'];
|
||||||
|
|
||||||
const initialNodes: Node[] = [
|
const initialNodes: CustomNodeType[] = [
|
||||||
{
|
{
|
||||||
id: 'default-node',
|
id: 'default-node',
|
||||||
type: 'custom',
|
type: 'custom',
|
||||||
@@ -51,12 +50,12 @@
|
|||||||
|
|
||||||
const initialEdges: Edge[] = [];
|
const initialEdges: Edge[] = [];
|
||||||
|
|
||||||
const nodes = writable<Node[]>(initialNodes);
|
let nodes = $state.raw<Node[]>(initialNodes);
|
||||||
const edges = writable<Edge[]>(initialEdges);
|
let edges = $state.raw<Edge[]>(initialEdges);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div style="height: 100vh;">
|
<div style="height: 100vh;">
|
||||||
<SvelteFlow {nodes} {edges} {nodeTypes} fitView>
|
<SvelteFlow bind:nodes bind:edges {nodeTypes} fitView>
|
||||||
<Background />
|
<Background />
|
||||||
<SelectedNodesToolbar />
|
<SelectedNodesToolbar />
|
||||||
</SvelteFlow>
|
</SvelteFlow>
|
||||||
|
|||||||
@@ -1,7 +1,18 @@
|
|||||||
|
<script lang="ts" module>
|
||||||
|
import { type Node } from '@xyflow/svelte';
|
||||||
|
|
||||||
|
export type CustomNodeType = Node<{
|
||||||
|
toolbarVisible?: boolean;
|
||||||
|
toolbarPosition: Position;
|
||||||
|
toolbarAlign?: 'start' | 'center' | 'end';
|
||||||
|
label: string;
|
||||||
|
}>;
|
||||||
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { NodeToolbar, type NodeProps, Handle, Position } from '@xyflow/svelte';
|
import { NodeToolbar, type NodeProps, Handle, Position } from '@xyflow/svelte';
|
||||||
|
|
||||||
let { data }: NodeProps = $props();
|
let { data }: NodeProps<CustomNodeType> = $props();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<NodeToolbar
|
<NodeToolbar
|
||||||
|
|||||||
@@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
const nodes = useNodes();
|
const nodes = useNodes();
|
||||||
|
|
||||||
let selectedNodeIds = $derived($nodes.filter((node) => node.selected).map((node) => node.id));
|
let selectedNodeIds = $derived(
|
||||||
|
nodes.current.filter((node) => node.selected).map((node) => node.id)
|
||||||
|
);
|
||||||
let isVisible = $derived(selectedNodeIds.length > 1);
|
let isVisible = $derived(selectedNodeIds.length > 1);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
+12
-4
@@ -7,8 +7,12 @@ import { useStore } from '$lib/store';
|
|||||||
* @returns store with an array of nodes
|
* @returns store with an array of nodes
|
||||||
*/
|
*/
|
||||||
export function useNodes() {
|
export function useNodes() {
|
||||||
const { nodes } = useStore();
|
const { nodes } = $derived(useStore());
|
||||||
return nodes;
|
return {
|
||||||
|
get current() {
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,6 +22,10 @@ export function useNodes() {
|
|||||||
* @returns store with an array of edges
|
* @returns store with an array of edges
|
||||||
*/
|
*/
|
||||||
export function useEdges() {
|
export function useEdges() {
|
||||||
const { edges } = useStore();
|
const { edges } = $derived(useStore());
|
||||||
return edges;
|
return {
|
||||||
|
get current() {
|
||||||
|
return edges;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ export * from '$lib/utils';
|
|||||||
export * from '$lib/hooks/useSvelteFlow';
|
export * from '$lib/hooks/useSvelteFlow';
|
||||||
export * from '$lib/hooks/useUpdateNodeInternals';
|
export * from '$lib/hooks/useUpdateNodeInternals';
|
||||||
export * from '$lib/hooks/useConnection.svelte';
|
export * from '$lib/hooks/useConnection.svelte';
|
||||||
export * from '$lib/hooks/useNodesEdges';
|
export * from '$lib/hooks/useNodesEdges.svelte';
|
||||||
export * from '$lib/hooks/useHandleConnections.svelte';
|
export * from '$lib/hooks/useHandleConnections.svelte';
|
||||||
export * from '$lib/hooks/useNodesData';
|
export * from '$lib/hooks/useNodesData';
|
||||||
export * from '$lib/hooks/useInternalNode';
|
export * from '$lib/hooks/useInternalNode';
|
||||||
|
|||||||
Reference in New Issue
Block a user