Skip to content

Commit

Permalink
fix: Regard line connections of a line to the same shape
Browse files Browse the repository at this point in the history
- Previous code regarded only one connection
  • Loading branch information
miyanokomiya committed Nov 7, 2024
1 parent 4068479 commit c490e2c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
39 changes: 39 additions & 0 deletions src/composables/connectedLineHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ describe("newConnectedLineHandler", () => {
});
});

test("should regard multiple connections of a line to the same shape", () => {
const line = createShape<LineShape>(getCommonStruct, "line", {
id: "line",
pConnection: { rate: { x: 0.1, y: 0 }, id: "a" },
qConnection: { rate: { x: 0, y: 1 }, id: "a" },
body: [
{ p: { x: 0, y: 0 }, c: { rate: { x: 0, y: 0.5 }, id: "a" } },
{ p: { x: 10, y: 10 }, c: { rate: { x: 1, y: 0.5 }, id: "a" } },
],
});
const target = newConnectedLineHandler({
connectedLinesMap: {
a: [line],
},
ctx: {
getShapeComposite: () =>
newShapeComposite({
shapes: [line, a],
getStruct: getCommonStruct,
}),
},
});

expect(
target.onModified({
a: { width: 50, height: 100 } as Partial<RectangleShape>,
}),
).toEqual({
line: {
p: { x: 5, y: 0 },
q: { x: 0, y: 100 },
body: [
{ p: { x: 0, y: 50 }, c: { rate: { x: 0, y: 0.5 }, id: "a" } },
{ p: { x: 50, y: 50 }, c: { rate: { x: 1, y: 0.5 }, id: "a" } },
],
},
});
});

test("should regard body connections", () => {
const l0 = struct.create({
id: "l0",
Expand Down
19 changes: 12 additions & 7 deletions src/composables/connectedLineHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ export function newConnectedLineHandler(option: Option) {
const latestLine = latestPatch ? { ...line, ...latestPatch } : line;

const connections = getConnections(latestLine);
const index = connections.findIndex((c) => c?.id === id);
const connection = connections[index];
if (!connection) return;

const p = getLocationFromRateOnRectPath(rectPath, rotation, connection.rate);
const vertexPatch = patchVertex(latestLine, index, p, connection);
ret[latestLine.id] = latestPatch ? { ...latestPatch, ...vertexPatch } : vertexPatch;
let currentPatch = latestPatch;
let currentLine = latestLine;
connections.forEach((c, index) => {
if (c?.id !== id) return;

const p = getLocationFromRateOnRectPath(rectPath, rotation, c.rate);
const vertexPatch = patchVertex(currentLine, index, p, c);
currentPatch = { ...currentPatch, ...vertexPatch };
currentLine = { ...currentLine, ...vertexPatch };
});

ret[latestLine.id] = currentPatch;
});
});

Expand Down

0 comments on commit c490e2c

Please sign in to comment.