Username UI Improvements
Co-authored-by: Fedor Indutny <238531+indutny@users.noreply.github.com>
This commit is contained in:
parent
ecafaa788a
commit
d811dd1ed4
9 changed files with 379 additions and 219 deletions
46
ts/util/splitText.ts
Normal file
46
ts/util/splitText.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
export type SplitTextOptionsType = Readonly<{
|
||||
granularity: 'grapheme' | 'word';
|
||||
shouldBreak: (slice: string) => boolean;
|
||||
}>;
|
||||
|
||||
export function splitText(
|
||||
text: string,
|
||||
{ granularity, shouldBreak }: SplitTextOptionsType
|
||||
): Array<string> {
|
||||
const isWordBased = granularity === 'word';
|
||||
const segmenter = new Intl.Segmenter(undefined, {
|
||||
granularity,
|
||||
});
|
||||
|
||||
const result = new Array<string>();
|
||||
|
||||
// Compute number of lines and height of text
|
||||
let acc = '';
|
||||
let best = '';
|
||||
for (const { segment, isWordLike } of segmenter.segment(text)) {
|
||||
acc += segment;
|
||||
|
||||
// For "grapheme" segmenting, "isWordLike" is always "undefined"
|
||||
if (isWordLike === false) {
|
||||
best = acc;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (shouldBreak(isWordBased ? acc.trim() : acc)) {
|
||||
result.push(best);
|
||||
acc = acc.slice(best.length);
|
||||
best = acc;
|
||||
} else {
|
||||
best = acc;
|
||||
}
|
||||
}
|
||||
|
||||
if (best) {
|
||||
result.push(best);
|
||||
}
|
||||
|
||||
return isWordBased ? result.map(x => x.trim()) : result;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue