mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-15 14:29:25 +02:00
refactor text width computing func
This commit is contained in:
@@ -83,13 +83,13 @@ function createTspan(textElement, lineIndex, lineHeight) {
|
|||||||
* @param {string} text
|
* @param {string} text
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
function testWidthOfText(parentNode, lineHeight, text) {
|
function computeWidthOfText(parentNode, lineHeight, text) {
|
||||||
const testElement = parentNode.append('text').attr('y', '-10.1');
|
const testElement = parentNode.append('text');
|
||||||
const testSpan = createTspan(testElement, 1, lineHeight);
|
const testSpan = createTspan(testElement, 1, lineHeight);
|
||||||
updateTextContentAndStyles(testSpan, [{ content: text, type: 'normal' }]);
|
updateTextContentAndStyles(testSpan, [{ content: text, type: 'normal' }]);
|
||||||
const val = testSpan.node().getComputedTextLength();
|
const textLength = testSpan.node().getComputedTextLength();
|
||||||
testElement.remove();
|
testElement.remove();
|
||||||
return val;
|
return textLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -122,7 +122,7 @@ function createFormattedText(width, g, structuredText, addBackground = false) {
|
|||||||
for (let i = 0; i <= fullStr.length; i++) {
|
for (let i = 0; i <= fullStr.length; i++) {
|
||||||
tempStr = fullStr.slice(prevIndex, i);
|
tempStr = fullStr.slice(prevIndex, i);
|
||||||
log.info(tempStr, prevIndex, i);
|
log.info(tempStr, prevIndex, i);
|
||||||
if (testWidthOfText(labelGroup, lineHeight, tempStr) > width) {
|
if (computeWidthOfText(labelGroup, lineHeight, tempStr) > width) {
|
||||||
const subStr = fullStr.slice(prevIndex, i);
|
const subStr = fullStr.slice(prevIndex, i);
|
||||||
// Break at space if any
|
// Break at space if any
|
||||||
const lastSpaceIndex = subStr.lastIndexOf(' ');
|
const lastSpaceIndex = subStr.lastIndexOf(' ');
|
||||||
|
Reference in New Issue
Block a user