feat(svelte): add node option 'origin'
This commit is contained in:
@@ -73,6 +73,7 @@ export function calcNextPosition(
|
|||||||
if (node.extent === 'parent') {
|
if (node.extent === 'parent') {
|
||||||
if (node.parentNode && node.width && node.height) {
|
if (node.parentNode && node.width && node.height) {
|
||||||
const parent = nodes.find((n) => n.id === node.parentNode)!;
|
const parent = nodes.find((n) => n.id === node.parentNode)!;
|
||||||
|
const parentOrigin = parent.origin || nodeOrigin;
|
||||||
const { x: parentX, y: parentY } = parent.positionAbsolute!;
|
const { x: parentX, y: parentY } = parent.positionAbsolute!;
|
||||||
currentExtent =
|
currentExtent =
|
||||||
parent &&
|
parent &&
|
||||||
@@ -81,10 +82,10 @@ export function calcNextPosition(
|
|||||||
isNumeric(parent.width) &&
|
isNumeric(parent.width) &&
|
||||||
isNumeric(parent.height)
|
isNumeric(parent.height)
|
||||||
? [
|
? [
|
||||||
[parentX + node.width * nodeOrigin[0], parentY + node.height * nodeOrigin[1]],
|
[parentX + node.width * parentOrigin[0], parentY + node.height * parentOrigin[1]],
|
||||||
[
|
[
|
||||||
parentX + parent.width! - node.width + node.width * nodeOrigin[0],
|
parentX + parent.width! - node.width + node.width * parentOrigin[0],
|
||||||
parentY + parent.height! - node.height + node.height * nodeOrigin[1]
|
parentY + parent.height! - node.height + node.height * parentOrigin[1]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
: currentExtent;
|
: currentExtent;
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
import { Selection } from '$lib/components/Selection';
|
import { Selection } from '$lib/components/Selection';
|
||||||
import drag from '$lib/actions/drag';
|
import drag from '$lib/actions/drag';
|
||||||
|
|
||||||
const { selectionRectMode, nodes, nodeOrigin, transform, updateNodePositions } = useStore();
|
const { selectionRectMode, nodes, transform, updateNodePositions } = useStore();
|
||||||
|
|
||||||
$: selectedNodes = $nodes.filter((n) => n.selected);
|
$: selectedNodes = $nodes.filter((n) => n.selected);
|
||||||
$: rect = getRectOfNodes(selectedNodes, $nodeOrigin);
|
$: rect = getRectOfNodes(selectedNodes);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if selectedNodes && $selectionRectMode === 'nodes'}
|
{#if selectedNodes && $selectionRectMode === 'nodes'}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
import { NodeWrapper } from '$lib/components/NodeWrapper';
|
import { NodeWrapper } from '$lib/components/NodeWrapper';
|
||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
|
|
||||||
const { nodes, nodeOrigin, updateNodeDimensions } = useStore();
|
const { nodes, updateNodeDimensions } = useStore();
|
||||||
|
|
||||||
const resizeObserver: ResizeObserver | null =
|
const resizeObserver: ResizeObserver | null =
|
||||||
typeof ResizeObserver === 'undefined'
|
typeof ResizeObserver === 'undefined'
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
y: node.positionAbsolute?.y ?? 0,
|
y: node.positionAbsolute?.y ?? 0,
|
||||||
width: node.width ?? 0,
|
width: node.width ?? 0,
|
||||||
height: node.height ?? 0,
|
height: node.height ?? 0,
|
||||||
origin: $nodeOrigin
|
origin: node.origin
|
||||||
})}
|
})}
|
||||||
<NodeWrapper
|
<NodeWrapper
|
||||||
id={node.id}
|
id={node.id}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { derived } from 'svelte/store';
|
import { derived } from 'svelte/store';
|
||||||
import { Position } from '@reactflow/system';
|
import { Position } from '@reactflow/system';
|
||||||
|
import { getHandle } from '@reactflow/edge-utils';
|
||||||
|
|
||||||
import { getEdgePositions, getHandle, getNodeData } from '$lib/container/EdgeRenderer/utils';
|
import { getEdgePositions, getNodeData } from '$lib/container/EdgeRenderer/utils';
|
||||||
import type { EdgeLayouted } from '$lib/types';
|
import type { EdgeLayouted } from '$lib/types';
|
||||||
import type { SvelteFlowStoreState } from './types';
|
import type { SvelteFlowStoreState } from './types';
|
||||||
|
|
||||||
@@ -26,6 +27,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,11 @@ import {
|
|||||||
fitView as fitViewUtil,
|
fitView as fitViewUtil,
|
||||||
getD3Transition,
|
getD3Transition,
|
||||||
getDimensions,
|
getDimensions,
|
||||||
getElementsToRemove
|
getElementsToRemove,
|
||||||
|
getHandleBounds
|
||||||
} from '@reactflow/utils';
|
} from '@reactflow/utils';
|
||||||
|
|
||||||
import { getHandleBounds, addEdge as addEdgeUtil } from '$lib/utils';
|
import { addEdge as addEdgeUtil } from '$lib/utils';
|
||||||
import type { EdgeTypes, NodeTypes, Node, Edge, ConnectionData } from '$lib/types';
|
import type { EdgeTypes, NodeTypes, Node, Edge, ConnectionData } from '$lib/types';
|
||||||
import { getEdgesLayouted } from './edges-layouted';
|
import { getEdgesLayouted } from './edges-layouted';
|
||||||
import { getConnectionPath } from './connection-path';
|
import { getConnectionPath } from './connection-path';
|
||||||
@@ -104,8 +105,8 @@ export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
|||||||
node[internalsSymbol] = {
|
node[internalsSymbol] = {
|
||||||
...node[internalsSymbol],
|
...node[internalsSymbol],
|
||||||
handleBounds: {
|
handleBounds: {
|
||||||
source: getHandleBounds('.source', update.nodeElement, zoom),
|
source: getHandleBounds('.source', update.nodeElement, zoom, node.origin),
|
||||||
target: getHandleBounds('.target', update.nodeElement, zoom)
|
target: getHandleBounds('.target', update.nodeElement, zoom, node.origin)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
node.width = dimensions.width;
|
node.width = dimensions.width;
|
||||||
@@ -176,7 +177,7 @@ export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
|||||||
maxZoom: 2,
|
maxZoom: 2,
|
||||||
d3Selection,
|
d3Selection,
|
||||||
d3Zoom,
|
d3Zoom,
|
||||||
nodeOrigin: get(store.nodeOrigin)
|
nodeOrigin: [0, 0]
|
||||||
},
|
},
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import {
|
|||||||
ConnectionMode,
|
ConnectionMode,
|
||||||
ConnectionLineType,
|
ConnectionLineType,
|
||||||
type SelectionRect,
|
type SelectionRect,
|
||||||
type Transform,
|
type Transform
|
||||||
type NodeOrigin
|
|
||||||
} from '@reactflow/system';
|
} from '@reactflow/system';
|
||||||
|
|
||||||
import DefaultNode from '$lib/components/nodes/DefaultNode.svelte';
|
import DefaultNode from '$lib/components/nodes/DefaultNode.svelte';
|
||||||
@@ -49,7 +48,6 @@ export const initialStoreState = {
|
|||||||
maxZoom: writable<number>(2),
|
maxZoom: writable<number>(2),
|
||||||
fitViewOnInit: writable<boolean>(false),
|
fitViewOnInit: writable<boolean>(false),
|
||||||
fitViewOnInitDone: writable<boolean>(false),
|
fitViewOnInitDone: writable<boolean>(false),
|
||||||
nodeOrigin: writable<NodeOrigin>([0, 0]),
|
|
||||||
d3: writable<{ zoom: D3ZoomInstance | null; selection: D3SelectionInstance | null }>({
|
d3: writable<{ zoom: D3ZoomInstance | null; selection: D3SelectionInstance | null }>({
|
||||||
zoom: null,
|
zoom: null,
|
||||||
selection: null
|
selection: null
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
data: { label: 'Output Node' },
|
data: { label: 'Output Node' },
|
||||||
position: { x: 300, y: 150 }
|
position: { x: 300, y: 150 }
|
||||||
}
|
}
|
||||||
]);
|
], { origin: [0.5, 0.5] });
|
||||||
|
|
||||||
const edges = createEdges([
|
const edges = createEdges([
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user