Skip to content

Commit 7262a87

Browse files
authored
fix firefox node text bug (#188)
1 parent 2097a81 commit 7262a87

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

teammapper-frontend/mmp/src/map/handlers/draw.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,23 +407,27 @@ export default class Draw {
407407
foreignObject: SVGForeignObjectElement = name?.parentNode as SVGForeignObjectElement
408408

409409
const [width, height]: number[] = (() => {
410-
if (name?.offsetWidth !== 0) {
410+
if (!this.browserIsFirefox()) {
411411
// Default case
412412
// Text is rendered based on needed width and height
413+
// works well at least for chrome and safari
413414
name.style.setProperty('width', 'auto')
414415
name.style.setProperty('height', 'auto')
415416
return [name.clientWidth, name.clientHeight]
416-
} else if(node?.name?.length > 0) {
417+
} else {
417418
// More recent versions of firefox seem to render too late to actually fetch the width and height of the dom element.
418419
// In these cases, try to approximate height and width before rendering.
419420
name.style.setProperty('width', '100%')
420421
name.style.setProperty('height', '100%')
421-
return [node.name.length * node.font.size / 1.9, node.font.size * 1.2]
422-
} else {
423-
// Default values if empty
424-
return [20, 20]
422+
// split by line break
423+
const linesByLineBreaks = name.textContent.split(/\r?\n|\r|\n/g)
424+
// take longest line as width, when no lines are present use 1 as length
425+
const width = Math.max(...linesByLineBreaks.map((line: string) => line.length), 1)
426+
// take number of lines as height factor
427+
const height = linesByLineBreaks.length
428+
return [width * node.font.size / 1.2, height * node.font.size * 1.2]
425429
}
426-
})()
430+
})().map((value: number) => Math.max(value, 25))
427431

428432
foreignObject.setAttribute('x', (-width / 2).toString())
429433
foreignObject.setAttribute('y', (-height / 2).toString())
@@ -469,4 +473,11 @@ export default class Draw {
469473
return event.target['classList'][0] === 'link-text'
470474
}
471475

476+
/**
477+
* Checks if the browser is firefox
478+
* @returns {boolean}
479+
*/
480+
private browserIsFirefox(): boolean {
481+
return navigator.userAgent.toLowerCase().indexOf('firefox') > -1
482+
}
472483
}

teammapper-frontend/src/app/modules/about/components/footer/footer.component.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<footer class="footer">
22
<div class="container">
33
<div class="license">
4-
{{ currentYear }}
5-
<a href="https://b310.de" target="_blank">
6-
B310 Digital GmbH
7-
</a>
4+
{{ currentYear }}
5+
<a href="https://b310.de" target="_blank"> B310 Digital GmbH </a>
86
</div>
97
<div class="links">
108
<a href="https://github.com/b310-digital/teammapper" target="_blank">

0 commit comments

Comments
 (0)