Skip to content

Commit

Permalink
Merge pull request #112 from andymchugh/format
Browse files Browse the repository at this point in the history
fix label positioning on formatted SVGs
  • Loading branch information
andymchugh authored Sep 12, 2024
2 parents e83143d + da3bcb0 commit 87cd4aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## 1.16.x

SVG formatting support improvements
-----------------------------------
Formatting of diagrams edited in the latest draw.io (v24.7.8) caused label layout problems; especially
if the labels were multi-word. This should now be fixed by removing multi-spaces from labels at initialization
and further limiting the application of whitespace style 'pre'.

TimeSlider label
----------------
Improvements made to the timeSlider timezone application and label positioning to remove artifacts
Expand All @@ -27,7 +33,7 @@ New config terms in panelConfig:

SVG formating support
---------------------
Using SVGs post formatting for better readabilitycould cause label positions to shift.
Using SVGs post formatting for better readability could cause label positions to shift.
This is now fixed by limiting the scope of how style->whitespace is applied dynamically to labels.

## 1.16.2
Expand Down
29 changes: 27 additions & 2 deletions src/components/SvgUpdater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export type BespokeStateHolder = {
}

function generateLabelPreamble(label: string | null, separator: LabelSeparator | null) {
label = (label || '').trim();

// label space stripping is needed when the svg has been formatted to allow us to change
// the whitespace style to 'pre'.
label = (label || '').replace(/\s+/g, ' ');
label = label.trim();

if (separator === 'cr') {
return label + '\n';
}
Expand Down Expand Up @@ -106,9 +111,29 @@ function dimensionCoherence(doc: Document) {
}
}

function innerMostDiv(el: HTMLElement) {
if (el.nodeName === 'div') {
if (el.hasChildNodes()) {
for (const child of el.childNodes) {
const childNode = child as HTMLElement;
if (childNode.nodeName === 'div') {
return false;
}
}
}
return true;
}
return false;
}

function recurseElements(el: HTMLElement, cellData: SvgCell, cellIdMaker: CellIdMaker, additions: HTMLElement[], bespokeStateHolder: BespokeStateHolder): boolean {
const setAttributes = function(el: HTMLElement) {
if ((el.nodeName === 'div') || (el.nodeName === 'text')) {

// 'pre' is needed to honour the CRs we embed in the label whilst also ensuring text doesn't
// wrap if the label extends beyond the bounding box. It's needed on the innermost div and text
// elements but if applied on outer divs can result in a right-shifting of the label when the svg
// has been formatted.
if (cellData.cellProps.label && (innerMostDiv(el) || (el.nodeName === 'text'))) {
el.style.whiteSpace = 'pre';
}
if (cellData.cellProps.link) {
Expand Down

0 comments on commit 87cd4aa

Please sign in to comment.