-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wrapping/truncating for <Box> text content
- Loading branch information
Vadim Demedes
committed
Mar 27, 2019
1 parent
dcaaafa
commit e8e6811
Showing
11 changed files
with
172 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import widestLine from 'widest-line'; | ||
|
||
export default text => { | ||
const width = widestLine(text); | ||
const height = text.split('\n').length; | ||
|
||
return {width, height}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import wrapAnsi from 'wrap-ansi'; | ||
import cliTruncate from 'cli-truncate'; | ||
import measureText from './measure-text'; | ||
|
||
const wrapText = node => { | ||
if (node.textContent && typeof node.parentNode.style.textWrap === 'string') { | ||
const {yogaNode} = node; | ||
const parentYogaNode = node.parentNode.yogaNode; | ||
|
||
const maxWidth = parentYogaNode.getComputedWidth() - (parentYogaNode.getComputedPadding() * 2); | ||
const currentWidth = yogaNode.getComputedWidth(); | ||
|
||
if (currentWidth > maxWidth) { | ||
const {textWrap} = node.parentNode.style; | ||
let wrappedText = node.textContent; | ||
|
||
if (textWrap === 'wrap') { | ||
wrappedText = wrapAnsi(node.textContent, maxWidth, { | ||
trim: false, | ||
hard: true | ||
}); | ||
} | ||
|
||
if (textWrap.startsWith('truncate')) { | ||
let position; | ||
|
||
if (textWrap === 'truncate' || textWrap === 'truncate-end') { | ||
position = 'end'; | ||
} | ||
|
||
if (textWrap === 'truncate-middle') { | ||
position = 'middle'; | ||
} | ||
|
||
if (textWrap === 'truncate-start') { | ||
position = 'start'; | ||
} | ||
|
||
wrappedText = cliTruncate(node.textContent, maxWidth, {position}); | ||
} | ||
|
||
const {width, height} = measureText(wrappedText); | ||
node.textContent = wrappedText; | ||
|
||
yogaNode.setWidth(width); | ||
yogaNode.setHeight(height); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (Array.isArray(node.childNodes) && node.childNodes.length > 0) { | ||
for (const childNode of node.childNodes) { | ||
wrapText(childNode); | ||
} | ||
} | ||
}; | ||
|
||
export default wrapText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters