Skip to content

Commit

Permalink
add data-compartment attribute to svg output
Browse files Browse the repository at this point in the history
  • Loading branch information
skanaar committed Dec 14, 2024
1 parent 6987ba9 commit d76a085
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/GraphicsSvg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function GraphicsSvg(document?: HTMLDocument): ISvgGraphics {
return this.parent
}
serialize(): string {
const data = getDefined(this.group(), (e) => e.data) ?? {}
const data = getAncestorData(this.group()) ?? {}
const attrs = toAttrString({ ...this.attr, ...data })
const content = this.children.map((o) => o.serialize()).join('\n')
if (this.text && this.children.length === 0)
Expand All @@ -116,6 +116,11 @@ export function GraphicsSvg(document?: HTMLDocument): ISvgGraphics {
}
}

function getAncestorData(group: GroupElement | undefined): Record<string, string> | undefined {
if (!group) return syntheticRoot.data
return { ...getAncestorData(group.parent), ...group.data }
}

function getDefined<T>(
group: GroupElement | undefined,
getter: (e: GroupElement) => T | undefined
Expand All @@ -129,7 +134,7 @@ export function GraphicsSvg(document?: HTMLDocument): ISvgGraphics {
super('g', {}, parent)
}
elideEmpty = true
data: Record<string, string>
data: Record<string, string> | undefined
group() {
return this
}
Expand Down
4 changes: 4 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function render(graphics: Graphics, config: Config, compartment: Layouted

g.save()
g.setData('name', node.id)
g.setData('compartment', undefined)

g.save()
g.fillStyle(style.fill || config.fill[level] || last(config.fill))
Expand All @@ -73,9 +74,11 @@ export function render(graphics: Graphics, config: Config, compartment: Layouted
}
g.restore()

let partIndex = 0
for (let part of node.parts) {
const textStyle = part === node.parts[0] ? style.title : style.body
g.save()
g.setData('compartment', String(partIndex))
g.translate(x + part.x!, y + part.y!)
g.setFont(
config.font,
Expand All @@ -84,6 +87,7 @@ export function render(graphics: Graphics, config: Config, compartment: Layouted
textStyle.italic ? 'italic' : 'normal'
)
renderCompartment(part, style.stroke, textStyle, level + 1)
partIndex++
g.restore()
}

Expand Down

0 comments on commit d76a085

Please sign in to comment.