fixed some more stuff

This commit is contained in:
peterkogo
2024-04-10 11:18:30 +02:00
parent 2d1d4717d9
commit 91a642bb9c
9 changed files with 57 additions and 31 deletions
@@ -13,7 +13,7 @@
import '@xyflow/svelte/dist/style.css'; import '@xyflow/svelte/dist/style.css';
const nodes = writable([ const nodes = writable<Node[]>([
{ {
id: '1', id: '1',
type: 'input', type: 'input',
@@ -63,6 +63,7 @@
}; };
const onDrop = (event: DragEvent) => { const onDrop = (event: DragEvent) => {
console.log(event);
event.preventDefault(); event.preventDefault();
if (!event.dataTransfer) { if (!event.dataTransfer) {
@@ -81,7 +82,8 @@
data: { label: `${type} node` } data: { label: `${type} node` }
}; };
nodes.update((nds) => nds.concat(newNode)); $nodes.push(newNode);
$nodes = $nodes;
}; };
$: { $: {
@@ -11,6 +11,7 @@
type $$Props = NodeProps; type $$Props = NodeProps;
export let id: $$Props['id']; export let id: $$Props['id'];
$$restProps;
const connections = useHandleConnections({ const connections = useHandleConnections({
nodeId: id, nodeId: id,
@@ -7,6 +7,7 @@
export let data: $$Props['data']; export let data: $$Props['data'];
const { updateNodeData } = useSvelteFlow(); const { updateNodeData } = useSvelteFlow();
$$restProps;
</script> </script>
<div class="custom"> <div class="custom">
@@ -12,6 +12,8 @@
type $$Props = NodeProps; type $$Props = NodeProps;
export let id: $$Props['id']; export let id: $$Props['id'];
export let data: $$Props['data'];
$$restProps;
const { updateNodeData } = useSvelteFlow(); const { updateNodeData } = useSvelteFlow();
const connections = useHandleConnections({ const connections = useHandleConnections({
@@ -22,8 +24,12 @@
$: nodeData = useNodesData<MyNode>($connections[0]?.source); $: nodeData = useNodesData<MyNode>($connections[0]?.source);
$: textNode = isTextNode($nodeData) ? $nodeData : null; $: textNode = isTextNode($nodeData) ? $nodeData : null;
$: console.log(textNode?.data, data);
$: { $: {
updateNodeData(id, { text: textNode?.data.text.toUpperCase() || '' }); const input = textNode?.data.text.toUpperCase() ?? '';
updateNodeData(id, { text: input });
console.log('updatedNodeData with', input);
} }
</script> </script>
@@ -20,8 +20,6 @@
$: selectedNodes = $nodes.filter((n) => n.selected); $: selectedNodes = $nodes.filter((n) => n.selected);
$: bounds = getNodesBounds(selectedNodes); $: bounds = getNodesBounds(selectedNodes);
$: console.log($nodes);
function onContextMenu(event: MouseEvent | TouchEvent) { function onContextMenu(event: MouseEvent | TouchEvent) {
dispatch('selectioncontextmenu', { nodes: selectedNodes, event }); dispatch('selectioncontextmenu', { nodes: selectedNodes, event });
} }
@@ -29,7 +29,7 @@ export function useNodesData(nodeIds: any): any {
const _nodeIds = isArrayOfIds ? nodeIds : [nodeIds]; const _nodeIds = isArrayOfIds ? nodeIds : [nodeIds];
for (const nodeId of _nodeIds) { for (const nodeId of _nodeIds) {
const node = nodeLookup.get(nodeId); const node = nodeLookup.get(nodeId)?.internals.userNode;
if (node) { if (node) {
nextNodesData.push({ nextNodesData.push({
id: node.id, id: node.id,
+34 -15
View File
@@ -256,17 +256,28 @@ export function useSvelteFlow(): {
nodeUpdate: Partial<Node> | ((node: Node) => Partial<Node>), nodeUpdate: Partial<Node> | ((node: Node) => Partial<Node>),
options: { replace: boolean } = { replace: false } options: { replace: boolean } = { replace: false }
) => { ) => {
nodes.update((nds) => const node = get(nodeLookup).get(id)?.internals.userNode;
nds.map((node) => {
if (node.id === id) {
const nextNode = typeof nodeUpdate === 'function' ? nodeUpdate(node as Node) : nodeUpdate;
return options.replace && isNode(nextNode) ? nextNode : { ...node, ...nextNode }; if (!node) {
} return;
}
return node; const nextNode = typeof nodeUpdate === 'function' ? nodeUpdate(node as Node) : nodeUpdate;
})
); if (options.replace) {
nodes.update((nds) =>
nds.map((node) => {
if (node.id === id) {
return isNode(nextNode) ? nextNode : { ...node, ...nextNode };
}
return node;
})
);
} else {
Object.assign(node, nextNode);
nodes.update((nds) => nds);
}
}; };
return { return {
@@ -448,13 +459,21 @@ export function useSvelteFlow(): {
}, },
updateNode, updateNode,
updateNodeData: (id, dataUpdate, options) => { updateNodeData: (id, dataUpdate, options) => {
updateNode(id, (node) => { const node = get(nodeLookup).get(id)?.internals.userNode;
const nextData = typeof dataUpdate === 'function' ? dataUpdate(node) : dataUpdate;
return options?.replace if (!node) {
? { ...node, data: nextData } return;
: { ...node, data: { ...node.data, ...nextData } }; }
});
const nextData = typeof dataUpdate === 'function' ? dataUpdate(node) : dataUpdate;
if (options?.replace) {
node.data = nextData;
} else {
node.data = { ...node.data, ...nextData };
}
nodes.update((nds) => nds);
}, },
viewport viewport
}; };
@@ -69,7 +69,7 @@
}; };
}, },
onChange: (change: XYResizerChange, childChanges: XYResizerChildChange[]) => { onChange: (change: XYResizerChange, childChanges: XYResizerChildChange[]) => {
const node = $nodeLookup.get(id); const node = $nodeLookup.get(id)?.internals.userNode;
if (node) { if (node) {
node.height = change.isHeightChange ? change.height : node.height; node.height = change.isHeightChange ? change.height : node.height;
node.width = change.isWidthChange ? change.width : node.width; node.width = change.isWidthChange ? change.width : node.width;
@@ -79,7 +79,7 @@
: node.position; : node.position;
for (const childChange of childChanges) { for (const childChange of childChanges) {
const childNode = $nodeLookup.get(childChange.id); const childNode = $nodeLookup.get(childChange.id)?.internals.userNode;
if (childNode) { if (childNode) {
childNode.position = childChange.position; childNode.position = childChange.position;
} }
+7 -8
View File
@@ -65,20 +65,17 @@ export function createStore({
const nodeLookup = get(store.nodeLookup); const nodeLookup = get(store.nodeLookup);
for (const nodeDragItem of nodeDragItems) { for (const nodeDragItem of nodeDragItems) {
const node = nodeLookup.get(nodeDragItem.id); const node = nodeLookup.get(nodeDragItem.id)?.internals.userNode;
if (!node) { if (!node) {
continue; continue;
} }
const userNode = node.internals.userNode; node.position = nodeDragItem.position;
userNode.position = nodeDragItem.position; node.dragging = dragging;
userNode.dragging = dragging;
// node.internals.positionAbsolute = nodeDragItem.internals.positionAbsolute;
} }
store.nodes.set(get(store.nodes)); store.nodes.set(get(store.nodes));
//$nodes = $nodes
}; };
function updateNodeDimensions(updates: Map<string, NodeDimensionUpdate>) { function updateNodeDimensions(updates: Map<string, NodeDimensionUpdate>) {
@@ -111,11 +108,13 @@ export function createStore({
} }
switch (nodeUpdate.type) { switch (nodeUpdate.type) {
case 'dimensions': case 'dimensions': {
const measured = { ...node.measured, ...nodeUpdate.dimensions };
node.width = nodeUpdate.dimensions?.width ?? node.width; node.width = nodeUpdate.dimensions?.width ?? node.width;
node.height = nodeUpdate.dimensions?.height ?? node.height; node.height = nodeUpdate.dimensions?.height ?? node.height;
// TODO: do we need measured here? node.measured = measured;
break; break;
}
case 'position': case 'position':
node.position = nodeUpdate.position ?? node.position; node.position = nodeUpdate.position ?? node.position;
break; break;