Skip to content

Commit 0c9eb0c

Browse files
committed
fully support composite states
1 parent b3c8a34 commit 0c9eb0c

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

src/parser/state.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -262,70 +262,60 @@ const parseDoc = (
262262
return nodes;
263263
};
264264

265-
function isNodeInCluster(node: Element | null) {
266-
if (!node) {
267-
return false;
268-
}
269-
270-
let parentNode = node.parentNode as Element;
271-
while (parentNode && parentNode?.tagName !== "svg") {
272-
if (parentNode.getAttribute("transform")) {
273-
return true;
274-
}
275-
parentNode = parentNode.parentNode as Element;
276-
}
277-
return false;
278-
}
279-
280265
const parseEdges = (nodes: ParsedDoc[], containerEl: Element): any[] => {
281266
let rootEdgeIndex = 0;
282267

283-
function parse(nodes: ParsedDoc[]): any[] {
268+
function parse(nodes: ParsedDoc[], isCluster = false): any[] {
284269
return nodes.flatMap((node, index) => {
285270
if (node.stmt === "state" && node?.doc) {
286-
return parse(node.doc);
271+
const clusters = containerEl
272+
?.querySelector(`[id="${node.id}"]`)
273+
?.closest(".root");
274+
275+
return parse(node.doc, clusters?.hasAttribute("transform"));
287276
} else if (node.stmt === "relation") {
288277
const startId = node.state1.id;
289278
const endId = node.state2.id;
290279

291-
let nodeStartElement = containerEl.querySelector<SVGPathElement>(
280+
let nodeStartElement = containerEl.querySelector(
292281
`[data-id*="${startId}"]`
293282
)!;
294283

295-
let nodeEndElement = containerEl.querySelector<SVGPathElement>(
284+
let nodeEndElement = containerEl.querySelector(
296285
`[data-id*="${endId}"]`
297286
)!;
298287

299-
// It's a cluster node
300-
if (
288+
const isClusterStartRelation =
301289
nodeStartElement.id.includes(`${startId}_start`) ||
302-
nodeStartElement.id.includes(`${startId}_end`)
303-
) {
290+
nodeStartElement.id.includes(`${startId}_end`);
291+
const isClusterEndRelation =
292+
nodeEndElement.id.includes(`${endId}_end`) ||
293+
nodeEndElement.id.includes(`${endId}_start`);
294+
295+
if (isClusterStartRelation) {
304296
nodeStartElement = containerEl.querySelector(`[id="${startId}"]`)!;
305297
}
306298

307-
// It's a cluster node
308-
if (
309-
nodeEndElement.id.includes(`${endId}_end`) ||
310-
nodeEndElement.id.includes(`${endId}_start`)
311-
) {
299+
if (isClusterEndRelation) {
312300
nodeEndElement = containerEl.querySelector(`[id="${endId}"]`)!;
313301
}
314302

315-
const rootContainer = nodeStartElement.parentElement?.parentElement;
303+
const rootContainer = nodeStartElement.closest(".root");
316304

317305
if (!rootContainer) {
318306
throw new Error("Root container not found");
319307
}
320308

321-
const edges = rootContainer.querySelector(".edgePaths")?.children;
309+
const edges = isCluster
310+
? rootContainer.querySelector(".edgePaths")?.children
311+
: containerEl.querySelector(".edgePaths")?.children;
322312

323313
if (!edges) {
324314
throw new Error("Edges not found");
325315
}
326316

327317
const edgeStartElement = edges[
328-
isNodeInCluster(nodeStartElement) ? index : rootEdgeIndex
318+
isCluster ? index : rootEdgeIndex
329319
] as SVGPathElement;
330320

331321
const position = computeElementPosition(edgeStartElement, containerEl);

0 commit comments

Comments
 (0)