feat(workbench): add junction tools workspace shell
This commit is contained in:
1 parent
96833d3a63
commit
2504e8342f
6 files changed
+277
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import { useMemo } from 'react';
|
||||
import type { JunctionDescriptor } from './hotspots';
|
||||
import { canEditJunction, type JunctionSession } from './workspace';
|
||||
import { Button } from '../ui/button';
|
||||
|
||||
interface Props {
|
||||
descriptor: JunctionDescriptor;
|
||||
session: JunctionSession;
|
||||
onClose: () => void;
|
||||
onApply: () => void;
|
||||
onDiscard: () => void;
|
||||
onSave: () => void;
|
||||
}
|
||||
|
||||
export function JunctionWorkspace({ descriptor, session, onClose, onApply, onDiscard, onSave }: Props) {
|
||||
const editable = useMemo(() => canEditJunction(descriptor), [descriptor]);
|
||||
return (
|
||||
<section className="junction-workspace" aria-label="路口编辑工作区">
|
||||
<header className="junction-workspace__header">
|
||||
<div>
|
||||
<span className="junction-workspace__eyebrow">JunctionTools</span>
|
||||
<h2>
|
||||
{descriptor.ref.type === 'node' ? '路口节点' : '复杂路口上下文'} {descriptor.ref.id}
|
||||
</h2>
|
||||
<p>{editable ? '可编辑路口几何参数' : '只读上下文:当前路口类型暂不支持编辑'}</p>
|
||||
</div>
|
||||
<Button onClick={onClose}>返回主地图</Button>
|
||||
</header>
|
||||
<div className="junction-workspace__body">
|
||||
<dl>
|
||||
<dt>组成节点</dt>
|
||||
<dd>{descriptor.nodeIds.join(', ') || '—'}</dd>
|
||||
<dt>进口道路</dt>
|
||||
<dd>{descriptor.approachRoadIds.join(', ') || '—'}</dd>
|
||||
<dt>诊断</dt>
|
||||
<dd>{descriptor.diagnosticIds.length ? `${descriptor.diagnosticIds.length} 条` : '无'}</dd>
|
||||
<dt>状态</dt>
|
||||
<dd>
|
||||
{session.status}
|
||||
{session.dirty ? '(有未应用草稿)' : ''}
|
||||
</dd>
|
||||
</dl>
|
||||
{editable ? (
|
||||
<div className="junction-workspace__actions">
|
||||
<Button onClick={onApply} disabled={!session.dirty}>
|
||||
应用到工作区
|
||||
</Button>
|
||||
<Button onClick={onSave} disabled={session.dirty}>
|
||||
保存
|
||||
</Button>
|
||||
<Button onClick={onDiscard} disabled={!session.dirty}>
|
||||
放弃草稿
|
||||
</Button>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
applyJunctionDraft,
|
||||
canEditJunction,
|
||||
createJunctionSession,
|
||||
leaveJunctionSession,
|
||||
markJunctionDraft,
|
||||
saveJunctionSession,
|
||||
} from './workspace';
|
||||
|
||||
const node = { ref: { type: 'node' as const, id: 'n1' }, status: 'editable' as const };
|
||||
const cluster = { ref: { type: 'cluster' as const, id: 'c1' }, status: 'readonly' as const };
|
||||
|
||||
describe('junction workspace session', () => {
|
||||
it('keeps draft until apply, then emits one applied state', () => {
|
||||
const initial = createJunctionSession(node.ref, 3);
|
||||
expect(initial.status).toBe('draft');
|
||||
const applied = applyJunctionDraft(markJunctionDraft(initial));
|
||||
expect(applied).toMatchObject({ status: 'applied', dirty: false, appliedRevision: 3 });
|
||||
});
|
||||
|
||||
it('rejects saving against a changed document revision', () => {
|
||||
const session = applyJunctionDraft(markJunctionDraft(createJunctionSession(node.ref, 3)));
|
||||
expect(() => saveJunctionSession(session, 4)).toThrow(/发生变化/);
|
||||
expect(saveJunctionSession(session, 3).status).toBe('saved');
|
||||
});
|
||||
|
||||
it('requires an explicit leave action for dirty sessions', () => {
|
||||
const session = markJunctionDraft(createJunctionSession(node.ref));
|
||||
expect(leaveJunctionSession(session, 'stay')).toBe(session);
|
||||
expect(leaveJunctionSession(session, 'discard')?.status).toBe('cancelled');
|
||||
expect(leaveJunctionSession(session, 'apply')?.status).toBe('applied');
|
||||
});
|
||||
|
||||
it('only ordinary editable nodes can be changed', () => {
|
||||
expect(canEditJunction(node as never)).toBe(true);
|
||||
expect(canEditJunction(cluster as never)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { JunctionDescriptor, JunctionRef } from './hotspots';
|
||||
|
||||
export type JunctionSessionStatus = 'draft' | 'applied' | 'saved' | 'cancelled';
|
||||
export type LeaveAction = 'apply' | 'discard' | 'stay';
|
||||
|
||||
export interface JunctionSession {
|
||||
ref: JunctionRef;
|
||||
status: JunctionSessionStatus;
|
||||
dirty: boolean;
|
||||
openedRevision?: number;
|
||||
appliedRevision?: number;
|
||||
}
|
||||
|
||||
export function createJunctionSession(ref: JunctionRef, revision?: number): JunctionSession {
|
||||
return { ref, status: 'draft', dirty: false, openedRevision: revision };
|
||||
}
|
||||
|
||||
export function markJunctionDraft(session: JunctionSession): JunctionSession {
|
||||
return { ...session, status: 'draft', dirty: true };
|
||||
}
|
||||
|
||||
export function applyJunctionDraft(session: JunctionSession): JunctionSession {
|
||||
return session.dirty
|
||||
? { ...session, status: 'applied', dirty: false, appliedRevision: session.openedRevision }
|
||||
: session;
|
||||
}
|
||||
|
||||
export function saveJunctionSession(session: JunctionSession, currentRevision?: number): JunctionSession {
|
||||
if (
|
||||
session.openedRevision !== undefined &&
|
||||
currentRevision !== undefined &&
|
||||
currentRevision !== session.openedRevision
|
||||
) {
|
||||
throw new Error('路口文档已发生变化,请重新加载后再保存');
|
||||
}
|
||||
return { ...session, status: 'saved', dirty: false, openedRevision: currentRevision ?? session.openedRevision };
|
||||
}
|
||||
|
||||
export function cancelJunctionSession(session: JunctionSession): JunctionSession {
|
||||
return { ...session, status: 'cancelled', dirty: false };
|
||||
}
|
||||
|
||||
export function leaveJunctionSession(session: JunctionSession, action: LeaveAction): JunctionSession | null {
|
||||
if (action === 'stay') return session;
|
||||
if (action === 'discard') return cancelJunctionSession(session);
|
||||
return applyJunctionDraft(session);
|
||||
}
|
||||
|
||||
export function canEditJunction(descriptor: JunctionDescriptor): boolean {
|
||||
return descriptor.ref.type === 'node' && descriptor.status === 'editable';
|
||||
}
|
||||
Reference in new issue
Block a user