about summary refs log tree commit diff
path: root/src/view/com/composer/text-input/TextInput.web.tsx
diff options
context:
space:
mode:
authorAnsh <anshnanda10@gmail.com>2023-08-22 11:01:00 -0700
committerGitHub <noreply@github.com>2023-08-22 11:01:00 -0700
commit16b265a86164e682486a3d8fa51bfa18d51bb945 (patch)
tree672a87170b11d9c9a6b7990f5b828da469981062 /src/view/com/composer/text-input/TextInput.web.tsx
parent3aadc43c896e1c54552387f028a476d7d8ec2d3c (diff)
downloadvoidsky-16b265a86164e682486a3d8fa51bfa18d51bb945.tar.zst
[APP-834] Allow @ing someone in post directly from profile (#1241)
* setup `initMention` for mobile

* setup creating post with profile tagged on web
Diffstat (limited to 'src/view/com/composer/text-input/TextInput.web.tsx')
-rw-r--r--src/view/com/composer/text-input/TextInput.web.tsx60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/view/com/composer/text-input/TextInput.web.tsx b/src/view/com/composer/text-input/TextInput.web.tsx
index f64880e15..b7ebdd989 100644
--- a/src/view/com/composer/text-input/TextInput.web.tsx
+++ b/src/view/com/composer/text-input/TextInput.web.tsx
@@ -114,7 +114,10 @@ export const TextInput = React.forwardRef(
             }
           },
         },
-        content: richtext.text.toString(),
+        content: textToEditorJson(richtext.text.toString()),
+        onFocus: ({editor: e}) => {
+          e.chain().focus().setTextSelection(richtext.text.length).run() // focus to the end of the text
+        },
         autofocus: true,
         editable: true,
         injectCSS: true,
@@ -166,6 +169,61 @@ function editorJsonToText(json: JSONContent): string {
   return text
 }
 
+function textToEditorJson(text: string): JSONContent {
+  if (text === '' || text.length === 0) {
+    return {
+      text: '',
+    }
+  }
+
+  const lines = text.split('\n')
+  const docContent: JSONContent[] = []
+
+  for (const line of lines) {
+    if (line.trim() === '') {
+      continue // skip empty lines
+    }
+
+    const paragraphContent: JSONContent[] = []
+    let position = 0
+
+    while (position < line.length) {
+      if (line[position] === '@') {
+        // Handle mentions
+        let endPosition = position + 1
+        while (endPosition < line.length && /\S/.test(line[endPosition])) {
+          endPosition++
+        }
+        const mentionId = line.substring(position + 1, endPosition)
+        paragraphContent.push({
+          type: 'mention',
+          attrs: {id: mentionId},
+        })
+        position = endPosition
+      } else {
+        // Handle regular text
+        let endPosition = line.indexOf('@', position)
+        if (endPosition === -1) endPosition = line.length
+        paragraphContent.push({
+          type: 'text',
+          text: line.substring(position, endPosition),
+        })
+        position = endPosition
+      }
+    }
+
+    docContent.push({
+      type: 'paragraph',
+      content: paragraphContent,
+    })
+  }
+
+  return {
+    type: 'doc',
+    content: docContent,
+  }
+}
+
 function editorJsonToLinks(json: JSONContent): string[] {
   let links: string[] = []
   if (json.content?.length) {