chatluna-plugin-onebot-sandbox 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/client/app.tsx +306 -0
- package/client/chat/chat-pane.tsx +303 -0
- package/client/chat/composer.css +635 -0
- package/client/chat/composer.tsx +646 -0
- package/client/chat/details-panel.tsx +196 -0
- package/client/chat/emoji-picker.tsx +70 -0
- package/client/chat/extension-mark.tsx +16 -0
- package/client/chat/forward.tsx +257 -0
- package/client/chat/image-preview.tsx +93 -0
- package/client/chat/member-menu.tsx +64 -0
- package/client/chat/message-list.tsx +617 -0
- package/client/chat/messages.css +1018 -0
- package/client/chat/notifications.tsx +83 -0
- package/client/chat/reactions.tsx +52 -0
- package/client/chat/search-panel.tsx +231 -0
- package/client/chat/sidebar.tsx +424 -0
- package/client/chat/webqq-view.tsx +71 -0
- package/client/environment/avatar-picker.tsx +92 -0
- package/client/environment/create-popover.tsx +228 -0
- package/client/environment/directory-model.ts +63 -0
- package/client/environment/entity-dialog.css +383 -0
- package/client/environment/entity-dialog.tsx +442 -0
- package/client/environment/page.css +14 -0
- package/client/environment/page.tsx +280 -0
- package/client/i18n.ts +19 -0
- package/client/index.ts +17 -0
- package/client/locales/en.yml +617 -0
- package/client/locales/zh.yml +617 -0
- package/client/mcp/capability-catalog.tsx +277 -0
- package/client/mcp/credential-manager.tsx +257 -0
- package/client/onebot-debug/page.tsx +329 -0
- package/client/records/record-layout.tsx +429 -0
- package/client/records/records.css +259 -0
- package/client/settings/about.tsx +43 -0
- package/client/settings/draft.ts +47 -0
- package/client/settings/localize.ts +76 -0
- package/client/settings/page.tsx +210 -0
- package/client/settings/settings.css +47 -0
- package/client/shared/avatar.tsx +35 -0
- package/client/shared/confirm-dialog.tsx +44 -0
- package/client/shared/controls.css +17 -0
- package/client/shared/discard-confirm.tsx +45 -0
- package/client/shared/floating-panel.ts +40 -0
- package/client/shared/format-duration.ts +22 -0
- package/client/shared/format-time.ts +49 -0
- package/client/shared/glass.css +150 -0
- package/client/shared/implementation.ts +12 -0
- package/client/shared/overlay-scrollbar.css +45 -0
- package/client/shared/overlay-scrollbar.tsx +123 -0
- package/client/shared/use-element-width.ts +19 -0
- package/client/shared/use-retained.ts +12 -0
- package/client/test-call/page.tsx +260 -0
- package/client/test-space/agent-visuals.tsx +121 -0
- package/client/test-space/observe-overlay.tsx +42 -0
- package/client/test-space/overview.tsx +267 -0
- package/client/test-space/spaces.css +201 -0
- package/client/test-space/thumbnail-capture.ts +63 -0
- package/client/test-space/thumbnail-scale.ts +21 -0
- package/client/test-space/use-space-list.ts +35 -0
- package/client/test-space/zoom.ts +197 -0
- package/client/webqq/composer-draft-host.ts +578 -0
- package/client/webqq/composer-draft.ts +81 -0
- package/client/webqq/conversation-tree.ts +155 -0
- package/client/webqq/emoji-catalog.ts +78 -0
- package/client/webqq/fork-boundary.ts +28 -0
- package/client/webqq/forward-cluster.ts +20 -0
- package/client/webqq/forward-dialog-stack.ts +74 -0
- package/client/webqq/forward-preview.ts +40 -0
- package/client/webqq/friend-menu.ts +17 -0
- package/client/webqq/group-display.ts +18 -0
- package/client/webqq/group-menu.ts +25 -0
- package/client/webqq/mention.ts +4 -0
- package/client/webqq/message-capabilities.ts +37 -0
- package/client/webqq/message-cluster.ts +68 -0
- package/client/webqq/message-presentation.ts +197 -0
- package/client/webqq/message-reveal.ts +31 -0
- package/client/webqq/message-search-date.ts +40 -0
- package/client/webqq/message-selection.ts +85 -0
- package/client/webqq/notification-requests.ts +20 -0
- package/client/webqq/participant-presentation.ts +128 -0
- package/client/webqq/profile-card.ts +199 -0
- package/client/webqq/relationship-directory.ts +62 -0
- package/client/webqq/thinking-panel.ts +7 -0
- package/client/webqq/user-stack.ts +53 -0
- package/client/workspace/agent-overlay-effect.ts +238 -0
- package/client/workspace/context.tsx +117 -0
- package/client/workspace/overlay-host.tsx +299 -0
- package/client/workspace/profile-card.css +164 -0
- package/client/workspace/state.ts +28 -0
- package/client/workspace/store.ts +388 -0
- package/client/workspace/use-test-spaces.ts +21 -0
- package/dist/.vite/manifest.json +21 -0
- package/dist/app-63rfTh6w.js +85 -0
- package/dist/client.css +4 -0
- package/dist/client.js +1 -0
- package/lib/account-profile.d.ts +17 -0
- package/lib/account-profile.d.ts.map +1 -0
- package/lib/base-dir.d.ts +4 -0
- package/lib/base-dir.d.ts.map +1 -0
- package/lib/bot.d.ts +185 -0
- package/lib/bot.d.ts.map +1 -0
- package/lib/builtin-avatar-options.d.ts +11 -0
- package/lib/builtin-avatar-options.d.ts.map +1 -0
- package/lib/builtin-avatars.d.ts +6 -0
- package/lib/builtin-avatars.d.ts.map +1 -0
- package/lib/channel-assignee.d.ts +52 -0
- package/lib/channel-assignee.d.ts.map +1 -0
- package/lib/chatluna/conversation-lines.d.ts +46 -0
- package/lib/chatluna/conversation-lines.d.ts.map +1 -0
- package/lib/chatluna/runtime.d.ts +26 -0
- package/lib/chatluna/runtime.d.ts.map +1 -0
- package/lib/chatluna/state.d.ts +67 -0
- package/lib/chatluna/state.d.ts.map +1 -0
- package/lib/chatluna/thinking.d.ts +10 -0
- package/lib/chatluna/thinking.d.ts.map +1 -0
- package/lib/chatluna/tools.d.ts +36 -0
- package/lib/chatluna/tools.d.ts.map +1 -0
- package/lib/chatluna/wakeup.d.ts +143 -0
- package/lib/chatluna/wakeup.d.ts.map +1 -0
- package/lib/console-contract.d.ts +131 -0
- package/lib/console-contract.d.ts.map +1 -0
- package/lib/console.d.ts +31 -0
- package/lib/console.d.ts.map +1 -0
- package/lib/control-service.d.ts +425 -0
- package/lib/control-service.d.ts.map +1 -0
- package/lib/conversation-resolution.d.ts +157 -0
- package/lib/conversation-resolution.d.ts.map +1 -0
- package/lib/error-detail.d.ts +3 -0
- package/lib/error-detail.d.ts.map +1 -0
- package/lib/inbound-delivery.d.ts +156 -0
- package/lib/inbound-delivery.d.ts.map +1 -0
- package/lib/index.d.ts +76 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +12834 -0
- package/lib/index.js.map +1 -0
- package/lib/lifecycle.d.ts +8 -0
- package/lib/lifecycle.d.ts.map +1 -0
- package/lib/mcp/arguments.d.ts +71 -0
- package/lib/mcp/arguments.d.ts.map +1 -0
- package/lib/mcp/call-records.d.ts +32 -0
- package/lib/mcp/call-records.d.ts.map +1 -0
- package/lib/mcp/http-api.d.ts +147 -0
- package/lib/mcp/http-api.d.ts.map +1 -0
- package/lib/mcp/page-cursor.d.ts +37 -0
- package/lib/mcp/page-cursor.d.ts.map +1 -0
- package/lib/mcp/server.d.ts +60 -0
- package/lib/mcp/server.d.ts.map +1 -0
- package/lib/mcp/service.d.ts +219 -0
- package/lib/mcp/service.d.ts.map +1 -0
- package/lib/mcp/tool-registry.d.ts +113 -0
- package/lib/mcp/tool-registry.d.ts.map +1 -0
- package/lib/mcp/types.d.ts +144 -0
- package/lib/mcp/types.d.ts.map +1 -0
- package/lib/media-storage.d.ts +33 -0
- package/lib/media-storage.d.ts.map +1 -0
- package/lib/message-capabilities.d.ts +78 -0
- package/lib/message-capabilities.d.ts.map +1 -0
- package/lib/onebot-debug.d.ts +115 -0
- package/lib/onebot-debug.d.ts.map +1 -0
- package/lib/onebot-message.d.ts +23 -0
- package/lib/onebot-message.d.ts.map +1 -0
- package/lib/onebot-profiles.d.ts +45 -0
- package/lib/onebot-profiles.d.ts.map +1 -0
- package/lib/persistence.d.ts +148 -0
- package/lib/persistence.d.ts.map +1 -0
- package/lib/random.d.ts +5 -0
- package/lib/random.d.ts.map +1 -0
- package/lib/record-store.d.ts +80 -0
- package/lib/record-store.d.ts.map +1 -0
- package/lib/relationship-actions.d.ts +157 -0
- package/lib/relationship-actions.d.ts.map +1 -0
- package/lib/scope-directory.d.ts +105 -0
- package/lib/scope-directory.d.ts.map +1 -0
- package/lib/settings.d.ts +33 -0
- package/lib/settings.d.ts.map +1 -0
- package/lib/test-spaces.d.ts +69 -0
- package/lib/test-spaces.d.ts.map +1 -0
- package/lib/types.d.ts +739 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/webui.d.ts +26 -0
- package/lib/webui.d.ts.map +1 -0
- package/package.json +112 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { PlusIcon, Trash2Icon, XIcon } from 'lucide-react'
|
|
3
|
+
import {
|
|
4
|
+
Button,
|
|
5
|
+
cn,
|
|
6
|
+
ContextMenu,
|
|
7
|
+
ContextMenuContent,
|
|
8
|
+
ContextMenuTrigger,
|
|
9
|
+
Input,
|
|
10
|
+
ResizableHandle,
|
|
11
|
+
type ResizableLayout,
|
|
12
|
+
ResizablePanel,
|
|
13
|
+
ResizablePanelGroup,
|
|
14
|
+
Textarea,
|
|
15
|
+
} from '@chatluna/ui-kit'
|
|
16
|
+
import { readStored, writeStored } from '@chatluna/ui-storage'
|
|
17
|
+
import { useT } from '../i18n'
|
|
18
|
+
import { SandboxAvatar } from '../shared/avatar'
|
|
19
|
+
import { ConfirmDialog, type ConfirmRequest } from '../shared/confirm-dialog'
|
|
20
|
+
import { getGroupMemberDisplayName } from '../webqq/group-display'
|
|
21
|
+
import { errorText } from '../workspace/store'
|
|
22
|
+
import { useSandbox, useWorkspace } from '../workspace/context'
|
|
23
|
+
import { GroupMemberMenuItems } from './member-menu'
|
|
24
|
+
// 群主/管理员/头衔标签复用消息列表的 `sandbox-message-role-badge`,与聊天页保持同一套样式。
|
|
25
|
+
import './messages.css'
|
|
26
|
+
|
|
27
|
+
const layoutStorageKey = 'chatluna.onebot-sandbox.details-layout'
|
|
28
|
+
const announcementsPanelId = 'announcements'
|
|
29
|
+
const membersPanelId = 'members'
|
|
30
|
+
|
|
31
|
+
/** 群公告与群成员的分隔比例(百分比),拖动分隔线后存进浏览器。 */
|
|
32
|
+
function loadDetailsLayout(): ResizableLayout | undefined {
|
|
33
|
+
const stored = readStored(layoutStorageKey)
|
|
34
|
+
if (!stored || typeof stored !== 'object') return undefined
|
|
35
|
+
const record = stored as Record<string, unknown>
|
|
36
|
+
const announcements = record[announcementsPanelId]
|
|
37
|
+
const members = record[membersPanelId]
|
|
38
|
+
if (typeof announcements !== 'number' || typeof members !== 'number') return undefined
|
|
39
|
+
return { [announcementsPanelId]: announcements, [membersPanelId]: members }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 群聊信息栏:群公告与群成员。 */
|
|
43
|
+
export function DetailsPanel({ onClose, onMention, className }: {
|
|
44
|
+
onClose(): void
|
|
45
|
+
onMention(participantId: string): void
|
|
46
|
+
className?: string
|
|
47
|
+
}) {
|
|
48
|
+
const t = useT()
|
|
49
|
+
const { store, dialogs } = useSandbox()
|
|
50
|
+
const { group, conversation, participants } = useWorkspace()
|
|
51
|
+
const [memberQuery, setMemberQuery] = React.useState('')
|
|
52
|
+
const [editorOpen, setEditorOpen] = React.useState(false)
|
|
53
|
+
const [draft, setDraft] = React.useState('')
|
|
54
|
+
const [sending, setSending] = React.useState(false)
|
|
55
|
+
const [deleting, setDeleting] = React.useState('')
|
|
56
|
+
const [confirm, setConfirm] = React.useState<ConfirmRequest>()
|
|
57
|
+
const [error, setError] = React.useState('')
|
|
58
|
+
const [defaultLayout] = React.useState(loadDetailsLayout)
|
|
59
|
+
|
|
60
|
+
React.useEffect(() => {
|
|
61
|
+
setMemberQuery('')
|
|
62
|
+
setEditorOpen(false)
|
|
63
|
+
setDraft('')
|
|
64
|
+
setDeleting('')
|
|
65
|
+
setError('')
|
|
66
|
+
}, [conversation?.id])
|
|
67
|
+
|
|
68
|
+
const memberName = (participantId: string, card?: string) => getGroupMemberDisplayName({ card }, participants[participantId]?.name ?? participantId)
|
|
69
|
+
const needle = memberQuery.trim().toLowerCase()
|
|
70
|
+
const members = group?.members.filter((member) => !needle || memberName(member.participantId, member.card).toLowerCase().includes(needle) || member.participantId.includes(needle)) ?? []
|
|
71
|
+
|
|
72
|
+
const publish = async (event: React.FormEvent) => {
|
|
73
|
+
event.preventDefault()
|
|
74
|
+
const content = draft.trim()
|
|
75
|
+
if (!group || !content || sending) return
|
|
76
|
+
setSending(true)
|
|
77
|
+
setError('')
|
|
78
|
+
try {
|
|
79
|
+
await store.setGroupAnnouncement({ groupId: group.id, content })
|
|
80
|
+
setDraft('')
|
|
81
|
+
setEditorOpen(false)
|
|
82
|
+
} catch (failure) {
|
|
83
|
+
setError(errorText(failure, t('sandbox.details.publishFailed')))
|
|
84
|
+
} finally {
|
|
85
|
+
setSending(false)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const remove = async (announcementId: string) => {
|
|
90
|
+
if (!group || deleting) return
|
|
91
|
+
setDeleting(announcementId)
|
|
92
|
+
setError('')
|
|
93
|
+
try {
|
|
94
|
+
await store.deleteGroupAnnouncement({ groupId: group.id, announcementId })
|
|
95
|
+
} catch (failure) {
|
|
96
|
+
setError(errorText(failure, t('sandbox.details.deleteFailed')))
|
|
97
|
+
} finally {
|
|
98
|
+
setDeleting('')
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 私聊不显示信息栏(webqq-view.tsx 只在群聊时渲染本组件)。
|
|
103
|
+
if (!group) return null
|
|
104
|
+
const label = t('sandbox.details.group')
|
|
105
|
+
return (
|
|
106
|
+
<aside className={cn('flex min-h-0 w-72 shrink-0 flex-col border-l border-border bg-sidebar/40', className)} aria-label={label}>
|
|
107
|
+
<header className="flex h-14 shrink-0 items-center justify-between border-b border-border px-4">
|
|
108
|
+
<strong className="text-sm font-medium">{label}</strong>
|
|
109
|
+
<Button variant="ghost" size="icon-sm" aria-label={t('sandbox.details.close')} onClick={onClose}><XIcon /></Button>
|
|
110
|
+
</header>
|
|
111
|
+
{/* 面板内层默认 overflow:auto,这里交给公告与成员列表各自滚动。 */}
|
|
112
|
+
<ResizablePanelGroup
|
|
113
|
+
orientation="vertical"
|
|
114
|
+
className="min-h-0 flex-1"
|
|
115
|
+
defaultLayout={defaultLayout}
|
|
116
|
+
onLayoutChanged={(layout) => writeStored(layoutStorageKey, layout)}
|
|
117
|
+
>
|
|
118
|
+
<ResizablePanel id={announcementsPanelId} defaultSize="40" minSize={96} maxSize="75" className="flex min-h-0 flex-col gap-2 p-4" style={{ overflow: 'hidden' }}>
|
|
119
|
+
<div className="flex items-center justify-between">
|
|
120
|
+
<h3 className="text-[13px] font-medium">{t('sandbox.details.announcements')}</h3>
|
|
121
|
+
<Button variant="ghost" size="icon-xs" aria-label={t(editorOpen ? 'sandbox.details.cancelAnnouncement' : 'sandbox.details.addAnnouncement')} onClick={() => {
|
|
122
|
+
setEditorOpen((open) => !open)
|
|
123
|
+
setDraft('')
|
|
124
|
+
setError('')
|
|
125
|
+
}}>
|
|
126
|
+
<PlusIcon className={cn('transition-transform duration-200 ease-spring', editorOpen && 'rotate-45')} />
|
|
127
|
+
</Button>
|
|
128
|
+
</div>
|
|
129
|
+
{/* 提示、发布表单与公告列表同在一个滚动区:面板拖到最矮时发布按钮滚得到,不会被裁掉。 */}
|
|
130
|
+
<div className="flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto">
|
|
131
|
+
{error && <span role="alert" className="text-xs text-destructive">{error}</span>}
|
|
132
|
+
{editorOpen && (
|
|
133
|
+
<form className="flex shrink-0 flex-col gap-2" onSubmit={publish}>
|
|
134
|
+
<Textarea rows={3} value={draft} placeholder={t('sandbox.details.announcementPlaceholder')} onChange={(event) => setDraft(event.target.value)} />
|
|
135
|
+
<Button type="submit" size="sm" className="self-end" disabled={sending || !draft.trim()}>{sending ? t('sandbox.details.publishing') : t('sandbox.details.publish')}</Button>
|
|
136
|
+
</form>
|
|
137
|
+
)}
|
|
138
|
+
{!group.announcements.length && <p className="py-2 text-xs text-muted-foreground">{t('sandbox.details.noAnnouncement')}</p>}
|
|
139
|
+
{group.announcements.map((announcement) => (
|
|
140
|
+
<article key={announcement.id} className="group/notice relative rounded-lg bg-muted/60 p-2.5 pr-8">
|
|
141
|
+
<p className="text-xs leading-5 whitespace-pre-wrap">{announcement.content}</p>
|
|
142
|
+
<time className="mt-1 block text-[11px] text-muted-foreground">
|
|
143
|
+
{participants[announcement.authorId]?.name ?? announcement.authorId} · {new Intl.DateTimeFormat(undefined, { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }).format(new Date(announcement.createdAt))}
|
|
144
|
+
</time>
|
|
145
|
+
<Button
|
|
146
|
+
variant="ghost"
|
|
147
|
+
size="icon-xs"
|
|
148
|
+
className="absolute top-1.5 right-1.5 opacity-0 group-hover/notice:opacity-100 focus-visible:opacity-100"
|
|
149
|
+
disabled={deleting === announcement.id}
|
|
150
|
+
aria-label={t('sandbox.details.deleteAnnouncement', { content: announcement.content })}
|
|
151
|
+
onClick={() => setConfirm({
|
|
152
|
+
title: t('sandbox.details.deleteAnnouncementTitle'),
|
|
153
|
+
description: t('sandbox.details.deleteAnnouncementDescription', { content: announcement.content }),
|
|
154
|
+
action: t('sandbox.common.delete'),
|
|
155
|
+
onConfirm: () => void remove(announcement.id),
|
|
156
|
+
})}
|
|
157
|
+
>
|
|
158
|
+
<Trash2Icon />
|
|
159
|
+
</Button>
|
|
160
|
+
</article>
|
|
161
|
+
))}
|
|
162
|
+
</div>
|
|
163
|
+
</ResizablePanel>
|
|
164
|
+
<ResizableHandle aria-label={t('sandbox.details.resize')} />
|
|
165
|
+
<ResizablePanel id={membersPanelId} minSize={160} className="flex min-h-0 flex-col gap-2 p-4" style={{ overflow: 'hidden' }}>
|
|
166
|
+
<h3 className="text-[13px] font-medium">{t('sandbox.details.members', { count: group.members.length })}</h3>
|
|
167
|
+
<Input type="search" className="sandbox-search-input h-8" value={memberQuery} placeholder={t('sandbox.details.memberSearch')} onChange={(event) => setMemberQuery(event.target.value)} />
|
|
168
|
+
<div className="-mx-2 flex min-h-0 flex-1 flex-col gap-0.5 overflow-y-auto">
|
|
169
|
+
{!members.length && <p className="px-2 py-4 text-center text-xs text-muted-foreground">{t('sandbox.details.noMember')}</p>}
|
|
170
|
+
{members.map((member) => (
|
|
171
|
+
<ContextMenu key={member.participantId}>
|
|
172
|
+
<ContextMenuTrigger render={(
|
|
173
|
+
<button type="button" className="flex items-center gap-2.5 rounded-lg px-2 py-1.5 text-left hover:bg-accent" onClick={(event) => dialogs.openProfile(member.participantId, { x: event.clientX, y: event.clientY })} />
|
|
174
|
+
)}>
|
|
175
|
+
<SandboxAvatar kind={participants[member.participantId]?.isBot ? 'bot' : 'user'} name={memberName(member.participantId, member.card)} src={participants[member.participantId]?.avatar} className="size-8" />
|
|
176
|
+
<span className="flex min-w-0 flex-1 flex-col">
|
|
177
|
+
<strong className="truncate text-xs font-medium">{memberName(member.participantId, member.card)}</strong>
|
|
178
|
+
<small className="text-[11px] text-muted-foreground">{member.participantId}</small>
|
|
179
|
+
</span>
|
|
180
|
+
<span className="flex shrink-0 gap-1">
|
|
181
|
+
{member.title && <span className="sandbox-message-role-badge is-title">{member.title}</span>}
|
|
182
|
+
{member.role !== 'member' && <span className={`sandbox-message-role-badge is-${member.role}`}>{t(`sandbox.role.${member.role}`)}</span>}
|
|
183
|
+
</span>
|
|
184
|
+
</ContextMenuTrigger>
|
|
185
|
+
<ContextMenuContent className="sandbox-glass">
|
|
186
|
+
<GroupMemberMenuItems target={member} onMention={onMention} />
|
|
187
|
+
</ContextMenuContent>
|
|
188
|
+
</ContextMenu>
|
|
189
|
+
))}
|
|
190
|
+
</div>
|
|
191
|
+
</ResizablePanel>
|
|
192
|
+
</ResizablePanelGroup>
|
|
193
|
+
<ConfirmDialog request={confirm} onClose={() => setConfirm(undefined)} />
|
|
194
|
+
</aside>
|
|
195
|
+
)
|
|
196
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { Dialog, DialogContent, DialogHeader, DialogTitle, Input } from '@chatluna/ui-kit'
|
|
3
|
+
import { useT } from '../i18n'
|
|
4
|
+
import {
|
|
5
|
+
getCommonSandboxEmojiFaces,
|
|
6
|
+
getSandboxEmojiFace,
|
|
7
|
+
loadRecentSandboxEmojiIds,
|
|
8
|
+
rememberSandboxEmojiId,
|
|
9
|
+
searchSandboxEmojiFaces,
|
|
10
|
+
type SandboxEmojiFace,
|
|
11
|
+
} from '../webqq/emoji-catalog'
|
|
12
|
+
|
|
13
|
+
/** 贴表情面板:常用(最近 24 个)、推荐与全部表情,支持按名称、拼音或 ID 搜索。 */
|
|
14
|
+
export function EmojiPicker({ open, onOpenChange, onSelect }: {
|
|
15
|
+
open: boolean
|
|
16
|
+
onOpenChange(open: boolean): void
|
|
17
|
+
onSelect(emojiId: string): void
|
|
18
|
+
}) {
|
|
19
|
+
const t = useT()
|
|
20
|
+
const [query, setQuery] = React.useState('')
|
|
21
|
+
const [recentIds, setRecentIds] = React.useState<string[]>([])
|
|
22
|
+
React.useEffect(() => {
|
|
23
|
+
if (!open) return
|
|
24
|
+
setQuery('')
|
|
25
|
+
setRecentIds(loadRecentSandboxEmojiIds())
|
|
26
|
+
}, [open])
|
|
27
|
+
const searching = !!query.trim()
|
|
28
|
+
const recent = recentIds.map(getSandboxEmojiFace).filter((face): face is SandboxEmojiFace => !!face)
|
|
29
|
+
const visible = React.useMemo(() => searchSandboxEmojiFaces(query), [query])
|
|
30
|
+
const select = (id: string) => {
|
|
31
|
+
setRecentIds(rememberSandboxEmojiId(id))
|
|
32
|
+
onSelect(id)
|
|
33
|
+
onOpenChange(false)
|
|
34
|
+
}
|
|
35
|
+
const grid = (faces: SandboxEmojiFace[], prefix: string) => (
|
|
36
|
+
<div className="grid grid-cols-8 gap-1">
|
|
37
|
+
{faces.map((face) => (
|
|
38
|
+
<button
|
|
39
|
+
key={`${prefix}:${face.id}`}
|
|
40
|
+
type="button"
|
|
41
|
+
title={face.label}
|
|
42
|
+
className="flex aspect-square items-center justify-center rounded-md transition-colors hover:bg-accent"
|
|
43
|
+
onClick={() => select(face.id)}
|
|
44
|
+
>
|
|
45
|
+
{face.url ? <img src={face.url} alt={face.label} className="size-6" /> : <span className="text-[10px]">{face.label}</span>}
|
|
46
|
+
</button>
|
|
47
|
+
))}
|
|
48
|
+
</div>
|
|
49
|
+
)
|
|
50
|
+
return (
|
|
51
|
+
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
52
|
+
<DialogContent className="flex flex-col overflow-hidden sm:max-w-md">
|
|
53
|
+
<DialogHeader><DialogTitle>{t('sandbox.emoji.title')}</DialogTitle></DialogHeader>
|
|
54
|
+
<Input type="search" className="sandbox-search-input" value={query} autoFocus placeholder={t('sandbox.emoji.search')} aria-label={t('sandbox.emoji.search')} onChange={(event) => setQuery(event.target.value)} />
|
|
55
|
+
<div className="-mx-5 flex max-h-[55vh] min-h-0 flex-col gap-4 overflow-y-auto px-5">
|
|
56
|
+
{!searching && recent.length > 0 && (
|
|
57
|
+
<section className="flex flex-col gap-2"><h3 className="text-xs text-muted-foreground">{t('sandbox.emoji.recent')}</h3>{grid(recent, 'recent')}</section>
|
|
58
|
+
)}
|
|
59
|
+
{!searching && (
|
|
60
|
+
<section className="flex flex-col gap-2"><h3 className="text-xs text-muted-foreground">{t('sandbox.emoji.common')}</h3>{grid(getCommonSandboxEmojiFaces(), 'common')}</section>
|
|
61
|
+
)}
|
|
62
|
+
<section className="flex flex-col gap-2">
|
|
63
|
+
<h3 className="text-xs text-muted-foreground">{t(searching ? 'sandbox.emoji.results' : 'sandbox.emoji.all')}</h3>
|
|
64
|
+
{visible.length ? grid(visible, 'all') : <p className="py-6 text-center text-xs text-muted-foreground">{t('sandbox.emoji.empty')}</p>}
|
|
65
|
+
</section>
|
|
66
|
+
</div>
|
|
67
|
+
</DialogContent>
|
|
68
|
+
</Dialog>
|
|
69
|
+
)
|
|
70
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FlaskConicalIcon } from 'lucide-react'
|
|
2
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '@chatluna/ui-kit'
|
|
3
|
+
import { useT } from '../i18n'
|
|
4
|
+
|
|
5
|
+
/** 菜单项尾部标记:该操作不是 OneBot action,只用于构造测试场景。 */
|
|
6
|
+
export function ExtensionMark() {
|
|
7
|
+
const t = useT()
|
|
8
|
+
return (
|
|
9
|
+
<Tooltip>
|
|
10
|
+
<TooltipTrigger render={<span className="ml-auto inline-flex text-muted-foreground" aria-label={t('sandbox.extension.description')} />}>
|
|
11
|
+
<FlaskConicalIcon className="size-3.5" />
|
|
12
|
+
</TooltipTrigger>
|
|
13
|
+
<TooltipContent side="right">{t('sandbox.extension.description')}</TooltipContent>
|
|
14
|
+
</Tooltip>
|
|
15
|
+
)
|
|
16
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { ChevronRightIcon, PaperclipIcon, SearchIcon } from 'lucide-react'
|
|
3
|
+
import {
|
|
4
|
+
Button,
|
|
5
|
+
cn,
|
|
6
|
+
Dialog,
|
|
7
|
+
DialogContent,
|
|
8
|
+
DialogDescription,
|
|
9
|
+
DialogFooter,
|
|
10
|
+
DialogHeader,
|
|
11
|
+
DialogTitle,
|
|
12
|
+
Input,
|
|
13
|
+
Tabs,
|
|
14
|
+
TabsList,
|
|
15
|
+
TabsTrigger,
|
|
16
|
+
} from '@chatluna/ui-kit'
|
|
17
|
+
import { forwardNodeText, type SandboxForwardNode } from '../../src/types'
|
|
18
|
+
import { useT } from '../i18n'
|
|
19
|
+
import { SandboxAvatar, type AvatarKind } from '../shared/avatar'
|
|
20
|
+
import { useRetained } from '../shared/use-retained'
|
|
21
|
+
import type { ForwardDialogFrame } from '../webqq/forward-dialog-stack'
|
|
22
|
+
import { getForwardNodeClusterClass, isMergedForwardNode } from '../webqq/forward-cluster'
|
|
23
|
+
import { buildForwardPreview } from '../webqq/forward-preview'
|
|
24
|
+
import { formatMediaSize, getMediaLabel } from '../webqq/message-presentation'
|
|
25
|
+
import { useWorkspace } from '../workspace/context'
|
|
26
|
+
import { errorText } from '../workspace/store'
|
|
27
|
+
import './messages.css'
|
|
28
|
+
|
|
29
|
+
export interface ForwardTargetOption {
|
|
30
|
+
conversationId: string
|
|
31
|
+
title: string
|
|
32
|
+
subtitle?: string
|
|
33
|
+
avatar?: string
|
|
34
|
+
avatarKind: AvatarKind
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type ForwardTargets = Record<'recent' | 'friends' | 'groups', ForwardTargetOption[]>
|
|
38
|
+
|
|
39
|
+
/** 选择合并转发的目标:最近会话、好友或群组中的一个根会话。 */
|
|
40
|
+
export function ForwardTargetDialog({ open, targets, onOpenChange, onConfirm }: {
|
|
41
|
+
open: boolean
|
|
42
|
+
targets: ForwardTargets
|
|
43
|
+
onOpenChange(open: boolean): void
|
|
44
|
+
onConfirm(conversationId: string): Promise<void>
|
|
45
|
+
}) {
|
|
46
|
+
const t = useT()
|
|
47
|
+
const [tab, setTab] = React.useState<keyof ForwardTargets>('recent')
|
|
48
|
+
const [query, setQuery] = React.useState('')
|
|
49
|
+
const [selected, setSelected] = React.useState('')
|
|
50
|
+
const [submitting, setSubmitting] = React.useState(false)
|
|
51
|
+
const [error, setError] = React.useState('')
|
|
52
|
+
const needle = query.trim().toLowerCase()
|
|
53
|
+
const visible = targets[tab].filter((target) => !needle
|
|
54
|
+
|| target.title.toLowerCase().includes(needle)
|
|
55
|
+
|| target.conversationId.toLowerCase().includes(needle)
|
|
56
|
+
|| !!target.subtitle?.toLowerCase().includes(needle))
|
|
57
|
+
|
|
58
|
+
React.useEffect(() => {
|
|
59
|
+
if (!open) return
|
|
60
|
+
setTab('recent')
|
|
61
|
+
setQuery('')
|
|
62
|
+
setSubmitting(false)
|
|
63
|
+
setError('')
|
|
64
|
+
setSelected(targets.recent[0]?.conversationId ?? targets.friends[0]?.conversationId ?? targets.groups[0]?.conversationId ?? '')
|
|
65
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
66
|
+
}, [open])
|
|
67
|
+
React.useEffect(() => {
|
|
68
|
+
if (!visible.some(({ conversationId }) => conversationId === selected)) setSelected(visible[0]?.conversationId ?? '')
|
|
69
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
70
|
+
}, [tab, needle])
|
|
71
|
+
|
|
72
|
+
const confirm = async () => {
|
|
73
|
+
if (!selected || submitting) return
|
|
74
|
+
setSubmitting(true)
|
|
75
|
+
setError('')
|
|
76
|
+
try {
|
|
77
|
+
await onConfirm(selected)
|
|
78
|
+
onOpenChange(false)
|
|
79
|
+
} catch (failure) {
|
|
80
|
+
setError(errorText(failure, t('sandbox.forward.failed')))
|
|
81
|
+
} finally {
|
|
82
|
+
setSubmitting(false)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
88
|
+
<DialogContent className="flex flex-col overflow-hidden sm:max-w-md">
|
|
89
|
+
<DialogHeader>
|
|
90
|
+
<DialogTitle>{t('sandbox.forward.targetTitle')}</DialogTitle>
|
|
91
|
+
<DialogDescription>{t('sandbox.forward.targetHint')}</DialogDescription>
|
|
92
|
+
</DialogHeader>
|
|
93
|
+
<div className="relative">
|
|
94
|
+
<SearchIcon className="pointer-events-none absolute top-1/2 left-2.5 size-4 -translate-y-1/2 text-muted-foreground" />
|
|
95
|
+
<Input type="search" className="sandbox-search-input pl-8!" value={query} placeholder={t('sandbox.forward.search')} aria-label={t('sandbox.forward.search')} onChange={(event) => setQuery(event.target.value)} />
|
|
96
|
+
</div>
|
|
97
|
+
<Tabs value={tab} onValueChange={(value) => setTab(value as keyof ForwardTargets)}>
|
|
98
|
+
<TabsList className="w-full">
|
|
99
|
+
{(['recent', 'friends', 'groups'] as const).map((id) => (
|
|
100
|
+
<TabsTrigger key={id} value={id} className="text-xs">{t(`sandbox.sidebar.${id}`)}</TabsTrigger>
|
|
101
|
+
))}
|
|
102
|
+
</TabsList>
|
|
103
|
+
</Tabs>
|
|
104
|
+
{/* 视口很矮时列表先收缩,页头、搜索与页脚保持可见,不从透明的毛玻璃页头下面滚过。
|
|
105
|
+
上下留 4px 内边距:滚动容器会裁掉首末项选中时的 ring。 */}
|
|
106
|
+
<div className="-mx-5 flex max-h-72 min-h-0 flex-col gap-0.5 overflow-y-auto px-5 py-1" role="listbox" aria-label={t('sandbox.forward.targets')}>
|
|
107
|
+
{visible.map((target) => (
|
|
108
|
+
<button
|
|
109
|
+
key={target.conversationId}
|
|
110
|
+
type="button"
|
|
111
|
+
role="option"
|
|
112
|
+
aria-selected={selected === target.conversationId}
|
|
113
|
+
className={cn('flex items-center gap-3 rounded-lg px-2 py-1.5 text-left hover:bg-accent', selected === target.conversationId && 'bg-accent ring-1 ring-primary/40')}
|
|
114
|
+
onClick={() => setSelected(target.conversationId)}
|
|
115
|
+
>
|
|
116
|
+
<SandboxAvatar kind={target.avatarKind} name={target.title} src={target.avatar} className="size-8" />
|
|
117
|
+
<span className="flex min-w-0 flex-col">
|
|
118
|
+
<strong className="truncate text-[13px] font-medium">{target.title}</strong>
|
|
119
|
+
<small className="truncate text-xs text-muted-foreground">{target.subtitle || target.conversationId}</small>
|
|
120
|
+
</span>
|
|
121
|
+
</button>
|
|
122
|
+
))}
|
|
123
|
+
{!visible.length && (
|
|
124
|
+
<p className="py-10 text-center text-xs text-muted-foreground">
|
|
125
|
+
{needle ? t('sandbox.forward.noMatch') : t(`sandbox.forward.empty.${tab}`)}
|
|
126
|
+
</p>
|
|
127
|
+
)}
|
|
128
|
+
</div>
|
|
129
|
+
{error && <p role="alert" className="text-xs text-destructive">{error}</p>}
|
|
130
|
+
<DialogFooter>
|
|
131
|
+
<Button variant="outline" disabled={submitting} onClick={() => onOpenChange(false)}>{t('sandbox.common.cancel')}</Button>
|
|
132
|
+
<Button disabled={!selected || submitting} onClick={confirm}>{submitting ? t('sandbox.forward.sending') : t('sandbox.forward.send')}</Button>
|
|
133
|
+
</DialogFooter>
|
|
134
|
+
</DialogContent>
|
|
135
|
+
</Dialog>
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 合并转发详情:每层嵌套转发是叠在上一层之上的二级窗口,右上角关闭只退回上一层,关闭根层一并关闭整条链。
|
|
141
|
+
* 各层以 React 子树嵌套,Esc 与点遮罩只作用于最上层。
|
|
142
|
+
*/
|
|
143
|
+
export function ForwardViewer({ stack, imageOpen, onCloseFrom, onOpenForward, onOpenImage }: {
|
|
144
|
+
stack: readonly ForwardDialogFrame[]
|
|
145
|
+
/** 大图预览开着时:预览挂在 body 下,点它或按 Esc 会被弹窗当成外部关闭,这期间不响应关闭请求。 */
|
|
146
|
+
imageOpen: boolean
|
|
147
|
+
/** 关闭第 `depth` 层及其上的全部层。 */
|
|
148
|
+
onCloseFrom(depth: number): void
|
|
149
|
+
onOpenForward(forwardId: string): void
|
|
150
|
+
onOpenImage(url: string): void
|
|
151
|
+
}) {
|
|
152
|
+
return <ForwardFrameDialog stack={stack} depth={0} imageOpen={imageOpen} onCloseFrom={onCloseFrom} onOpenForward={onOpenForward} onOpenImage={onOpenImage} />
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function ForwardFrameDialog({ stack, depth, imageOpen, onCloseFrom, onOpenForward, onOpenImage }: {
|
|
156
|
+
stack: readonly ForwardDialogFrame[]
|
|
157
|
+
depth: number
|
|
158
|
+
imageOpen: boolean
|
|
159
|
+
onCloseFrom(depth: number): void
|
|
160
|
+
onOpenForward(forwardId: string): void
|
|
161
|
+
onOpenImage(url: string): void
|
|
162
|
+
}) {
|
|
163
|
+
const t = useT()
|
|
164
|
+
const { participants, media, mediaFailures, workspace } = useWorkspace()
|
|
165
|
+
const current = stack[depth]
|
|
166
|
+
// 退场动画期间保留这一层的内容,不闪成「暂无消息」。
|
|
167
|
+
const frame = useRetained(current)
|
|
168
|
+
if (!frame) return null
|
|
169
|
+
// 媒体自动生成的标签与嵌套转发的占位都和画出来的内容重复,不再作为正文显示。
|
|
170
|
+
const nodeText = (item: SandboxForwardNode) => {
|
|
171
|
+
const text = forwardNodeText(item)
|
|
172
|
+
return item.media?.length === 1 && text === `[${getMediaLabel(item.media[0]!)}] ${item.media[0]!.name}` ? '' : text
|
|
173
|
+
}
|
|
174
|
+
const { appearance } = workspace
|
|
175
|
+
return (
|
|
176
|
+
<Dialog open={!!current} onOpenChange={(open) => !open && !imageOpen && onCloseFrom(depth)}>
|
|
177
|
+
<DialogContent className="flex max-h-[80vh] flex-col gap-3 overflow-hidden sm:max-w-lg">
|
|
178
|
+
{/* 右侧留出关闭按钮的位置,长标题截断而不压到 X 上。 */}
|
|
179
|
+
<DialogHeader className="min-w-0 pr-8">
|
|
180
|
+
<DialogTitle className="truncate leading-6">{frame.title || t('sandbox.forward.title')}</DialogTitle>
|
|
181
|
+
</DialogHeader>
|
|
182
|
+
<div className="-mx-5 flex min-h-0 flex-1 flex-col overflow-y-auto px-5">
|
|
183
|
+
{frame.items.length ? (
|
|
184
|
+
// 与聊天页同一套消息行结构与类名(messages.css):气泡底色、连续收角、尖角与描边设置都和聊天页一致。
|
|
185
|
+
// 弹窗经 Portal 挂到 body,不在 .sandbox-webqq 里,强调色在这里补上。
|
|
186
|
+
<ol
|
|
187
|
+
className={cn('sandbox-message-list sandbox-forward-list', appearance.sandboxTimBubbleTail && 'has-tail', appearance.sandboxIncomingBubbleStyle === 'outline' && 'has-outline')}
|
|
188
|
+
style={{ '--sandbox-accent': appearance.sandboxAccentColor } as React.CSSProperties}
|
|
189
|
+
>
|
|
190
|
+
{frame.items.map((item, index) => {
|
|
191
|
+
const merged = isMergedForwardNode(frame.items, index)
|
|
192
|
+
const nested = item.forwardId ? frame.nestedForwards[item.forwardId] : undefined
|
|
193
|
+
const preview = nested ? buildForwardPreview(nested) : undefined
|
|
194
|
+
const text = nodeText(item)
|
|
195
|
+
return (
|
|
196
|
+
<li key={index} className={cn('sandbox-message-row is-incoming', getForwardNodeClusterClass(frame.items, index), merged && 'is-merged')}>
|
|
197
|
+
{/* 合并项保留头像占位(visibility 隐藏),气泡与上一条对齐。 */}
|
|
198
|
+
<span className="sandbox-message-avatar-wrap">
|
|
199
|
+
<SandboxAvatar kind={participants[item.userId]?.isBot ? 'bot' : 'user'} name={item.nickname} src={participants[item.userId]?.avatar} className="size-8" />
|
|
200
|
+
</span>
|
|
201
|
+
<div className="sandbox-message-content">
|
|
202
|
+
{!merged && <div className="sandbox-message-sender-line"><span>{item.nickname}</span></div>}
|
|
203
|
+
<div className="sandbox-message-body">
|
|
204
|
+
<div className="sandbox-message-stack">
|
|
205
|
+
<div className="sandbox-message-bubble">
|
|
206
|
+
{/* 自定义节点可以同时带嵌套转发、媒体与文字:转发卡片与聊天页的引用一样放在最上面。 */}
|
|
207
|
+
{item.forwardId && (
|
|
208
|
+
<button type="button" className="sandbox-message-quote sandbox-message-forward" aria-label={t('sandbox.forward.open')} onClick={() => item.forwardId && onOpenForward(item.forwardId)}>
|
|
209
|
+
<strong className="sandbox-message-quote-title">{preview?.title || t('sandbox.forward.title')}</strong>
|
|
210
|
+
{preview ? (
|
|
211
|
+
<>
|
|
212
|
+
{preview.lines.map((line, lineIndex) => <span key={lineIndex}>{line}</span>)}
|
|
213
|
+
<span className="sandbox-message-forward-entry">{t('sandbox.forward.total', { count: preview.total })}<ChevronRightIcon className="size-4" /></span>
|
|
214
|
+
</>
|
|
215
|
+
) : <span>{t('sandbox.forward.placeholder')}</span>}
|
|
216
|
+
</button>
|
|
217
|
+
)}
|
|
218
|
+
{item.media?.map((entry) => {
|
|
219
|
+
const source = media[entry.id]
|
|
220
|
+
return (
|
|
221
|
+
<div key={entry.id} className="sandbox-message-media">
|
|
222
|
+
{!source ? (
|
|
223
|
+
<span className="sandbox-message-media-loading">{mediaFailures[entry.id] ? t('sandbox.media.unavailable') : t('sandbox.media.loading')}</span>
|
|
224
|
+
) : entry.type === 'image' ? (
|
|
225
|
+
<button type="button" className="sandbox-message-image" aria-label={t('sandbox.media.viewImage')} onClick={() => onOpenImage(source)}>
|
|
226
|
+
<img src={source} alt={entry.name} />
|
|
227
|
+
</button>
|
|
228
|
+
) : entry.type === 'audio' ? (
|
|
229
|
+
<audio src={source} controls preload="metadata" />
|
|
230
|
+
) : entry.type === 'video' ? (
|
|
231
|
+
<video src={source} controls preload="metadata" />
|
|
232
|
+
) : (
|
|
233
|
+
<a href={source} download={entry.name} className="sandbox-message-file">
|
|
234
|
+
<PaperclipIcon className="size-[18px]" />
|
|
235
|
+
<span><strong>{entry.name}</strong><small>{formatMediaSize(entry.size)}</small></span>
|
|
236
|
+
</a>
|
|
237
|
+
)}
|
|
238
|
+
</div>
|
|
239
|
+
)
|
|
240
|
+
})}
|
|
241
|
+
{text && <span className="sandbox-message-text">{text}</span>}
|
|
242
|
+
{!text && !item.media?.length && !item.forwardId && <span className="sandbox-message-text">{t('sandbox.forward.emptyNode')}</span>}
|
|
243
|
+
</div>
|
|
244
|
+
</div>
|
|
245
|
+
</div>
|
|
246
|
+
</div>
|
|
247
|
+
</li>
|
|
248
|
+
)
|
|
249
|
+
})}
|
|
250
|
+
</ol>
|
|
251
|
+
) : <p className="py-8 text-center text-xs text-muted-foreground">{t('sandbox.forward.noItems')}</p>}
|
|
252
|
+
</div>
|
|
253
|
+
<ForwardFrameDialog stack={stack} depth={depth + 1} imageOpen={imageOpen} onCloseFrom={onCloseFrom} onOpenForward={onOpenForward} onOpenImage={onOpenImage} />
|
|
254
|
+
</DialogContent>
|
|
255
|
+
</Dialog>
|
|
256
|
+
)
|
|
257
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { createPortal } from 'react-dom'
|
|
3
|
+
import { XIcon } from 'lucide-react'
|
|
4
|
+
import { Button } from '@chatluna/ui-kit'
|
|
5
|
+
import { useT } from '../i18n'
|
|
6
|
+
|
|
7
|
+
const MIN_SCALE = 1
|
|
8
|
+
const MAX_SCALE = 8
|
|
9
|
+
const ZOOM_FACTOR = 1.2
|
|
10
|
+
|
|
11
|
+
/** 大图预览:滚轮以指针为锚点缩放(1–8 倍),放大后可拖动平移,Esc 或点背景关闭。 */
|
|
12
|
+
export function ImagePreview({ url, onClose }: { url: string, onClose(): void }) {
|
|
13
|
+
const t = useT()
|
|
14
|
+
const image = React.useRef<HTMLImageElement>(null)
|
|
15
|
+
const [scale, setScale] = React.useState(1)
|
|
16
|
+
const [offset, setOffset] = React.useState({ x: 0, y: 0 })
|
|
17
|
+
const drag = React.useRef<{ id: number, x: number, y: number, ox: number, oy: number, moved: boolean } | undefined>(undefined)
|
|
18
|
+
const suppressClick = React.useRef(0)
|
|
19
|
+
|
|
20
|
+
React.useEffect(() => {
|
|
21
|
+
const onKey = (event: KeyboardEvent) => {
|
|
22
|
+
if (event.key === 'Escape') onClose()
|
|
23
|
+
}
|
|
24
|
+
window.addEventListener('keydown', onKey)
|
|
25
|
+
return () => window.removeEventListener('keydown', onKey)
|
|
26
|
+
}, [onClose])
|
|
27
|
+
|
|
28
|
+
const clampOffset = (next: { x: number, y: number }, nextScale: number) => {
|
|
29
|
+
const element = image.current
|
|
30
|
+
if (!element) return next
|
|
31
|
+
const limitX = Math.max((element.offsetWidth * nextScale - window.innerWidth) / 2, 0)
|
|
32
|
+
const limitY = Math.max((element.offsetHeight * nextScale - window.innerHeight) / 2, 0)
|
|
33
|
+
return { x: Math.min(Math.max(next.x, -limitX), limitX), y: Math.min(Math.max(next.y, -limitY), limitY) }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const onWheel = (event: React.WheelEvent) => {
|
|
37
|
+
if (!event.deltaY) return
|
|
38
|
+
const target = Math.min(Math.max(scale * (event.deltaY < 0 ? ZOOM_FACTOR : 1 / ZOOM_FACTOR), MIN_SCALE), MAX_SCALE)
|
|
39
|
+
if (target === scale) return
|
|
40
|
+
const ratio = target / scale
|
|
41
|
+
const dx = event.clientX - window.innerWidth / 2
|
|
42
|
+
const dy = event.clientY - window.innerHeight / 2
|
|
43
|
+
setScale(target)
|
|
44
|
+
setOffset(clampOffset({ x: dx - (dx - offset.x) * ratio, y: dy - (dy - offset.y) * ratio }, target))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return createPortal(
|
|
48
|
+
<div
|
|
49
|
+
role="dialog"
|
|
50
|
+
aria-modal="true"
|
|
51
|
+
aria-label={t('sandbox.media.preview')}
|
|
52
|
+
className="fixed inset-0 z-[100] flex items-center justify-center overflow-hidden bg-black/80 text-sm leading-6 animate-in fade-in-0"
|
|
53
|
+
onWheel={onWheel}
|
|
54
|
+
onClick={(event) => {
|
|
55
|
+
if (performance.now() < suppressClick.current) return
|
|
56
|
+
if (event.target === event.currentTarget) onClose()
|
|
57
|
+
}}
|
|
58
|
+
>
|
|
59
|
+
<img
|
|
60
|
+
ref={image}
|
|
61
|
+
src={url}
|
|
62
|
+
alt=""
|
|
63
|
+
draggable={false}
|
|
64
|
+
className="max-h-[90vh] max-w-[90vw] select-none object-contain transition-transform duration-75"
|
|
65
|
+
style={{ transform: `translate(${offset.x}px, ${offset.y}px) scale(${scale})` }}
|
|
66
|
+
onPointerDown={(event) => {
|
|
67
|
+
if (event.button !== 0 || scale <= MIN_SCALE) return
|
|
68
|
+
drag.current = { id: event.pointerId, x: event.clientX, y: event.clientY, ox: offset.x, oy: offset.y, moved: false }
|
|
69
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
70
|
+
}}
|
|
71
|
+
onPointerMove={(event) => {
|
|
72
|
+
const state = drag.current
|
|
73
|
+
if (!state || state.id !== event.pointerId) return
|
|
74
|
+
const dx = event.clientX - state.x
|
|
75
|
+
const dy = event.clientY - state.y
|
|
76
|
+
if (!state.moved && Math.hypot(dx, dy) < 3) return
|
|
77
|
+
state.moved = true
|
|
78
|
+
setOffset(clampOffset({ x: state.ox + dx, y: state.oy + dy }, scale))
|
|
79
|
+
}}
|
|
80
|
+
onPointerUp={(event) => {
|
|
81
|
+
if (drag.current?.moved) suppressClick.current = performance.now() + 250
|
|
82
|
+
drag.current = undefined
|
|
83
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId)
|
|
84
|
+
}}
|
|
85
|
+
/>
|
|
86
|
+
<span className="absolute bottom-6 left-1/2 -translate-x-1/2 rounded-full bg-black/60 px-3 py-1 text-xs text-white tabular-nums">{Math.round(scale * 100)}%</span>
|
|
87
|
+
<Button variant="ghost" size="icon" className="absolute top-4 right-4 text-white hover:bg-white/15 hover:text-white" aria-label={t('sandbox.common.close')} onClick={onClose}>
|
|
88
|
+
<XIcon />
|
|
89
|
+
</Button>
|
|
90
|
+
</div>,
|
|
91
|
+
document.body,
|
|
92
|
+
)
|
|
93
|
+
}
|