Skip to content

Commit

Permalink
Add caps-lock attribute to the style block
Browse files Browse the repository at this point in the history
This allows overriding the functionality provided by the theme, and
turning off caps lock. By providing `caps-lock: true` this will force
the text to uppercase.

 - Introduced a new object `d2graph.Boolean` to store the bool for the
 `caps-lock` option.
- Introduced 2 helper methods `IsCapsLocked()` and `IsNotCapsLocked()`
  to avoid the duplication of the nil & boolean checks.
  • Loading branch information
alexstoick committed Mar 31, 2023
1 parent a53eb48 commit 0188918
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
2 changes: 2 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ func compileStyleFieldInit(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.Left = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "double-border":
attrs.Style.DoubleBorder = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
case "caps-lock":
attrs.Style.CapsLock = &d2graph.Boolean{MapKey: f.LastPrimaryKey()}
}
}

Expand Down
78 changes: 55 additions & 23 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ type Scalar struct {
MapKey *d2ast.Key `json:"-"`
}

// Boolean type - which holds a Value of type bool.
type Boolean struct {
Value bool `json:"value"`
MapKey *d2ast.Key `json:"-"`
}

// TODO maybe rename to Shape
type Object struct {
Graph *Graph `json:"-"`
Expand Down Expand Up @@ -151,25 +157,34 @@ func (r Reference) InEdge() bool {
}

type Style struct {
Opacity *Scalar `json:"opacity,omitempty"`
Stroke *Scalar `json:"stroke,omitempty"`
Fill *Scalar `json:"fill,omitempty"`
FillPattern *Scalar `json:"fillPattern,omitempty"`
StrokeWidth *Scalar `json:"strokeWidth,omitempty"`
StrokeDash *Scalar `json:"strokeDash,omitempty"`
BorderRadius *Scalar `json:"borderRadius,omitempty"`
Shadow *Scalar `json:"shadow,omitempty"`
ThreeDee *Scalar `json:"3d,omitempty"`
Multiple *Scalar `json:"multiple,omitempty"`
Font *Scalar `json:"font,omitempty"`
FontSize *Scalar `json:"fontSize,omitempty"`
FontColor *Scalar `json:"fontColor,omitempty"`
Animated *Scalar `json:"animated,omitempty"`
Bold *Scalar `json:"bold,omitempty"`
Italic *Scalar `json:"italic,omitempty"`
Underline *Scalar `json:"underline,omitempty"`
Filled *Scalar `json:"filled,omitempty"`
DoubleBorder *Scalar `json:"doubleBorder,omitempty"`
Opacity *Scalar `json:"opacity,omitempty"`
Stroke *Scalar `json:"stroke,omitempty"`
Fill *Scalar `json:"fill,omitempty"`
FillPattern *Scalar `json:"fillPattern,omitempty"`
StrokeWidth *Scalar `json:"strokeWidth,omitempty"`
StrokeDash *Scalar `json:"strokeDash,omitempty"`
BorderRadius *Scalar `json:"borderRadius,omitempty"`
Shadow *Scalar `json:"shadow,omitempty"`
ThreeDee *Scalar `json:"3d,omitempty"`
Multiple *Scalar `json:"multiple,omitempty"`
Font *Scalar `json:"font,omitempty"`
FontSize *Scalar `json:"fontSize,omitempty"`
FontColor *Scalar `json:"fontColor,omitempty"`
Animated *Scalar `json:"animated,omitempty"`
Bold *Scalar `json:"bold,omitempty"`
Italic *Scalar `json:"italic,omitempty"`
Underline *Scalar `json:"underline,omitempty"`
Filled *Scalar `json:"filled,omitempty"`
DoubleBorder *Scalar `json:"doubleBorder,omitempty"`
CapsLock *Boolean `json:"capsLock,omitempty"`
}

func (s Style) IsNotCapsLocked() bool {
return s.CapsLock != nil && !s.CapsLock.Value
}

func (s Style) IsCapsLocked() bool {
return s.CapsLock != nil && s.CapsLock.Value
}

func (s *Style) Apply(key, value string) error {
Expand Down Expand Up @@ -340,6 +355,15 @@ func (s *Style) Apply(key, value string) error {
return errors.New(`expected "double-border" to be true or false`)
}
s.DoubleBorder.Value = value
case "caps-lock":
if s.CapsLock == nil {
break
}
b, err := strconv.ParseBool(value)
if err != nil {
return errors.New(`expected "caps-lock" to be true or false`)
}
s.CapsLock.Value = b
default:
return fmt.Errorf("unknown style key: %s", key)
}
Expand Down Expand Up @@ -1305,10 +1329,13 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler
}

if g.Theme != nil && g.Theme.SpecialRules.CapsLock && !strings.EqualFold(obj.Attributes.Shape.Value, d2target.ShapeCode) {
if obj.Attributes.Language != "latex" {
if obj.Attributes.Language != "latex" && !obj.Attributes.Style.IsNotCapsLocked() {
obj.Attributes.Label.Value = strings.ToUpper(obj.Attributes.Label.Value)
}
}
if obj.Attributes.Style.IsCapsLocked() {
obj.Attributes.Label.Value = strings.ToUpper(obj.Attributes.Label.Value)
}

labelDims, err := obj.GetLabelSize(mtexts, ruler, fontFamily)
if err != nil {
Expand Down Expand Up @@ -1427,7 +1454,8 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler
continue
}

if g.Theme != nil && g.Theme.SpecialRules.CapsLock {
if (g.Theme != nil && g.Theme.SpecialRules.CapsLock && !edge.Attributes.Style.IsNotCapsLocked()) ||
edge.Attributes.Style.IsCapsLocked() {
edge.Attributes.Label.Value = strings.ToUpper(edge.Attributes.Label.Value)
}
usedFont := fontFamily
Expand Down Expand Up @@ -1457,10 +1485,13 @@ func (g *Graph) Texts() []*d2target.MText {
if obj.Attributes.Label.Value != "" {
text := obj.Text()
if capsLock && !strings.EqualFold(obj.Attributes.Shape.Value, d2target.ShapeCode) {
if obj.Attributes.Language != "latex" {
if obj.Attributes.Language != "latex" && !obj.Attributes.Style.IsNotCapsLocked() {
text.Text = strings.ToUpper(text.Text)
}
}
if obj.Attributes.Style.IsCapsLocked() {
text.Text = strings.ToUpper(text.Text)
}
texts = appendTextDedup(texts, text)
}
if obj.Class != nil {
Expand Down Expand Up @@ -1489,9 +1520,10 @@ func (g *Graph) Texts() []*d2target.MText {
for _, edge := range g.Edges {
if edge.Attributes.Label.Value != "" {
text := edge.Text()
if capsLock {
if (capsLock && !edge.Attributes.Style.IsNotCapsLocked()) || edge.Attributes.Style.IsCapsLocked() {
text.Text = strings.ToUpper(text.Text)
}

texts = appendTextDedup(texts, text)
}
if edge.SrcArrowhead != nil && edge.SrcArrowhead.Label.Value != "" {
Expand Down

0 comments on commit 0188918

Please sign in to comment.