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

feat: Backspace deletes clipboard text in MiniApp #1833

Merged
Show file tree
Hide file tree
Changes from all commits
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: 26 additions & 9 deletions src/renderer/src/windows/mini/home/HomeWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { uuid } from '@renderer/utils'
import { Divider } from 'antd'
import dayjs from 'dayjs'
import { isEmpty } from 'lodash'
import { FC, useCallback, useEffect, useState } from 'react'
import React, { FC, useCallback, useEffect, useState } from 'react'
import { useHotkeys } from 'react-hotkeys-hook'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'
Expand All @@ -25,6 +25,8 @@ const HomeWindow: FC = () => {
const [clipboardText, setClipboardText] = useState('')
const [selectedText, setSelectedText] = useState('')
const [text, setText] = useState('')
const [lastClipboardText, setLastClipboardText] = useState<string | null>(null)
const textChange = useState(() => {})[1]
const { defaultAssistant } = useDefaultAssistant()
const { defaultModel: model } = useDefaultModel()
const { language } = useSettings()
Expand All @@ -35,9 +37,12 @@ const HomeWindow: FC = () => {
const content = (referenceText === text ? text : `${referenceText}\n\n${text}`).trim()

const onReadClipboard = useCallback(async () => {
const text = await navigator.clipboard.readText()
setClipboardText(text.trim())
}, [])
const text = await navigator.clipboard.readText().catch(() => null)
if (text && text !== lastClipboardText) {
setLastClipboardText(text)
setClipboardText(text.trim())
}
}, [lastClipboardText])

useEffect(() => {
onReadClipboard()
Expand All @@ -50,9 +55,10 @@ const HomeWindow: FC = () => {
const onCloseWindow = () => window.api.miniWindow.hide()

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
const isEnterPressed = e.keyCode == 13
const isEnterPressed = e.code === 'Enter'
const isBackspacePressed = e.code === 'Backspace'

if (e.key === 'Escape') {
if (e.code === 'Escape') {
setText('')
setRoute('home')
route === 'home' && onCloseWindow()
Expand All @@ -67,6 +73,18 @@ const HomeWindow: FC = () => {
setTimeout(() => setText(''), 100)
}
}

if (isBackspacePressed) {
textChange(() => {
if (text.length === 0) {
clearClipboard()
}
})
}
}

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setText(e.target.value)
}

const onSendMessage = useCallback(
Expand Down Expand Up @@ -95,7 +113,6 @@ const HomeWindow: FC = () => {
const clearClipboard = () => {
setClipboardText('')
setSelectedText('')
navigator.clipboard.writeText('')
}

useHotkeys('esc', () => {
Expand Down Expand Up @@ -132,7 +149,7 @@ const HomeWindow: FC = () => {
referenceText={referenceText}
placeholder={t('miniwindow.input.placeholder.empty', { model: model.name })}
handleKeyDown={handleKeyDown}
setText={setText}
handleChange={handleChange}
/>
<Divider style={{ margin: '10px 0' }} />
</>
Expand Down Expand Up @@ -171,7 +188,7 @@ const HomeWindow: FC = () => {
: t('miniwindow.input.placeholder.empty', { model: model.name })
}
handleKeyDown={handleKeyDown}
setText={setText}
handleChange={handleChange}
/>
<Divider style={{ margin: '10px 0' }} />
<ClipboardPreview referenceText={referenceText} clearClipboard={clearClipboard} t={t} />
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/src/windows/mini/home/components/InputBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ModelAvatar from '@renderer/components/Avatar/ModelAvatar'
import { useRuntime } from '@renderer/hooks/useRuntime'
import { Input as AntdInput } from 'antd'
import { FC } from 'react'
import React, { FC } from 'react'
import styled from 'styled-components'

interface InputBarProps {
Expand All @@ -10,10 +10,10 @@ interface InputBarProps {
referenceText: string
placeholder: string
handleKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void
setText: (text: string) => void
handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void
}

const InputBar: FC<InputBarProps> = ({ text, model, placeholder, handleKeyDown, setText }) => {
const InputBar: FC<InputBarProps> = ({ text, model, placeholder, handleKeyDown, handleChange }) => {
const { generating } = useRuntime()
return (
<InputWrapper>
Expand All @@ -24,7 +24,7 @@ const InputBar: FC<InputBarProps> = ({ text, model, placeholder, handleKeyDown,
bordered={false}
autoFocus
onKeyDown={handleKeyDown}
onChange={(e) => setText(e.target.value)}
onChange={handleChange}
disabled={generating}
/>
</InputWrapper>
Expand Down