flowchart.go 8.4 KB

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