git-subtree-dir: services/pascal-editor git-subtree-split: 312064e6e8122f958d2961e28ea85ad8b9db40c5
159 lines
4.7 KiB
TypeScript
159 lines
4.7 KiB
TypeScript
'use client'
|
|
|
|
import type { SceneGraph } from '@pascal-app/editor'
|
|
import { useRouter } from 'next/navigation'
|
|
import { useCallback, useState } from 'react'
|
|
import { withPascalBasePath } from '@/lib/pascal-base-path'
|
|
|
|
const EMPTY_GRAPH: SceneGraph = {
|
|
nodes: {},
|
|
rootNodeIds: [],
|
|
}
|
|
|
|
interface SaveButtonProps {
|
|
sceneId: string
|
|
name: string
|
|
version: number
|
|
getGraph: () => SceneGraph | null
|
|
}
|
|
|
|
/**
|
|
* Creates a new empty scene and navigates the user to it.
|
|
*/
|
|
export function CreateSceneButton({ label = '新建场景' }: { label?: string } = {}) {
|
|
const router = useRouter()
|
|
const [isCreating, setIsCreating] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const handleCreate = useCallback(async () => {
|
|
setIsCreating(true)
|
|
setError(null)
|
|
try {
|
|
const response = await fetch(withPascalBasePath('/api/scenes'), {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: '未命名场景', graph: EMPTY_GRAPH }),
|
|
})
|
|
if (!response.ok) {
|
|
setError(`创建场景失败 (${response.status})`)
|
|
return
|
|
}
|
|
const meta = (await response.json()) as { id: string }
|
|
router.push(`/scene/${meta.id}`)
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : '创建场景失败')
|
|
} finally {
|
|
setIsCreating(false)
|
|
}
|
|
}, [router])
|
|
|
|
return (
|
|
<div className="flex items-center gap-3">
|
|
{error && <span className="text-destructive text-xs">{error}</span>}
|
|
<button
|
|
className="rounded-md border border-border bg-accent px-3 py-1.5 font-medium text-sm hover:bg-accent/80 disabled:opacity-50"
|
|
disabled={isCreating}
|
|
onClick={handleCreate}
|
|
type="button"
|
|
>
|
|
{isCreating ? '正在创建…' : label}
|
|
</button>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Save + Save-as buttons that call the scenes API directly.
|
|
* Used for UIs that want explicit save controls outside of the Editor's
|
|
* built-in autosave plumbing.
|
|
*/
|
|
export function SaveButton({ sceneId, name, version, getGraph }: SaveButtonProps) {
|
|
const router = useRouter()
|
|
const [isSaving, setIsSaving] = useState(false)
|
|
const [status, setStatus] = useState<string | null>(null)
|
|
|
|
const handleSave = useCallback(async () => {
|
|
const graph = getGraph()
|
|
if (!graph) {
|
|
setStatus('没有可保存的场景')
|
|
return
|
|
}
|
|
setIsSaving(true)
|
|
setStatus(null)
|
|
try {
|
|
const response = await fetch(withPascalBasePath(`/api/scenes/${sceneId}`), {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'If-Match': String(version),
|
|
},
|
|
body: JSON.stringify({ name, graph }),
|
|
})
|
|
if (response.status === 409) {
|
|
setStatus('保存冲突,请刷新后继续')
|
|
return
|
|
}
|
|
if (!response.ok) {
|
|
setStatus(`保存失败 (${response.status})`)
|
|
return
|
|
}
|
|
setStatus('已保存')
|
|
} catch (error) {
|
|
setStatus(error instanceof Error ? error.message : '保存失败')
|
|
} finally {
|
|
setIsSaving(false)
|
|
}
|
|
}, [getGraph, name, sceneId, version])
|
|
|
|
const handleSaveAs = useCallback(async () => {
|
|
const graph = getGraph()
|
|
if (!graph) {
|
|
setStatus('没有可保存的场景')
|
|
return
|
|
}
|
|
const newName = typeof window !== 'undefined' ? window.prompt('请输入新场景名称', name) : null
|
|
if (!newName) return
|
|
setIsSaving(true)
|
|
setStatus(null)
|
|
try {
|
|
const response = await fetch(withPascalBasePath('/api/scenes'), {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name: newName, graph }),
|
|
})
|
|
if (!response.ok) {
|
|
setStatus(`另存为失败 (${response.status})`)
|
|
return
|
|
}
|
|
const meta = (await response.json()) as { id: string }
|
|
router.push(`/scene/${meta.id}`)
|
|
} catch (error) {
|
|
setStatus(error instanceof Error ? error.message : '另存为失败')
|
|
} finally {
|
|
setIsSaving(false)
|
|
}
|
|
}, [getGraph, name, router])
|
|
|
|
return (
|
|
<div className="flex items-center gap-2">
|
|
<button
|
|
className="rounded-md border border-border bg-accent px-3 py-1.5 font-medium text-xs hover:bg-accent/80 disabled:opacity-50"
|
|
disabled={isSaving}
|
|
onClick={handleSave}
|
|
type="button"
|
|
>
|
|
{isSaving ? '正在保存…' : '保存'}
|
|
</button>
|
|
<button
|
|
className="rounded-md border border-border bg-background px-3 py-1.5 font-medium text-xs hover:bg-accent/40 disabled:opacity-50"
|
|
disabled={isSaving}
|
|
onClick={handleSaveAs}
|
|
type="button"
|
|
>
|
|
另存为…
|
|
</button>
|
|
{status && <span className="text-muted-foreground text-xs">{status}</span>}
|
|
</div>
|
|
)
|
|
}
|