From bf7bb286afb4b842939d1440b21983a593a7f6d8 Mon Sep 17 00:00:00 2001 From: codytseng Date: Sun, 25 May 2025 16:45:20 +0800 Subject: [PATCH] feat: add shortcut key support for posting --- src/components/PostEditor/PostContent.tsx | 10 ++++------ src/components/PostEditor/PostTextarea/index.tsx | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/components/PostEditor/PostContent.tsx b/src/components/PostEditor/PostContent.tsx index ab12512..4db424b 100644 --- a/src/components/PostEditor/PostContent.tsx +++ b/src/components/PostEditor/PostContent.tsx @@ -38,13 +38,10 @@ export default function PostContent({ const [mentions, setMentions] = useState([]) 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({ setText={setText} defaultContent={defaultContent} parentEvent={parentEvent} + onSubmit={() => post()} /> void @@ -28,8 +28,9 @@ const PostTextarea = forwardRef< setText: Dispatch> 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< 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 }),