Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Element Access #389

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CeTZ 0.2.0 requires Typst 0.10.0
- Added `radius` style to `rect` for drawing rounded rects
- Added `hide` function for hiding elements
- Added distance, ratio and angle anchors to elements
- Added nested element access via dot separated paths

### Plot
- Added `plot.add-contour(..)` for plotting contour plots
Expand Down
8 changes: 6 additions & 2 deletions src/coordinate.typ
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@

#let resolve-anchor(ctx, c) = {
// (name: <string>, anchor: <number, angle, string> or <none>)
// "name.anchor"
// "{name.}name.anchor"
// "name"
let (name, anchor) = if type(c) == str {
let parts = c.split(".")
if parts.len() == 1 {
(parts.first(), "default")
} else {
(parts.slice(0, -1).join("."), parts.last())
if parts.join(".") in ctx.nodes {
(parts.join("."), "default")
} else {
(parts.slice(0, -1).join("."), parts.last())
}
}
} else {
(c.name, c.at("anchor", default: "default"))
Expand Down
12 changes: 11 additions & 1 deletion src/draw/grouping.typ
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,19 @@
let bounds = none
let drawables = ()
let group-ctx = ctx
group-ctx.groups.push((anchors: (:)))
group-ctx.groups.push((anchors: (:), nodes: (:)))
(ctx: group-ctx, drawables, bounds) = process.many(group-ctx, if type(body) == function {body(ctx)} else {body})

// Append group elements to context
if name != none {
for (elem-name, elem) in group-ctx.groups.last().nodes {
ctx.nodes.insert(name + "." + elem-name, elem)
if ctx.groups != () {
ctx.groups.last().nodes.insert(name + "." + elem-name, elem)
}
}
}

// Apply bounds padding
let bounds = if bounds != none {
let padding = util.as-padding-dict(style.padding)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/plot/legend.typ
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
}

draw.on-layer(style.layer, {
draw.group(name: "legend", padding: style.padding, ctx => {
draw.group(name: "legend-container", padding: style.padding, ctx => {
import draw: *

set-origin(position)
Expand Down Expand Up @@ -173,7 +173,7 @@

// Fill legend background
draw.on-layer(style.layer - .5, {
draw.rect("legend.south-west",
"legend.north-east", fill: style.fill, stroke: style.stroke, radius: style.radius)
draw.rect("legend-container.south-west",
"legend-container.north-east", fill: style.fill, stroke: style.stroke, radius: style.radius)
})
}
3 changes: 3 additions & 0 deletions src/process.typ
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
}
if "name" in element and type(element.name) == "string" and "anchors" in element {
ctx.nodes.insert(element.name, element)
if ctx.groups != () {
ctx.groups.last().nodes.insert(element.name, element)
}
}

if ctx.debug and bounds != none {
Expand Down
Binary file added tests/group/nested-element/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions tests/group/nested-element/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#set page(width: auto, height: auto)
#import "/src/lib.typ": *

#box(stroke: 2pt + red, canvas({
import draw: *

group(name: "parent", {
content((0,0), [Content], name: "content")
group(name: "child", {
circle((1,-2), fill: blue, name: "circle")
})
})

rect("parent.content.south-west",
"parent.content.north-east", stroke: green)
rect((name: "parent.child.circle", anchor: -45deg),
(name: "parent.child.circle", anchor: +135deg), stroke: green)
}))
Loading