-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
456 additions
and
262 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 @@ | ||
export const ELLIPSIS = '...'; |
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,107 @@ | ||
import { SkFont } from '@shopify/react-native-skia'; | ||
|
||
import { ELLIPSIS } from '../constants'; | ||
import { TextLineData } from '../types'; | ||
|
||
export const trimLineStart = ( | ||
line: TextLineData, | ||
font: SkFont, | ||
width: number, | ||
prefix = ELLIPSIS | ||
): TextLineData => { | ||
// If the line is too short, return it as is | ||
if (line.width <= width) { | ||
return line; | ||
} | ||
|
||
// Go from the first char and find how many chars must be sliced to | ||
// display ellipsis at the beginning | ||
let firstIndex = 0; | ||
let lineWidth = font.getTextWidth(line.text) + font.getTextWidth(prefix); | ||
|
||
while (lineWidth > width && firstIndex < line.text.length) { | ||
lineWidth -= font.getTextWidth(line.text[firstIndex]!); | ||
firstIndex++; | ||
} | ||
|
||
return { | ||
text: `${prefix}${line.text.slice(firstIndex).trimStart()}`, | ||
width: lineWidth | ||
}; | ||
}; | ||
|
||
export const trimLineEnd = ( | ||
line: TextLineData, | ||
font: SkFont, | ||
width: number, | ||
suffix = ELLIPSIS | ||
): TextLineData => { | ||
// If the line is too short, return it as is | ||
if (line.width <= width) { | ||
return line; | ||
} | ||
|
||
// Go from the last char and find how many chars must be sliced to | ||
// display ellipsis at the end or clip the end of the line | ||
let lastIndex = line.text.length - 1; | ||
let lineWidth = font.getTextWidth(line.text) + font.getTextWidth(suffix); | ||
|
||
while (lineWidth > width && lastIndex >= 0) { | ||
lineWidth -= font.getTextWidth(line.text[lastIndex]!); | ||
lastIndex--; | ||
} | ||
|
||
return { | ||
text: `${line.text.slice(0, lastIndex + 1).trimEnd()}${suffix}`, | ||
width: lineWidth | ||
}; | ||
}; | ||
|
||
export const trimLineCenter = ( | ||
line: TextLineData, | ||
font: SkFont, | ||
width: number, | ||
infix = ELLIPSIS | ||
): TextLineData => { | ||
// If the line is too short, return it as is | ||
if (line.width <= width) { | ||
return line; | ||
} | ||
|
||
// Get first half of the line in terms of width | ||
let firstHalfWidth = 0; | ||
let firstHalfIndex = 0; | ||
|
||
while (firstHalfWidth < line.width / 2) { | ||
firstHalfWidth += font.getTextWidth(line.text[firstHalfIndex]!); | ||
firstHalfIndex++; | ||
} | ||
|
||
// Get second half of the line in terms of width | ||
let secondHalfWidth = 0; | ||
let secondHalfIndex = line.text.length - 1; | ||
|
||
while (secondHalfWidth < line.width / 2) { | ||
secondHalfWidth += font.getTextWidth(line.text[secondHalfIndex]!); | ||
secondHalfIndex--; | ||
} | ||
|
||
// Remove chars from the end of the first half and from the start of the second half | ||
// until the line width with infix is less than or equal to the width | ||
let lineWidth = firstHalfWidth + secondHalfWidth + font.getTextWidth(infix); | ||
while (lineWidth > width) { | ||
if (firstHalfWidth > secondHalfWidth) { | ||
firstHalfWidth -= font.getTextWidth(line.text[--firstHalfIndex]!); | ||
} else { | ||
secondHalfWidth -= font.getTextWidth(line.text[++secondHalfIndex]!); | ||
} | ||
lineWidth = firstHalfWidth + secondHalfWidth + font.getTextWidth(infix); | ||
} | ||
|
||
return { | ||
text: `${line.text.slice(0, firstHalfIndex).trimEnd()}${infix}${line.text | ||
.slice(secondHalfIndex) | ||
.trimStart()}`, | ||
width: lineWidth | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './alignment'; | ||
export * from './ellipsize'; | ||
export * from './reanimated'; | ||
export * from './text'; | ||
export * from './wrapping'; |
Oops, something went wrong.