diff --git a/examples/main.go b/examples/main.go index f82c222..70f11a1 100644 --- a/examples/main.go +++ b/examples/main.go @@ -6,7 +6,34 @@ func main() { userStartsChatting() userBIsNotPartOfChat() userBIsPartOfChat() + userJoinsChat() +} + +func userJoinsChat() { + d := sequence.NewDiagram("new_chat") + userA := "userA (name could be quite longer)" + userB := "userB" + b := "backend" + + d.SetTitle("User starts a chat with someone (doesn't matter if new or not)") + + d.AddParticipants(userA, userB, b) + + _ = d.AddDirectionalEdge(userA, b, `POST /inbox {"recipient": userID, "project": projectID, "projectType": challenge|solution}"`) + _ = d.AddDirectionalEdge(b, userA, "Response Status Code: 202, Body: Inbox{id, secret, recipient, ...}") + _ = d.AddDirectionalEdge(userA, b, "WS connect") + _ = d.AddDirectionalEdge(userA, b, `"{"action": "JOIN", "data": {"inboxId": str, "userId": "str", "inboxSecret": str}}"`) + _ = d.AddDirectionalEdge(b, b, "STORE_CONNECTION_ID(userA)") + _ = d.AddDirectionalEdge(b, b, "sets unread inbox userA = 0") + _ = d.AddDirectionalEdge(b, userA, `{"event": "new_message", "messages": [{"id": str, "inboxId": str, "content": str, "userId": str, "createdAt": str}]}`) + _ = d.AddDirectionalEdge(userA, b, `"{"action": "SEND_MESSAGE", "data": {"inboxId": str, "userId": "str", "inboxSecret": str, "content": "hi"}}"`) + _ = d.AddDirectionalEdge(b, b, "check connectionID userB") + _ = d.AddUndirectionalEdge(b, b, "if connected: ") + _ = d.AddDirectionalEdge(b, userB, `{"event": "new_message", "messages": [{"id": str, "inboxId": str, "content": str, "userId": str, "createdAt": str}]}`) + _ = d.AddUndirectionalEdge(b, b, "else: ") + _ = d.AddUndirectionalEdge(b, b, "increment unread inbox userB") + d.Render() } func userBIsPartOfChat() { @@ -71,6 +98,7 @@ func userStartsChatting() { d.AddParticipants(backend) d.AddParticipants(db) + d.AddDirectionalEdge(client, backend, "GET /inbox/") d.AddDirectionalEdge(client, backend, "PUT /chat/user/") d.AddDirectionalEdge(backend, db, "checks or create inbox for user") d.AddDirectionalEdge(backend, db, "set all unread messages to read if existing") diff --git a/sequence/diagram.go b/sequence/diagram.go index c0eafb6..184abac 100644 --- a/sequence/diagram.go +++ b/sequence/diagram.go @@ -78,10 +78,11 @@ func (d *Diagram) renderParticipants() { } } spacePerBlock := float64(d.dc.Width() / len(d.participants)) - startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding - // startX := float64(participantsPadding + (len(d.renderedParticipants) * (participantBoxWidth + 1000/(len(d.participants))))) - endX := startX + participantBoxWidth - startY := 1000 * 0.1 // 10% from the top + strWidth, strHeight := d.dc.MeasureString(p.Name) + startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding - strWidth/2 + + endX := startX + participantBoxWidth + strWidth + startY := height * 0.1 // 10% from the top endY := startY + participantBoxHeight // draw the border d.dc.SetColor(color.Black) @@ -104,11 +105,10 @@ func (d *Diagram) renderParticipants() { d.dc.DrawRectangle( startX, startY, - participantBoxWidth, + participantBoxWidth+strWidth, participantBoxHeight, ) d.dc.SetColor(color.Black) - strWidth, strHeight := d.dc.MeasureString(p.Name) centerStrWidth := startX + ((endX - startX) / 2) - strWidth/2 centerStrHeight := (endY-startY)/2 + startY + (strHeight / 2) @@ -141,11 +141,13 @@ func (d *Diagram) renderEdges() { for idx := range d.edges { e := &d.edges[idx] + fromStrWidth, _ := d.dc.MeasureString(e.from.Name) + toStrWidth, _ := d.dc.MeasureString(e.to.Name) fromCords := d.participantsCoordMap[e.from.Name] toCords := d.participantsCoordMap[e.to.Name] - startX := fromCords.X + participantBoxWidth/2 - 2.5 // 2.5 = half of stroke width + startX := fromCords.X + participantBoxWidth/2 - 2.5 + fromStrWidth/2 // 2.5 = half of stroke width startY := fromCords.Y + participantBoxHeight + 2.5 + float64((1+renderedEdges)*verticalSpaceBetweenEdges) - endX := toCords.X + participantBoxWidth/2 - 2.5 + endX := toCords.X + participantBoxWidth/2 - 2.5 + toStrWidth/2 isReverseEdge := endX < startX d.dc.SetDash(6)