Skip to content

Commit

Permalink
feat: breakText method support line wrapping text
Browse files Browse the repository at this point in the history
  • Loading branch information
Qinghui.Liu authored and Qinghui.Liu committed Sep 13, 2023
1 parent 78e5274 commit 54e4f69
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions packages/x6-common/src/dom/text.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -442,18 +443,37 @@ 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) {
return text
}

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
Expand Down

0 comments on commit 54e4f69

Please sign in to comment.