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

feat: breakText method support line wrapping text #3910

Merged
merged 2 commits into from
Sep 19, 2023
Merged
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
27 changes: 22 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,34 @@ 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((line) => {
const part = breakText(line, { ...size, height: Number.MAX_SAFE_INTEGER }, styles, { ...options, eol: delimiter })
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的 eol 可以不传吧,因为 split 后 line 里面肯定不会继续有 eol 了。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

split后的line在进行breakText时,返回值会用eol进行join,为了将join后的字符串分割,所以这里用uuid作为新的eol


if (part) {
splitText.push(...part.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
Loading