Use grapheme-splitter instead of lodash for saving emoji from being ripped apart (#10976)

* Use grapheme-splitter instead of lodash for saving emoji from being ripped apart

* Move to a more appropriate place

* Add tests and improve types
This commit is contained in:
Michael Telatynski 2023-05-25 09:32:20 +01:00 committed by GitHub
parent 277a3c0146
commit f4a265b2c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 22 deletions

View file

@ -21,6 +21,7 @@ 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 "grapheme-splitter";
export async function copyPlaintext(text: string): Promise<boolean> {
try {
@ -83,3 +84,15 @@ export function copyNode(ref?: Element | null): boolean {
export function getSelectedText(): string {
return window.getSelection()!.toString();
}
/**
* Returns the first grapheme in the given string,
* especially useful for strings containing emoji, will not break compound emoji up.
* @param str string to parse
* @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;
}