|
|
|
|
@@ -30,9 +30,10 @@ 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 commandState = (commandId: string, activeWorkbench: WorkbenchId, selectedObjectId: string): CommandState => {
|
|
|
|
|
const known = Object.values(workbenchDefinitions).some((definition) => definition.groups.some((group) => group.commands.some((command) => command.id === commandId)))
|
|
|
|
|
const known = systemCommands.has(commandId) || Object.values(workbenchDefinitions).some((definition) => definition.groups.some((group) => group.commands.some((command) => command.id === commandId)))
|
|
|
|
|
if (!known) return { id: commandId, status: 'disabled', reason: 'Command is not registered in the active manifest.' }
|
|
|
|
|
if (commandId === 'pad' && activeWorkbench !== 'Part Design') return { id: commandId, status: 'disabled', reason: 'Switch to Part Design to use Pad.' }
|
|
|
|
|
if (selectionRequired.has(commandId) && !selectedObjectId) return { id: commandId, status: 'disabled', reason: 'Select a compatible object or sub-shape first.' }
|
|
|
|
|
@@ -42,11 +43,14 @@ const commandState = (commandId: string, activeWorkbench: WorkbenchId, selectedO
|
|
|
|
|
export function createMockFacade(): BitBybitWebCadFacade {
|
|
|
|
|
let state: FacadeState = { apiVersion: '0.1', activeWorkbench: 'Part Design', selectedObjectId: 'pad', document: createDocument(), task: null, lastNotice: '', diagnostics: [] }
|
|
|
|
|
const listeners = new Set<FacadeListener>()
|
|
|
|
|
const undoStack: FacadeState[] = []
|
|
|
|
|
const redoStack: FacadeState[] = []
|
|
|
|
|
let requestSequence = 0
|
|
|
|
|
|
|
|
|
|
const emit = (event: FacadeEvent) => listeners.forEach((listener) => listener(event))
|
|
|
|
|
const emitState = () => emit({ type: 'state.changed', state: getState() })
|
|
|
|
|
const getState = () => ({ ...state, diagnostics: state.diagnostics.map((diagnostic) => ({ ...diagnostic })), document: { ...state.document, tree: state.document.tree.map((item) => ({ ...item, children: item.children ? [...item.children] : undefined })) }, task: state.task ? { ...state.task, draft: { ...state.task.draft } } : null })
|
|
|
|
|
const commit = (next: FacadeState) => { undoStack.push(getState()); redoStack.length = 0; state = next; emitState() }
|
|
|
|
|
const notify = (message: string) => { state = { ...state, lastNotice: message }; emit({ type: 'notice', message }); emitState() }
|
|
|
|
|
const setActive = (id: WorkbenchId) => { state = { ...state, activeWorkbench: id }; emitState(); notify(`${id} workbench loaded`) }
|
|
|
|
|
const select = (objectId: string) => { state = { ...state, selectedObjectId: objectId }; emitState() }
|
|
|
|
|
@@ -61,8 +65,8 @@ export function createMockFacade(): BitBybitWebCadFacade {
|
|
|
|
|
emit({ type: 'diagnostic.added', diagnostic, context }); emit({ type: 'command.failed', commandId, context, message: status.reason }); notify(status.reason || 'Command is disabled'); return requestId
|
|
|
|
|
}
|
|
|
|
|
emit({ type: 'command.started', commandId, context })
|
|
|
|
|
if (commandId === 'new-document') state = { ...state, document: createDocument('Untitled document') }
|
|
|
|
|
else if (commandId === 'save') state = { ...state, document: { ...state.document, dirty: false, version: state.document.version + 1 } }
|
|
|
|
|
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')
|
|
|
|
|
@@ -70,7 +74,8 @@ export function createMockFacade(): BitBybitWebCadFacade {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const facade: BitBybitWebCadFacade = {
|
|
|
|
|
app: { document: { getActive: () => getState().document, create: (label) => { state = { ...state, document: createDocument(label), selectedObjectId: '' }; emitState(); return getState().document }, markDirty: () => { state = { ...state, document: { ...state.document, dirty: true } }; emitState() } } },
|
|
|
|
|
app: { document: { getActive: () => getState().document, create: (label) => { commit({ ...state, document: createDocument(label), selectedObjectId: '' }); return getState().document }, markDirty: () => { commit({ ...state, document: { ...state.document, dirty: true } }) } } },
|
|
|
|
|
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() } },
|
|
|
|
|
|