Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #274 Auto-complete for emoji #434

Merged
merged 5 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/components/compose/AutoCompleteTextarea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Input } from 'rsuite'

type ArgProps = {}
h3poteto marked this conversation as resolved.
Show resolved Hide resolved

const AutoCompleteTextarea: React.ForwardRefRenderFunction<HTMLTextAreaElement, ArgProps> = (props, ref) => {
return <Input {...props} as="textarea" ref={ref} />
}

// Refs: https://github.com/mastodon/mastodon/blob/7ecf783dd3bfc07f80aab495273b6d01ba972c40/app/javascript/mastodon/components/autosuggest_textarea.jsx#L11
const textAtCursorMatchesToken = (str: string, caretPosition: number) => {
h3poteto marked this conversation as resolved.
Show resolved Hide resolved
let word: string

let left = str.slice(0, caretPosition).search(/\S+$/)
h3poteto marked this conversation as resolved.
Show resolved Hide resolved
h3poteto marked this conversation as resolved.
Show resolved Hide resolved
let right = str.slice(caretPosition).search(/\s/)
h3poteto marked this conversation as resolved.
Show resolved Hide resolved
h3poteto marked this conversation as resolved.
Show resolved Hide resolved

if (right < 0) {
word = str.slice(left)
} else {
word = str.slice(left, right + caretPosition)
}

if (!word || word.trim().length < 3 || ['@', ':', '#'].indexOf(word[0]) === -1) {
return [null, null]
}

word = word.trim().toLowerCase()

if (word.length > 0) {
return [left + 1, word]
} else {
return [null, null]
}
}

export default AutoCompleteTextarea
3 changes: 2 additions & 1 deletion src/components/compose/Status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { CustomEmojiCategory } from 'src/entities/emoji'
import alert from 'src/components/utils/alert'
import { Account } from 'src/entities/account'
import { useTranslation } from 'react-i18next'
import AutoCompleteTextarea from './AutoCompleteTextarea'

type Props = {
server: Server
Expand Down Expand Up @@ -435,7 +436,7 @@ const privacyIcon = (visibility: 'public' | 'unlisted' | 'private' | 'direct') =
}
}

const Textarea = forwardRef<HTMLTextAreaElement>((props, ref) => <Input {...props} as="textarea" ref={ref} />)
const Textarea = forwardRef<HTMLTextAreaElement>(AutoCompleteTextarea)

const defaultPoll = () => ({
options: ['', ''],
Expand Down