Skip to content

Commit

Permalink
feat: nested groupids
Browse files Browse the repository at this point in the history
  • Loading branch information
igorwessel committed May 30, 2024
1 parent fa27c7b commit a346e77
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
31 changes: 30 additions & 1 deletion src/converter/types/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const StateToExcalidrawSkeletonConvertor = new GraphConverter({
const elements: ExcalidrawElementSkeleton[] = [];

chart.nodes.forEach((node) => {
const groupIds = node.groupId?.split(", ");

switch (node.type) {
case "ellipse":
case "diamond":
Expand All @@ -29,10 +31,27 @@ export const StateToExcalidrawSkeletonConvertor = new GraphConverter({
});
}

if (node.groupId) {
Object.assign(element, {
label: {
...element.label,
groupIds,
},
groupIds,
});
}

elements.push(element);
break;
case "line":
const line = transformToExcalidrawLineSkeleton(node);

if (node.groupId) {
Object.assign(line, {
groupIds,
});
}

elements.push(line);
break;
case "text":
Expand Down Expand Up @@ -67,6 +86,8 @@ export const StateToExcalidrawSkeletonConvertor = new GraphConverter({
return;
}

const groupIds = edge.groupId?.split(", ");

if (endVertex.id?.includes("note") || startVertex.id?.includes("note")) {
arrow.endArrowhead = null;
arrow.strokeStyle = "dashed";
Expand All @@ -81,7 +102,15 @@ export const StateToExcalidrawSkeletonConvertor = new GraphConverter({
type: "rectangle",
};

elements.push(transformToExcalidrawArrowSkeleton(arrow));
const arrowSkeleton = transformToExcalidrawArrowSkeleton(arrow);

if (groupIds) {
Object.assign(arrowSkeleton, {
groupIds,
});
}

elements.push(arrowSkeleton);
});

return { elements };
Expand Down
41 changes: 25 additions & 16 deletions src/parser/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,6 @@ const getRelationElement = (containerEl: Element, id: string) => {
return containerEl.querySelector<SVGSVGElement>(`[data-id="${id}"]`)!;
};

const getDividerId = (id: string): [string, number] => {
const matchDomId = id.includes("divider") ? 2 : 1;
const [identifier, randomId, count] = id.split("-");

const dividerCount = Number(count.split("_")[0]) - matchDomId;

return [`${identifier}-${randomId}-${dividerCount}`, dividerCount];
};

const getDividerElement = (containerEl: Element, id: string) => {
return containerEl.querySelector<SVGSVGElement>(`.clusters > [id*="${id}"]`)!;
};

const computeSpecialType = (specialType: SpecialState): Partial<Container> => {
switch (specialType.type) {
case "choice":
Expand Down Expand Up @@ -277,6 +264,11 @@ const createRelationExcalidrawElement = (
width: relationContainer.width!,
height: relationContainer.height!,
});

relationContainer.groupId = groupId
? `${relationContainer.id}, ${groupId}`
: relationContainer.id;
innerEllipse.groupId += groupId ? `, ${groupId}` : "";
}

if (specialTypes[relation.id]) {
Expand Down Expand Up @@ -361,6 +353,7 @@ const parseDoc = (
{ label: { text: state.description || state.id } }
);

stateElement.groupId += groupId ? `, ${groupId}` : "";
processedNodeRelations.add(state.id);
nodes.push(stateElement);
return;
Expand All @@ -380,6 +373,8 @@ const parseDoc = (
specialTypes,
groupId
);

return;
}

if (isConcurrencyState(state)) {
Expand All @@ -398,7 +393,9 @@ const parseDoc = (
}
);

groupId = `${dividerNode.id}${groupId ? `, ${groupId}` : ""}`;
dividerElement.bgColor = "#e9ecef";
dividerElement.groupId = groupId;

nodes.push(dividerElement);

Expand All @@ -408,8 +405,10 @@ const parseDoc = (
nodes,
processedNodeRelations,
specialTypes,
state.id
groupId
);

groupId = undefined;
return;
}

Expand All @@ -419,6 +418,9 @@ const parseDoc = (
const { clusterElementSkeleton, topLine } =
createClusterExcalidrawElement(clusterElement, containerEl, state);

groupId = `${state.id}${groupId ? `, ${groupId}` : ""}`;
clusterElementSkeleton.groupId = groupId;
topLine.groupId = groupId;
nodes.push(clusterElementSkeleton);
nodes.push(topLine);

Expand All @@ -428,8 +430,10 @@ const parseDoc = (
nodes,
processedNodeRelations,
specialTypes,
state.id
groupId
);

groupId = undefined;
}
});

Expand Down Expand Up @@ -460,8 +464,13 @@ const parseEdges = (nodes: ParsedDoc[], containerEl: Element): any[] => {
);

const clusterHasOwnEdges = clusters?.hasAttribute("transform");
clusterId = `${node.id}${clusterId ? `, ${clusterId}` : ""}`;

const edges = parse(node.doc, clusterHasOwnEdges, clusterId);

clusterId = undefined;

return parse(node.doc, clusterHasOwnEdges, node.id);
return edges;
} else if (node.stmt === "relation") {
const startId = node.state1.id;
const endId = node.state2.id;
Expand Down

0 comments on commit a346e77

Please sign in to comment.