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

Fix Timeline LongText wrap issue #6258

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
52 changes: 32 additions & 20 deletions packages/mermaid/src/diagrams/timeline/svgDraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,37 +451,49 @@ const initGraphics = function (graphics) {
* @param {string} text The text to be wrapped
* @param {number} width The max width of the text
*/

function wrap(text, width) {
text.each(function () {
var text = select(this),
words = text
.text()
.split(/(\s+|<br>)/)
.reverse(),
word,
line = [],
lineHeight = 1.1, // ems
y = text.attr('y'),
dy = parseFloat(text.attr('dy')),
tspan = text
.text(null)
.append('tspan')
.attr('x', 0)
.attr('y', y)
.attr('dy', dy + 'em');
for (let j = 0; j < words.length; j++) {
word = words[words.length - 1 - j];
const text = select(this);
let words = text
.text()
.split(/(\s+|<br>)/)
.reverse(); // Split text into words and reverse for easier processing
let word;
let line = [];
const lineHeight = 1.1; // Line height in ems
const y = text.attr('y');
const dy = parseFloat(text.attr('dy')) || 0;
let tspan = text
.text(null) // Clear existing text
.append('tspan')
.attr('x', 0)
.attr('y', y)
.attr('dy', dy + 'em');
while (words.length) {
word = words.pop();
// Sanitize and ensure word is a valid string
if (typeof word !== 'string') {
word = word !== null && word !== undefined ? String(word) : '';
}
// Handle long words that exceed the width
if (word.length * 8 > width) {
const splitWord = word.match(/.{1,10}/g) || []; // Split long words into chunks
words.push(...splitWord.reverse()); // Push chunks back into words
Comment on lines +480 to +482
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coding these values could cause issues if font size is different.

continue;
}
line.push(word);
tspan.text(line.join(' ').trim());
if (tspan.node().getComputedTextLength() > width || word === '<br>') {
line.pop();
line.pop(); // Remove the last word if it exceeds the width
tspan.text(line.join(' ').trim());
// Reset the line for the next tspan
if (word === '<br>') {
line = [''];
} else {
line = [word];
}

// Create a new tspan for the next line
tspan = text
.append('tspan')
.attr('x', 0)
Expand Down
Loading