Skip to content

Commit

Permalink
feat: support horizontal or vertical layout
Browse files Browse the repository at this point in the history
  • Loading branch information
abrgr committed Aug 31, 2023
1 parent 7886894 commit bc945c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/components/flow-graph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Action =
export type FlowGraphProps = {
flow: DrawableFlow;
selectedItems?: Array<FlowItemIdentifier>;
direction?: "horizontal" | "vertical";
editable?: {
getAvailableStates: () => Array<{ id: schema.StateId; name: string }>;
onUpdateTransitionTarget: (
Expand Down Expand Up @@ -125,6 +126,7 @@ export const FlowGraph = ({
selectedItems: providedSelectedItems,
editable,
header,
direction = "vertical",
renderButtons,
}: FlowGraphProps) => {
const selectedItems = providedSelectedItems ?? emptySelectedItems;
Expand Down Expand Up @@ -202,7 +204,12 @@ export const FlowGraph = ({
}

const thisLayoutId = nextLayoutId();
const graph = flowToElkGraph(sizeMap.current!, rootId, fullFlow);
const graph = flowToElkGraph(
sizeMap.current!,
rootId,
fullFlow,
direction
);
const elk = getElk();
elk.layout(graph).then((layout) => {
dispatch({
Expand Down
31 changes: 19 additions & 12 deletions src/transformers/elk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ export type EnrichedElkNode = ElkNode & {
children?: EnrichedElkNode[];
};

const rootNodeLayoutOptions = {
const rootNodeLayoutOptions = (direction: "horizontal" | "vertical") => ({
"elk.hierarchyHandling": "INCLUDE_CHILDREN",
"elk.algorithm": "layered",
"elk.layered.considerModelOrder": "NODES_AND_EDGES",
"elk.layered.wrapping.strategy": "MULTI_EDGE",
"elk.aspectRatio": "0.5",
"elk.direction": "DOWN",
"elk.aspectRatio": direction === "vertical" ? "0.5" : "2",
"elk.direction": direction === "vertical" ? "DOWN" : "RIGHT",
"elk.layered.spacing.baseValue": "25",
//"elk.layered.compaction.postCompaction.strategy": "TOP", // TODO: this may fail
};
});

const nodeLayoutOptions = {
const nodeLayoutOptions = (direction: "horizontal" | "vertical") => ({
"elk​.alignment": "CENTER",
"elk.aspectRatio": "0.5",
"elk.direction": "DOWN",
"elk.aspectRatio": direction === "vertical" ? "0.5" : "2",
"elk.direction": direction === "vertical" ? "DOWN" : "RIGHT",
"elk.layered.edgeRouting.selfLoopOrdering": "SEQUENCED",
"elk.layered.edgeRouting.selfLoopDistribution": "EQUALLY",
"elk.layered.spacing.baseValue": "25",
//"elk.layered.compaction.postCompaction.strategy": "TOP", // TODO: this may fail
};
});

export type PositionedItemId = string & { _BRAND: "positionedItem" };

Expand Down Expand Up @@ -168,15 +168,18 @@ export type Size = {
export function flowToElkGraph(
sizeMap: Map<PositionedItemId, Size>,
rootId: schema.StateId,
flow: DrawableFlowWithTopLevelState
flow: DrawableFlowWithTopLevelState,
direction: "horizontal" | "vertical"
): EnrichedElkNode {
const edgeIdsByTarget = getEdgeIdsByTarget(flow.states);
const node = _flowToElkGraph(
sizeMap,
rootId,
edgeIdsByTarget,
flow.states,
rootId
rootId,
rootNodeLayoutOptions(direction),
nodeLayoutOptions(direction)
)[0];
return node;
}
Expand Down Expand Up @@ -230,7 +233,9 @@ export function _flowToElkGraph(
rootId: schema.StateId,
edgeIdsByTarget: EdgeIdsByTarget,
states: DrawableFlow["states"],
stateId: schema.StateId
stateId: schema.StateId,
rootNodeLayoutOptions: Record<string, string>,
nodeLayoutOptions: Record<string, string>
): [EnrichedElkNode, Set<string>] {
const childNodes = Object.entries(states)
.filter(([_stateId, state]) => state!.parent === stateId)
Expand Down Expand Up @@ -318,7 +323,9 @@ export function _flowToElkGraph(
rootId,
edgeIdsByTarget,
states,
stateId
stateId,
rootNodeLayoutOptions,
nodeLayoutOptions
);

descendants = union(descendants, nodes);
Expand Down

0 comments on commit bc945c6

Please sign in to comment.