Skip to content

feat: add ability to fade multiple - #2509

Open
melsonic wants to merge 12 commits into
d2lang:masterfrom
melsonic:issue-1516
Open

melsonic wants to merge 12 commits into
d2lang:masterfrom
melsonic:issue-1516

Conversation

@melsonic

@melsonic melsonic commented May 4, 2025

Copy link
Copy Markdown
Contributor

Added ability to face the background shape when multiple style is specified.
/fixes #1516

image

@melsonic

Copy link
Copy Markdown
Contributor Author

Hi @alixander, please review this PR once you have time.

Comment thread d2ast/keywords.go Outdated
Comment thread d2ast/keywords.go Outdated
"filled": {},
}

var CompositeStyleKeywords = map[string]string{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, CompositeStyleKeywords is of type map[string]string, but in future if we support more styles under multiple we can convert it to map[string][]string to hold more values under one style.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't need this. for example, the ability to do label.near is implemented, but there's no special map for that.

Comment thread d2compiler/compile.go Outdated
}

func compileCompositeStyleFieldInit(styles *d2graph.Style, f *d2ir.Field, parent string) {
switch f.Name.ScalarString() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I coundn't find an way to fetch parent value for current keyword, so defined a new function instead of using the existing func compileStyleFieldInit, since we needed an extra parameter holding the value of parent. We can use the existing function also, but have to include a 3rd parameter parent.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the other comment, since nested keywords are already implemented elsewhere, you should question why you'd need a new function for this case.

Comment thread d2graph/d2graph.go Outdated
return nil
}

func (s *Style) ApplyComposite(key, value, parent string) error {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to compileStyleFieldInit, we need an extra paramater holding the parent value here also, so created a new func ApplyComposite.

@melsonic
melsonic requested a review from alixander May 17, 2025 06:19
@melsonic

Copy link
Copy Markdown
Contributor Author

Hi @alixander, please review!

@alixander alixander left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big apologies for the delay @melsonic . Had a personal matter for a couple weeks but I'm 100% back now.

Comment thread d2ast/keywords.go Outdated
"filled": {},
}

var CompositeStyleKeywords = map[string]string{

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't need this. for example, the ability to do label.near is implemented, but there's no special map for that.

Comment thread d2compiler/compile.go Outdated
}

func compileCompositeStyleFieldInit(styles *d2graph.Style, f *d2ir.Field, parent string) {
switch f.Name.ScalarString() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the other comment, since nested keywords are already implemented elsewhere, you should question why you'd need a new function for this case.

@melsonic

Copy link
Copy Markdown
Contributor Author

Big apologies for the delay @melsonic . Had a personal matter for a couple weeks but I'm 100% back now.

Hey @alixander, no worries at all. I noticed you were inactive for a couple of weeks, I just put the comment to notify you once you cone back.

And welcome back btw : )

@melsonic

Copy link
Copy Markdown
Contributor Author

Regarding the comments, I'll look into it and come up with an efficient solution.

@melsonic
melsonic requested a review from alixander June 28, 2025 09:03

@alixander alixander left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the delay @melsonic .

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test results aren't right. this shouldn't have changed

Comment thread d2compiler/compile.go
Comment on lines +760 to +786
if f.Map() != nil {
for _, ff := range f.Map().Fields {
if ff.Name.ScalarString() == "opacity" && ff.Name.IsUnquoted() {
if ff.Primary() == nil {
c.errorf(ff.LastPrimaryKey(), `invalid "opacity" field`)
} else {
scalar := ff.Primary().Value
switch f.Name.ScalarString() {
case "multiple":
styles.MultipleOpacity = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
err := styles.Apply(f.Name.ScalarString()+ff.Name.ScalarString(), scalar.ScalarString())
if err != nil {
c.errorf(scalar, err.Error())
return
}
default:
c.errorf(f.Name, `invalid "opacity" style for "%s"`, f.Name.ScalarString())
return
}
}
} else {
if ff.LastPrimaryKey() != nil {
c.errorf(ff.LastPrimaryKey(), `unexpected field %s`, ff.Name.ScalarString())
}
}
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be a code smell to you that you need this much extra code. There's a more precise place to make these changes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can simplify the process by modifying
https://github.com/terrastruct/d2/blob/b2841d586f6c2b238b677b4043f7c8cadfc5f266/d2compiler/compile.go#L788-L792
as:

func (c *compiler) compileStyle(styles *d2graph.Style, m *d2ir.Map) {
	for _, f := range m.Fields {
		c.compileStyleField(styles, f)
		if f.Map() != nil {
			for _, ff := range f.Map().Fields {
				c.compileStyleField(styles, ff)
			}
		}
	}
}

However, as it’s not possible to get the parent field of a particular field, we need to pass a new parameter to
https://github.com/terrastruct/d2/blob/b2841d586f6c2b238b677b4043f7c8cadfc5f266/d2compiler/compile.go#L794
to identify the parent field for opacity.

Comment thread d2graph/d2graph.go
return fmt.Errorf(`expected "text-transform" to be one of (%s)`, strings.Join(d2ast.TextTransforms, ", "))
}
s.TextTransform.Value = value
case "multipleopacity":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does this come from?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only have access to the key and value in the Apply function, if we pass only the "opacity" keyword as the key, there’s no way to distinguish whether it specifies the global-level opacity or multiple opacity.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

faded multiple

3 participants