diff --git a/tp-flow-node.js b/tp-flow-node.js index c7ac09c..a5ff961 100644 --- a/tp-flow-node.js +++ b/tp-flow-node.js @@ -185,4 +185,15 @@ export class TpFlowNode extends LitElement { this.style.transform = `translate(${data.position.x}px, ${data.position.y}px)`; } } + + dispatchDataUpdate() { + this.dispatchEvent(new CustomEvent('flow-changed', { + detail: { + type: 'node-data-changed', + data: this.exportData() + }, + bubbles: true, + composed: true + })); + } } \ No newline at end of file diff --git a/tp-flow-nodes.js b/tp-flow-nodes.js index eeff9e9..ccc4400 100644 --- a/tp-flow-nodes.js +++ b/tp-flow-nodes.js @@ -163,12 +163,16 @@ export class TpFlowNodes extends zoom(panning(connections(LitElement))) { } } + // Adjust position for current canvas translation and scale + const adjustedX = (x - this.currentX) / this.scale; + const adjustedY = (y - this.currentY) / this.scale; + const node = new NodeClass(); node.setAttribute('part', 'node'); node.setAttribute('exportparts', 'node-content'); node.importData({ id: `node_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`, - position: { x, y }, + position: { x: adjustedX, y: adjustedY }, data: initialData }); @@ -231,9 +235,8 @@ export class TpFlowNodes extends zoom(panning(connections(LitElement))) { _dispatchChangeEvent(detail = {}) { this.dispatchEvent(new CustomEvent('flow-changed', { detail: { - type: detail.type, // Type of change: 'node-added', 'node-removed', 'node-moved', 'connection-added', etc. - data: detail.data, // Additional data specific to the change - flow: this.exportFlow() // Current state of the entire flow + type: detail.type, // Type of change: 'node-added', 'node-removed', 'node-moved', 'connection-added', 'node-data-changed'. + data: detail.data, }, bubbles: true, composed: true