@@ -1,12 +1,11 @@
|
||||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "main",
|
||||
"description": "AI 游戏创作主窗口允许读取系统剪贴板内容以粘贴素材,并写入 UI 设计 JSON。",
|
||||
"description": "AI 游戏创作主窗口允许读取系统剪贴板图片,用于粘贴素材附件。",
|
||||
"windows": ["client"],
|
||||
"permissions": [
|
||||
"clipboard-manager:allow-read-image",
|
||||
"clipboard-manager:allow-read-text",
|
||||
"clipboard-manager:allow-write-text",
|
||||
"core:image:allow-rgba",
|
||||
"core:image:allow-size",
|
||||
"core:resources:allow-close",
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { writeText } from '@tauri-apps/plugin-clipboard-manager';
|
||||
import { ChevronLeft } from 'lucide-react';
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { useMemo, useState } from 'react';
|
||||
|
||||
import { ThemedModal } from '../../components/modal/ThemedModal';
|
||||
import {
|
||||
@@ -49,29 +48,10 @@ export default function UiEditorPage({
|
||||
const session = useUiEditorSession(projectPath, resourceId, resolvedStore);
|
||||
const [saveWarningOpen, setSaveWarningOpen] = useState(false);
|
||||
const [saveAfterReturn, setSaveAfterReturn] = useState(false);
|
||||
const [saveAndCopyJson, setSaveAndCopyJson] = useState(false);
|
||||
const [copyFeedback, setCopyFeedback] = useState<'copied' | 'failed' | null>(
|
||||
null,
|
||||
);
|
||||
const [returnConfirmOpen, setReturnConfirmOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (copyFeedback === null) return;
|
||||
const timeoutId = window.setTimeout(() => setCopyFeedback(null), 2000);
|
||||
return () => window.clearTimeout(timeoutId);
|
||||
}, [copyFeedback]);
|
||||
|
||||
async function save(afterReturn = false, copyJson = false) {
|
||||
const savedSnapshot = await session.save.save();
|
||||
if (savedSnapshot) {
|
||||
if (copyJson) {
|
||||
try {
|
||||
await writeText(JSON.stringify(savedSnapshot, null, 2));
|
||||
setCopyFeedback('copied');
|
||||
} catch {
|
||||
setCopyFeedback('failed');
|
||||
}
|
||||
}
|
||||
async function save(afterReturn = false) {
|
||||
if (await session.save.save()) {
|
||||
if (afterReturn) {
|
||||
setReturnConfirmOpen(false);
|
||||
onBack?.();
|
||||
@@ -79,17 +59,16 @@ export default function UiEditorPage({
|
||||
}
|
||||
}
|
||||
|
||||
function requestSave(afterReturn = false, copyJson = false) {
|
||||
function requestSave(afterReturn = false) {
|
||||
if (!resourceId || session.save.isSaving || session.workflow.isAiRunning) {
|
||||
return;
|
||||
}
|
||||
setSaveAfterReturn(afterReturn);
|
||||
setSaveAndCopyJson(copyJson);
|
||||
if (session.save.hasWarnings()) {
|
||||
setSaveWarningOpen(true);
|
||||
return;
|
||||
}
|
||||
void save(afterReturn, copyJson);
|
||||
void save(afterReturn);
|
||||
}
|
||||
|
||||
function requestBack() {
|
||||
@@ -113,40 +92,20 @@ export default function UiEditorPage({
|
||||
返回资源
|
||||
</button>
|
||||
<strong className="text-sm">{resourceLabel ?? 'UI 设计'}</strong>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-lg bg-orange-600 px-3 py-1.5 text-sm font-semibold text-white disabled:opacity-60"
|
||||
disabled={
|
||||
session.save.isSaving ||
|
||||
session.save.isLoading ||
|
||||
Boolean(session.save.loadError) ||
|
||||
session.save.persistedRevision === null ||
|
||||
session.save.isLocked
|
||||
}
|
||||
onClick={() => requestSave()}
|
||||
>
|
||||
{session.save.isSaving ? '保存中…' : '保存'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-lg border border-orange-600 px-3 py-1.5 text-sm font-semibold text-orange-700 disabled:opacity-60"
|
||||
disabled={
|
||||
session.save.isSaving ||
|
||||
session.save.isLoading ||
|
||||
Boolean(session.save.loadError) ||
|
||||
session.save.persistedRevision === null ||
|
||||
session.save.isLocked
|
||||
}
|
||||
onClick={() => requestSave(false, true)}
|
||||
>
|
||||
{session.save.isSaving
|
||||
? '保存中…'
|
||||
: copyFeedback === 'copied'
|
||||
? '已保存并复制'
|
||||
: '保存并复制 JSON 结构'}
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-lg bg-orange-600 px-3 py-1.5 text-sm font-semibold text-white disabled:opacity-60"
|
||||
disabled={
|
||||
session.save.isSaving ||
|
||||
session.save.isLoading ||
|
||||
Boolean(session.save.loadError) ||
|
||||
session.save.persistedRevision === null ||
|
||||
session.save.isLocked
|
||||
}
|
||||
onClick={() => requestSave()}
|
||||
>
|
||||
{session.save.isSaving ? '保存中…' : '保存'}
|
||||
</button>
|
||||
</header>
|
||||
) : null}
|
||||
{session.save.saveError ? (
|
||||
@@ -157,14 +116,6 @@ export default function UiEditorPage({
|
||||
<span>{session.save.saveError}</span>
|
||||
</div>
|
||||
) : null}
|
||||
{copyFeedback === 'failed' ? (
|
||||
<div
|
||||
className="mx-4 mt-3 flex shrink-0 items-center justify-between gap-3 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-xs text-amber-900"
|
||||
role="status"
|
||||
>
|
||||
<span>保存成功,但复制失败。</span>
|
||||
</div>
|
||||
) : null}
|
||||
<ToolNavigation
|
||||
activeStep={session.workflow.activeStep}
|
||||
furthestStepIndex={session.workflow.furthestStepIndex}
|
||||
@@ -305,7 +256,7 @@ export default function UiEditorPage({
|
||||
className="rounded-lg bg-orange-600 px-3 py-2 text-xs font-semibold text-white"
|
||||
onClick={() => {
|
||||
setSaveWarningOpen(false);
|
||||
void save(saveAfterReturn, saveAndCopyJson);
|
||||
void save(saveAfterReturn);
|
||||
}}
|
||||
>
|
||||
仍然保存
|
||||
|
||||
@@ -1000,7 +1000,7 @@ export function useUiEditorSession(
|
||||
persistedRevision === null ||
|
||||
editor.isLocked
|
||||
) {
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
setSaveError(null);
|
||||
setIsSaving(true);
|
||||
@@ -1014,16 +1014,16 @@ export function useUiEditorSession(
|
||||
);
|
||||
if (result.status === 'conflict') {
|
||||
setSaveError('资源已在别处更新;请重新加载后再保存。');
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
setPersistedRevision(result.revision);
|
||||
if (JSON.stringify(editor.state) === snapshotSignature) {
|
||||
setSavedStateSignature(snapshotSignature);
|
||||
}
|
||||
return snapshot;
|
||||
return true;
|
||||
} catch (error) {
|
||||
setSaveError(error instanceof Error ? error.message : String(error));
|
||||
return null;
|
||||
return false;
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
|
||||
@@ -5,5 +5,3 @@ export async function readImage(): Promise<never> {
|
||||
export async function readText(): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
export async function writeText(_text: string): Promise<void> {}
|
||||
|
||||
@@ -13,10 +13,6 @@ import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn() }));
|
||||
|
||||
vi.mock('@tauri-apps/plugin-clipboard-manager', () => ({
|
||||
writeText: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../src/components/AssetImporter', () => ({
|
||||
AssetImporter: ({ open }: { open: boolean }) =>
|
||||
open ? createElement('div', { role: 'dialog' }, '素材导入器') : null,
|
||||
@@ -28,7 +24,6 @@ vi.mock('../src/components/modal/ThemedModal', () => ({
|
||||
}));
|
||||
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { writeText } from '@tauri-apps/plugin-clipboard-manager';
|
||||
|
||||
import type { Node as UiNode } from '../src/features/ui-editor/types/Node';
|
||||
import type { State } from '../src/features/ui-editor/types/State';
|
||||
@@ -211,95 +206,6 @@ describe('UiEditorPage', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('saves the warning-confirmed snapshot before copying its formatted JSON', async () => {
|
||||
vi.mocked(writeText).mockClear();
|
||||
const state = structuredClone(EMPTY_SNAPSHOT.state);
|
||||
const stateStore: UiDesignStateStore = {
|
||||
load: vi.fn().mockResolvedValue({ revision: 0, state }),
|
||||
save: vi.fn().mockResolvedValue({
|
||||
status: 'saved',
|
||||
state,
|
||||
revision: 1,
|
||||
committedProjectRevision: 1,
|
||||
}),
|
||||
};
|
||||
vi.mocked(writeText).mockResolvedValue();
|
||||
render(
|
||||
createElement(UiEditorPage, {
|
||||
projectPath: '/tmp/ui-editor',
|
||||
resourceId: 'ui-resource',
|
||||
stateStore,
|
||||
}),
|
||||
);
|
||||
|
||||
fireEvent.click(
|
||||
await screen.findByRole('button', { name: '保存并复制 JSON 结构' }),
|
||||
);
|
||||
fireEvent.click(await screen.findByRole('button', { name: '仍然保存' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(stateStore.save).toHaveBeenCalledWith('ui-resource', 0, state);
|
||||
expect(writeText).toHaveBeenCalledWith(JSON.stringify(state, null, 2));
|
||||
});
|
||||
expect(screen.getByRole('button', { name: '已保存并复制' })).toBeTruthy();
|
||||
});
|
||||
|
||||
it('does not copy JSON when saving fails', async () => {
|
||||
vi.mocked(writeText).mockClear();
|
||||
const stateStore: UiDesignStateStore = {
|
||||
load: vi.fn().mockResolvedValue(structuredClone(EMPTY_SNAPSHOT)),
|
||||
save: vi.fn().mockRejectedValue(new Error('临时存储不可用')),
|
||||
};
|
||||
render(
|
||||
createElement(UiEditorPage, {
|
||||
projectPath: '/tmp/ui-editor',
|
||||
resourceId: 'ui-resource',
|
||||
stateStore,
|
||||
}),
|
||||
);
|
||||
|
||||
fireEvent.click(
|
||||
await screen.findByRole('button', { name: '保存并复制 JSON 结构' }),
|
||||
);
|
||||
fireEvent.click(await screen.findByRole('button', { name: '仍然保存' }));
|
||||
|
||||
await screen.findByRole('alert');
|
||||
expect(writeText).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps a successful save when JSON copying fails', async () => {
|
||||
vi.mocked(writeText).mockClear();
|
||||
const state = structuredClone(EMPTY_SNAPSHOT.state);
|
||||
const stateStore: UiDesignStateStore = {
|
||||
load: vi.fn().mockResolvedValue({ revision: 0, state }),
|
||||
save: vi.fn().mockResolvedValue({
|
||||
status: 'saved',
|
||||
state,
|
||||
revision: 1,
|
||||
committedProjectRevision: 1,
|
||||
}),
|
||||
};
|
||||
vi.mocked(writeText).mockRejectedValue(new Error('剪贴板不可用'));
|
||||
render(
|
||||
createElement(UiEditorPage, {
|
||||
projectPath: '/tmp/ui-editor',
|
||||
resourceId: 'ui-resource',
|
||||
stateStore,
|
||||
}),
|
||||
);
|
||||
|
||||
fireEvent.click(
|
||||
await screen.findByRole('button', { name: '保存并复制 JSON 结构' }),
|
||||
);
|
||||
fireEvent.click(await screen.findByRole('button', { name: '仍然保存' }));
|
||||
|
||||
await waitFor(() => expect(stateStore.save).toHaveBeenCalledTimes(1));
|
||||
expect((await screen.findByRole('status')).textContent).toContain(
|
||||
'保存成功,但复制失败。',
|
||||
);
|
||||
expect(screen.queryByRole('alert')).toBeNull();
|
||||
});
|
||||
|
||||
it('renders split input tools over a real empty State', () => {
|
||||
render(createElement(UiEditorPage, { projectPath: '/tmp/ui-editor' }));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user