flowchart.go 9.1 KB

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