Commit 425db4d4 by liimin

feat(components): 优化复制按钮并更新外部链接对话框

- 更新 CopyBtn 组件,使用 Remix 图标替换原有图标
- 改进 ExternalLink 组件,增加复制链接功能和新窗口打开按钮
- 调整 VoiceInput 组件的录音状态管理
- 优化 Markdown 组件的渲染逻辑
- 更新多语言翻译文件,添加复制功能相关文本
1 parent ae3743ab
......@@ -5,6 +5,7 @@ import { debounce } from 'lodash-es'
import copy from 'copy-to-clipboard'
import s from './style.module.css'
import Tooltip from '@/app/components/base/tooltip'
import { RiCheckboxMultipleFill, RiClipboardLine, RiFileCopy2Fill, RiFileCopy2Line, RiFileCopyFill } from '@remixicon/react'
type ICopyBtnProps = {
value: string
......@@ -31,11 +32,11 @@ const CopyBtn = ({
return (
<div className={`${className}`}>
<Tooltip
popupContent={(isCopied ? t('appApi.copied') : t('appApi.copy'))}
popupContent={(isCopied ? t('share.common.copied') : t('share.common.copy'))}
asChild={false}
>
<div
onMouseLeave={onMouseLeave}
// onMouseLeave={onMouseLeave}
className={'box-border flex cursor-pointer items-center justify-center rounded-md bg-components-button-secondary-bg p-0.5'}
style={!isPlain
? {
......@@ -44,7 +45,9 @@ const CopyBtn = ({
: {}}
onClick={onClickCopy}
>
<div className={`h-6 w-6 rounded-md hover:bg-components-button-secondary-bg-hover ${s.copyIcon} ${isCopied ? s.copied : ''}`}></div>
{!isCopied && <RiFileCopy2Line className="h-4 w-4" />}
{isCopied && <RiCheckboxMultipleFill className="h-4 w-4" />}
{/* <div className={`h-6 w-6 rounded-md hover:bg-components-button-secondary-bg-hover ${s.copied} ${isCopied ? s.copied : ''}`}></div> */}
</div>
</Tooltip>
</div>
......
......@@ -3,7 +3,7 @@ import Button from '@/app/components/base/button'
import cn from '@/utils/classnames'
import { isValidUrl } from './utils'
import { InputMessageType } from '@/app/components/base/chat/types'
const MarkdownButton = ({ node }:any) => {
const MarkdownButton = ({ node }: any) => {
const { onSend } = useChatContext()
const variant = node.properties.dataVariant
const message = node.properties.dataMessage
......@@ -20,7 +20,7 @@ const MarkdownButton = ({ node }:any) => {
return
}
if (message) {
onSend?.(message,InputMessageType.QUESTION_IN_ANSWER )
onSend?.(message, InputMessageType.QUESTION_IN_ANSWER)
// if (messageId) {
// document.getElementById(messageId)?.classList.add('pointer-events-none', 'opacity-50')
// }
......
......@@ -3,6 +3,8 @@ import { InputMessageType } from '../../../chat/types'
import { useChatContext } from '@/app/components/base/chat/chat-with-history/context/chat-context'
import type { LinkProps } from './index'
import { Dialog, DialogPanel, DialogTitle } from '@headlessui/react'
import Copy from '@/app/components/base/chat/chat/answer/components/operation/copy'
import CopyBtn from '@/app/components/base/copy-btn'
const ExternalLink: FC<LinkProps> = ({ node, children }) => {
const [isOpen, setIsOpen] = useState(false)
const [isLoading, setIsLoading] = useState(true)
......@@ -23,56 +25,72 @@ const ExternalLink: FC<LinkProps> = ({ node, children }) => {
{children || 'Open'}
</a>
<Dialog open={isOpen} onClose={() => setIsOpen(false)} className="relative z-50">
<div className="fixed inset-0 bg-black/30" aria-hidden="true" />
<Dialog open={isOpen} onClose={() => setIsOpen(false)} className="relative z-50">
<div className="fixed inset-0 bg-black/30" aria-hidden="true" />
<div className="fixed inset-0 flex items-center justify-center p-1">
<DialogPanel className="w-full h-full bg-gray-800/80 backdrop-blur-sm rounded-lg flex flex-col border-8 border-t-0 border-transparent">
<div className="flex justify-between items-center border-b border-gray-600 h-8">
{/* <DialogTitle className="text-sm font-medium text-white/80">外部页面</DialogTitle> */}
<button
onClick={() => setIsOpen(false)}
className="ml-auto p-1 rounded-full hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-primary-500">
<span className="sr-only">关闭</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" className="w-5 h-5 text-gray-400 hover:text-white">
<path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />
</svg>
</button>
</div>
<div className="fixed inset-0 flex items-center justify-center h-full">
<DialogPanel className="w-full h-full backdrop-blur-sm rounded-lg flex flex-col">
<div className="flex bg-white h-8 border-b border-gray-200">
{/* 左侧:自适应宽度区域 */}
<div className="flex-1 flex items-center overflow-hidden">
<span className="text-sm text-gray-500 truncate">{node.properties.href}</span>
<CopyBtn value={node.properties.href} className="ml-2" />
</div>
<div className="flex-1 relative flex items-center justify-center">
{isLoading && (
<div className="absolute inset-0 flex items-center justify-center">
<div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary-500" />
</div>
)}
{/* 右侧:按钮组固定宽度 */}
<div className="flex items-center gap-2">
<button
onClick={() => window.open(node.properties.href, '_blank')}
className="px-2 py-1 text-xs bg-primary-500 text-white rounded hover:bg-primary-600">
新窗口打开
</button>
<button
onClick={() => setIsOpen(false)}
className="p-1 rounded-full bg-gray-400 hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-primary-500">
<span className="sr-only">关闭</span>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
className="w-5 h-5 text-blue-500-600 hover:text-white">
<path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />
</svg>
</button>
</div>
</div>
<div className="flex-1 relative flex items-center justify-center p-4 pt-2">
{isLoading && (
<div className="absolute inset-0 flex items-center justify-center">
<div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary-500" />
</div>
)}
{iframeError ? (
<div className="absolute inset-0 flex items-center justify-center flex-col gap-2">
<p className="text-red-400">该页面不支持内嵌显示</p>
<button
onClick={() => window.open(node.properties.href, '_blank')}
className="px-4 py-2 bg-primary-500 text-white rounded hover:bg-primary-600">
在浏览器中打开
</button>
</div>
) : (
<iframe
src={node.properties.href}
className="w-full max-w-[800px] h-full border-0 mx-auto"
allow="fullscreen"
loading="lazy"
onLoad={() => setIsLoading(false)}
onError={() => {
setIsLoading(false)
setIframeError(true)
}}
/>
)}
</div>
</DialogPanel>
</div>
</Dialog>
{iframeError ? (
<div className="absolute inset-0 flex items-center justify-center flex-col gap-2">
<p className="text-red-400">该页面不支持内嵌显示</p>
<button
onClick={() => window.open(node.properties.href, '_blank')}
className="px-4 py-2 bg-primary-500 text-white rounded hover:bg-primary-600">
在浏览器中打开
</button>
</div>
) : (
<iframe
src={node.properties.href}
className="w-full max-w-[800px] h-full bg-white border-0 mx-auto"
allow="fullscreen"
loading="lazy"
onLoad={() => setIsLoading(false)}
onError={() => {
setIsLoading(false)
setIframeError(true)
}}
/>
)}
</div>
</DialogPanel>
</div>
</Dialog>
</>
)
}
......
......@@ -26,60 +26,57 @@ type MarkdownProps = {
operationClassName?: string
}
export function Markdown(props: MarkdownProps) {
const markdown = useMemo(() => {
const latexContent = flow([preprocessThinkTag, preprocessLaTeX])(props.content)
return (
<div className={cn('markdown-body', '!text-text-primary', props.className)}>
<ReactMarkdown
remarkPlugins={[RemarkGfm, [RemarkMath, { singleDollarTextMath: false }], RemarkBreaks]}
rehypePlugins={[
RehypeKatex,
RehypeRaw as any,
// The Rehype plug-in is used to remove the ref attribute of an element
() => {
return (tree: any) => {
const iterate = (node: any) => {
if (node.type === 'element' && node.properties?.ref) delete node.properties.ref
const latexContent = flow([preprocessThinkTag, preprocessLaTeX])(props.content)
return (
<div className={cn('markdown-body', '!text-text-primary', props.className)}>
<ReactMarkdown
remarkPlugins={[RemarkGfm, [RemarkMath, { singleDollarTextMath: false }], RemarkBreaks]}
rehypePlugins={[
RehypeKatex,
RehypeRaw as any,
// The Rehype plug-in is used to remove the ref attribute of an element
() => {
return (tree: any) => {
const iterate = (node: any) => {
if (node.type === 'element' && node.properties?.ref) delete node.properties.ref
if (node.type === 'element' && !/^[a-z][a-z0-9]*$/i.test(node.tagName)) {
node.type = 'text'
node.value = `<${node.tagName}`
}
if (node.children) node.children.forEach(iterate)
if (node.type === 'element' && !/^[a-z][a-z0-9]*$/i.test(node.tagName)) {
node.type = 'text'
node.value = `<${node.tagName}`
}
tree.children.forEach(iterate)
if (node.children) node.children.forEach(iterate)
}
},
]}
urlTransform={customUrlTransform}
disallowedElements={[
'iframe',
'head',
'html',
'meta',
'link',
'style',
'body',
...(props.customDisallowedElements || []),
]}
components={{
code: Code,
img: Img,
video: Video,
audio: Audio,
a: Link,
p: Paragraph,
button: MarkdownButton,
form: MarkdownForm,
script: Script as any,
details: Think,
}}>
{/* Markdown detect has problem. */}
{latexContent}
</ReactMarkdown>
</div>
)
}, [props.content])
return markdown
tree.children.forEach(iterate)
}
},
]}
urlTransform={customUrlTransform}
disallowedElements={[
'iframe',
'head',
'html',
'meta',
'link',
'style',
'body',
...(props.customDisallowedElements || []),
]}
components={{
code: Code,
img: Img,
video: Video,
audio: Audio,
a: Link,
p: Paragraph,
button: MarkdownButton,
form: MarkdownForm,
script: Script as any,
details: Think,
}}>
{/* Markdown detect has problem. */}
{latexContent}
</ReactMarkdown>
</div>
)
}
......@@ -24,8 +24,9 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
const ctxRef = useRef<CanvasRenderingContext2D | null>(null)
const drawRecordId = useRef<number | null>(null)
const [originDuration, setOriginDuration] = useState(0)
const [startRecord, setStartRecord] = useState(false)
const [startConvert, setStartConvert] = useState(false)
// const [startRecord, setStartRecord] = useState(false)
// const [startConvert, setStartConvert] = useState(false)
const [statement, setStatement] = useState<'recording' | 'converting' | 'none'>('none')
const pathname = usePathname()
const params = useParams()
const clearInterval = useRafInterval(() => {
......@@ -44,7 +45,7 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
const drawRecord = useCallback(() => {
if (!recorder.current) return
drawRecordId.current = requestAnimationFrame(drawRecord)
const canvas = canvasRef.current!
const ctx = ctxRef.current!
......@@ -77,19 +78,18 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
const handleStopRecorder = useCallback(async () => {
clearInterval()
setStartRecord(false)
setStartConvert(true)
// setStartRecord(false)
// setStartConvert(true)
setStatement('converting')
// 修改点3: 安全访问recorder实例
if (recorder.current) {
recorder.current.stop()
drawRecordId.current && cancelAnimationFrame(drawRecordId.current)
drawRecordId.current = null
const canvas = canvasRef.current!
const ctx = ctxRef.current!
ctx.clearRect(0, 0, canvas.width, canvas.height)
const mp3Blob = convertToMp3(recorder.current)
const mp3File = new File([mp3Blob], 'temp.mp3', { type: 'audio/mp3' })
const formData = new FormData()
......@@ -110,9 +110,10 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
try {
const audioResponse = await audioToText(url, isPublic, formData)
onConverted(audioResponse.text)
onCancel()
} catch (e) {
onConverted('')
} finally {
setStatement('none')
onCancel()
}
}
......@@ -122,26 +123,23 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
try {
// 修改点4: 确保清理之前的录音实例
if (recorder.current) {
try {
if (recorder.current.isRecording) {
await recorder.current.stop()
}
recorder.current.destroy()
} catch (e) {
console.error('Error destroying previous recorder:', e)
}
// if (recorder.current.isRecording) {
recorder.current.stop()
// }
recorder.current.destroy()
}
// 创建新的录音实例
recorder.current = createRecorder()
await recorder.current.start()
setStartRecord(true)
setStartConvert(false)
// setStartRecord(true)
// setStartConvert(false)
setStatement('recording')
setOriginDuration(0) // 重置录音时间
// 修改点5: 确保canvas正确初始化
initCanvas()
if (canvasRef.current && ctxRef.current) {
drawRecord()
} else {
......@@ -180,28 +178,24 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
// 修改点6: 添加录音超时处理
useEffect(() => {
if (originDuration >= 600 && startRecord) {
if (originDuration >= 600 && statement === 'recording') {
handleStopRecorder()
}
}, [originDuration, startRecord, handleStopRecorder])
}, [originDuration, statement, handleStopRecorder])
useEffect(() => {
handleStartRecord()
// 修改点7: 清理函数确保释放资源
return () => {
if (recorder.current) {
try {
if (recorder.current.isRecording) {
recorder.current.stop()
}
recorder.current.destroy()
} catch (e) {
console.error('Cleanup error:', e)
}
// if (recorder.current.isRecording) {
recorder.current.stop()
// }
recorder.current.destroy()
recorder.current = null
}
drawRecordId.current && cancelAnimationFrame(drawRecordId.current)
clearInterval()
}
......@@ -212,25 +206,30 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
return (
<div className={cn(s.wrapper, 'absolute inset-0 rounded-xl')}>
{/* <div className="absolute inset-[1.5px] flex items-center overflow-hidden rounded-[10.5px] bg-primary-25 py-[14px] pl-[14.5px] pr-[6.5px]"></div> */}
{/* <div className="absolute inset-[1.5px] flex items-center overflow-hidden rounded-[10.5px] bg-primary-25 py-[14px] pl-[14.5px] pr-[6.5px]"></div> */}
<div className="absolute inset-[1.5px] flex items-center overflow-hidden rounded-[10.5px] bg-primary-25 pl-[14.5px] pr-[6.5px]">
<canvas id="voice-input-record" className="absolute bottom-0 left-0 h-5 w-full" />
{startConvert && <RiLoader2Line className="mr-2 h-4 w-4 animate-spin text-primary-700" />}
{statement === 'converting' && <RiLoader2Line className="mr-2 h-4 w-4 animate-spin text-primary-700" />}
<div className="grow">
{startRecord && <div className="text-sm text-gray-500">{t('common.voiceInput.speaking')}</div>}
{startConvert && <div className={cn(s.convert, 'text-sm')}>{t('common.voiceInput.converting')}</div>}
{statement === 'recording' && <div className="text-sm text-gray-500">{t('common.voiceInput.speaking')}</div>}
{statement === 'converting' && (
<div className={cn(s.convert, 'text-sm')}>{t('common.voiceInput.converting')}</div>
)}
</div>
{startRecord && (
<div className="mr-1 flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-primary-100" onClick={handleStopRecorder}>
{statement === 'recording' && (
<div
className="mr-1 flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-primary-100"
onClick={handleStopRecorder}>
<StopCircle className="h-5 w-5 text-primary-600" />
</div>
)}
{startConvert && (
{/* {statement==='converting' && (
<div className="mr-1 flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-gray-200" onClick={onCancel}>
<RiCloseLine className="h-4 w-4 text-gray-500" />
</div>
)}
<div className={`w-[45px] pl-1 text-xs font-medium ${originDuration > 500 ? 'text-[#F04438]' : 'text-gray-700'}`}>
)} */}
<div
className={`w-[45px] pl-1 text-xs font-medium ${originDuration > 500 ? 'text-[#F04438]' : 'text-gray-700'}`}>
{`${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`}
</div>
</div>
......
......@@ -3,6 +3,8 @@ const translation = {
welcome: '',
appUnavailable: 'App is unavailable',
appUnknownError: 'App is unavailable',
copy: 'Copy',
copied: 'Copied',
},
chat: {
newChat: 'Start New chat',
......
......@@ -3,6 +3,8 @@ const translation = {
welcome: '',
appUnavailable: '应用不可用',
appUnknownError: '应用不可用',
copy: '复制',
copied: '已复制',
},
chat: {
newChat: '开启新对话',
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!