flowchart.mdx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Flowchart Parser
  2. As mentioned in the previous section, we use [getDiagramFromText](https://github.com/mermaid-js/mermaid/blob/00d06c7282a701849793680c1e97da1cfdfcce62/packages/mermaid/src/Diagram.ts#L80) to parse the full defination and get the [Diagram](https://github.com/mermaid-js/mermaid/blob/00d06c7282a701849793680c1e97da1cfdfcce62/packages/mermaid/src/Diagram.ts#L15) json from it.
  3. In this section we will be diving into how the [flowchart parser](https://github.com/excalidraw/mermaid-to-excalidraw/blob/master/src/parser/flowchart.ts#L256) works behind the scenes.
  4. ![image](https://github.com/excalidraw/excalidraw/assets/11256141/2a097bbb-64bf-49d6-bf7f-21172bdb538d)
  5. We use `diagram.parser.yy` attribute to parse the data. If you want to know more about how the `diagram.parse.yy` attribute looks like, you can check it [here](https://github.com/mermaid-js/mermaid/blob/00d06c7282a701849793680c1e97da1cfdfcce62/packages/mermaid/src/diagrams/flowchart/flowDb.js#L768), however for scope of flowchart we are using **3** APIs from this parser to compute `vertices`, `edges` and `clusters` as we need these data to transform to [ExcalidrawElementSkeleton](https://github.com/excalidraw/excalidraw/blob/master/src/data/transform.ts#L133C13-L133C38).
  6. For computing `vertices` and `edge`s lets consider the below svg generated by mermaid
  7. ![image](https://github.com/excalidraw/excalidraw/assets/11256141/d7013305-0b90-4fa0-a66e-b4f4604ad0b2)
  8. ## Computing the vertices
  9. We use `getVertices` API from `diagram.parse.yy` to get the vertices for a given flowchart.
  10. Considering the same example this is the response from the API
  11. ```js
  12. {
  13. "start": {
  14. "id": "start",
  15. "labelType": "text",
  16. "domId": "flowchart-start-1414",
  17. "styles": [],
  18. "classes": [],
  19. "text": "start",
  20. "props": {}
  21. },
  22. "stop": {
  23. "id": "stop",
  24. "labelType": "text",
  25. "domId": "flowchart-stop-1415",
  26. "styles": [],
  27. "classes": [],
  28. "text": "stop",
  29. "props": {}
  30. }
  31. }
  32. ```
  33. The dimensions and position is missing in this response and we need that to transform to [ExcalidrawElementSkeleton](https://github.com/excalidraw/excalidraw/blob/master/src/data/transform.ts#L133C13-L133C38), for this we have our own parser [`parseVertex`](https://github.com/excalidraw/mermaid-to-excalidraw/blob/master/src/parseMermaid.ts#L178) which takes the above response and uses the `svg` together to compute position, dimensions and cleans up the response.
  34. The final output from `parseVertex` looks like :point_down:
  35. ```js
  36. {
  37. "start": {
  38. "id": "start",
  39. "labelType": "text",
  40. "text": "start",
  41. "x": 0,
  42. "y": 0,
  43. "width": 67.796875,
  44. "height": 44,
  45. "containerStyle": {},
  46. "labelStyle": {}
  47. },
  48. "stop": {
  49. "id": "stop",
  50. "labelType": "text",
  51. "text": "stop",
  52. "x": 117.796875,
  53. "y": 0,
  54. "width": 62.3828125,
  55. "height": 44,
  56. "containerStyle": {},
  57. "labelStyle": {}
  58. }
  59. }
  60. ```
  61. ## Computing the edges
  62. The lines and arrows are considered as `edges` in mermaid as shown in the above diagram.
  63. We use `getEdges` API from `diagram.parse.yy` to get the edges for a given flowchart.
  64. Considering the same example this is the response from the API
  65. ```js
  66. [
  67. {
  68. "start": "start",
  69. "end": "stop",
  70. "type": "arrow_point",
  71. "text": "",
  72. "labelType": "text",
  73. "stroke": "normal",
  74. "length": 1
  75. }
  76. ]
  77. ```
  78. Similarly here the dimensions and position is missing and we compute that from the svg. The [`parseEdge`](https://github.com/excalidraw/mermaid-to-excalidraw/blob/master/src/parseMermaid.ts#L245) takes the above response along with `svg` and computes the position, dimensions and cleans up the response.
  79. The final output from `parseEdge` looks like :point_down:
  80. ```js
  81. [
  82. {
  83. "start": "start",
  84. "end": "stop",
  85. "type": "arrow_point",
  86. "text": "",
  87. "labelType": "text",
  88. "stroke": "normal",
  89. "startX": 67.797,
  90. "startY": 22,
  91. "endX": 117.797,
  92. "endY": 22,
  93. "reflectionPoints": [
  94. {
  95. "x": 67.797,
  96. "y": 22
  97. },
  98. {
  99. "x": 117.797,
  100. "y": 22
  101. }
  102. ]
  103. }
  104. ]
  105. ```
  106. ## Computing the Subgraphs
  107. `Subgraphs` is collection of elements grouped together. The Subgraphs map to `grouping` elements in Excalidraw.
  108. Lets consider the below example :point_down:
  109. ![image](https://github.com/excalidraw/excalidraw/assets/11256141/5243ce4c-beaa-43d2-812a-0577b0a574d7)
  110. We use `getSubgraphs` API to get the subgraph data for a given flowchart.
  111. Considering the same example this is the response from the API
  112. ```js
  113. [
  114. {
  115. "id": "one",
  116. "nodes": [
  117. "flowchart-a2-1399",
  118. "flowchart-a1-1400"
  119. ],
  120. "title": "one",
  121. "classes": [],
  122. "labelType": "text"
  123. }
  124. ]
  125. ```
  126. For position and dimensions we use the svg to compute. The [`parseSubgraph`](https://github.com/excalidraw/mermaid-to-excalidraw/blob/master/src/parseMermaid.ts#L139) takes the above response along with `svg` and computes the position, dimensions and cleans up the response.
  127. ```js
  128. [
  129. {
  130. "id": "one",
  131. "nodes": [
  132. "flowchart-a2-1399",
  133. "flowchart-a1-1400"
  134. ],
  135. "title": "one",
  136. "labelType": "text",
  137. "nodeIds": [
  138. "a2",
  139. "a1"
  140. ],
  141. "x": 75.4921875,
  142. "y": 0,
  143. "width": 121.25,
  144. "height": 188,
  145. "text": "one"
  146. }
  147. ]
  148. ```