52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
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';
|
|
}
|