about summary refs log tree commit diff
path: root/src/lib/strings/mention-manip.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/strings/mention-manip.ts')
-rw-r--r--src/lib/strings/mention-manip.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/lib/strings/mention-manip.ts b/src/lib/strings/mention-manip.ts
new file mode 100644
index 000000000..1f7cbe434
--- /dev/null
+++ b/src/lib/strings/mention-manip.ts
@@ -0,0 +1,37 @@
+interface FoundMention {
+  value: string
+  index: number
+}
+
+export function getMentionAt(
+  text: string,
+  cursorPos: number,
+): FoundMention | undefined {
+  let re = /(^|\s)@([a-z0-9.]*)/gi
+  let match
+  while ((match = re.exec(text))) {
+    const spaceOffset = match[1].length
+    const index = match.index + spaceOffset
+    if (
+      cursorPos >= index &&
+      cursorPos <= index + match[0].length - spaceOffset
+    ) {
+      return {value: match[2], index}
+    }
+  }
+  return undefined
+}
+
+export function insertMentionAt(
+  text: string,
+  cursorPos: number,
+  mention: string,
+) {
+  const target = getMentionAt(text, cursorPos)
+  if (target) {
+    return `${text.slice(0, target.index)}@${mention} ${text.slice(
+      target.index + target.value.length + 1, // add 1 to include the "@"
+    )}`
+  }
+  return text
+}