-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for wrapping with newlines (#88)
* Add support for wrapping with newlines It can be useful to more accurately control formatting in table cells by means of inserting newlines. For example, we can now put bulleted lists or pretty-printed JSON objects in table cells. This change makes it so that any '\n' characters encountered in the cell's values will be translated into line breaks. Implementation note: the cell wrapping logic that used to be duplicated between `mapDataUsingRowHeightIndex` and `calculateCellHeight` has been moved to `wrapCell` to be shared between them. * Add test with color coding * More readable way to write ANSI test
- Loading branch information
Showing
5 changed files
with
110 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import wrapString from './wrapString'; | ||
import wrapWord from './wrapWord'; | ||
|
||
/** | ||
* Wrap a single cell value into a list of lines | ||
* | ||
* Always wraps on newlines, for the remainder uses either word or string wrapping | ||
* depending on user configuration. | ||
* | ||
* @param {string} cellValue | ||
* @param {number} columnWidth | ||
* @param {boolean} useWrapWord | ||
* @returns {Array} | ||
*/ | ||
export default (cellValue, columnWidth, useWrapWord) => { | ||
// First split on literal newlines | ||
const cellLines = cellValue.split('\n'); | ||
|
||
// Then iterate over the list and word-wrap every remaining line if necessary. | ||
for (let lineNr = 0; lineNr < cellLines.length;) { | ||
let lineChunks; | ||
|
||
if (useWrapWord) { | ||
lineChunks = wrapWord(cellLines[lineNr], columnWidth); | ||
} else { | ||
lineChunks = wrapString(cellLines[lineNr], columnWidth); | ||
} | ||
|
||
// Replace our original array element with whatever the wrapping returned | ||
cellLines.splice(lineNr, 1, ...lineChunks); | ||
lineNr += lineChunks.length; | ||
} | ||
|
||
return cellLines; | ||
}; |
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