From 6d2ddd8eeac663f9c88938fce7d6f51dbf4b5ba5 Mon Sep 17 00:00:00 2001 From: felipeog <17603069+felipeog@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:38:56 -0300 Subject: [PATCH] Attempt to fix creation of unwanted lines --- src/handlers/handleBroadcastChannelMessage.js | 37 ++++--------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/src/handlers/handleBroadcastChannelMessage.js b/src/handlers/handleBroadcastChannelMessage.js index ff135b0..11d8c05 100644 --- a/src/handlers/handleBroadcastChannelMessage.js +++ b/src/handlers/handleBroadcastChannelMessage.js @@ -11,7 +11,7 @@ export function handleBroadcastChannelMessage(event) { if (event.data.action === "update") { if (!state[event.data.payload.id]) { - createElementsFromUpdate(event.data.payload); + createElements(event.data.payload); } else { state[event.data.payload.id] = event.data.payload; } @@ -40,6 +40,13 @@ function createElements(w) { for (let toIndex = fromIndex + 1; toIndex < allWindows.length; toIndex++) { const to = allWindows[toIndex]; + const existingLine = lineGroup.querySelector( + `[data-from="${from.id}"][data-to="${to.id}"]` + ); + if (existingLine) { + continue; + } + const line = createSvgElement("line"); line.setAttribute("data-from", from.id); line.setAttribute("data-to", to.id); @@ -54,34 +61,6 @@ function createElements(w) { } } -function createElementsFromUpdate(w) { - state[w.id] = w; - - const circle = createSvgElement("circle"); - circle.setAttribute("data-id", w.id); - circle.setAttribute("cx", w.position.x - window.screenLeft); - circle.setAttribute("cy", w.position.y - window.screenTop); - circle.setAttribute("r", 50); - circle.setAttribute("fill", w.color); - circleGroup.appendChild(circle); - - const otherWindows = Object.values(state); - for (let toIndex = 0; toIndex < otherWindows.length; toIndex++) { - const to = otherWindows[toIndex]; - - const line = createSvgElement("line"); - line.setAttribute("data-from", currentWindow.id); - line.setAttribute("data-to", to.id); - line.setAttribute("x1", currentWindow.position.x - window.screenLeft); - line.setAttribute("y1", currentWindow.position.y - window.screenTop); - line.setAttribute("x2", to.position.x - window.screenLeft); - line.setAttribute("y2", to.position.y - window.screenTop); - line.setAttribute("stroke", "var(--foreground)"); - line.setAttribute("stroke-width", getStrokeWidth(currentWindow, to)); - lineGroup.appendChild(line); - } -} - function deleteElements(w) { delete state[w.id];