P4-01: commit feature tasks to document history
This commit is contained in:
@@ -31,6 +31,14 @@ const createDocument = (label = 'Pump Housing'): DocumentSnapshot => ({
|
||||
|
||||
const selectionRequired = new Set(['pad', 'pocket', 'revolution', 'fillet', 'chamfer', 'hole', 'linear-pattern', 'polar-pattern', 'measure-distance', 'measure-angle', 'measure-area'])
|
||||
const systemCommands = new Set(['new-document', 'save', 'select-object'])
|
||||
const featureCommands: Record<string, { label: string; detail: string }> = {
|
||||
'create-body': { label: 'Body', detail: 'Part Design body' },
|
||||
'create-sketch': { label: 'Sketch', detail: 'Fully constrained' },
|
||||
pad: { label: 'Pad', detail: 'Length 42 mm' },
|
||||
pocket: { label: 'Pocket', detail: 'Through all' },
|
||||
fillet: { label: 'Fillet', detail: 'Radius 3 mm' },
|
||||
chamfer: { label: 'Chamfer', detail: 'Length 2 mm' },
|
||||
}
|
||||
|
||||
const commandState = (commandId: string, activeWorkbench: WorkbenchId, selectedObjectId: string): CommandState => {
|
||||
const known = systemCommands.has(commandId) || Object.values(workbenchDefinitions).some((definition) => definition.groups.some((group) => group.commands.some((command) => command.id === commandId)))
|
||||
@@ -55,6 +63,38 @@ export function createMockFacade(): BitBybitWebCadFacade {
|
||||
const setActive = (id: WorkbenchId) => { state = { ...state, activeWorkbench: id }; emitState(); notify(`${id} workbench loaded`) }
|
||||
const select = (objectId: string) => { state = { ...state, selectedObjectId: objectId }; emitState() }
|
||||
const beginTask = (commandId: string, draft: Record<string, unknown> = {}) => { const task: TaskSnapshot = { id: `task-${++requestSequence}`, commandId, title: workbenchDefinitions[state.activeWorkbench].taskTitle, status: 'preview', draft }; state = { ...state, task }; emitState(); return task }
|
||||
const nextFeatureId = (label: string) => {
|
||||
const base = label.toLowerCase()
|
||||
const existing = state.document.tree.filter((item) => item.label.toLowerCase().startsWith(base)).length
|
||||
return existing === 0 ? base : `${base}${String(existing).padStart(3, '0')}`
|
||||
}
|
||||
const appendFeature = (document: DocumentSnapshot, commandId: string): { document: DocumentSnapshot; objectId: string } => {
|
||||
const definition = featureCommands[commandId]
|
||||
if (!definition) return { document, objectId: '' }
|
||||
const objectId = nextFeatureId(definition.label)
|
||||
const type = commandId === 'create-sketch' ? 'sketch' : commandId === 'create-body' ? 'body' : 'feature'
|
||||
const item: ModelTreeItem = { id: objectId, label: definition.label, type, state: type === 'body' ? 'active' : 'valid', detail: definition.detail }
|
||||
const tree: ModelTreeItem[] = document.tree.map((entry) => ({ ...entry, children: entry.children ? [...entry.children] : undefined }))
|
||||
if (type === 'body') tree.push({ ...item, children: [] })
|
||||
else {
|
||||
const body = tree.find((entry) => entry.type === 'body')
|
||||
if (body) body.children = [...(body.children || []), objectId]
|
||||
tree.push(item)
|
||||
}
|
||||
return { document: { ...document, version: document.version + 1, dirty: true, tree }, objectId }
|
||||
}
|
||||
const applyTask = () => {
|
||||
const task = state.task
|
||||
if (!task || task.status !== 'preview') return
|
||||
const result = appendFeature(state.document, task.commandId)
|
||||
if (!result.objectId) {
|
||||
state = { ...state, task: { ...task, status: 'completed' } }
|
||||
emitState()
|
||||
return
|
||||
}
|
||||
commit({ ...state, document: result.document, selectedObjectId: result.objectId, task: { ...task, status: 'completed' } })
|
||||
notify(`${featureCommands[task.commandId].label} created`)
|
||||
}
|
||||
const execute = ({ commandId, payload }: ExecuteCommandInput) => {
|
||||
const requestId = `req-${++requestSequence}`
|
||||
const context: FacadeRequestContext = { apiVersion: state.apiVersion, requestId, documentId: state.document.id, documentVersion: state.document.version, workbench: state.activeWorkbench }
|
||||
@@ -68,8 +108,7 @@ export function createMockFacade(): BitBybitWebCadFacade {
|
||||
if (commandId === 'new-document') commit({ ...state, document: createDocument('Untitled document'), selectedObjectId: '' })
|
||||
else if (commandId === 'save') commit({ ...state, document: { ...state.document, dirty: false, version: state.document.version + 1 } })
|
||||
else if (commandId === 'select-object' && typeof payload?.objectId === 'string') select(payload.objectId)
|
||||
else if (commandId === 'create-body') beginTask('create-body')
|
||||
else if (commandId === 'create-sketch') beginTask('create-sketch')
|
||||
else if (featureCommands[commandId]) beginTask(commandId, { source: state.selectedObjectId || null })
|
||||
emit({ type: 'command.completed', commandId, context }); emitState(); return requestId
|
||||
}
|
||||
|
||||
@@ -78,7 +117,7 @@ export function createMockFacade(): BitBybitWebCadFacade {
|
||||
history: { canUndo: () => undoStack.length > 0, canRedo: () => redoStack.length > 0, undo: () => { const previous = undoStack.pop(); if (!previous) return; redoStack.push(getState()); state = previous; emitState(); notify('Undo applied') }, redo: () => { const next = redoStack.pop(); if (!next) return; undoStack.push(getState()); state = next; emitState(); notify('Redo applied') } },
|
||||
gui: { workbench: { list: () => Object.keys(workbenchDefinitions) as WorkbenchId[], getActive: () => state.activeWorkbench, setActive }, command: { getState: (commandId) => commandState(commandId, state.activeWorkbench, state.selectedObjectId), list: (workbench) => workbenchDefinitions[workbench].groups.flatMap((group) => group.commands), execute } },
|
||||
selection: { getObjectId: () => state.selectedObjectId, select, clear: () => select('') },
|
||||
task: { getActive: () => getState().task, begin: beginTask, update: (draft) => { if (state.task) state = { ...state, task: { ...state.task, draft: { ...state.task.draft, ...draft } } }; emitState() }, apply: () => { if (state.task) state = { ...state, task: { ...state.task, status: 'completed' } }; emitState() }, cancel: () => { if (state.task) state = { ...state, task: { ...state.task, status: 'cancelled' } }; emitState() } },
|
||||
task: { getActive: () => getState().task, begin: beginTask, update: (draft) => { if (state.task) state = { ...state, task: { ...state.task, draft: { ...state.task.draft, ...draft } } }; emitState() }, apply: applyTask, cancel: () => { if (state.task) state = { ...state, task: { ...state.task, status: 'cancelled' } }; emitState() } },
|
||||
viewport: { createAdapter: () => new ThreeViewportAdapter() },
|
||||
getState, subscribe: (listener) => { listeners.add(listener); return () => { listeners.delete(listener) } }, notify,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user