flowchart.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Package ast defines the Abstract Syntax Tree structures for Mermaid diagrams.
  2. // This package mirrors the TypeScript types from mermaid.js for compatibility.
  3. package ast
  4. // DiagramType represents the type of mermaid diagram
  5. type DiagramType string
  6. const (
  7. DiagramTypeFlowchart DiagramType = "flowchart"
  8. DiagramTypeSequence DiagramType = "sequenceDiagram"
  9. DiagramTypeClassDiagram DiagramType = "classDiagram"
  10. DiagramTypeStateDiagram DiagramType = "stateDiagram"
  11. DiagramTypeERDiagram DiagramType = "erDiagram"
  12. DiagramTypeUserJourney DiagramType = "journey"
  13. DiagramTypeTimeline DiagramType = "timeline"
  14. DiagramTypeBlock DiagramType = "block"
  15. DiagramTypeGantt DiagramType = "gantt"
  16. DiagramTypePie DiagramType = "pie"
  17. DiagramTypeQuadrant DiagramType = "quadrantChart"
  18. DiagramTypeRequirement DiagramType = "requirementDiagram"
  19. )
  20. // Diagram is the base interface for all mermaid diagrams
  21. type Diagram interface {
  22. Type() DiagramType
  23. Validate() error
  24. }
  25. // FlowVertexTypeParam represents valid vertex types from flow.jison
  26. // Corresponds to FlowVertexTypeParam in mermaid.js
  27. type FlowVertexTypeParam string
  28. const (
  29. VertexTypeSquare FlowVertexTypeParam = "square"
  30. VertexTypeDoubleCircle FlowVertexTypeParam = "doublecircle"
  31. VertexTypeCircle FlowVertexTypeParam = "circle"
  32. VertexTypeEllipse FlowVertexTypeParam = "ellipse"
  33. VertexTypeStadium FlowVertexTypeParam = "stadium"
  34. VertexTypeSubroutine FlowVertexTypeParam = "subroutine"
  35. VertexTypeRect FlowVertexTypeParam = "rect"
  36. VertexTypeCylinder FlowVertexTypeParam = "cylinder"
  37. VertexTypeRound FlowVertexTypeParam = "round"
  38. VertexTypeDiamond FlowVertexTypeParam = "diamond"
  39. VertexTypeHexagon FlowVertexTypeParam = "hexagon"
  40. VertexTypeOdd FlowVertexTypeParam = "odd"
  41. VertexTypeTrapezoid FlowVertexTypeParam = "trapezoid"
  42. VertexTypeInvTrapezoid FlowVertexTypeParam = "inv_trapezoid"
  43. VertexTypeLeanRight FlowVertexTypeParam = "lean_right"
  44. VertexTypeLeanLeft FlowVertexTypeParam = "lean_left"
  45. VertexTypeFlag FlowVertexTypeParam = "flag"
  46. )
  47. // FlowVertex represents a node in a flowchart
  48. // Mirrors the FlowVertex interface from mermaid.js
  49. type FlowVertex struct {
  50. ID string `json:"id"`
  51. Classes []string `json:"classes"`
  52. Dir *string `json:"dir,omitempty"`
  53. DomID string `json:"domId"`
  54. HaveCallback *bool `json:"haveCallback,omitempty"`
  55. LabelType string `json:"labelType"` // Always "text" in original
  56. Link *string `json:"link,omitempty"`
  57. LinkTarget *string `json:"linkTarget,omitempty"`
  58. Props map[string]any `json:"props,omitempty"`
  59. Styles []string `json:"styles"`
  60. Text *string `json:"text,omitempty"`
  61. Type *FlowVertexTypeParam `json:"type,omitempty"`
  62. Icon *string `json:"icon,omitempty"`
  63. Form *string `json:"form,omitempty"`
  64. Pos *string `json:"pos,omitempty"` // 't' or 'b'
  65. Img *string `json:"img,omitempty"`
  66. AssetWidth *int `json:"assetWidth,omitempty"`
  67. AssetHeight *int `json:"assetHeight,omitempty"`
  68. DefaultWidth *int `json:"defaultWidth,omitempty"`
  69. ImageAspectRatio *float64 `json:"imageAspectRatio,omitempty"`
  70. Constraint *string `json:"constraint,omitempty"` // "on" or "off"
  71. Tooltip *string `json:"tooltip,omitempty"`
  72. OnClick *ClickEvent `json:"onClick,omitempty"`
  73. }
  74. // FlowText represents text content in diagrams
  75. type FlowText struct {
  76. Text string `json:"text"`
  77. Type string `json:"type"` // Always "text"
  78. }
  79. // FlowEdgeStroke represents edge stroke types
  80. type FlowEdgeStroke string
  81. const (
  82. StrokeNormal FlowEdgeStroke = "normal"
  83. StrokeThick FlowEdgeStroke = "thick"
  84. StrokeInvisible FlowEdgeStroke = "invisible"
  85. StrokeDotted FlowEdgeStroke = "dotted"
  86. )
  87. // FlowEdge represents a connection between vertices
  88. // Mirrors the FlowEdge interface from mermaid.js
  89. type FlowEdge struct {
  90. ID string `json:"id,omitempty"`
  91. IsUserDefinedID bool `json:"isUserDefinedId"`
  92. Start string `json:"start"`
  93. End string `json:"end"`
  94. Interpolate *string `json:"interpolate,omitempty"`
  95. Type *string `json:"type,omitempty"`
  96. Stroke *FlowEdgeStroke `json:"stroke,omitempty"`
  97. Style []string `json:"style,omitempty"`
  98. Length *int `json:"length,omitempty"`
  99. Text string `json:"text"`
  100. LabelType string `json:"labelType"` // Always "text"
  101. Classes []string `json:"classes"`
  102. Animation *string `json:"animation,omitempty"` // "fast" or "slow"
  103. Animate *bool `json:"animate,omitempty"`
  104. Pattern *string `json:"pattern,omitempty"`
  105. Tooltip *string `json:"tooltip,omitempty"`
  106. }
  107. // FlowClass represents CSS class definitions
  108. type FlowClass struct {
  109. ID string `json:"id"`
  110. Styles []string `json:"styles"`
  111. TextStyles []string `json:"textStyles"`
  112. }
  113. // FlowSubGraph represents a subgraph container
  114. type FlowSubGraph struct {
  115. ID string `json:"id"`
  116. Classes []string `json:"classes"`
  117. Dir *string `json:"dir,omitempty"`
  118. LabelType string `json:"labelType"`
  119. Nodes []string `json:"nodes"`
  120. Title string `json:"title"`
  121. }
  122. // FlowLink represents link connection details
  123. type FlowLink struct {
  124. Length *int `json:"length,omitempty"`
  125. Stroke string `json:"stroke"`
  126. Type string `json:"type"`
  127. Text *string `json:"text,omitempty"`
  128. }
  129. // Flowchart represents a complete flowchart diagram
  130. // Mirrors the FlowDB state in mermaid.js
  131. type Flowchart struct {
  132. Direction string `json:"direction,omitempty"`
  133. Vertices map[string]*FlowVertex `json:"vertices"`
  134. Edges []*FlowEdge `json:"edges"`
  135. Classes map[string]*FlowClass `json:"classes"`
  136. SubGraphs []*FlowSubGraph `json:"subGraphs"`
  137. SubGraphLookup map[string]*FlowSubGraph `json:"subGraphLookup"`
  138. Tooltips map[string]string `json:"tooltips"`
  139. Version string `json:"version"` // "gen-1" or "gen-2"
  140. }
  141. // Type returns the diagram type
  142. func (f *Flowchart) Type() DiagramType {
  143. return DiagramTypeFlowchart
  144. }
  145. // Validate checks if the flowchart is valid
  146. func (f *Flowchart) Validate() error {
  147. // Basic validation - ensure all edges reference valid vertices
  148. for _, edge := range f.Edges {
  149. if _, exists := f.Vertices[edge.Start]; !exists {
  150. return NewValidationError("edge references non-existent start vertex: " + edge.Start)
  151. }
  152. if _, exists := f.Vertices[edge.End]; !exists {
  153. return NewValidationError("edge references non-existent end vertex: " + edge.End)
  154. }
  155. }
  156. return nil
  157. }
  158. // ValidationError represents a diagram validation error
  159. type ValidationError struct {
  160. Message string
  161. }
  162. func (e *ValidationError) Error() string {
  163. return "validation error: " + e.Message
  164. }
  165. // NewValidationError creates a new validation error
  166. func NewValidationError(message string) *ValidationError {
  167. return &ValidationError{Message: message}
  168. }
  169. // EdgeAnimation represents animation properties for edges
  170. type EdgeAnimation struct {
  171. Duration string `json:"duration,omitempty"`
  172. Delay string `json:"delay,omitempty"`
  173. Easing string `json:"easing,omitempty"`
  174. }
  175. // MarkdownText represents markdown-formatted text
  176. type MarkdownText struct {
  177. Content string `json:"content"`
  178. Bold bool `json:"bold,omitempty"`
  179. Italic bool `json:"italic,omitempty"`
  180. Strike bool `json:"strike,omitempty"`
  181. }
  182. // NewFlowchart creates a new flowchart with default values
  183. func NewFlowchart() *Flowchart {
  184. return &Flowchart{
  185. Vertices: make(map[string]*FlowVertex),
  186. Edges: make([]*FlowEdge, 0),
  187. Classes: make(map[string]*FlowClass),
  188. SubGraphs: make([]*FlowSubGraph, 0),
  189. SubGraphLookup: make(map[string]*FlowSubGraph),
  190. Tooltips: make(map[string]string),
  191. Version: "gen-2",
  192. }
  193. }