diff --git a/src/view/com/composer/text-input/TextInput.web.tsx b/src/view/com/composer/text-input/TextInput.web.tsx index 64abeea37d..3e0dac2f7d 100644 --- a/src/view/com/composer/text-input/TextInput.web.tsx +++ b/src/view/com/composer/text-input/TextInput.web.tsx @@ -213,7 +213,8 @@ export const TextInput = React.forwardRef(function TextInputImpl( mayBePaste, onNewLink, prevAddedLinks: prevAddedLinks.current, - byteEnd: facet.index.byteEnd, + endIndex: newRt.unicodeText.slice(0, facet.index.byteEnd) + .length, }) } } diff --git a/src/view/com/composer/text-input/text-input-util.ts b/src/view/com/composer/text-input/text-input-util.ts index 4fe466bd4d..a59db37767 100644 --- a/src/view/com/composer/text-input/text-input-util.ts +++ b/src/view/com/composer/text-input/text-input-util.ts @@ -5,7 +5,7 @@ export function addLinkCardIfNecessary({ mayBePaste, onNewLink, prevAddedLinks, - byteEnd, + endIndex, }: { uri: string newText: string @@ -13,10 +13,8 @@ export function addLinkCardIfNecessary({ mayBePaste: boolean onNewLink: (uri: string) => void prevAddedLinks: Set - byteEnd: number + endIndex: number }) { - const utf16Index = utf8IndexToUtf16Index(newText, byteEnd) - // Because we always trim the whitespace at the end of the text newText = newText + ' ' let toAdd = 2 @@ -24,16 +22,11 @@ export function addLinkCardIfNecessary({ const backOne = newText.charAt(cursorLocation - 1) const backTwo = newText.charAt(cursorLocation - 2) - if (backOne === ' ' && !/[.!?]/.test(backTwo)) { + if (backOne === ' ' && /[^.!?]/.test(backTwo)) { toAdd = 1 } - console.log({ - utf8Index: byteEnd, - utf16Index: utf16Index, - }) - - if (!mayBePaste && utf16Index + toAdd !== cursorLocation) { + if (!mayBePaste && endIndex + toAdd !== cursorLocation) { return } @@ -77,36 +70,3 @@ export function findIndexInText(term: string, text: string) { const match = pattern.exec(text) return match ? match.index : -1 } - -function utf8IndexToUtf16Index(inStr: string, utf8Index: number) { - let utf16Index = 0 - let bytesCounted = 0 - - for (let i = 0; i < inStr.length; i++) { - // Check the current Unicode code point size in UTF-8 - const codePoint = inStr.codePointAt(i) - - if (!codePoint) return -1 - - // Add the UTF-8 byte length of this code point - if (codePoint <= 0x7f) { - bytesCounted += 1 // 1 byte in UTF-8 - } else if (codePoint <= 0x7ff) { - bytesCounted += 2 // 2 bytes in UTF-8 - } else if (codePoint <= 0xffff) { - bytesCounted += 3 // 3 bytes in UTF-8 - } else { - bytesCounted += 4 // 4 bytes in UTF-8 - i++ // Move past the high surrogate - } - - // Update UTF-16 index and break when the UTF-8 index is reached or exceeded - if (bytesCounted > utf8Index) { - break - } - - utf16Index = i + 1 - } - - return utf16Index -}