about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorHailey <me@haileyok.com>2024-04-24 07:10:29 -0700
committerGitHub <noreply@github.com>2024-04-24 15:10:29 +0100
commitb3df0b177f88cc4ee9565ae7f9209d738370942a (patch)
treec33402bd70ec4ba85a3a8fc020312e2c8852dfba /src
parent05beb1bbadf86f8b20ab950497947e31beea798e (diff)
downloadvoidsky-b3df0b177f88cc4ee9565ae7f9209d738370942a.tar.zst
Remove unnecessary `.trimEnd()` and fix facet detection (#3672)
* Fix link facet detection

Exclude the trailing period from the link, similar as done in the atproto package

* Fix JSON to text conversion

The trimEnd() call was only needed because we were always appending \n\n at the end. Fix that by only adding line breaks after non-final paragraphs.

* Detect paste reliably

---------

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/view/com/composer/text-input/TextInput.web.tsx29
-rw-r--r--src/view/com/composer/text-input/web/LinkDecorator.ts6
2 files changed, 23 insertions, 12 deletions
diff --git a/src/view/com/composer/text-input/TextInput.web.tsx b/src/view/com/composer/text-input/TextInput.web.tsx
index 1038fe5db..1b5d7a821 100644
--- a/src/view/com/composer/text-input/TextInput.web.tsx
+++ b/src/view/com/composer/text-input/TextInput.web.tsx
@@ -61,7 +61,6 @@ export const TextInput = React.forwardRef(function TextInputImpl(
   ref,
 ) {
   const autocomplete = useActorAutocompleteFn()
-  const prevLength = React.useRef(0)
   const prevAddedLinks = useRef(new Set<string>())
 
   const pal = usePalette('default')
@@ -185,8 +184,8 @@ export const TextInput = React.forwardRef(function TextInputImpl(
       },
       onUpdate({editor: editorProp}) {
         const json = editorProp.getJSON()
-        const newText = editorJsonToText(json).trimEnd()
-        const mayBePaste = newText.length > prevLength.current + 1
+        const newText = editorJsonToText(json)
+        const mayBePaste = window.event?.type === 'paste'
 
         const newRt = new RichText({text: newText})
         newRt.detectFacetsWithoutResolution()
@@ -219,8 +218,6 @@ export const TextInput = React.forwardRef(function TextInputImpl(
             prevAddedLinks.current.delete(uri)
           }
         }
-
-        prevLength.current = newText.length
       },
     },
     [modeClass],
@@ -277,15 +274,29 @@ export const TextInput = React.forwardRef(function TextInputImpl(
   )
 })
 
-function editorJsonToText(json: JSONContent): string {
+function editorJsonToText(
+  json: JSONContent,
+  isLastDocumentChild: boolean = false,
+): string {
   let text = ''
-  if (json.type === 'doc' || json.type === 'paragraph') {
+  if (json.type === 'doc') {
+    if (json.content?.length) {
+      for (let i = 0; i < json.content.length; i++) {
+        const node = json.content[i]
+        const isLastNode = i === json.content.length - 1
+        text += editorJsonToText(node, isLastNode)
+      }
+    }
+  } else if (json.type === 'paragraph') {
     if (json.content?.length) {
-      for (const node of json.content) {
+      for (let i = 0; i < json.content.length; i++) {
+        const node = json.content[i]
         text += editorJsonToText(node)
       }
     }
-    text += '\n'
+    if (!isLastDocumentChild) {
+      text += '\n'
+    }
   } else if (json.type === 'hardBreak') {
     text += '\n'
   } else if (json.type === 'text') {
diff --git a/src/view/com/composer/text-input/web/LinkDecorator.ts b/src/view/com/composer/text-input/web/LinkDecorator.ts
index e36ac80e4..60f2d4085 100644
--- a/src/view/com/composer/text-input/web/LinkDecorator.ts
+++ b/src/view/com/composer/text-input/web/LinkDecorator.ts
@@ -14,11 +14,11 @@
  * the facet-set.
  */
 
+import {URL_REGEX} from '@atproto/api'
 import {Mark} from '@tiptap/core'
-import {Plugin, PluginKey} from '@tiptap/pm/state'
 import {Node as ProsemirrorNode} from '@tiptap/pm/model'
+import {Plugin, PluginKey} from '@tiptap/pm/state'
 import {Decoration, DecorationSet} from '@tiptap/pm/view'
-import {URL_REGEX} from '@atproto/api'
 
 import {isValidDomain} from 'lib/strings/url-helpers'
 
@@ -91,7 +91,7 @@ function iterateUris(str: string, cb: (from: number, to: number) => void) {
       uri = `https://${uri}`
     }
     let from = str.indexOf(match[2], match.index)
-    let to = from + match[2].length + 1
+    let to = from + match[2].length
     // strip ending puncuation
     if (/[.,;!?]$/.test(uri)) {
       uri = uri.slice(0, -1)