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,63 @@
|
|
|
1
|
+
export interface WorkspaceThumbnailCapture {
|
|
2
|
+
element: HTMLElement
|
|
3
|
+
width: number
|
|
4
|
+
height: number
|
|
5
|
+
scrollOffsets: Array<{ index: number, left: number, top: number }>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function captureWorkspaceThumbnail(root: HTMLElement): WorkspaceThumbnailCapture {
|
|
9
|
+
const rect = root.getBoundingClientRect()
|
|
10
|
+
const nodes = [root, ...root.querySelectorAll<HTMLElement>('*')]
|
|
11
|
+
const scrollOffsets = nodes.flatMap((node, index) => node.scrollLeft || node.scrollTop
|
|
12
|
+
? [{ index, left: node.scrollLeft, top: node.scrollTop }]
|
|
13
|
+
: [])
|
|
14
|
+
const element = root.cloneNode(true) as HTMLElement
|
|
15
|
+
copyCanvasPixels(root, element)
|
|
16
|
+
// 缩略图只保留工作区画面;被控覆盖层的 scrim、点阵、WebGL 跑马灯和任务栏会挡住真实页面。
|
|
17
|
+
stripAgentObserveOverlay(element)
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
element,
|
|
21
|
+
width: rect.width,
|
|
22
|
+
height: rect.height,
|
|
23
|
+
scrollOffsets,
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function instantiateWorkspaceThumbnail(capture: WorkspaceThumbnailCapture) {
|
|
28
|
+
const element = capture.element.cloneNode(true) as HTMLElement
|
|
29
|
+
copyCanvasPixels(capture.element, element)
|
|
30
|
+
const nodes = [element, ...element.querySelectorAll<HTMLElement>('*')]
|
|
31
|
+
for (const node of nodes) node.removeAttribute('id')
|
|
32
|
+
element.inert = true
|
|
33
|
+
element.setAttribute('aria-hidden', 'true')
|
|
34
|
+
element.classList.add('sandbox-space-thumbnail-live')
|
|
35
|
+
return element
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function copyCanvasPixels(sourceRoot: HTMLElement, targetRoot: HTMLElement) {
|
|
39
|
+
const sourceCanvases = [...sourceRoot.querySelectorAll('canvas')].filter((node) => !node.closest('.sandbox-agent-observe'))
|
|
40
|
+
const targetCanvases = [...targetRoot.querySelectorAll('canvas')].filter((node) => !node.closest('.sandbox-agent-observe'))
|
|
41
|
+
for (const [index, source] of sourceCanvases.entries()) {
|
|
42
|
+
const target = targetCanvases[index]
|
|
43
|
+
if (!target) continue
|
|
44
|
+
target.width = source.width
|
|
45
|
+
target.height = source.height
|
|
46
|
+
// cloneNode 不复制 Canvas 的像素缓冲;转成位图后再写入,才能保留工作区里真正需要展示的画布。
|
|
47
|
+
target.getContext('2d')?.drawImage(source, 0, 0)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function stripAgentObserveOverlay(root: HTMLElement) {
|
|
52
|
+
for (const node of root.querySelectorAll('.sandbox-agent-observe')) node.remove()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function restoreWorkspaceThumbnailScroll(capture: WorkspaceThumbnailCapture, element: HTMLElement) {
|
|
56
|
+
const nodes = [element, ...element.querySelectorAll<HTMLElement>('*')]
|
|
57
|
+
for (const { index, left, top } of capture.scrollOffsets) {
|
|
58
|
+
const node = nodes[index]
|
|
59
|
+
if (!node) continue
|
|
60
|
+
node.scrollLeft = left
|
|
61
|
+
node.scrollTop = top
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface WorkspaceThumbnailTransform {
|
|
2
|
+
scale: number
|
|
3
|
+
left: number
|
|
4
|
+
top: number
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function calculateContainedWorkspaceThumbnailTransform(input: {
|
|
8
|
+
containerWidth: number
|
|
9
|
+
containerHeight: number
|
|
10
|
+
canvasWidth: number
|
|
11
|
+
canvasHeight: number
|
|
12
|
+
}): WorkspaceThumbnailTransform | undefined {
|
|
13
|
+
const { containerWidth, containerHeight, canvasWidth, canvasHeight } = input
|
|
14
|
+
if (!containerWidth || !containerHeight || !canvasWidth || !canvasHeight) return
|
|
15
|
+
const scale = Math.min(containerWidth / canvasWidth, containerHeight / canvasHeight)
|
|
16
|
+
return {
|
|
17
|
+
scale,
|
|
18
|
+
left: (containerWidth - canvasWidth * scale) / 2,
|
|
19
|
+
top: (containerHeight - canvasHeight * scale) / 2,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import type { SandboxTestSpaceSummary } from '../../src/test-spaces'
|
|
3
|
+
import { invoke } from '../workspace/store'
|
|
4
|
+
|
|
5
|
+
const POLL_INTERVAL = 1500
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* AI 测试空间列表的轮询:标签页上的 AI 控制态、总览卡片与被控覆盖层都依赖空间 status,而状态变化
|
|
9
|
+
* 不经实时信号推送,所以不论当前在哪个工作区都每 1.5 秒重读一次。只有空间、状态或快照修订号变化时
|
|
10
|
+
* 才替换列表,避免每次轮询都让缩略图重渲染。
|
|
11
|
+
*/
|
|
12
|
+
export function useSpaceList() {
|
|
13
|
+
const [spaces, setSpaces] = React.useState<SandboxTestSpaceSummary[]>([])
|
|
14
|
+
const [loaded, setLoaded] = React.useState(false)
|
|
15
|
+
const signature = React.useRef('')
|
|
16
|
+
|
|
17
|
+
const reload = React.useCallback(async () => {
|
|
18
|
+
const next = await invoke('onebot-sandbox.test-spaces', undefined)
|
|
19
|
+
const key = next.map(({ id, name, status, snapshot }) => `${id}:${name}:${status}:${snapshot.revision}`).join('|')
|
|
20
|
+
setLoaded(true)
|
|
21
|
+
if (key === signature.current) return
|
|
22
|
+
signature.current = key
|
|
23
|
+
setSpaces(next)
|
|
24
|
+
}, [])
|
|
25
|
+
|
|
26
|
+
React.useEffect(() => {
|
|
27
|
+
// 轮询失败不打断界面:下一轮会重试,操作类请求的失败另有 Toast。
|
|
28
|
+
const poll = () => void reload().catch(() => {})
|
|
29
|
+
poll()
|
|
30
|
+
const timer = setInterval(poll, POLL_INTERVAL)
|
|
31
|
+
return () => clearInterval(timer)
|
|
32
|
+
}, [reload])
|
|
33
|
+
|
|
34
|
+
return { spaces, loaded, reload }
|
|
35
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { spring } from 'animejs'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* 进出空间仿 iOS 打开与关闭 App:
|
|
5
|
+
* - 进入:刚挂载的工作区从卡片里的缩略画面弹簧放大就位,垫在背后的总览(app.tsx 的 `launch`)以卡片为中心推近淡出;
|
|
6
|
+
* - 退回:刚离开的空间对应的整张卡片从工作区位置缩回网格位,卡片里的缩略画面始终与工作区画面重合,其余卡片错落浮现。
|
|
7
|
+
*
|
|
8
|
+
* 动画一律交给 WAAPI(Element.animate),transform 与 opacity 在合成线程上运行:新页面挂载时主线程会被渲染
|
|
9
|
+
* 占满几帧,用 JS 逐帧写样式恰好在这几帧掉帧。圆角不能合成,单独拆成一条动画,免得把同一元素的 transform
|
|
10
|
+
* 也拖回主线程。
|
|
11
|
+
*/
|
|
12
|
+
const SURFACE = springTiming(400, 0.17)
|
|
13
|
+
const BACKDROP = springTiming(340, 0)
|
|
14
|
+
const CARD_IN = springTiming(300, 0.25)
|
|
15
|
+
const CARD_STAGGER = 24
|
|
16
|
+
/** 工作区铺满时总览推近的倍数,对应 iOS 主屏幕在 App 打开时的推近。 */
|
|
17
|
+
const BACKDROP_SCALE = 1.08
|
|
18
|
+
const CARD_RADIUS = 18
|
|
19
|
+
const ZOOMING_CLASS = 'sandbox-workspace-zooming'
|
|
20
|
+
/** 动画期间隐藏卡片缩略图的标记;用属性而不是类名,React 重渲染卡片时不会把它冲掉。 */
|
|
21
|
+
const HIDDEN_ATTRIBUTE = 'data-zoom-hidden'
|
|
22
|
+
const THUMBNAIL_IMAGE = '.sandbox-space-thumbnail-live, .sandbox-space-thumbnail-preview'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 弹簧曲线转成 CSS `linear()`。anime.js 的 `spring` 按 SwiftUI 的感知时长换算刚度与阻尼,完全静止要到
|
|
26
|
+
* 感知时长的近两倍;最后那段位移不到 1%,在 1.5 倍处截断并补到终点,早些解除变换、恢复交互。
|
|
27
|
+
*/
|
|
28
|
+
function springTiming(duration: number, bounce: number): KeyframeAnimationOptions {
|
|
29
|
+
const curve = spring({ duration, bounce })
|
|
30
|
+
const total = Math.min(curve.settlingDuration, Math.round(duration * 1.5))
|
|
31
|
+
const ratio = total / curve.settlingDuration
|
|
32
|
+
const samples = 64
|
|
33
|
+
const points = Array.from({ length: samples + 1 }, (_, index) => index === samples ? 1 : Math.round(curve.ease(index / samples * ratio) * 1e4) / 1e4)
|
|
34
|
+
return { duration: total, easing: `linear(${points.join(', ')})` }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Motion {
|
|
38
|
+
animations: Animation[]
|
|
39
|
+
cleanup(): void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let activeMotion: Motion | undefined
|
|
43
|
+
|
|
44
|
+
function prefersReducedMotion(): boolean {
|
|
45
|
+
return window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function captureZoomRect(element: Element | null | undefined): DOMRect | undefined {
|
|
49
|
+
if (prefersReducedMotion()) return
|
|
50
|
+
const rect = element?.getBoundingClientRect()
|
|
51
|
+
return rect && rect.width > 0 && rect.height > 0 ? rect : undefined
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 卡片里缩略画面实际占的矩形(缩略图按 contain 缩放居中,四周可能留白),画面在这一端与缩略图严丝合缝。
|
|
56
|
+
* 新建的空间还没有缩略图,退回整张卡片。
|
|
57
|
+
*/
|
|
58
|
+
export function captureCardZoomRect(card: Element | null | undefined): DOMRect | undefined {
|
|
59
|
+
return captureZoomRect(card?.querySelector(THUMBNAIL_IMAGE) ?? card)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 连续触发时先把上一组动画收尾,避免两组 transform 同时驱动同一元素。
|
|
63
|
+
function finishActiveMotion() {
|
|
64
|
+
const motion = activeMotion
|
|
65
|
+
activeMotion = undefined
|
|
66
|
+
if (!motion) return
|
|
67
|
+
for (const animation of motion.animations) animation.cancel()
|
|
68
|
+
motion.cleanup()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 让进行中的进出动画立即就位,供需要读取静止画面的调用方(离开空间前克隆工作区)使用。 */
|
|
72
|
+
export function settleZoom() {
|
|
73
|
+
finishActiveMotion()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 元素自带 transform 过渡时,动画结束、内联样式复位那一刻会被过渡再补间一次,动画期间整体禁用,收尾时恢复。
|
|
78
|
+
*/
|
|
79
|
+
function holdStyles(element: HTMLElement, styles: Partial<Record<'transformOrigin' | 'overflow' | 'zIndex', string>>) {
|
|
80
|
+
Object.assign(element.style, { transition: 'none', ...styles })
|
|
81
|
+
return () => {
|
|
82
|
+
for (const key of ['transition', ...Object.keys(styles)]) element.style.removeProperty(key.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`))
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 一组动画全部播完后收尾;中途被下一组顶替时由 `finishActiveMotion` 收尾。 */
|
|
87
|
+
function play(animations: Animation[], cleanup: () => void): Motion {
|
|
88
|
+
const motion: Motion = { animations, cleanup }
|
|
89
|
+
activeMotion = motion
|
|
90
|
+
void Promise.all(animations.map((animation) => animation.finished)).then(() => {
|
|
91
|
+
if (activeMotion !== motion) return
|
|
92
|
+
activeMotion = undefined
|
|
93
|
+
cleanup()
|
|
94
|
+
}, () => {})
|
|
95
|
+
return motion
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 从 `from` 等比缩放到元素原位的 transform 起点(transform-origin 为左上角):`anchor` 是元素内要与 `from` 重合的
|
|
100
|
+
* 区域,缺省为元素自身;宽高比不一致时(快照渲染的缩略图)按宽度等比缩放并纵向居中。
|
|
101
|
+
*/
|
|
102
|
+
function transformFrom(from: DOMRect, box: DOMRect, anchor: DOMRect = box) {
|
|
103
|
+
const scale = from.width / anchor.width
|
|
104
|
+
const x = from.left - box.left - (anchor.left - box.left) * scale
|
|
105
|
+
const y = from.top + (from.height - anchor.height * scale) / 2 - box.top - (anchor.top - box.top) * scale
|
|
106
|
+
return { scale, transform: `translate(${x}px, ${y}px) scale(${scale})` }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const IDENTITY = 'translate(0px, 0px) scale(1)'
|
|
110
|
+
|
|
111
|
+
/** 进入空间:刚挂载的工作区从卡片缩略画面放大就位,垫在背后的总览推近淡出,结束后调用 `onDone` 卸下它。 */
|
|
112
|
+
export function zoomWorkspaceFromRect(workspace: HTMLElement, thumb: DOMRect, backdrop?: HTMLElement | null, card?: HTMLElement | null, onDone?: () => void) {
|
|
113
|
+
finishActiveMotion()
|
|
114
|
+
const full = workspace.getBoundingClientRect()
|
|
115
|
+
if (prefersReducedMotion() || !full.width || !full.height) {
|
|
116
|
+
onDone?.()
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
const surface = workspace
|
|
120
|
+
const { scale, transform } = transformFrom(thumb, full)
|
|
121
|
+
// 缩放期间在根节点标记状态,供需要回避变换中坐标的覆盖层(如挂在 body 上的弹层)据此隐藏。
|
|
122
|
+
document.documentElement.classList.add(ZOOMING_CLASS)
|
|
123
|
+
// 工作区平时不裁剪(聊天列要延伸到页头背后),缩放期间按圆角裁剪成卡片形状。
|
|
124
|
+
const releases = [holdStyles(surface, { transformOrigin: '0 0', overflow: 'hidden' })]
|
|
125
|
+
const animations = [
|
|
126
|
+
surface.animate({ transform: [transform, IDENTITY] }, SURFACE),
|
|
127
|
+
// 圆角按起始缩放比预放大,首帧在屏幕上与卡片圆角一致。
|
|
128
|
+
surface.animate({ borderRadius: [`${CARD_RADIUS / scale}px`, '0px'] }, SURFACE),
|
|
129
|
+
]
|
|
130
|
+
if (backdrop) {
|
|
131
|
+
const box = backdrop.getBoundingClientRect()
|
|
132
|
+
releases.push(holdStyles(backdrop, { transformOrigin: `${thumb.left + thumb.width / 2 - box.left}px ${thumb.top + thumb.height / 2 - box.top}px` }))
|
|
133
|
+
// 播完保持隐藏,等调用方卸下总览。
|
|
134
|
+
animations.push(backdrop.animate({
|
|
135
|
+
transform: ['scale(1)', `scale(${BACKDROP_SCALE})`],
|
|
136
|
+
opacity: [1, 0],
|
|
137
|
+
}, { ...BACKDROP, fill: 'forwards' }))
|
|
138
|
+
}
|
|
139
|
+
if (card) {
|
|
140
|
+
card.setAttribute(HIDDEN_ATTRIBUTE, '')
|
|
141
|
+
releases.push(() => card.removeAttribute(HIDDEN_ATTRIBUTE))
|
|
142
|
+
}
|
|
143
|
+
play(animations, () => {
|
|
144
|
+
for (const release of releases) release()
|
|
145
|
+
document.documentElement.classList.remove(ZOOMING_CLASS)
|
|
146
|
+
onDone?.()
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 退回总览:`exit.card` 是刚离开的空间对应的卡片,整张卡片从工作区位置缩回网格位,卡片里的缩略画面始终与
|
|
152
|
+
* 工作区画面重合;其余卡片错落浮现。缺少起点(从其他标签页进入总览)时只做错落浮现。
|
|
153
|
+
*
|
|
154
|
+
* 返回取消函数:本次动画还在播时立即收尾并返回 true。开发环境的 StrictMode 会把挂载时的 layout effect
|
|
155
|
+
* 卸载再重跑一遍,调用方据此把起点放回去,重跑时照常缩回,而不是只剩错落浮现。
|
|
156
|
+
*/
|
|
157
|
+
export function zoomOverviewIn(overview: HTMLElement, exit?: { card: HTMLElement | null, rect: DOMRect }): () => boolean {
|
|
158
|
+
finishActiveMotion()
|
|
159
|
+
if (prefersReducedMotion()) return () => false
|
|
160
|
+
const releases: Array<() => void> = []
|
|
161
|
+
const animations: Animation[] = []
|
|
162
|
+
const card = exit?.card
|
|
163
|
+
|
|
164
|
+
if (card && exit) {
|
|
165
|
+
// 放开总览裁剪并抬高层级,卡片才能越过总览边界覆盖整个工作区再缩回网格位;视口外的卡片也照常渲染。
|
|
166
|
+
overview.classList.add('is-zooming')
|
|
167
|
+
releases.push(() => overview.classList.remove('is-zooming'))
|
|
168
|
+
const box = card.getBoundingClientRect()
|
|
169
|
+
const image = card.querySelector(THUMBNAIL_IMAGE)?.getBoundingClientRect()
|
|
170
|
+
const { transform } = transformFrom(exit.rect, box, image?.width ? image : box)
|
|
171
|
+
releases.push(holdStyles(card, { transformOrigin: '0 0', zIndex: '30' }))
|
|
172
|
+
animations.push(
|
|
173
|
+
card.animate({ transform: [transform, IDENTITY] }, SURFACE),
|
|
174
|
+
// 缩略画面与工作区重合时卡片铺满工作区,那一端不带圆角。
|
|
175
|
+
card.animate({ borderRadius: ['0px', `${CARD_RADIUS}px`] }, SURFACE),
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const others = [...overview.querySelectorAll<HTMLElement>('.sandbox-space-card')].filter((element) => element !== card)
|
|
180
|
+
for (const [index, element] of others.entries()) {
|
|
181
|
+
releases.push(holdStyles(element, {}))
|
|
182
|
+
animations.push(element.animate({
|
|
183
|
+
transform: ['translateY(16px)', 'translateY(0px)'],
|
|
184
|
+
opacity: [0, 1],
|
|
185
|
+
}, { ...CARD_IN, delay: index * CARD_STAGGER, fill: 'backwards' }))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!animations.length) return () => false
|
|
189
|
+
const motion = play(animations, () => {
|
|
190
|
+
for (const release of releases) release()
|
|
191
|
+
})
|
|
192
|
+
return () => {
|
|
193
|
+
if (activeMotion !== motion) return false
|
|
194
|
+
finishActiveMotion()
|
|
195
|
+
return true
|
|
196
|
+
}
|
|
197
|
+
}
|