refactor: 优化eslint配置,提升代码质量 (#1341)

* feat: 统一并标准化eslint

* lint: napcat.webui

* lint: napcat.webui

* lint: napcat.core

* build: fix

* lint: napcat.webui

* refactor: 重构eslint

* Update README.md
This commit is contained in:
时瑾 authored and GitHub committed 2025-11-03 16:30:45 +08:00
1 parent d5b8f886d6
commit 06f6a542f5
651 files changed
+37978 -37584

No files matched your search

@@ -1,170 +1,170 @@
import { Button } from '@heroui/button'
import { Input } from '@heroui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover'
import { Tooltip } from '@heroui/tooltip'
import clsx from 'clsx'
import { useEffect, useRef, useState } from 'react'
import toast from 'react-hot-toast'
import { FaMicrophone } from 'react-icons/fa6'
import { IoMic } from 'react-icons/io5'
import { MdEdit, MdUpload } from 'react-icons/md'
import { Button } from '@heroui/button';
import { Input } from '@heroui/input';
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover';
import { Tooltip } from '@heroui/tooltip';
import clsx from 'clsx';
import { useEffect, useRef, useState } from 'react';
import toast from 'react-hot-toast';
import { FaMicrophone } from 'react-icons/fa6';
import { IoMic } from 'react-icons/io5';
import { MdEdit, MdUpload } from 'react-icons/md';
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message'
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message';
import { isURI } from '@/utils/url'
import { isURI } from '@/utils/url';
import type { OB11Segment } from '@/types/onebot'
import type { OB11Segment } from '@/types/onebot';
const AudioInsert = () => {
const [audioUrl, setAudioUrl] = useState<string>('')
const audioInputRef = useRef<HTMLInputElement>(null)
const showStructuredMessage = useShowStructuredMessage()
const [audioUrl, setAudioUrl] = useState<string>('');
const audioInputRef = useRef<HTMLInputElement>(null);
const showStructuredMessage = useShowStructuredMessage();
const showAudioSegment = (file: string) => {
const messages: OB11Segment[] = [
{
type: 'record',
data: {
file: file
}
}
]
showStructuredMessage(messages)
}
file,
},
},
];
showStructuredMessage(messages);
};
const [isRecording, setIsRecording] = useState(false)
const mediaRecorderRef = useRef<MediaRecorder | null>(null)
const audioChunksRef = useRef<Blob[]>([])
const [audioPreview, setAudioPreview] = useState<string | null>(null)
const [showPreview, setShowPreview] = useState(false)
const streamRef = useRef<MediaStream | null>(null)
const [recordingTime, setRecordingTime] = useState(0)
const recordingIntervalRef = useRef<NodeJS.Timeout | null>(null)
const [isRecording, setIsRecording] = useState(false);
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
const audioChunksRef = useRef<Blob[]>([]);
const [audioPreview, setAudioPreview] = useState<string | null>(null);
const [showPreview, setShowPreview] = useState(false);
const streamRef = useRef<MediaStream | null>(null);
const [recordingTime, setRecordingTime] = useState(0);
const recordingIntervalRef = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
if (isRecording) {
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
streamRef.current = stream
const recorder = new MediaRecorder(stream)
mediaRecorderRef.current = recorder
recorder.start()
streamRef.current = stream;
const recorder = new MediaRecorder(stream);
mediaRecorderRef.current = recorder;
recorder.start();
recorder.ondataavailable = (event) => {
if (event.data.size > 0) {
audioChunksRef.current.push(event.data)
audioChunksRef.current.push(event.data);
}
}
};
recorder.onstop = () => {
if (audioChunksRef.current.length > 0) {
const audioBlob = new Blob(audioChunksRef.current, {
type: 'audio/wav'
})
const reader = new FileReader()
reader.readAsDataURL(audioBlob)
type: 'audio/wav',
});
const reader = new FileReader();
reader.readAsDataURL(audioBlob);
reader.onloadend = () => {
const base64Audio = reader.result as string
setAudioPreview(base64Audio)
setShowPreview(true)
}
audioChunksRef.current = []
const base64Audio = reader.result as string;
setAudioPreview(base64Audio);
setShowPreview(true);
};
audioChunksRef.current = [];
}
stream.getTracks().forEach((track) => track.stop())
}
})
stream.getTracks().forEach((track) => track.stop());
};
});
recordingIntervalRef.current = setInterval(() => {
setRecordingTime((prevTime) => prevTime + 1)
}, 1000)
setRecordingTime((prevTime) => prevTime + 1);
}, 1000);
} else {
mediaRecorderRef.current?.stop()
mediaRecorderRef.current?.stop();
if (recordingIntervalRef.current) {
clearInterval(recordingIntervalRef.current)
recordingIntervalRef.current = null
clearInterval(recordingIntervalRef.current);
recordingIntervalRef.current = null;
}
}
}, [isRecording])
}, [isRecording]);
const startRecording = () => {
setAudioPreview(null)
setShowPreview(false)
setRecordingTime(0)
setIsRecording(true)
}
setAudioPreview(null);
setShowPreview(false);
setRecordingTime(0);
setIsRecording(true);
};
const stopRecording = () => {
setIsRecording(false)
}
setIsRecording(false);
};
const handleShowPreview = () => {
if (audioPreview) {
showAudioSegment(audioPreview)
showAudioSegment(audioPreview);
}
}
};
const formatTime = (time: number) => {
const minutes = Math.floor(time / 60)
const seconds = time % 60
return `${minutes}:${seconds.toString().padStart(2, '0')}`
}
const minutes = Math.floor(time / 60);
const seconds = time % 60;
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
};
return (
<>
<Popover>
<Tooltip content="发送音频">
<div className="max-w-fit">
<Tooltip content='发送音频'>
<div className='max-w-fit'>
<PopoverTrigger>
<Button color="primary" variant="flat" isIconOnly radius="full">
<IoMic className="text-xl" />
<Button color='primary' variant='flat' isIconOnly radius='full'>
<IoMic className='text-xl' />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-2 p-4">
<Tooltip content="上传音频">
<PopoverContent className='flex-row gap-2 p-4'>
<Tooltip content='上传音频'>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
onPress={() => {
audioInputRef?.current?.click()
audioInputRef?.current?.click();
}}
>
<MdUpload />
</Button>
</Tooltip>
<Popover>
<Tooltip content="输入音频地址">
<div className="max-w-fit">
<PopoverTrigger tooltip="输入音频地址">
<Tooltip content='输入音频地址'>
<div className='max-w-fit'>
<PopoverTrigger tooltip='输入音频地址'>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
>
<MdEdit />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-1 p-2">
<PopoverContent className='flex-row gap-1 p-2'>
<Input
value={audioUrl}
onChange={(e) => setAudioUrl(e.target.value)}
placeholder="请输入音频地址"
placeholder='请输入音频地址'
/>
<Button
color="primary"
variant="flat"
color='primary'
variant='flat'
isIconOnly
radius="full"
radius='full'
onPress={() => {
if (!isURI(audioUrl)) {
toast.error('请输入正确的音频地址')
return
toast.error('请输入正确的音频地址');
return;
}
showAudioSegment(audioUrl)
setAudioUrl('')
showAudioSegment(audioUrl);
setAudioUrl('');
}}
>
<FaMicrophone />
@@ -172,34 +172,34 @@ const AudioInsert = () => {
</PopoverContent>
</Popover>
<Popover>
<Tooltip content="录制音频">
<div className="max-w-fit">
<Tooltip content='录制音频'>
<div className='max-w-fit'>
<PopoverTrigger>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
>
<IoMic />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-col gap-2 p-4">
<div className="flex gap-2">
<PopoverContent className='flex-col gap-2 p-4'>
<div className='flex gap-2'>
<Button
color={isRecording ? 'primary' : 'primary'}
variant="flat"
variant='flat'
onPress={isRecording ? stopRecording : startRecording}
>
{isRecording ? '停止录制' : '开始录制'}
</Button>
{showPreview && audioPreview && (
<Button
color="primary"
variant="flat"
color='primary'
variant='flat'
onPress={handleShowPreview}
>
查看消息
@@ -207,7 +207,7 @@ const AudioInsert = () => {
)}
</div>
{(isRecording || audioPreview) && (
<div className="flex gap-1 items-center">
<div className='flex gap-1 items-center'>
<span
className={clsx(
'w-4 h-4 rounded-full',
@@ -215,7 +215,7 @@ const AudioInsert = () => {
? 'animate-pulse bg-primary-400'
: 'bg-success-400'
)}
></span>
/>
<span>录制时长: {formatTime(recordingTime)}</span>
</div>
)}
@@ -228,27 +228,27 @@ const AudioInsert = () => {
</Popover>
<input
type="file"
type='file'
ref={audioInputRef}
hidden
accept="audio/*"
className="hidden"
accept='audio/*'
className='hidden'
onChange={(e) => {
const file = e.target.files?.[0]
const file = e.target.files?.[0];
if (!file) {
return
return;
}
const reader = new FileReader()
reader.readAsDataURL(file)
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const dataURL = event.target?.result
showAudioSegment(dataURL as string)
e.target.value = ''
}
const dataURL = event.target?.result;
showAudioSegment(dataURL as string);
e.target.value = '';
};
}}
/>
</>
)
}
);
};
export default AudioInsert
export default AudioInsert;
@@ -1,31 +1,31 @@
import { Button } from '@heroui/button'
import { Tooltip } from '@heroui/tooltip'
import { BsDice3Fill } from 'react-icons/bs'
import { Button } from '@heroui/button';
import { Tooltip } from '@heroui/tooltip';
import { BsDice3Fill } from 'react-icons/bs';
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message'
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message';
const DiceInsert = () => {
const showStructuredMessage = useShowStructuredMessage()
const showStructuredMessage = useShowStructuredMessage();
return (
<Tooltip content="发送骰子">
<Tooltip content='发送骰子'>
<Button
color="primary"
variant="flat"
color='primary'
variant='flat'
isIconOnly
radius="full"
radius='full'
onPress={() => {
showStructuredMessage([
{
type: 'dice'
}
])
type: 'dice',
},
]);
}}
>
<BsDice3Fill className="text-lg" />
<BsDice3Fill className='text-lg' />
</Button>
</Tooltip>
)
}
);
};
export default DiceInsert
export default DiceInsert;
@@ -1,20 +1,20 @@
import { Button } from '@heroui/button'
import { Image } from '@heroui/image'
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover'
import { Tooltip } from '@heroui/tooltip'
import { data, getUrl } from 'qface'
import { useEffect, useRef, useState } from 'react'
import { MdEmojiEmotions } from 'react-icons/md'
import { Button } from '@heroui/button';
import { Image } from '@heroui/image';
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover';
import { Tooltip } from '@heroui/tooltip';
import { data, getUrl } from 'qface';
import { useEffect, useRef, useState } from 'react';
import { MdEmojiEmotions } from 'react-icons/md';
import { EmojiValue } from '../formats/emoji_blot'
import { EmojiValue } from '../formats/emoji_blot';
const emojis = data.map((item) => {
return {
alt: item.QDes,
src: getUrl(item.QSid),
id: item.QSid
} as EmojiValue
})
id: item.QSid,
} as EmojiValue;
});
export interface EmojiPickerProps {
onInsertEmoji: (emoji: EmojiValue) => void
@@ -22,62 +22,62 @@ export interface EmojiPickerProps {
}
const EmojiPicker = ({ onInsertEmoji, onOpenChange }: EmojiPickerProps) => {
const [visibleEmojis, setVisibleEmojis] = useState<EmojiValue[]>([])
const [isPopoverOpen, setIsPopoverOpen] = useState<boolean>(false)
const containerRef = useRef<HTMLDivElement>(null)
const [visibleEmojis, setVisibleEmojis] = useState<EmojiValue[]>([]);
const [isPopoverOpen, setIsPopoverOpen] = useState<boolean>(false);
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (isPopoverOpen) {
setVisibleEmojis([]) // Reset visible emojis
requestAnimationFrame(() => loadEmojis()) // Start loading emojis
setVisibleEmojis([]); // Reset visible emojis
requestAnimationFrame(() => loadEmojis()); // Start loading emojis
}
}, [isPopoverOpen])
}, [isPopoverOpen]);
const loadEmojis = (index = 0, batchSize = 10) => {
if (index < emojis.length) {
setVisibleEmojis((prev) => [
...prev,
...emojis.slice(index, index + batchSize)
])
requestAnimationFrame(() => loadEmojis(index + batchSize, batchSize))
...emojis.slice(index, index + batchSize),
]);
requestAnimationFrame(() => loadEmojis(index + batchSize, batchSize));
}
}
};
return (
<div ref={containerRef}>
<Popover
portalContainer={containerRef.current!}
shouldCloseOnScroll={false}
placement="right-start"
placement='right-start'
onOpenChange={(v) => {
onOpenChange(v)
setIsPopoverOpen(v)
onOpenChange(v);
setIsPopoverOpen(v);
}}
>
<Tooltip content="插入表情">
<div className="max-w-fit">
<Tooltip content='插入表情'>
<div className='max-w-fit'>
<PopoverTrigger>
<Button color="primary" variant="flat" isIconOnly radius="full">
<MdEmojiEmotions className="text-xl" />
<Button color='primary' variant='flat' isIconOnly radius='full'>
<MdEmojiEmotions className='text-xl' />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="grid grid-cols-8 gap-1 flex-wrap justify-start items-start overflow-y-auto max-w-full max-h-96 p-2">
<PopoverContent className='grid grid-cols-8 gap-1 flex-wrap justify-start items-start overflow-y-auto max-w-full max-h-96 p-2'>
{visibleEmojis.map((emoji) => (
<Button
key={emoji.id}
color="primary"
variant="flat"
color='primary'
variant='flat'
isIconOnly
radius="full"
radius='full'
onPress={() => onInsertEmoji(emoji)}
>
<Image src={emoji.src} alt={emoji.alt} className="w-6 h-6" />
<Image src={emoji.src} alt={emoji.alt} className='w-6 h-6' />
</Button>
))}
</PopoverContent>
</Popover>
</div>
)
}
);
};
export default EmojiPicker
export default EmojiPicker;
@@ -1,95 +1,95 @@
import { Button } from '@heroui/button'
import { Input } from '@heroui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover'
import { Tooltip } from '@heroui/tooltip'
import { useRef, useState } from 'react'
import toast from 'react-hot-toast'
import { FaFolder } from 'react-icons/fa6'
import { LuFilePlus2 } from 'react-icons/lu'
import { MdEdit, MdUpload } from 'react-icons/md'
import { Button } from '@heroui/button';
import { Input } from '@heroui/input';
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover';
import { Tooltip } from '@heroui/tooltip';
import { useRef, useState } from 'react';
import toast from 'react-hot-toast';
import { FaFolder } from 'react-icons/fa6';
import { LuFilePlus2 } from 'react-icons/lu';
import { MdEdit, MdUpload } from 'react-icons/md';
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message'
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message';
import { isURI } from '@/utils/url'
import { isURI } from '@/utils/url';
import type { OB11Segment } from '@/types/onebot'
import type { OB11Segment } from '@/types/onebot';
const FileInsert = () => {
const [fileUrl, setFileUrl] = useState<string>('')
const fileInputRef = useRef<HTMLInputElement>(null)
const showStructuredMessage = useShowStructuredMessage()
const [fileUrl, setFileUrl] = useState<string>('');
const fileInputRef = useRef<HTMLInputElement>(null);
const showStructuredMessage = useShowStructuredMessage();
const showFileSegment = (file: string) => {
const messages: OB11Segment[] = [
{
type: 'file',
data: {
file: file
}
}
]
showStructuredMessage(messages)
}
file,
},
},
];
showStructuredMessage(messages);
};
return (
<>
<Popover>
<Tooltip content="发送文件">
<div className="max-w-fit">
<Tooltip content='发送文件'>
<div className='max-w-fit'>
<PopoverTrigger>
<Button color="primary" variant="flat" isIconOnly radius="full">
<FaFolder className="text-lg" />
<Button color='primary' variant='flat' isIconOnly radius='full'>
<FaFolder className='text-lg' />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-2 p-4">
<Tooltip content="上传文件">
<PopoverContent className='flex-row gap-2 p-4'>
<Tooltip content='上传文件'>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
onPress={() => {
fileInputRef?.current?.click()
fileInputRef?.current?.click();
}}
>
<MdUpload />
</Button>
</Tooltip>
<Popover>
<Tooltip content="输入文件地址">
<div className="max-w-fit">
<PopoverTrigger tooltip="输入文件地址">
<Tooltip content='输入文件地址'>
<div className='max-w-fit'>
<PopoverTrigger tooltip='输入文件地址'>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
>
<MdEdit />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-1 p-2">
<PopoverContent className='flex-row gap-1 p-2'>
<Input
value={fileUrl}
onChange={(e) => setFileUrl(e.target.value)}
placeholder="请输入文件地址"
placeholder='请输入文件地址'
/>
<Button
color="primary"
variant="flat"
color='primary'
variant='flat'
isIconOnly
radius="full"
radius='full'
onPress={() => {
if (!isURI(fileUrl)) {
toast.error('请输入正确的文件地址')
return
toast.error('请输入正确的文件地址');
return;
}
showFileSegment(fileUrl)
setFileUrl('')
showFileSegment(fileUrl);
setFileUrl('');
}}
>
<LuFilePlus2 />
@@ -100,26 +100,26 @@ const FileInsert = () => {
</Popover>
<input
type="file"
type='file'
ref={fileInputRef}
hidden
className="hidden"
className='hidden'
onChange={(e) => {
const file = e.target.files?.[0]
const file = e.target.files?.[0];
if (!file) {
return
return;
}
const reader = new FileReader()
reader.readAsDataURL(file)
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const dataURL = event.target?.result
showFileSegment(dataURL as string)
e.target.value = ''
}
const dataURL = event.target?.result;
showFileSegment(dataURL as string);
e.target.value = '';
};
}}
/>
</>
)
}
);
};
export default FileInsert
export default FileInsert;
@@ -1,12 +1,12 @@
import { Button } from '@heroui/button'
import { Input } from '@heroui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover'
import { Tooltip } from '@heroui/tooltip'
import { useRef, useState } from 'react'
import toast from 'react-hot-toast'
import { MdAddPhotoAlternate, MdEdit, MdImage, MdUpload } from 'react-icons/md'
import { Button } from '@heroui/button';
import { Input } from '@heroui/input';
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover';
import { Tooltip } from '@heroui/tooltip';
import { useRef, useState } from 'react';
import toast from 'react-hot-toast';
import { MdAddPhotoAlternate, MdEdit, MdImage, MdUpload } from 'react-icons/md';
import { isURI } from '@/utils/url'
import { isURI } from '@/utils/url';
export interface ImageInsertProps {
insertImage: (url: string) => void
@@ -14,70 +14,70 @@ export interface ImageInsertProps {
}
const ImageInsert = ({ insertImage, onOpenChange }: ImageInsertProps) => {
const [imgUrl, setImgUrl] = useState<string>('')
const imageInputRef = useRef<HTMLInputElement>(null)
const [imgUrl, setImgUrl] = useState<string>('');
const imageInputRef = useRef<HTMLInputElement>(null);
return (
<>
<Popover onOpenChange={onOpenChange}>
<Tooltip content="插入图片">
<div className="max-w-fit">
<Tooltip content='插入图片'>
<div className='max-w-fit'>
<PopoverTrigger>
<Button color="primary" variant="flat" isIconOnly radius="full">
<MdImage className="text-xl" />
<Button color='primary' variant='flat' isIconOnly radius='full'>
<MdImage className='text-xl' />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-2 p-4">
<Tooltip content="上传图片">
<PopoverContent className='flex-row gap-2 p-4'>
<Tooltip content='上传图片'>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
onPress={() => {
imageInputRef?.current?.click()
imageInputRef?.current?.click();
}}
>
<MdUpload />
</Button>
</Tooltip>
<Popover>
<Tooltip content="输入图片地址">
<div className="max-w-fit">
<PopoverTrigger tooltip="输入图片地址">
<Tooltip content='输入图片地址'>
<div className='max-w-fit'>
<PopoverTrigger tooltip='输入图片地址'>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
>
<MdEdit />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-1 p-2">
<PopoverContent className='flex-row gap-1 p-2'>
<Input
value={imgUrl}
onChange={(e) => setImgUrl(e.target.value)}
placeholder="请输入图片地址"
placeholder='请输入图片地址'
/>
<Button
color="primary"
variant="flat"
color='primary'
variant='flat'
isIconOnly
radius="full"
radius='full'
onPress={() => {
if (!isURI(imgUrl)) {
toast.error('请输入正确的图片地址')
return
toast.error('请输入正确的图片地址');
return;
}
insertImage(imgUrl)
setImgUrl('')
insertImage(imgUrl);
setImgUrl('');
}}
>
<MdAddPhotoAlternate />
@@ -88,27 +88,27 @@ const ImageInsert = ({ insertImage, onOpenChange }: ImageInsertProps) => {
</Popover>
<input
type="file"
type='file'
ref={imageInputRef}
hidden
accept="image/*"
className="hidden"
accept='image/*'
className='hidden'
onChange={(e) => {
const file = e.target.files?.[0]
const file = e.target.files?.[0];
if (!file) {
return
return;
}
const reader = new FileReader()
reader.readAsDataURL(file)
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const dataURL = event.target?.result
insertImage(dataURL as string)
e.target.value = ''
}
const dataURL = event.target?.result;
insertImage(dataURL as string);
e.target.value = '';
};
}}
/>
</>
)
}
);
};
export default ImageInsert
export default ImageInsert;
@@ -1,35 +1,35 @@
import { Button } from '@heroui/button'
import { Form } from '@heroui/form'
import { Input } from '@heroui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover'
import { Select, SelectItem } from '@heroui/select'
import type { SharedSelection } from '@heroui/system'
import { Tab, Tabs } from '@heroui/tabs'
import { Tooltip } from '@heroui/tooltip'
import type { Key } from '@react-types/shared'
import { useRef, useState } from 'react'
import { Controller, useForm } from 'react-hook-form'
import toast from 'react-hot-toast'
import { IoMusicalNotes } from 'react-icons/io5'
import { TbMusicPlus } from 'react-icons/tb'
import { Button } from '@heroui/button';
import { Form } from '@heroui/form';
import { Input } from '@heroui/input';
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover';
import { Select, SelectItem } from '@heroui/select';
import type { SharedSelection } from '@heroui/system';
import { Tab, Tabs } from '@heroui/tabs';
import { Tooltip } from '@heroui/tooltip';
import type { Key } from '@react-types/shared';
import { useRef, useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import toast from 'react-hot-toast';
import { IoMusicalNotes } from 'react-icons/io5';
import { TbMusicPlus } from 'react-icons/tb';
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message'
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message';
import { isURI } from '@/utils/url'
import { isURI } from '@/utils/url';
import type {
CustomMusicSegment,
MusicSegment,
OB11Segment
} from '@/types/onebot'
OB11Segment,
} from '@/types/onebot';
type MusicData = CustomMusicSegment['data'] | MusicSegment['data']
type MusicData = CustomMusicSegment['data'] | MusicSegment['data'];
const MusicInsert = () => {
const [musicId, setMusicId] = useState<string>('')
const [musicType, setMusicType] = useState<SharedSelection>(new Set(['163']))
const [mode, setMode] = useState<Key>('default')
const containerRef = useRef<HTMLDivElement>(null)
const [musicId, setMusicId] = useState<string>('');
const [musicType, setMusicType] = useState<SharedSelection>(new Set(['163']));
const [mode, setMode] = useState<Key>('default');
const containerRef = useRef<HTMLDivElement>(null);
const { control, handleSubmit, reset } = useForm<
Omit<CustomMusicSegment['data'], 'type'>
>({
@@ -38,84 +38,84 @@ const MusicInsert = () => {
audio: '',
title: '',
image: '',
content: ''
}
})
const showStructuredMessage = useShowStructuredMessage()
content: '',
},
});
const showStructuredMessage = useShowStructuredMessage();
const showMusicSegment = (data: MusicData) => {
const messages: OB11Segment[] = []
const messages: OB11Segment[] = [];
if (data.type === 'custom') {
messages.push({
type: 'music',
data: {
...data,
type: 'custom'
}
})
type: 'custom',
},
});
} else {
messages.push({
type: 'music',
data
})
data,
});
}
showStructuredMessage(messages)
}
showStructuredMessage(messages);
};
const onSubmit = (data: Omit<CustomMusicSegment['data'], 'type'>) => {
showMusicSegment({
type: 'custom',
...data
})
reset()
}
...data,
});
reset();
};
return (
<div ref={containerRef} className="overflow-visible">
<div ref={containerRef} className='overflow-visible'>
<Popover
placement="right-start"
placement='right-start'
shouldCloseOnScroll={false}
portalContainer={containerRef.current!}
>
<Tooltip content="发送音乐">
<div className="max-w-fit">
<Tooltip content='发送音乐'>
<div className='max-w-fit'>
<PopoverTrigger>
<Button color="primary" variant="flat" isIconOnly radius="full">
<IoMusicalNotes className="text-xl" />
<Button color='primary' variant='flat' isIconOnly radius='full'>
<IoMusicalNotes className='text-xl' />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="gap-2 p-4">
<PopoverContent className='gap-2 p-4'>
<Tabs
placement="top"
className="w-96"
placement='top'
className='w-96'
fullWidth
selectedKey={mode}
onSelectionChange={(key) => {
if (key !== null) setMode(key)
if (key !== null) setMode(key);
}}
>
<Tab title="主流平台" key="default" className="flex flex-col gap-2">
<Tab title='主流平台' key='default' className='flex flex-col gap-2'>
<Select
onClick={(e) => e.stopPropagation()}
aria-label="音乐平台"
aria-label='音乐平台'
selectedKeys={musicType}
label="音乐平台"
placeholder="请选择音乐平台"
label='音乐平台'
placeholder='请选择音乐平台'
items={[
{
name: 'QQ音乐',
id: 'qq'
id: 'qq',
},
{
name: '网易云音乐',
id: '163'
id: '163',
},
{
name: '虾米音乐',
id: 'xm'
}
id: 'xm',
},
]}
onSelectionChange={setMusicType}
>
@@ -128,27 +128,27 @@ const MusicInsert = () => {
<Input
value={musicId}
onChange={(e) => setMusicId(e.target.value)}
placeholder="请输入音乐ID"
label="音乐ID"
placeholder='请输入音乐ID'
label='音乐ID'
/>
<Button
fullWidth
size="lg"
color="primary"
variant="flat"
radius="full"
size='lg'
color='primary'
variant='flat'
radius='full'
onPress={() => {
if (!musicId) {
toast.error('请输入音乐ID')
return
toast.error('请输入音乐ID');
return;
}
showMusicSegment({
type: Array.from(
musicType
)[0] as MusicSegment['data']['type'],
id: musicId
})
setMusicId('')
id: musicId,
});
setMusicId('');
}}
startContent={<TbMusicPlus />}
>
@@ -156,92 +156,92 @@ const MusicInsert = () => {
</Button>
</Tab>
<Tab
title="自定义音乐"
key="custom"
className="flex flex-col gap-2"
title='自定义音乐'
key='custom'
className='flex flex-col gap-2'
>
<Form
onSubmit={handleSubmit(onSubmit)}
className="flex flex-col gap-2"
validationBehavior="native"
className='flex flex-col gap-2'
validationBehavior='native'
>
<Controller
name="url"
name='url'
control={control}
render={({ field }) => (
<Input
{...field}
isRequired
validate={(v) => {
return !isURI(v) ? '请输入正确的音乐URL' : null
return !isURI(v) ? '请输入正确的音乐URL' : null;
}}
size="sm"
placeholder="请输入音乐URL"
label="音乐URL"
size='sm'
placeholder='请输入音乐URL'
label='音乐URL'
/>
)}
/>
<Controller
name="audio"
name='audio'
control={control}
render={({ field }) => (
<Input
{...field}
isRequired
validate={(v) => {
return !isURI(v) ? '请输入正确的音频URL' : null
return !isURI(v) ? '请输入正确的音频URL' : null;
}}
size="sm"
placeholder="请输入音频URL"
label="音频URL"
size='sm'
placeholder='请输入音频URL'
label='音频URL'
/>
)}
/>
<Controller
name="title"
name='title'
control={control}
render={({ field }) => (
<Input
{...field}
isRequired
size="sm"
errorMessage="请输入音乐标题"
placeholder="请输入音乐标题"
label="音乐标题"
size='sm'
errorMessage='请输入音乐标题'
placeholder='请输入音乐标题'
label='音乐标题'
/>
)}
/>
<Controller
name="image"
name='image'
control={control}
render={({ field }) => (
<Input
{...field}
size="sm"
placeholder="请输入封面图片URL"
label="封面图片URL"
size='sm'
placeholder='请输入封面图片URL'
label='封面图片URL'
/>
)}
/>
<Controller
name="content"
name='content'
control={control}
render={({ field }) => (
<Input
{...field}
size="sm"
placeholder="请输入音乐描述"
label="音乐描述"
size='sm'
placeholder='请输入音乐描述'
label='音乐描述'
/>
)}
/>
<Button
fullWidth
size="lg"
color="primary"
variant="flat"
radius="full"
type="submit"
size='lg'
color='primary'
variant='flat'
radius='full'
type='submit'
startContent={<TbMusicPlus />}
>
创建自定义音乐
@@ -252,7 +252,7 @@ const MusicInsert = () => {
</PopoverContent>
</Popover>
</div>
)
}
);
};
export default MusicInsert
export default MusicInsert;
@@ -1,50 +1,50 @@
import { Button } from '@heroui/button'
import { Input } from '@heroui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover'
import { Tooltip } from '@heroui/tooltip'
import { useState } from 'react'
import { BsChatQuoteFill } from 'react-icons/bs'
import { MdAdd } from 'react-icons/md'
import { Button } from '@heroui/button';
import { Input } from '@heroui/input';
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover';
import { Tooltip } from '@heroui/tooltip';
import { useState } from 'react';
import { BsChatQuoteFill } from 'react-icons/bs';
import { MdAdd } from 'react-icons/md';
export interface ReplyInsertProps {
insertReply: (messageId: string) => void
}
const ReplyInsert = ({ insertReply }: ReplyInsertProps) => {
const [replyId, setReplyId] = useState<string>('')
const [replyId, setReplyId] = useState<string>('');
return (
<>
<Popover>
<Tooltip content="回复消息">
<div className="max-w-fit">
<Tooltip content='回复消息'>
<div className='max-w-fit'>
<PopoverTrigger>
<Button color="primary" variant="flat" isIconOnly radius="full">
<BsChatQuoteFill className="text-lg" />
<Button color='primary' variant='flat' isIconOnly radius='full'>
<BsChatQuoteFill className='text-lg' />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-2 p-4">
<PopoverContent className='flex-row gap-2 p-4'>
<Input
placeholder="输入消息 ID"
placeholder='输入消息 ID'
value={replyId}
onChange={(e) => {
const value = e.target.value
const isNumberReg = /^(?:0|(?:-?[1-9]\d*))$/
const value = e.target.value;
const isNumberReg = /^(?:0|(?:-?[1-9]\d*))$/;
if (isNumberReg.test(value)) {
setReplyId(value)
setReplyId(value);
}
}}
/>
<Button
color="primary"
variant="flat"
radius="full"
color='primary'
variant='flat'
radius='full'
isIconOnly
onPress={() => {
insertReply(replyId)
setReplyId('')
insertReply(replyId);
setReplyId('');
}}
>
<MdAdd />
@@ -52,7 +52,7 @@ const ReplyInsert = ({ insertReply }: ReplyInsertProps) => {
</PopoverContent>
</Popover>
</>
)
}
);
};
export default ReplyInsert
export default ReplyInsert;
@@ -1,31 +1,31 @@
import { Button } from '@heroui/button'
import { Tooltip } from '@heroui/tooltip'
import { LiaHandScissors } from 'react-icons/lia'
import { Button } from '@heroui/button';
import { Tooltip } from '@heroui/tooltip';
import { LiaHandScissors } from 'react-icons/lia';
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message'
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message';
const RPSInsert = () => {
const showStructuredMessage = useShowStructuredMessage()
const showStructuredMessage = useShowStructuredMessage();
return (
<Tooltip content="发送猜拳">
<Tooltip content='发送猜拳'>
<Button
color="primary"
variant="flat"
color='primary'
variant='flat'
isIconOnly
radius="full"
radius='full'
onPress={() => {
showStructuredMessage([
{
type: 'rps'
}
])
type: 'rps',
},
]);
}}
>
<LiaHandScissors className="text-2xl" />
<LiaHandScissors className='text-2xl' />
</Button>
</Tooltip>
)
}
);
};
export default RPSInsert
export default RPSInsert;
@@ -1,6 +1,6 @@
import { Snippet } from '@heroui/snippet'
import { Snippet } from '@heroui/snippet';
import { OB11Segment } from '@/types/onebot'
import { OB11Segment } from '@/types/onebot';
export interface ShowStructedMessageProps {
messages: OB11Segment[]
@@ -11,22 +11,22 @@ const ShowStructedMessage = ({ messages }: ShowStructedMessageProps) => {
<Snippet
hideSymbol
tooltipProps={{
content: '点击复制'
content: '点击复制',
}}
classNames={{
copyButton: 'self-start sticky top-0 right-0'
copyButton: 'self-start sticky top-0 right-0',
}}
className="bg-content1 h-96 overflow-y-scroll items-start"
className='bg-content1 h-96 overflow-y-scroll items-start'
>
{JSON.stringify(messages, null, 2)
.split('\n')
.map((line, i) => (
<span key={i} className="whitespace-pre-wrap break-all">
<span key={i} className='whitespace-pre-wrap break-all'>
{line}
</span>
))}
</Snippet>
)
}
);
};
export default ShowStructedMessage
export default ShowStructedMessage;
@@ -1,95 +1,95 @@
import { Button } from '@heroui/button'
import { Input } from '@heroui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover'
import { Tooltip } from '@heroui/tooltip'
import { useRef, useState } from 'react'
import toast from 'react-hot-toast'
import { IoVideocam } from 'react-icons/io5'
import { MdEdit, MdUpload } from 'react-icons/md'
import { TbVideoPlus } from 'react-icons/tb'
import { Button } from '@heroui/button';
import { Input } from '@heroui/input';
import { Popover, PopoverContent, PopoverTrigger } from '@heroui/popover';
import { Tooltip } from '@heroui/tooltip';
import { useRef, useState } from 'react';
import toast from 'react-hot-toast';
import { IoVideocam } from 'react-icons/io5';
import { MdEdit, MdUpload } from 'react-icons/md';
import { TbVideoPlus } from 'react-icons/tb';
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message'
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message';
import { isURI } from '@/utils/url'
import { isURI } from '@/utils/url';
import type { OB11Segment } from '@/types/onebot'
import type { OB11Segment } from '@/types/onebot';
const VideoInsert = () => {
const [videoUrl, setVideoUrl] = useState<string>('')
const videoInputRef = useRef<HTMLInputElement>(null)
const showStructuredMessage = useShowStructuredMessage()
const [videoUrl, setVideoUrl] = useState<string>('');
const videoInputRef = useRef<HTMLInputElement>(null);
const showStructuredMessage = useShowStructuredMessage();
const showVideoSegment = (file: string) => {
const messages: OB11Segment[] = [
{
type: 'video',
data: {
file: file
}
}
]
showStructuredMessage(messages)
}
file,
},
},
];
showStructuredMessage(messages);
};
return (
<>
<Popover>
<Tooltip content="发送视频">
<div className="max-w-fit">
<Tooltip content='发送视频'>
<div className='max-w-fit'>
<PopoverTrigger>
<Button color="primary" variant="flat" isIconOnly radius="full">
<IoVideocam className="text-xl" />
<Button color='primary' variant='flat' isIconOnly radius='full'>
<IoVideocam className='text-xl' />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-2 p-4">
<Tooltip content="上传视频">
<PopoverContent className='flex-row gap-2 p-4'>
<Tooltip content='上传视频'>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
onPress={() => {
videoInputRef?.current?.click()
videoInputRef?.current?.click();
}}
>
<MdUpload />
</Button>
</Tooltip>
<Popover>
<Tooltip content="输入视频地址">
<div className="max-w-fit">
<PopoverTrigger tooltip="输入视频地址">
<Tooltip content='输入视频地址'>
<div className='max-w-fit'>
<PopoverTrigger tooltip='输入视频地址'>
<Button
className="text-lg"
color="primary"
className='text-lg'
color='primary'
isIconOnly
variant="flat"
radius="full"
variant='flat'
radius='full'
>
<MdEdit />
</Button>
</PopoverTrigger>
</div>
</Tooltip>
<PopoverContent className="flex-row gap-1 p-2">
<PopoverContent className='flex-row gap-1 p-2'>
<Input
value={videoUrl}
onChange={(e) => setVideoUrl(e.target.value)}
placeholder="请输入视频地址"
placeholder='请输入视频地址'
/>
<Button
color="primary"
variant="flat"
color='primary'
variant='flat'
isIconOnly
radius="full"
radius='full'
onPress={() => {
if (!isURI(videoUrl)) {
toast.error('请输入正确的视频地址')
return
toast.error('请输入正确的视频地址');
return;
}
showVideoSegment(videoUrl)
setVideoUrl('')
showVideoSegment(videoUrl);
setVideoUrl('');
}}
>
<TbVideoPlus />
@@ -100,27 +100,27 @@ const VideoInsert = () => {
</Popover>
<input
type="file"
type='file'
ref={videoInputRef}
hidden
accept="video/*"
className="hidden"
accept='video/*'
className='hidden'
onChange={(e) => {
const file = e.target.files?.[0]
const file = e.target.files?.[0];
if (!file) {
return
return;
}
const reader = new FileReader()
reader.readAsDataURL(file)
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const dataURL = event.target?.result
showVideoSegment(dataURL as string)
e.target.value = ''
}
const dataURL = event.target?.result;
showVideoSegment(dataURL as string);
e.target.value = '';
};
}}
/>
</>
)
}
);
};
export default VideoInsert
export default VideoInsert;
@@ -1,4 +1,4 @@
import Quill from 'quill'
import Quill from 'quill';
// eslint-disable-next-line
const Embed = Quill.import('blots/embed') as any
@@ -8,34 +8,34 @@ export interface EmojiValue {
id: string
}
class EmojiBlot extends Embed {
static blotName: string = 'emoji'
static tagName: string = 'img'
static classNames: string[] = ['w-6', 'h-6']
static blotName: string = 'emoji';
static tagName: string = 'img';
static classNames: string[] = ['w-6', 'h-6'];
static create(value: HTMLImageElement) {
const node = super.create(value)
node.setAttribute('alt', value.alt)
node.setAttribute('src', value.src)
node.setAttribute('data-id', value.id)
node.classList.add(...EmojiBlot.classNames)
return node
static create (value: HTMLImageElement) {
const node = super.create(value);
node.setAttribute('alt', value.alt);
node.setAttribute('src', value.src);
node.setAttribute('data-id', value.id);
node.classList.add(...EmojiBlot.classNames);
return node;
}
static formats(node: HTMLImageElement): EmojiValue {
static formats (node: HTMLImageElement): EmojiValue {
return {
alt: node.getAttribute('alt') ?? '',
src: node.getAttribute('src') ?? '',
id: node.getAttribute('data-id') ?? ''
}
id: node.getAttribute('data-id') ?? '',
};
}
static value(node: HTMLImageElement): EmojiValue {
static value (node: HTMLImageElement): EmojiValue {
return {
alt: node.getAttribute('alt') ?? '',
src: node.getAttribute('src') ?? '',
id: node.getAttribute('data-id') ?? ''
}
id: node.getAttribute('data-id') ?? '',
};
}
}
export default EmojiBlot
export default EmojiBlot;
@@ -1,4 +1,4 @@
import Quill from 'quill'
import Quill from 'quill';
// eslint-disable-next-line
const Embed = Quill.import('blots/embed') as any
@@ -7,24 +7,24 @@ export interface ImageValue {
src: string
}
class ImageBlot extends Embed {
static blotName = 'image'
static tagName = 'img'
static classNames: string[] = ['max-w-48', 'max-h-48', 'align-bottom']
static blotName = 'image';
static tagName = 'img';
static classNames: string[] = ['max-w-48', 'max-h-48', 'align-bottom'];
static create(value: ImageValue) {
let node = super.create()
node.setAttribute('alt', value.alt)
node.setAttribute('src', value.src)
node.classList.add(...ImageBlot.classNames)
return node
static create (value: ImageValue) {
const node = super.create();
node.setAttribute('alt', value.alt);
node.setAttribute('src', value.src);
node.classList.add(...ImageBlot.classNames);
return node;
}
static value(node: HTMLImageElement): ImageValue {
static value (node: HTMLImageElement): ImageValue {
return {
alt: node.getAttribute('alt') ?? '',
src: node.getAttribute('src') ?? ''
}
src: node.getAttribute('src') ?? '',
};
}
}
export default ImageBlot
export default ImageBlot;
@@ -1,4 +1,4 @@
import Quill from 'quill'
import Quill from 'quill';
// eslint-disable-next-line
const BlockEmbed = Quill.import('blots/block/embed') as any
@@ -6,38 +6,38 @@ export interface ReplyBlockValue {
messageId: string
}
class ReplyBlock extends BlockEmbed {
static blotName = 'reply'
static tagName = 'div'
static blotName = 'reply';
static tagName = 'div';
static classNames = [
'p-2',
'select-none',
'bg-default-100',
'rounded-md',
'pointer-events-none'
]
'pointer-events-none',
];
static create(value: ReplyBlockValue) {
const node = super.create()
node.setAttribute('data-message-id', value.messageId)
node.setAttribute('contenteditable', 'false')
node.classList.add(...ReplyBlock.classNames)
const innerDom = document.createElement('div')
innerDom.classList.add('text-sm', 'text-default-500', 'relative')
const svgContainer = document.createElement('div')
svgContainer.classList.add('w-3', 'h-3', 'absolute', 'top-0', 'right-0')
const svg = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M15.9082 12.3714H20.5982C20.5182 17.0414 19.5982 17.8114 16.7282 19.5114C16.3982 19.7114 16.2882 20.1314 16.4882 20.4714C16.6882 20.8014 17.1082 20.9114 17.4482 20.7114C20.8282 18.7114 22.0082 17.4914 22.0082 11.6714V6.28141C22.0082 4.57141 20.6182 3.19141 18.9182 3.19141H15.9182C14.1582 3.19141 12.8282 4.52141 12.8282 6.28141V9.28141C12.8182 11.0414 14.1482 12.3714 15.9082 12.3714Z" fill="#292D32"></path> <path d="M5.09 12.3714H9.78C9.7 17.0414 8.78 17.8114 5.91 19.5114C5.58 19.7114 5.47 20.1314 5.67 20.4714C5.87 20.8014 6.29 20.9114 6.63 20.7114C10.01 18.7114 11.19 17.4914 11.19 11.6714V6.28141C11.19 4.57141 9.8 3.19141 8.1 3.19141H5.1C3.33 3.19141 2 4.52141 2 6.28141V9.28141C2 11.0414 3.33 12.3714 5.09 12.3714Z" fill="#292D32"></path> </g></svg>`
svgContainer.innerHTML = svg
innerDom.innerHTML = `消息ID:${value.messageId}`
innerDom.appendChild(svgContainer)
node.appendChild(innerDom)
return node
static create (value: ReplyBlockValue) {
const node = super.create();
node.setAttribute('data-message-id', value.messageId);
node.setAttribute('contenteditable', 'false');
node.classList.add(...ReplyBlock.classNames);
const innerDom = document.createElement('div');
innerDom.classList.add('text-sm', 'text-default-500', 'relative');
const svgContainer = document.createElement('div');
svgContainer.classList.add('w-3', 'h-3', 'absolute', 'top-0', 'right-0');
const svg = '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M15.9082 12.3714H20.5982C20.5182 17.0414 19.5982 17.8114 16.7282 19.5114C16.3982 19.7114 16.2882 20.1314 16.4882 20.4714C16.6882 20.8014 17.1082 20.9114 17.4482 20.7114C20.8282 18.7114 22.0082 17.4914 22.0082 11.6714V6.28141C22.0082 4.57141 20.6182 3.19141 18.9182 3.19141H15.9182C14.1582 3.19141 12.8282 4.52141 12.8282 6.28141V9.28141C12.8182 11.0414 14.1482 12.3714 15.9082 12.3714Z" fill="#292D32"></path> <path d="M5.09 12.3714H9.78C9.7 17.0414 8.78 17.8114 5.91 19.5114C5.58 19.7114 5.47 20.1314 5.67 20.4714C5.87 20.8014 6.29 20.9114 6.63 20.7114C10.01 18.7114 11.19 17.4914 11.19 11.6714V6.28141C11.19 4.57141 9.8 3.19141 8.1 3.19141H5.1C3.33 3.19141 2 4.52141 2 6.28141V9.28141C2 11.0414 3.33 12.3714 5.09 12.3714Z" fill="#292D32"></path> </g></svg>';
svgContainer.innerHTML = svg;
innerDom.innerHTML = `消息ID:${value.messageId}`;
innerDom.appendChild(svgContainer);
node.appendChild(innerDom);
return node;
}
static value(node: HTMLElement): ReplyBlockValue {
static value (node: HTMLElement): ReplyBlockValue {
return {
messageId: node.getAttribute('data-message-id') || ''
}
messageId: node.getAttribute('data-message-id') || '',
};
}
}
export default ReplyBlock
export default ReplyBlock;
@@ -1,55 +1,55 @@
import { Button } from '@heroui/button'
import type { Range } from 'quill'
import 'quill/dist/quill.core.css'
import { useRef } from 'react'
import toast from 'react-hot-toast'
import { Button } from '@heroui/button';
import type { Range } from 'quill';
import 'quill/dist/quill.core.css';
import { useRef } from 'react';
import toast from 'react-hot-toast';
import { useCustomQuill } from '@/hooks/use_custom_quill'
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message'
import { useCustomQuill } from '@/hooks/use_custom_quill';
import useShowStructuredMessage from '@/hooks/use_show_strcuted_message';
import { quillToMessage } from '@/utils/onebot'
import { quillToMessage } from '@/utils/onebot';
import type { OB11Segment } from '@/types/onebot'
import type { OB11Segment } from '@/types/onebot';
import AudioInsert from './components/audio_insert'
import DiceInsert from './components/dice_insert'
import EmojiPicker from './components/emoji_picker'
import FileInsert from './components/file_insert'
import ImageInsert from './components/image_insert'
import MusicInsert from './components/music_insert'
import ReplyInsert from './components/reply_insert'
import RPSInsert from './components/rps_insert'
import VideoInsert from './components/video_insert'
import EmojiBlot from './formats/emoji_blot'
import type { EmojiValue } from './formats/emoji_blot'
import ImageBlot from './formats/image_blot'
import ReplyBlock from './formats/reply_blot'
import AudioInsert from './components/audio_insert';
import DiceInsert from './components/dice_insert';
import EmojiPicker from './components/emoji_picker';
import FileInsert from './components/file_insert';
import ImageInsert from './components/image_insert';
import MusicInsert from './components/music_insert';
import ReplyInsert from './components/reply_insert';
import RPSInsert from './components/rps_insert';
import VideoInsert from './components/video_insert';
import EmojiBlot from './formats/emoji_blot';
import type { EmojiValue } from './formats/emoji_blot';
import ImageBlot from './formats/image_blot';
import ReplyBlock from './formats/reply_blot';
const ChatInput = () => {
const memorizedRange = useRef<Range | null>(null)
const memorizedRange = useRef<Range | null>(null);
const showStructuredMessage = useShowStructuredMessage()
const formats: string[] = ['image', 'emoji', 'reply']
const showStructuredMessage = useShowStructuredMessage();
const formats: string[] = ['image', 'emoji', 'reply'];
const modules = {
toolbar: '#toolbar'
}
toolbar: '#toolbar',
};
const { quillRef, quill, Quill } = useCustomQuill({
modules,
formats,
placeholder: '请输入消息'
})
placeholder: '请输入消息',
});
if (Quill && !quill) {
Quill.register('formats/emoji', EmojiBlot)
Quill.register('formats/image', ImageBlot, true)
Quill.register('formats/reply', ReplyBlock)
Quill.register('formats/emoji', EmojiBlot);
Quill.register('formats/image', ImageBlot, true);
Quill.register('formats/reply', ReplyBlock);
}
if (quill) {
quill.on('selection-change', (range) => {
if (range) {
const editorContent = quill.getContents()
const firstOp = editorContent.ops[0]
const editorContent = quill.getContents();
const firstOp = editorContent.ops[0];
if (
typeof firstOp?.insert !== 'string' &&
@@ -57,126 +57,126 @@ const ChatInput = () => {
range.index === 0 &&
range.length !== quill.getLength()
) {
quill.setSelection(1, Quill.sources.SILENT)
quill.setSelection(1, Quill.sources.SILENT);
}
}
})
});
quill.on('text-change', () => {
const editorContent = quill.getContents()
const firstOp = editorContent.ops[0]
const editorContent = quill.getContents();
const firstOp = editorContent.ops[0];
if (
firstOp &&
typeof firstOp.insert !== 'string' &&
firstOp.insert?.reply &&
quill.getLength() === 1
) {
quill.insertText(1, '\n', Quill.sources.SILENT)
quill.insertText(1, '\n', Quill.sources.SILENT);
}
})
});
quill.on('editor-change', (eventName: string) => {
if (eventName === 'text-change') {
const editorContent = quill.getContents()
const firstOp = editorContent.ops[0]
const editorContent = quill.getContents();
const firstOp = editorContent.ops[0];
if (
firstOp &&
typeof firstOp.insert !== 'string' &&
firstOp.insert?.reply &&
quill.getLength() === 1
) {
quill.insertText(1, '\n', Quill.sources.SILENT)
quill.insertText(1, '\n', Quill.sources.SILENT);
}
}
})
});
quill.root.addEventListener('compositionstart', () => {
const editorContent = quill.getContents()
const firstOp = editorContent.ops[0]
const editorContent = quill.getContents();
const firstOp = editorContent.ops[0];
if (
firstOp &&
typeof firstOp.insert !== 'string' &&
firstOp.insert?.reply &&
quill.getLength() === 1
) {
quill.insertText(1, '\n', Quill.sources.SILENT)
quill.insertText(1, '\n', Quill.sources.SILENT);
}
})
});
}
const onOpenChange = (open: boolean) => {
if (open) {
const selection = quill?.getSelection()
if (selection) memorizedRange.current = selection
const selection = quill?.getSelection();
if (selection) memorizedRange.current = selection;
}
}
};
const insertImage = (url: string) => {
const selection = memorizedRange.current || quill?.getSelection()
quill?.deleteText(selection?.index || 0, selection?.length || 0)
const selection = memorizedRange.current || quill?.getSelection();
quill?.deleteText(selection?.index || 0, selection?.length || 0);
quill?.insertEmbed(selection?.index || 0, 'image', {
src: url,
alt: '图片'
})
quill?.setSelection((selection?.index || 0) + 1, 0)
}
function insertReplyBlock(messageId: string) {
const isNumberReg = /^(?:0|(?:-?[1-9]\d*))$/
alt: '图片',
});
quill?.setSelection((selection?.index || 0) + 1, 0);
};
function insertReplyBlock (messageId: string) {
const isNumberReg = /^(?:0|(?:-?[1-9]\d*))$/;
if (!isNumberReg.test(messageId)) {
toast.error('请输入正确的消息ID')
return
toast.error('请输入正确的消息ID');
return;
}
const editorContent = quill?.getContents()
const firstOp = editorContent?.ops[0]
const currentSelection = quill?.getSelection()
const editorContent = quill?.getContents();
const firstOp = editorContent?.ops[0];
const currentSelection = quill?.getSelection();
if (
firstOp &&
typeof firstOp.insert !== 'string' &&
firstOp.insert?.reply
) {
const delta = quill?.getContents()
const delta = quill?.getContents();
if (delta) {
delta.ops[0] = {
insert: { reply: { messageId } }
}
quill?.setContents(delta, Quill.sources.USER)
insert: { reply: { messageId } },
};
quill?.setContents(delta, Quill.sources.USER);
}
} else {
quill?.insertEmbed(0, 'reply', { messageId }, Quill.sources.USER)
quill?.insertEmbed(0, 'reply', { messageId }, Quill.sources.USER);
}
quill?.setSelection((currentSelection?.index || 0) + 1, 0)
quill?.blur()
quill?.setSelection((currentSelection?.index || 0) + 1, 0);
quill?.blur();
}
const onInsertEmoji = (emoji: EmojiValue) => {
const selection = memorizedRange.current || quill?.getSelection()
quill?.deleteText(selection?.index || 0, selection?.length || 0)
const selection = memorizedRange.current || quill?.getSelection();
quill?.deleteText(selection?.index || 0, selection?.length || 0);
quill?.insertEmbed(selection?.index || 0, 'emoji', {
alt: emoji.alt,
src: emoji.src,
id: emoji.id
})
quill?.setSelection((selection?.index || 0) + 1, 0)
}
id: emoji.id,
});
quill?.setSelection((selection?.index || 0) + 1, 0);
};
const getChatMessage = () => {
const delta = quill?.getContents()
const delta = quill?.getContents();
const ops =
delta?.ops?.filter((op) => {
return op.insert !== '\n'
}) ?? []
return op.insert !== '\n';
}) ?? [];
const messages: OB11Segment[] = ops.map((op) => {
return quillToMessage(op)
})
return messages
}
return quillToMessage(op);
});
return messages;
};
return (
<div>
<div
ref={quillRef}
className="border border-default-200 rounded-md !mb-2 !text-base !h-64"
className='border border-default-200 rounded-md !mb-2 !text-base !h-64'
/>
<div id="toolbar" className="!border-none flex gap-2">
<div id='toolbar' className='!border-none flex gap-2'>
<ImageInsert insertImage={insertImage} onOpenChange={onOpenChange} />
<EmojiPicker
onInsertEmoji={onInsertEmoji}
@@ -190,18 +190,18 @@ const ChatInput = () => {
<DiceInsert />
<RPSInsert />
<Button
color="primary"
color='primary'
onPress={() => {
const messages = getChatMessage()
showStructuredMessage(messages)
const messages = getChatMessage();
showStructuredMessage(messages);
}}
className="ml-auto"
className='ml-auto'
>
获取JSON格式
</Button>
</div>
</div>
)
}
);
};
export default ChatInput
export default ChatInput;
@@ -1,42 +1,42 @@
import { Button } from '@heroui/button'
import { Button } from '@heroui/button';
import {
Modal,
ModalBody,
ModalContent,
ModalFooter,
ModalHeader,
useDisclosure
} from '@heroui/modal'
useDisclosure,
} from '@heroui/modal';
import ChatInput from '.'
import ChatInput from '.';
export default function ChatInputModal() {
const { isOpen, onOpen, onOpenChange } = useDisclosure()
export default function ChatInputModal () {
const { isOpen, onOpen, onOpenChange } = useDisclosure();
return (
<>
<Button onPress={onOpen} color="primary" radius="full" variant="flat">
<Button onPress={onOpen} color='primary' radius='full' variant='flat'>
构造聊天消息
</Button>
<Modal
size="4xl"
scrollBehavior="inside"
size='4xl'
scrollBehavior='inside'
isOpen={isOpen}
onOpenChange={onOpenChange}
>
<ModalContent>
{(onClose) => (
<>
<ModalHeader className="flex flex-col gap-1">
<ModalHeader className='flex flex-col gap-1'>
构造消息
</ModalHeader>
<ModalBody className="overflow-y-auto">
<div className="overflow-y-auto">
<ModalBody className='overflow-y-auto'>
<div className='overflow-y-auto'>
<ChatInput />
</div>
</ModalBody>
<ModalFooter>
<Button color="primary" onPress={onClose} variant="flat">
<Button color='primary' onPress={onClose} variant='flat'>
关闭
</Button>
</ModalFooter>
@@ -45,5 +45,5 @@ export default function ChatInputModal() {
</ModalContent>
</Modal>
</>
)
);
}