Browse Source

feat: add shortcut key support for posting

imwald
codytseng 10 months ago
parent
commit
bf7bb286af
  1. 10
      src/components/PostEditor/PostContent.tsx
  2. 14
      src/components/PostEditor/PostTextarea/index.tsx

10
src/components/PostEditor/PostContent.tsx

@ -38,13 +38,10 @@ export default function PostContent({ @@ -38,13 +38,10 @@ export default function PostContent({
const [mentions, setMentions] = useState<string[]>([])
const canPost = !!text && !posting && !uploadingFiles
const post = async (e: React.MouseEvent) => {
e.stopPropagation()
const post = async (e?: React.MouseEvent) => {
e?.stopPropagation()
checkLogin(async () => {
if (!canPost) {
close()
return
}
if (!canPost) return
setPosting(true)
try {
@ -105,6 +102,7 @@ export default function PostContent({ @@ -105,6 +102,7 @@ export default function PostContent({
setText={setText}
defaultContent={defaultContent}
parentEvent={parentEvent}
onSubmit={() => post()}
/>
<SendOnlyToSwitch
parentEvent={parentEvent}

14
src/components/PostEditor/PostTextarea/index.tsx

@ -11,11 +11,11 @@ import { EditorContent, useEditor } from '@tiptap/react' @@ -11,11 +11,11 @@ import { EditorContent, useEditor } from '@tiptap/react'
import { Event } from 'nostr-tools'
import { Dispatch, forwardRef, SetStateAction, useImperativeHandle } from 'react'
import { useTranslation } from 'react-i18next'
import { usePostEditor } from '../PostEditorProvider'
import CustomMention from './CustomMention'
import { FileHandler } from './FileHandler'
import Preview from './Preview'
import suggestion from './suggestion'
import { usePostEditor } from '../PostEditorProvider'
export type TPostTextareaHandle = {
appendText: (text: string) => void
@ -28,8 +28,9 @@ const PostTextarea = forwardRef< @@ -28,8 +28,9 @@ const PostTextarea = forwardRef<
setText: Dispatch<SetStateAction<string>>
defaultContent?: string
parentEvent?: Event
onSubmit?: () => void
}
>(({ text = '', setText, defaultContent, parentEvent }, ref) => {
>(({ text = '', setText, defaultContent, parentEvent, onSubmit }, ref) => {
const { t } = useTranslation()
const { setUploadingFiles } = usePostEditor()
const editor = useEditor({
@ -54,6 +55,15 @@ const PostTextarea = forwardRef< @@ -54,6 +55,15 @@ const PostTextarea = forwardRef<
attributes: {
class:
'border rounded-lg p-3 min-h-52 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring'
},
handleKeyDown: (_view, event) => {
// Handle Ctrl+Enter or Cmd+Enter for submit
if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {
event.preventDefault()
onSubmit?.()
return true
}
return false
}
},
content: postContentCache.getPostCache({ defaultContent, parentEvent }),

Loading…
Cancel
Save