Adds captions in the viewer
This commit is contained in:
parent
247149c58e
commit
4015259def
4 changed files with 106 additions and 32 deletions
34
ts/util/graphemeAwareSlice.ts
Normal file
34
ts/util/graphemeAwareSlice.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
// Copyright 2021-2022 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
export function graphemeAwareSlice(
|
||||
str: string,
|
||||
length: number,
|
||||
buffer = 100
|
||||
): {
|
||||
hasReadMore: boolean;
|
||||
text: string;
|
||||
} {
|
||||
if (str.length <= length + buffer) {
|
||||
return { text: str, hasReadMore: false };
|
||||
}
|
||||
|
||||
let text: string | undefined;
|
||||
|
||||
for (const { index } of new Intl.Segmenter().segment(str)) {
|
||||
if (!text && index >= length) {
|
||||
text = str.slice(0, index);
|
||||
}
|
||||
if (text && index > length) {
|
||||
return {
|
||||
text,
|
||||
hasReadMore: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
text: str,
|
||||
hasReadMore: false,
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue