feat: add shortcut key support for posting

This commit is contained in:
codytseng
2025-05-25 16:45:20 +08:00
parent 8beb2430b2
commit bf7bb286af
2 changed files with 16 additions and 8 deletions

View File

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

View File

@@ -11,11 +11,11 @@ import { EditorContent, useEditor } from '@tiptap/react'
import { Event } from 'nostr-tools' import { Event } from 'nostr-tools'
import { Dispatch, forwardRef, SetStateAction, useImperativeHandle } from 'react' import { Dispatch, forwardRef, SetStateAction, useImperativeHandle } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { usePostEditor } from '../PostEditorProvider'
import CustomMention from './CustomMention' import CustomMention from './CustomMention'
import { FileHandler } from './FileHandler' import { FileHandler } from './FileHandler'
import Preview from './Preview' import Preview from './Preview'
import suggestion from './suggestion' import suggestion from './suggestion'
import { usePostEditor } from '../PostEditorProvider'
export type TPostTextareaHandle = { export type TPostTextareaHandle = {
appendText: (text: string) => void appendText: (text: string) => void
@@ -28,8 +28,9 @@ const PostTextarea = forwardRef<
setText: Dispatch<SetStateAction<string>> setText: Dispatch<SetStateAction<string>>
defaultContent?: string defaultContent?: string
parentEvent?: Event parentEvent?: Event
onSubmit?: () => void
} }
>(({ text = '', setText, defaultContent, parentEvent }, ref) => { >(({ text = '', setText, defaultContent, parentEvent, onSubmit }, ref) => {
const { t } = useTranslation() const { t } = useTranslation()
const { setUploadingFiles } = usePostEditor() const { setUploadingFiles } = usePostEditor()
const editor = useEditor({ const editor = useEditor({
@@ -54,6 +55,15 @@ const PostTextarea = forwardRef<
attributes: { attributes: {
class: class:
'border rounded-lg p-3 min-h-52 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring' '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 }), content: postContentCache.getPostCache({ defaultContent, parentEvent }),