chore(svelte): revise useSvelteFlow example
This commit is contained in:
@@ -42,7 +42,6 @@
|
|||||||
nodes,
|
nodes,
|
||||||
edges,
|
edges,
|
||||||
transform,
|
transform,
|
||||||
nodeOrigin,
|
|
||||||
dragging,
|
dragging,
|
||||||
selectionRect,
|
selectionRect,
|
||||||
selectionRectMode,
|
selectionRectMode,
|
||||||
@@ -117,8 +116,7 @@
|
|||||||
nextUserSelectRect,
|
nextUserSelectRect,
|
||||||
$transform,
|
$transform,
|
||||||
selectionMode === SelectionMode.Partial,
|
selectionMode === SelectionMode.Partial,
|
||||||
true,
|
true
|
||||||
$nodeOrigin
|
|
||||||
);
|
);
|
||||||
const selectedEdgeIds = getConnectedEdges(selectedNodes, $edges).map((e) => e.id);
|
const selectedEdgeIds = getConnectedEdges(selectedNodes, $edges).map((e) => e.id);
|
||||||
const selectedNodeIds = selectedNodes.map((n) => n.id);
|
const selectedNodeIds = selectedNodes.map((n) => n.id);
|
||||||
|
|||||||
@@ -1,19 +1,42 @@
|
|||||||
import type { ZoomInOut } from '@reactflow/system';
|
import type { Viewport, ZoomInOut } from '@reactflow/system';
|
||||||
|
|
||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
import type { FitViewOptions } from '$lib/types';
|
import type { FitViewOptions } from '$lib/types';
|
||||||
|
import { get, writable, type Writable } from 'svelte/store';
|
||||||
|
import type { SvelteFlowStore } from '$lib/store/types';
|
||||||
|
|
||||||
export function useSvelteFlow(): {
|
export function useSvelteFlow(): {
|
||||||
zoomIn: ZoomInOut;
|
zoomIn: ZoomInOut;
|
||||||
zoomOut: ZoomInOut;
|
zoomOut: ZoomInOut;
|
||||||
fitView: (options?: FitViewOptions) => void;
|
fitView: (options?: FitViewOptions) => void;
|
||||||
|
viewport: Writable<Viewport>;
|
||||||
|
nodes: SvelteFlowStore['nodes'];
|
||||||
|
edges: SvelteFlowStore['edges'];
|
||||||
} {
|
} {
|
||||||
// how to get the new context here? fit view doesn't work, because the store is not updated (uses old nodes store)
|
// how to get the new context here? fit view doesn't work, because the store is not updated (uses old nodes store)
|
||||||
const { zoomIn, zoomOut, fitView } = useStore();
|
const { zoomIn, zoomOut, fitView, transform, nodes, edges } = useStore();
|
||||||
|
|
||||||
|
const transformValues = get(transform);
|
||||||
|
const viewportWritable = writable({
|
||||||
|
x: transformValues[0],
|
||||||
|
y: transformValues[1],
|
||||||
|
zoom: transformValues[2]
|
||||||
|
});
|
||||||
|
|
||||||
|
transform.subscribe((ts) =>
|
||||||
|
viewportWritable.set({
|
||||||
|
x: ts[0],
|
||||||
|
y: ts[1],
|
||||||
|
zoom: ts[2]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
zoomIn,
|
zoomIn,
|
||||||
zoomOut,
|
zoomOut,
|
||||||
fitView
|
fitView,
|
||||||
|
nodes,
|
||||||
|
edges,
|
||||||
|
viewport: viewportWritable
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,6 @@ export function getEdgesLayouted(store: SvelteFlowStoreState) {
|
|||||||
const sourcePosition = sourceHandle?.position || Position.Bottom;
|
const sourcePosition = sourceHandle?.position || Position.Bottom;
|
||||||
const targetPosition = targetHandle?.position || Position.Top;
|
const targetPosition = targetHandle?.position || Position.Top;
|
||||||
|
|
||||||
console.log(sourceNodeRect);
|
|
||||||
|
|
||||||
if (!sourceHandle || !targetHandle) {
|
if (!sourceHandle || !targetHandle) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import type { HandleElement, Position } from '@reactflow/system';
|
|
||||||
import {
|
import {
|
||||||
isNodeBase,
|
isNodeBase,
|
||||||
isEdgeBase,
|
isEdgeBase,
|
||||||
@@ -6,8 +5,7 @@ import {
|
|||||||
getOutgoersBase,
|
getOutgoersBase,
|
||||||
getIncomersBase,
|
getIncomersBase,
|
||||||
updateEdgeBase,
|
updateEdgeBase,
|
||||||
getConnectedEdgesBase,
|
getConnectedEdgesBase
|
||||||
getDimensions
|
|
||||||
} from '@reactflow/utils';
|
} from '@reactflow/utils';
|
||||||
import type { DefaultEdgeOptions, DefaultNodeOptions, Edge, Node } from '$lib/types';
|
import type { DefaultEdgeOptions, DefaultNodeOptions, Edge, Node } from '$lib/types';
|
||||||
import { writable, type Writable } from 'svelte/store';
|
import { writable, type Writable } from 'svelte/store';
|
||||||
|
|||||||
@@ -1,32 +1,31 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {
|
import { createNodes, createEdges, SvelteFlowProvider } from '../../lib/index';
|
||||||
createEdges,
|
|
||||||
createNodes,
|
|
||||||
SvelteFlowProvider,
|
|
||||||
} from '../../lib/index';
|
|
||||||
|
|
||||||
import Flow from './Flow.svelte';
|
import Flow from './Flow.svelte';
|
||||||
|
|
||||||
const nodes = createNodes([
|
const nodes = createNodes(
|
||||||
{
|
[
|
||||||
id: '1',
|
{
|
||||||
type: 'input',
|
id: '1',
|
||||||
data: { label: 'Input Node' },
|
type: 'input',
|
||||||
position: { x: 150, y: 5 }
|
data: { label: 'Input Node' },
|
||||||
},
|
position: { x: 150, y: 5 }
|
||||||
{
|
},
|
||||||
id: '2',
|
{
|
||||||
type: 'default',
|
id: '2',
|
||||||
data: { label: 'Node' },
|
type: 'default',
|
||||||
position: { x: 0, y: 150 }
|
data: { label: 'Node' },
|
||||||
},
|
position: { x: 0, y: 150 }
|
||||||
{
|
},
|
||||||
id: '3',
|
{
|
||||||
type: 'output',
|
id: '3',
|
||||||
data: { label: 'Output Node' },
|
type: 'output',
|
||||||
position: { x: 300, y: 150 }
|
data: { label: 'Output Node' },
|
||||||
}
|
position: { x: 300, y: 150 }
|
||||||
], { origin: [0.5, 0.5] });
|
}
|
||||||
|
],
|
||||||
|
{ origin: [0.5, 0.5] }
|
||||||
|
);
|
||||||
|
|
||||||
const edges = createEdges([
|
const edges = createEdges([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,20 +4,24 @@
|
|||||||
Background,
|
Background,
|
||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
Minimap,
|
Minimap,
|
||||||
Panel,
|
|
||||||
useSvelteFlow
|
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
|
import Sidebar from './Sidebar.svelte';
|
||||||
const svelteFlow = useSvelteFlow();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlow>
|
<main>
|
||||||
<Controls />
|
<SvelteFlow
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
fitView
|
||||||
<Minimap />
|
>
|
||||||
<Panel position="top-right">
|
<Controls />
|
||||||
<button on:click={() => svelteFlow.zoomIn()}>zoom in</button>
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
<button on:click={() => svelteFlow.zoomOut({ duration: 1000 })}>zoom out transition</button>
|
<Minimap />
|
||||||
<button on:click={() => svelteFlow.fitView()}>fitView</button>
|
</SvelteFlow>
|
||||||
</Panel>
|
<Sidebar />
|
||||||
</SvelteFlow>
|
</main>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
main {
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import {
|
||||||
|
useSvelteFlow
|
||||||
|
} from '../../lib/index';
|
||||||
|
|
||||||
|
const { zoomIn, zoomOut, fitView, viewport, nodes } = useSvelteFlow();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<aside>
|
||||||
|
<div class="label">Functions:</div>
|
||||||
|
<button on:click={() => zoomIn()}>zoom in</button>
|
||||||
|
<button on:click={() => zoomOut({ duration: 1000 })}>zoom out transition</button>
|
||||||
|
<button on:click={() => fitView()}>fitView</button>
|
||||||
|
|
||||||
|
<div class="label">Nodes:</div>
|
||||||
|
{#each $nodes as node (node.id)}
|
||||||
|
<div>id: {node.id} | x: {node.position.x.toFixed(1)} y: {node.position.y.toFixed(1)}</div>
|
||||||
|
{/each}
|
||||||
|
|
||||||
|
<div class="label">Viewport:</div>
|
||||||
|
<div>x: {$viewport.x.toFixed(1)} y: {$viewport.y.toFixed(1)} zoom: {$viewport.zoom.toFixed(1)}</div>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.label {
|
||||||
|
font-weight: 700;
|
||||||
|
margin: 0.5rem 0 .25rem 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
aside {
|
||||||
|
width: 20vw;
|
||||||
|
background: #f4f4f4;
|
||||||
|
padding: 0.4rem 0.8rem;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Reference in New Issue
Block a user