|
@@ -1,7 +1,8 @@
|
|
|
-import { Show, createEffect, createSignal, onCleanup } from "solid-js"
|
|
|
|
|
|
|
+import { Show, createEffect, onCleanup, onMount } from "solid-js"
|
|
|
|
|
+import { createStore } from "solid-js/store"
|
|
|
import { Icon } from "@opencode-ai/ui/icon"
|
|
import { Icon } from "@opencode-ai/ui/icon"
|
|
|
import { usePlatform } from "@/context/platform"
|
|
import { usePlatform } from "@/context/platform"
|
|
|
-import type { PaasPlatform, PaasPreviewContent } from "@/paas"
|
|
|
|
|
|
|
+import { paasPreviewKind, paasRecord, paasText, type PaasPlatform, type PaasPreviewContent } from "@/paas"
|
|
|
import { ResearchReport } from "./report"
|
|
import { ResearchReport } from "./report"
|
|
|
|
|
|
|
|
export type PaasPreviewFile = {
|
|
export type PaasPreviewFile = {
|
|
@@ -14,9 +15,6 @@ export type PaasPreviewFile = {
|
|
|
size?: number
|
|
size?: number
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const textExtensions = /\.(txt|md|json|jsonl|ya?ml|csv|log|ts|tsx|js|jsx|py|sh|toml|ini|cfg|xml|html|css|sql|r|rb|go|rs|c|cpp|h|java)$/i
|
|
|
|
|
-const imageExtensions = /\.(png|jpe?g|webp|gif)$/i
|
|
|
|
|
-
|
|
|
|
|
function formatSize(value?: number) {
|
|
function formatSize(value?: number) {
|
|
|
if (!value || value < 0) return ""
|
|
if (!value || value < 0) return ""
|
|
|
if (value < 1024) return `${value} B`
|
|
if (value < 1024) return `${value} B`
|
|
@@ -26,21 +24,72 @@ function formatSize(value?: number) {
|
|
|
|
|
|
|
|
export function PaasPreview(props: { api?: PaasPlatform; file?: PaasPreviewFile; onClose: () => void }) {
|
|
export function PaasPreview(props: { api?: PaasPlatform; file?: PaasPreviewFile; onClose: () => void }) {
|
|
|
const platform = usePlatform()
|
|
const platform = usePlatform()
|
|
|
- const [content, setContent] = createSignal<PaasPreviewContent>()
|
|
|
|
|
- const [loading, setLoading] = createSignal(false)
|
|
|
|
|
- const [error, setError] = createSignal("")
|
|
|
|
|
- const [url, setUrl] = createSignal("")
|
|
|
|
|
|
|
+ const [state, setState] = createStore({
|
|
|
|
|
+ content: undefined as PaasPreviewContent | undefined,
|
|
|
|
|
+ loading: false,
|
|
|
|
|
+ error: "",
|
|
|
|
|
+ url: "",
|
|
|
|
|
+ retry: 0,
|
|
|
|
|
+ saving: false,
|
|
|
|
|
+ notice: "",
|
|
|
|
|
+ })
|
|
|
|
|
+ const content = () => state.content
|
|
|
|
|
+ const loading = () => state.loading
|
|
|
|
|
+ const error = () => state.error
|
|
|
|
|
+ const url = () => state.url
|
|
|
let request = 0
|
|
let request = 0
|
|
|
|
|
+ let pane: HTMLElement | undefined
|
|
|
|
|
+ let close: HTMLButtonElement | undefined
|
|
|
|
|
+ let origin: HTMLElement | undefined
|
|
|
|
|
|
|
|
const file = () => props.file
|
|
const file = () => props.file
|
|
|
- const kind = () => {
|
|
|
|
|
- const name = file()?.name || ""
|
|
|
|
|
- const mime = content()?.mime || file()?.mime || ""
|
|
|
|
|
- if (/\.pdf$/i.test(name) || mime === "application/pdf") return "pdf"
|
|
|
|
|
- if (imageExtensions.test(name) || mime.startsWith("image/")) return "image"
|
|
|
|
|
- if (/\.md$/i.test(name) || mime === "text/markdown") return "markdown"
|
|
|
|
|
- if (textExtensions.test(name) || mime.startsWith("text/")) return "text"
|
|
|
|
|
- return "unsupported"
|
|
|
|
|
|
|
+ const kind = () => paasPreviewKind(file()?.name || "", content()?.mime || file()?.mime || "")
|
|
|
|
|
+ onMount(() => {
|
|
|
|
|
+ origin = document.activeElement instanceof HTMLElement ? document.activeElement : undefined
|
|
|
|
|
+ close?.focus()
|
|
|
|
|
+ const handle = (event: KeyboardEvent) => {
|
|
|
|
|
+ if (event.defaultPrevented) return
|
|
|
|
|
+ if (event.key === "Escape") {
|
|
|
|
|
+ event.preventDefault()
|
|
|
|
|
+ props.onClose()
|
|
|
|
|
+ }
|
|
|
|
|
+ if (event.key !== "Tab" || !window.matchMedia("(max-width: 899px)").matches) return
|
|
|
|
|
+ const items = Array.from(
|
|
|
|
|
+ pane?.querySelectorAll<HTMLElement>('button:not(:disabled), a[href], iframe, [tabindex="0"]') || [],
|
|
|
|
|
+ ).filter((el) => el.getClientRects().length)
|
|
|
|
|
+ const next = event.shiftKey ? items.at(-1) : items[0]
|
|
|
|
|
+ const edge = event.shiftKey ? items[0] : items.at(-1)
|
|
|
|
|
+ if (document.activeElement === edge || !pane?.contains(document.activeElement)) {
|
|
|
|
|
+ event.preventDefault()
|
|
|
|
|
+ next?.focus()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ document.addEventListener("keydown", handle)
|
|
|
|
|
+ onCleanup(() => {
|
|
|
|
|
+ document.removeEventListener("keydown", handle)
|
|
|
|
|
+ if (origin?.isConnected) origin.focus()
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ onCleanup(() => {
|
|
|
|
|
+ request++
|
|
|
|
|
+ })
|
|
|
|
|
+ const save = async () => {
|
|
|
|
|
+ const target = file()
|
|
|
|
|
+ if (!target || !props.api || state.saving) return
|
|
|
|
|
+ const current = request
|
|
|
|
|
+ setState({ saving: true, notice: "" })
|
|
|
|
|
+ try {
|
|
|
|
|
+ const result = paasRecord(await props.api.request({ action: "download", ...target }))
|
|
|
|
|
+ if (request !== current) return
|
|
|
|
|
+ setState(
|
|
|
|
|
+ "notice",
|
|
|
|
|
+ result?.saved ? `已保存 ${paasText(result.name) || target.name}` : "已取消保存,文件仍保留在平台。",
|
|
|
|
|
+ )
|
|
|
|
|
+ } catch (reason) {
|
|
|
|
|
+ if (request === current) setState("notice", reason instanceof Error ? reason.message : "保存失败,请重试。")
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (request === current) setState("saving", false)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
const text = () => {
|
|
const text = () => {
|
|
|
const value = content()?.text || ""
|
|
const value = content()?.text || ""
|
|
@@ -55,10 +104,9 @@ export function PaasPreview(props: { api?: PaasPlatform; file?: PaasPreviewFile;
|
|
|
createEffect(() => {
|
|
createEffect(() => {
|
|
|
const target = file()
|
|
const target = file()
|
|
|
const api = props.api
|
|
const api = props.api
|
|
|
|
|
+ state.retry
|
|
|
const current = ++request
|
|
const current = ++request
|
|
|
- setContent(undefined)
|
|
|
|
|
- setError("")
|
|
|
|
|
- setLoading(!!target && !!api)
|
|
|
|
|
|
|
+ setState({ content: undefined, error: "", loading: !!target && !!api, saving: false, notice: "" })
|
|
|
if (!target || !api) return
|
|
if (!target || !api) return
|
|
|
void api
|
|
void api
|
|
|
.request({
|
|
.request({
|
|
@@ -75,37 +123,38 @@ export function PaasPreview(props: { api?: PaasPlatform; file?: PaasPreviewFile;
|
|
|
if (!value || typeof value.name !== "string" || typeof value.mime !== "string") {
|
|
if (!value || typeof value.name !== "string" || typeof value.mime !== "string") {
|
|
|
throw new Error("平台未返回可预览的文件内容。")
|
|
throw new Error("平台未返回可预览的文件内容。")
|
|
|
}
|
|
}
|
|
|
- setContent(value)
|
|
|
|
|
|
|
+ setState("content", value)
|
|
|
})
|
|
})
|
|
|
.catch((reason: unknown) => {
|
|
.catch((reason: unknown) => {
|
|
|
- if (current === request) setError(reason instanceof Error ? reason.message : "文件读取失败,请重试。")
|
|
|
|
|
|
|
+ if (current === request) setState("error", reason instanceof Error ? reason.message : "文件读取失败,请重试。")
|
|
|
})
|
|
})
|
|
|
.finally(() => {
|
|
.finally(() => {
|
|
|
- if (current === request) setLoading(false)
|
|
|
|
|
|
|
+ if (current === request) setState("loading", false)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
createEffect(() => {
|
|
|
const value = content()
|
|
const value = content()
|
|
|
if (!value?.data || !(kind() === "image" || kind() === "pdf")) {
|
|
if (!value?.data || !(kind() === "image" || kind() === "pdf")) {
|
|
|
- setUrl("")
|
|
|
|
|
|
|
+ setState("url", "")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
const copy = new Uint8Array(value.data.byteLength)
|
|
const copy = new Uint8Array(value.data.byteLength)
|
|
|
copy.set(value.data)
|
|
copy.set(value.data)
|
|
|
const next = URL.createObjectURL(new Blob([copy.buffer], { type: value.mime }))
|
|
const next = URL.createObjectURL(new Blob([copy.buffer], { type: value.mime }))
|
|
|
- setUrl(next)
|
|
|
|
|
|
|
+ setState("url", next)
|
|
|
onCleanup(() => URL.revokeObjectURL(next))
|
|
onCleanup(() => URL.revokeObjectURL(next))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
- <aside class="paas-preview" id="paas-preview" aria-label="右侧预览">
|
|
|
|
|
|
|
+ <aside ref={pane} class="paas-preview" id="paas-preview" aria-label="右侧预览">
|
|
|
<header class="paas-preview-header">
|
|
<header class="paas-preview-header">
|
|
|
<div class="paas-preview-heading">
|
|
<div class="paas-preview-heading">
|
|
|
<strong>预览</strong>
|
|
<strong>预览</strong>
|
|
|
<span>{file()?.name || "文件"}</span>
|
|
<span>{file()?.name || "文件"}</span>
|
|
|
</div>
|
|
</div>
|
|
|
<button
|
|
<button
|
|
|
|
|
+ ref={close}
|
|
|
type="button"
|
|
type="button"
|
|
|
class="paas-icon-action paas-preview-close"
|
|
class="paas-icon-action paas-preview-close"
|
|
|
aria-label="关闭预览"
|
|
aria-label="关闭预览"
|
|
@@ -131,8 +180,16 @@ export function PaasPreview(props: { api?: PaasPlatform; file?: PaasPreviewFile;
|
|
|
<strong>{target().name}</strong>
|
|
<strong>{target().name}</strong>
|
|
|
<small>
|
|
<small>
|
|
|
{target().mime || kind()}
|
|
{target().mime || kind()}
|
|
|
- {formatSize(content()?.size || target().size) ? ` · ${formatSize(content()?.size || target().size)}` : ""}
|
|
|
|
|
|
|
+ {formatSize(content()?.size || target().size)
|
|
|
|
|
+ ? ` · ${formatSize(content()?.size || target().size)}`
|
|
|
|
|
+ : ""}
|
|
|
</small>
|
|
</small>
|
|
|
|
|
+ <button type="button" disabled={state.saving || !props.api} onClick={() => void save()}>
|
|
|
|
|
+ {state.saving ? "正在保存…" : "保存文件"}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <Show when={state.notice}>
|
|
|
|
|
+ <p role="status">{state.notice}</p>
|
|
|
|
|
+ </Show>
|
|
|
</div>
|
|
</div>
|
|
|
<div
|
|
<div
|
|
|
class="paas-preview-body"
|
|
class="paas-preview-body"
|
|
@@ -152,6 +209,9 @@ export function PaasPreview(props: { api?: PaasPlatform; file?: PaasPreviewFile;
|
|
|
<div class="paas-preview-status" role="alert">
|
|
<div class="paas-preview-status" role="alert">
|
|
|
<strong>文件无法预览</strong>
|
|
<strong>文件无法预览</strong>
|
|
|
<p>{error()}</p>
|
|
<p>{error()}</p>
|
|
|
|
|
+ <button type="button" onClick={() => setState("retry", state.retry + 1)}>
|
|
|
|
|
+ 重试预览
|
|
|
|
|
+ </button>
|
|
|
</div>
|
|
</div>
|
|
|
</Show>
|
|
</Show>
|
|
|
<Show when={!loading() && !error() && content()}>
|
|
<Show when={!loading() && !error() && content()}>
|