d2 icon indicating copy to clipboard operation
d2 copied to clipboard

constant near keys should be able to be set on containers

Open alixander opened this issue 3 years ago • 8 comments

as long as nothing within is connected to anything outside

direction: right

x -> y: {
  style.stroke: green
}

y -> z: {
  style.stroke: red
}

legend: {
  # This is the TODO
  near: bottom-center
  color1: foo {
    shape: text
    style.font-color: green
  }

  color2: bar {
    shape: text
    style.font-color: red
  }
}

alixander avatar Feb 19 '23 07:02 alixander

@donglixiaoche if you want to try a hard one!

alixander avatar Mar 10 '23 05:03 alixander

@alixander yep,a hard one,exactly what I want

ShupingHe avatar Mar 10 '23 05:03 ShupingHe

@donglixiaoche so, for context, you can set near to constant values on individual shapes. it's implemented here: https://github.com/terrastruct/d2/pull/525

but people commonly want to make a legend, which is a container of stuff, and put it in the top-center.

setting constant nears works roughly like:

  1. identify which objects are constant nears
  2. remove them before the main layout engine runs
  3. run main layout engine
  4. add them back in to the appropriate spot

for containers, i think you'll have to

  1. validate in the d2compiler that nothing in the container or descendant of container is connected to anything outside, (because if it is, it has to run in the main layout engine)
  2. remove that container and all descendants of that container, including connections between children of that container
  3. add all those things back in after main layout engine runs

alixander avatar Mar 10 '23 05:03 alixander

@alixander just so you know, i'm still working on this issue...

as you mentioned, for shapes with near keyword, before main layout, i need to remove that container and all descendants of that container. but if having descendants is allowed for container with near keyword, these descandants also need to be layouted, which can't be done if we remove them from the graph. so i was thinking reolve this problem by contrusting a new graph, and then run another main layout? am i correct? or do you have any ideas?

ShupingHe avatar Mar 17 '23 04:03 ShupingHe

@donglixiaoche oh that's a good point, i didn't think about that.

  1. It looks like the core layout is coupled with sequence diagram layouts. that should probably be separated: https://github.com/terrastruct/d2/blob/7ba463c690d5771d42fdf808c1c33adb3b0d35ad/d2lib/d2.go#L73
  2. The descendants would also need to undergo layout, but they themselves might also have near containers, or sequence diagrams. So it should be recursive. Which means this whole portion of layout code should probably be separated so that it can be run recursively: https://github.com/terrastruct/d2/blob/7ba463c690d5771d42fdf808c1c33adb3b0d35ad/d2lib/d2.go#L66-L81 . By doing this, perhaps (1) won't be necessary.

contrusting a new graph, and then run another main layout?

yes, each constant-near-container would have to be its own graph. so you remove all the constant-near-containers, run layout on all of them (recursively), then run on the main graph, then re-insert all the objects and edges back into the main graph.

quite complicated, let me know when you run into anything at all, always available to help

alixander avatar Mar 17 '23 05:03 alixander

@alixander i didn't think about the recursive way cause previously only root level shape could have a near attribute, ok then, let me think about it

ShupingHe avatar Mar 17 '23 06:03 ShupingHe

@donglixiaoche yeah wait actually that is the case right now. sorry got confused with this issue: https://github.com/terrastruct/d2/issues/841

in that case, no need to handle non-root nears. they're still illegal for now

alixander avatar Mar 17 '23 06:03 alixander

Naively, I would expect this to work, i.e. that containers would be laid out hierarchically, from the inside out (and that any connections going out of an inner container to somewhere adapt to that). But specifically I would expect to be able to position a complex object in a corner or an a border of a container.

sitase avatar Oct 10 '25 12:10 sitase