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,15 @@
|
||||
# Implementation
|
||||
|
||||
依次实现入口/上下文、session 四态、互斥守卫、返回恢复;用状态机单测和最小浏览器流程验证后再接几何编辑。
|
||||
|
||||
## Progress
|
||||
|
||||
- [x] 独立 `/junction/:type/:id` 路由与浏览器返回监听
|
||||
- [x] 路口上下文摘要和只读/可编辑状态
|
||||
- [x] draft / applied / saved / cancelled 状态机
|
||||
- [x] 未处理草稿离开守卫与版本冲突检查
|
||||
- [x] 客户端类型检查、单元测试、格式检查和生产构建
|
||||
|
||||
## Deferred
|
||||
|
||||
- 几何约束控件和真实 preview/apply/save 数据流由 geometry-editor 子任务接入。
|
||||
@@ -54,6 +54,15 @@ import { Button } from './ui/button';
|
||||
import type { GeoFeature, GeoJson, Road, WorkbenchState } from './types/state';
|
||||
import type { LayerName } from './map/layers';
|
||||
import type { JunctionRef } from './junction/hotspots';
|
||||
import { junctionDescriptors, hotspotAt } from './junction/hotspots';
|
||||
import { JunctionWorkspace } from './junction/JunctionWorkspace';
|
||||
import {
|
||||
applyJunctionDraft,
|
||||
cancelJunctionSession,
|
||||
createJunctionSession,
|
||||
saveJunctionSession,
|
||||
type JunctionSession,
|
||||
} from './junction/workspace';
|
||||
|
||||
const layerLabels: Array<[LayerName, string]> = [
|
||||
['osm', '方向道路参考线'],
|
||||
@@ -84,6 +93,12 @@ function App() {
|
||||
const [status, setStatus] = useState('正在加载工作台...');
|
||||
const [filter, setFilter] = useState<'all' | 'candidates' | 'other'>('all');
|
||||
const [marking, setMarking] = useState<Record<string, unknown> | null>(null);
|
||||
const [junctionRoute, setJunctionRoute] = useState<JunctionRef | null>(() => {
|
||||
if (typeof window === 'undefined') return null;
|
||||
const match = window.location.pathname.match(/^\/junction\/(node|cluster)\/([^/]+)$/);
|
||||
return match ? { type: match[1] as JunctionRef['type'], id: decodeURIComponent(match[2]) } : null;
|
||||
});
|
||||
const [junctionSession, setJunctionSession] = useState<JunctionSession | null>(null);
|
||||
const editHistoryRef = useRef<EditHistory>({ undo: [], redo: [] });
|
||||
const recordSignalEdit = useMemo(
|
||||
() => () => {
|
||||
@@ -122,6 +137,14 @@ function App() {
|
||||
// Storage may be unavailable in private browsing; the switch still works for this page.
|
||||
}
|
||||
}, [editMode]);
|
||||
useEffect(() => {
|
||||
const onPopState = () => {
|
||||
const match = window.location.pathname.match(/^\/junction\/(node|cluster)\/([^/]+)$/);
|
||||
setJunctionRoute(match ? { type: match[1] as JunctionRef['type'], id: decodeURIComponent(match[2]) } : null);
|
||||
};
|
||||
window.addEventListener('popstate', onPopState);
|
||||
return () => window.removeEventListener('popstate', onPopState);
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
api
|
||||
.state()
|
||||
@@ -151,6 +174,17 @@ function App() {
|
||||
revision: editRevision,
|
||||
touch,
|
||||
} = directEdit;
|
||||
const junctionDescriptor = useMemo(
|
||||
() => (state && junctionRoute ? hotspotAt(junctionDescriptors(state), junctionRoute) : null),
|
||||
[junctionRoute, state],
|
||||
);
|
||||
useEffect(() => {
|
||||
if (!junctionRoute) {
|
||||
setJunctionSession(null);
|
||||
return;
|
||||
}
|
||||
setJunctionSession(createJunctionSession(junctionRoute, editRevision));
|
||||
}, [editRevision, junctionRoute]);
|
||||
const editHandles = useMemo(() => {
|
||||
if (!editMode || !state) return [];
|
||||
// Handle anchors carry a directional road id while reserves are keyed by
|
||||
@@ -665,6 +699,19 @@ function App() {
|
||||
(filter === 'all' ||
|
||||
(filter === 'candidates' ? Boolean(item.manualCandidates?.length) : !item.manualCandidates?.length)),
|
||||
);
|
||||
const openJunction = (ref: JunctionRef) => {
|
||||
window.history.pushState({}, '', `/junction/${ref.type}/${encodeURIComponent(ref.id)}`);
|
||||
setJunctionRoute(ref);
|
||||
};
|
||||
const closeJunction = () => {
|
||||
if (junctionSession?.dirty) {
|
||||
const choice = window.confirm('路口有未应用草稿。确定放弃并返回主地图吗?');
|
||||
if (!choice) return;
|
||||
setJunctionSession(cancelJunctionSession(junctionSession));
|
||||
}
|
||||
window.history.pushState({}, '', '/');
|
||||
setJunctionRoute(null);
|
||||
};
|
||||
const handleFeature = (properties: Record<string, unknown>) => {
|
||||
setMarking(properties);
|
||||
// A crosswalk is six features but one editable thing; a stop line is one.
|
||||
@@ -807,6 +854,7 @@ function App() {
|
||||
scene={scene}
|
||||
junctionHotspots={junctionHotspots}
|
||||
onJunctionHotspot={(ref: JunctionRef) => {
|
||||
openJunction(ref);
|
||||
setStatus(ref.type === 'node' ? `已定位路口节点 ${ref.id}(可编辑)` : `已定位路口 ${ref.id}(只读)`);
|
||||
}}
|
||||
onSelectRoad={setSelected}
|
||||
@@ -918,6 +966,23 @@ function App() {
|
||||
</WorkbenchInspector>
|
||||
</aside>
|
||||
</main>
|
||||
{junctionRoute && junctionDescriptor && junctionSession ? (
|
||||
<JunctionWorkspace
|
||||
descriptor={junctionDescriptor}
|
||||
session={junctionSession}
|
||||
onClose={closeJunction}
|
||||
onApply={() => setJunctionSession((current) => (current ? applyJunctionDraft(current) : current))}
|
||||
onDiscard={() => setJunctionSession((current) => (current ? cancelJunctionSession(current) : current))}
|
||||
onSave={() => {
|
||||
try {
|
||||
setJunctionSession((current) => (current ? saveJunctionSession(current, editRevision) : current));
|
||||
setStatus('路口草稿已保存');
|
||||
} catch (error) {
|
||||
setStatus((error as Error).message);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
@@ -531,3 +531,51 @@ pre {
|
||||
.form-hint {
|
||||
color: #7b6542;
|
||||
}
|
||||
/* JunctionTools local workspace overlay. */
|
||||
.junction-workspace {
|
||||
position: fixed;
|
||||
inset: 3.5rem 1rem 1rem;
|
||||
z-index: 20;
|
||||
overflow: auto;
|
||||
background: var(--panel, #fff);
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 14px 40px rgb(15 23 42 / 20%);
|
||||
padding: 1rem 1.25rem;
|
||||
}
|
||||
.junction-workspace__header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
gap: 1rem;
|
||||
}
|
||||
.junction-workspace__header h2 {
|
||||
margin: 0.2rem 0;
|
||||
}
|
||||
.junction-workspace__header p {
|
||||
margin: 0;
|
||||
color: #64748b;
|
||||
}
|
||||
.junction-workspace__eyebrow {
|
||||
font-size: 0.75rem;
|
||||
color: #64748b;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
}
|
||||
.junction-workspace__body {
|
||||
max-width: 42rem;
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
.junction-workspace__body dl {
|
||||
display: grid;
|
||||
grid-template-columns: 7rem 1fr;
|
||||
gap: 0.5rem 1rem;
|
||||
}
|
||||
.junction-workspace__body dt {
|
||||
color: #64748b;
|
||||
}
|
||||
.junction-workspace__actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
Reference in new issue
Block a user