From 54e4f6974673b6baf4a45563c330333208424b12 Mon Sep 17 00:00:00 2001 From: "Qinghui.Liu" Date: Wed, 13 Sep 2023 23:02:49 +0800 Subject: [PATCH] feat: breakText method support line wrapping text --- packages/x6-common/src/dom/text.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/x6-common/src/dom/text.ts b/packages/x6-common/src/dom/text.ts index 61d5685b9f3..a97eb6ad21b 100644 --- a/packages/x6-common/src/dom/text.ts +++ b/packages/x6-common/src/dom/text.ts @@ -1,6 +1,7 @@ /* eslint-disable no-control-regex */ import { Size } from '../types' +import { StringExt } from '../string' import { Text } from '../text' import { attr } from './attr' import { Vector } from '../vector' @@ -442,6 +443,30 @@ export function breakText( const width = size.width const height = size.height const eol = options.eol || '\n' + const fontSize = styles.fontSize || 14 + const lineHeight = styles.lineHeight + ? parseFloat(styles.lineHeight) + : Math.ceil(fontSize * 1.4) + const maxLines = Math.floor(height / lineHeight) + + if (text.indexOf(eol) > -1) { + const delimiter = StringExt.uuid() + const splitText: string[] = [] + + text.split(eol).map((fragment) => { + splitText.push( + ...breakText( + fragment, + { ...size, height: Number.MAX_SAFE_INTEGER }, + styles, + { ...options, eol: delimiter }, + ).split(delimiter), + ) + }) + + return splitText.slice(0, maxLines).join(eol) + } + const { width: textWidth } = measureText(text, styles) if (textWidth < width) { @@ -449,11 +474,6 @@ export function breakText( } const lines = [] - const fontSize = styles.fontSize || 14 - const lineHeight = styles.lineHeight - ? parseFloat(styles.lineHeight) - : Math.ceil(fontSize * 1.4) - const maxLines = Math.floor(height / lineHeight) let remainText = text let remainWidth = textWidth