Skip to content

Commit f371183

Browse files
author
Tomáš Vojtášek
committed
Use css-line-break for text breaks
1 parent da3f2bd commit f371183

File tree

4 files changed

+35
-38
lines changed

4 files changed

+35
-38
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
"react-dom": "^16.8.6"
5555
},
5656
"dependencies": {
57-
"@craigmorton/linebreak": "^0.4.5",
5857
"css-layout": "^1.1.1",
58+
"css-line-break": "^1.1.1",
5959
"invariant": "^2.2.4",
6060
"multi-key-cache": "^1.0.2",
6161
"prop-types": "^15.6.1",

src/measureText.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import LineBreaker from '@craigmorton/linebreak'
1+
import { LineBreaker } from 'css-line-break'
22
import MultiKeyCache from 'multi-key-cache'
3+
34
import { isFontLoaded } from './FontUtils'
45

56
const canvas = document.createElement('canvas')
@@ -45,9 +46,7 @@ export default function measureText(
4546
let lastMeasuredWidth
4647
let tryLine
4748
let currentLine
48-
let breaker
4949
let bk
50-
let lastBreak
5150

5251
ctx.font = `${fontFace.attributes.style} normal ${
5352
fontFace.attributes.weight
@@ -65,15 +64,17 @@ export default function measureText(
6564
// Break into multiple lines.
6665
measuredSize.width = width
6766
currentLine = ''
68-
breaker = new LineBreaker(text)
67+
const breaker = LineBreaker(text, {
68+
lineBreak: 'strict',
69+
wordBreak: 'normal'
70+
})
6971

7072
// eslint-disable-next-line no-cond-assign
71-
while ((bk = breaker.nextBreak())) {
72-
const word = text.slice(lastBreak ? lastBreak.position : 0, bk.position)
73-
73+
while (!(bk = breaker.next()).done) {
74+
const word = bk.value.slice()
7475
tryLine = currentLine + word
7576
textMetrics = ctx.measureText(tryLine)
76-
if (textMetrics.width > width || (lastBreak && lastBreak.required)) {
77+
if (textMetrics.width > width) {
7778
measuredSize.height += lineHeight
7879
measuredSize.lines.push({
7980
width: lastMeasuredWidth,
@@ -85,8 +86,6 @@ export default function measureText(
8586
currentLine = tryLine
8687
lastMeasuredWidth = textMetrics.width
8788
}
88-
89-
lastBreak = bk
9089
}
9190

9291
currentLine = currentLine.trim()

stories/Text.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,23 @@ storiesOf('Text', module).add('hello-world', () => {
3131
}}>
3232
Hello World 2
3333
</Text>
34+
<Text
35+
style={{
36+
top: 50,
37+
left: 0,
38+
color: 'red',
39+
width: 20,
40+
height: 100
41+
}}>
42+
Long text in with small width paramter
43+
</Text>
3444

3545
<Image
3646
style={{
37-
top: 50,
38-
left: 0
47+
top: 200,
48+
left: 0,
49+
width: 200,
50+
height: 200
3951
}}
4052
src="https://i.imgur.com/U1p9DSP.png"
4153
/>

yarn.lock

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -928,13 +928,6 @@
928928
lodash "^4.17.11"
929929
to-fast-properties "^2.0.0"
930930

931-
"@craigmorton/linebreak@^0.4.5":
932-
version "0.4.5"
933-
resolved "https://registry.yarnpkg.com/@craigmorton/linebreak/-/linebreak-0.4.5.tgz#b3a9787844bc5ad954eed23c778ba56769e78d2f"
934-
dependencies:
935-
base64-js "0.0.8"
936-
unicode-trie "^0.3.0"
937-
938931
"@emotion/cache@^10.0.9":
939932
version "10.0.9"
940933
resolved "https://registry.yarnpkg.com/@emotion/cache/-/cache-10.0.9.tgz#e0c7b7a289f7530edcfad4dcf3858bd2e5700a6f"
@@ -2301,9 +2294,10 @@ balanced-match@^1.0.0:
23012294
version "1.0.0"
23022295
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
23032296

2304-
base64-js@0.0.8:
2305-
version "0.0.8"
2306-
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-0.0.8.tgz#1101e9544f4a76b1bc3b26d452ca96d7a35e7978"
2297+
base64-arraybuffer@^0.2.0:
2298+
version "0.2.0"
2299+
resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-0.2.0.tgz#4b944fac0191aa5907afe2d8c999ccc57ce80f45"
2300+
integrity sha512-7emyCsu1/xiBXgQZrscw/8KPRT44I4Yq9Pe6EGs3aPRTsWuggML1/1DTuZUuIaJPIm1FTDUVXl4x/yW8s0kQDQ==
23072301

23082302
base64-js@^1.0.2:
23092303
version "1.3.0"
@@ -3092,6 +3086,13 @@ css-layout@^1.1.1:
30923086
version "1.1.1"
30933087
resolved "https://registry.yarnpkg.com/css-layout/-/css-layout-1.1.1.tgz#ada5bb8c901e475d41043a88e034ade54d939392"
30943088

3089+
css-line-break@^1.1.1:
3090+
version "1.1.1"
3091+
resolved "https://registry.yarnpkg.com/css-line-break/-/css-line-break-1.1.1.tgz#d5e9bdd297840099eb0503c7310fd34927a026ef"
3092+
integrity sha512-1feNVaM4Fyzdj4mKPIQNL2n70MmuYzAXZ1aytlROFX1JsOo070OsugwGjj7nl6jnDJWHDM8zRZswkmeYVWZJQA==
3093+
dependencies:
3094+
base64-arraybuffer "^0.2.0"
3095+
30953096
css-loader@^2.1.0:
30963097
version "2.1.1"
30973098
resolved "https://registry.yarnpkg.com/css-loader/-/css-loader-2.1.1.tgz#d8254f72e412bb2238bb44dd674ffbef497333ea"
@@ -6116,10 +6117,6 @@ p-try@^2.0.0:
61166117
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
61176118
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==
61186119

6119-
pako@^0.2.5:
6120-
version "0.2.9"
6121-
resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"
6122-
61236120
pako@~1.0.5:
61246121
version "1.0.6"
61256122
resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.6.tgz#0101211baa70c4bca4a0f63f2206e97b7dfaf258"
@@ -7887,10 +7884,6 @@ tiny-emitter@^2.0.0:
78877884
resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.1.0.tgz#1d1a56edfc51c43e863cbb5382a72330e3555423"
78887885
integrity sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==
78897886

7890-
tiny-inflate@^1.0.0:
7891-
version "1.0.2"
7892-
resolved "https://registry.yarnpkg.com/tiny-inflate/-/tiny-inflate-1.0.2.tgz#93d9decffc8805bd57eae4310f0b745e9b6fb3a7"
7893-
78947887
tiny-invariant@^1.0.2:
78957888
version "1.0.4"
78967889
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.0.4.tgz#346b5415fd93cb696b0c4e8a96697ff590f92463"
@@ -8017,13 +8010,6 @@ unicode-property-aliases-ecmascript@^1.0.4:
80178010
resolved "https://registry.yarnpkg.com/unicode-property-aliases-ecmascript/-/unicode-property-aliases-ecmascript-1.0.5.tgz#a9cc6cc7ce63a0a3023fc99e341b94431d405a57"
80188011
integrity sha512-L5RAqCfXqAwR3RriF8pM0lU0w4Ryf/GgzONwi6KnL1taJQa7x1TCxdJnILX59WIGOwR57IVxn7Nej0fz1Ny6fw==
80198012

8020-
unicode-trie@^0.3.0:
8021-
version "0.3.1"
8022-
resolved "https://registry.yarnpkg.com/unicode-trie/-/unicode-trie-0.3.1.tgz#d671dddd89101a08bac37b6a5161010602052085"
8023-
dependencies:
8024-
pako "^0.2.5"
8025-
tiny-inflate "^1.0.0"
8026-
80278013
unified@^7.1.0:
80288014
version "7.1.0"
80298015
resolved "https://registry.yarnpkg.com/unified/-/unified-7.1.0.tgz#5032f1c1ee3364bd09da12e27fdd4a7553c7be13"

0 commit comments

Comments
 (0)