Switch from graphemer to Intl.Segmenter (#12697)

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2024-06-26 10:34:07 +01:00 committed by GitHub
parent 95c8aa3d18
commit 86a95cfff7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 15 additions and 19 deletions

View file

@ -21,7 +21,6 @@ limitations under the License.
* @param text the plaintext to put in the user's clipboard
*/
import { logger } from "matrix-js-sdk/src/logger";
import GraphemeSplitter from "graphemer";
export async function copyPlaintext(text: string): Promise<boolean> {
try {
@ -85,6 +84,8 @@ export function getSelectedText(): string {
return window.getSelection()!.toString();
}
export const graphemeSegmenter = new Intl.Segmenter();
/**
* Returns the first grapheme in the given string,
* especially useful for strings containing emoji, will not break compound emoji up.
@ -92,7 +93,6 @@ export function getSelectedText(): string {
* @returns the first grapheme or an empty string if given an empty string
*/
export function getFirstGrapheme(str: string): string {
const splitter = new GraphemeSplitter();
const result = splitter.iterateGraphemes(str).next();
return result.done ? "" : result.value;
const result = graphemeSegmenter.segment(str)[Symbol.iterator]().next();
return result.done ? "" : result.value.segment;
}