This commit is contained in:
donglixiaoche 2023-03-10 09:54:35 +08:00
parent 77e2a23b2b
commit 1cb1fd4344
No known key found for this signature in database
GPG key ID: F235CD35048B3752
4 changed files with 28 additions and 25 deletions

View file

@ -11,13 +11,16 @@ import (
"oss.terrastruct.com/d2/lib/svg"
)
func classHeader(labelMaskID string, shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
func classHeader(diagramHash string, shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
rectEl := d2themes.NewThemableElement("rect")
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
rectEl.Width, rectEl.Height = box.Width, box.Height
rectEl.Fill = shape.Fill
rectEl.ClassName = "class_header"
str := rectEl.RenderWithClipPath(fmt.Sprintf("%v-%v", labelMaskID, shape.ID), shape.BorderRadius != 0)
if shape.BorderRadius != 0 {
rectEl.ClipPath = fmt.Sprintf("%v-%v", diagramHash, shape.ID)
}
str := rectEl.Render()
if text != "" {
tl := label.InsideMiddleCenter.GetPointOnBox(
@ -81,7 +84,7 @@ func classRow(shape d2target.Shape, box *geo.Box, prefix, nameText, typeText str
return out
}
func drawClass(writer io.Writer, labelMaskID string, targetShape d2target.Shape) {
func drawClass(writer io.Writer, diagramHash string, targetShape d2target.Shape) {
el := d2themes.NewThemableElement("rect")
el.X = float64(targetShape.Pos.X)
el.Y = float64(targetShape.Pos.Y)
@ -104,7 +107,7 @@ func drawClass(writer io.Writer, labelMaskID string, targetShape d2target.Shape)
headerBox := geo.NewBox(box.TopLeft, box.Width, 2*rowHeight)
fmt.Fprint(writer,
classHeader(labelMaskID, targetShape, headerBox, targetShape.Label, float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
classHeader(diagramHash, targetShape, headerBox, targetShape.Label, float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
)
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)

View file

@ -865,7 +865,7 @@ func render3dHexagon(targetShape d2target.Shape) string {
return borderMask + mainShapeRendered + renderedSides + renderedBorder
}
func drawShape(writer io.Writer, labelMaskID string, targetShape d2target.Shape, sketchRunner *d2sketch.Runner) (labelMask string, err error) {
func drawShape(writer io.Writer, diagramHash string, targetShape d2target.Shape, sketchRunner *d2sketch.Runner) (labelMask string, err error) {
closingTag := "</g>"
if targetShape.Link != "" {
@ -880,7 +880,7 @@ func drawShape(writer io.Writer, labelMaskID string, targetShape d2target.Shape,
// this clipPath must be defined outside `g` element
if targetShape.BorderRadius != 0 && (targetShape.Type == d2target.ShapeClass || targetShape.Type == d2target.ShapeSQLTable) {
fmt.Fprint(writer, clipPathForBorderRadius(labelMaskID, targetShape))
fmt.Fprint(writer, clipPathForBorderRadius(diagramHash, targetShape))
}
fmt.Fprintf(writer, `<g id="%s"%s>`, svg.EscapeText(targetShape.ID), opacityStyle)
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
@ -925,7 +925,7 @@ func drawShape(writer io.Writer, labelMaskID string, targetShape d2target.Shape,
}
fmt.Fprint(writer, out)
} else {
drawClass(writer, labelMaskID, targetShape)
drawClass(writer, diagramHash, targetShape)
}
addAppendixItems(writer, targetShape)
fmt.Fprint(writer, `</g>`)
@ -939,7 +939,7 @@ func drawShape(writer io.Writer, labelMaskID string, targetShape d2target.Shape,
}
fmt.Fprint(writer, out)
} else {
drawTable(writer, labelMaskID, targetShape)
drawTable(writer, diagramHash, targetShape)
}
addAppendixItems(writer, targetShape)
fmt.Fprint(writer, `</g>`)

View file

@ -13,7 +13,7 @@ import (
)
// this func helps define a clipPath for shape class and sql_table to draw border-radius
func clipPathForBorderRadius(labelMaskID string, shape d2target.Shape) string {
func clipPathForBorderRadius(diagramHash string, shape d2target.Shape) string {
box := geo.NewBox(
geo.NewPoint(float64(shape.Pos.X), float64(shape.Pos.Y)),
float64(shape.Width),
@ -21,7 +21,7 @@ func clipPathForBorderRadius(labelMaskID string, shape d2target.Shape) string {
)
topX, topY := box.TopLeft.X+box.Width, box.TopLeft.Y
out := fmt.Sprintf(`<clipPath id="%v-%v">`, labelMaskID, shape.ID)
out := fmt.Sprintf(`<clipPath id="%v-%v">`, diagramHash, shape.ID)
out += fmt.Sprintf(`<path d="M %f %f L %f %f S %f %f %f %f `, box.TopLeft.X, box.TopLeft.Y+float64(shape.BorderRadius), box.TopLeft.X, box.TopLeft.Y+float64(shape.BorderRadius), box.TopLeft.X, box.TopLeft.Y, box.TopLeft.X+float64(shape.BorderRadius), box.TopLeft.Y)
out += fmt.Sprintf(`L %f %f L %f %f `, box.TopLeft.X+box.Width-float64(shape.BorderRadius), box.TopLeft.Y, topX-float64(shape.BorderRadius), topY)
@ -40,13 +40,16 @@ func clipPathForBorderRadius(labelMaskID string, shape d2target.Shape) string {
return out + `fill="none" /> </clipPath>`
}
func tableHeader(labelMaskID string, shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
func tableHeader(diagramHash string, shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
rectEl := d2themes.NewThemableElement("rect")
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
rectEl.Width, rectEl.Height = box.Width, box.Height
rectEl.Fill = shape.Fill
rectEl.ClassName = "class_header"
str := rectEl.RenderWithClipPath(fmt.Sprintf("%v-%v", labelMaskID, shape.ID), shape.BorderRadius != 0)
if shape.BorderRadius != 0 {
rectEl.ClipPath = fmt.Sprintf("%v-%v", diagramHash, shape.ID)
}
str := rectEl.Render()
if text != "" {
tl := label.InsideMiddleLeft.GetPointOnBox(
@ -110,7 +113,7 @@ func tableRow(shape d2target.Shape, box *geo.Box, nameText, typeText, constraint
return out
}
func drawTable(writer io.Writer, labelMaskID string, targetShape d2target.Shape) {
func drawTable(writer io.Writer, diagramHash string, targetShape d2target.Shape) {
rectEl := d2themes.NewThemableElement("rect")
rectEl.X = float64(targetShape.Pos.X)
rectEl.Y = float64(targetShape.Pos.Y)
@ -134,7 +137,7 @@ func drawTable(writer io.Writer, labelMaskID string, targetShape d2target.Shape)
headerBox := geo.NewBox(box.TopLeft, box.Width, rowHeight)
fmt.Fprint(writer,
tableHeader(labelMaskID, targetShape, headerBox, targetShape.Label,
tableHeader(diagramHash, targetShape, headerBox, targetShape.Label,
float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
)

View file

@ -3,7 +3,6 @@ package d2themes
import (
"fmt"
"math"
"strings"
"oss.terrastruct.com/d2/lib/color"
)
@ -46,7 +45,8 @@ type ThemableElement struct {
Style string
Attributes string
Content string
Content string
ClipPath string
}
func NewThemableElement(tag string) *ThemableElement {
@ -85,6 +85,7 @@ func NewThemableElement(tag string) *ThemableElement {
"",
"",
"",
"",
}
}
@ -101,15 +102,6 @@ func (el *ThemableElement) SetMaskUrl(url string) {
el.Mask = fmt.Sprintf("url(#%s)", url)
}
func (el *ThemableElement) RenderWithClipPath(id string, shouldAddClipPath bool) string {
out := el.Render()
if shouldAddClipPath {
out = strings.Replace(out, "/>", fmt.Sprintf(`clip-path="url(#%v)" />`, id), 1)
}
return out
}
func (el *ThemableElement) Render() string {
out := "<" + el.tag
@ -211,8 +203,13 @@ func (el *ThemableElement) Render() string {
out += fmt.Sprintf(` %s`, el.Attributes)
}
if len(el.ClipPath) > 0 {
out += fmt.Sprintf(` clip-path="url(#%s)"`, el.ClipPath)
}
if len(el.Content) > 0 {
return fmt.Sprintf("%s>%s</%s>", out, el.Content, el.tag)
}
return out + " />"
}