diff --git a/src/simulator/src/data/undo.js b/src/simulator/src/data/undo.js deleted file mode 100644 index 67f22005..00000000 --- a/src/simulator/src/data/undo.js +++ /dev/null @@ -1,51 +0,0 @@ -/* eslint-disable import/no-cycle */ -/** - * Function to restore copy from backup - * @param {Scope=} scope - The circuit on which undo is called - * @category data - */ -import { layoutModeGet } from '../layoutMode' -import Scope, { scopeList } from '../circuit' -import { loadScope } from './load' -import { updateRestrictedElementsInScope } from '../restrictedElementDiv' -import { forceResetNodesSet } from '../engine' -/** - * Function called to generate a prompt to save an image - * @param {Scope=} - the circuit in which we want to call undo - * @category data - * @exports undo - */ -export default function undo(scope = globalScope) { - if (layoutModeGet()) return - if (scope.backups.length < 2) return - const backupOx = globalScope.ox - const backupOy = globalScope.oy - const backupScale = globalScope.scale - globalScope.ox = 0 - globalScope.oy = 0 - const tempScope = new Scope(scope.name) - loading = true - const undoData = scope.backups.pop() - scope.history.push(undoData) - scope.backups.length !== 0 && - loadScope( - tempScope, - JSON.parse(scope.backups[scope.backups.length - 1]) - ) - tempScope.backups = scope.backups - tempScope.history = scope.history - tempScope.id = scope.id - tempScope.name = scope.name - tempScope.testbenchData = scope.testbenchData - scopeList[scope.id] = tempScope - globalScope = tempScope - globalScope.ox = backupOx - globalScope.oy = backupOy - globalScope.scale = backupScale - loading = false - forceResetNodesSet(true) - - // Updated restricted elements - updateRestrictedElementsInScope() -} -// for html file diff --git a/src/simulator/src/data/undo.ts b/src/simulator/src/data/undo.ts new file mode 100644 index 00000000..c2bf64b2 --- /dev/null +++ b/src/simulator/src/data/undo.ts @@ -0,0 +1,82 @@ +/* eslint-disable import/no-cycle */ +import { layoutModeGet } from '../layoutMode' +import Scope, { scopeList } from '../circuit' +import { loadScope } from './load' +import { updateRestrictedElementsInScope } from '../restrictedElementDiv' +import { forceResetNodesSet } from '../engine' + +// Declare global variables +declare let globalScope: Scope +declare let loading: boolean + +/** + * Function to restore copy from backup + * @param scope - The circuit on which undo is called + * @category data + */ +export default function undo(scope: Scope = globalScope): void { + if (layoutModeGet() || scope.backups.length < 2) return + + const { ox, oy, scale } = saveGlobalScopePosition() + resetGlobalScopePosition() + + loading = true + const undoData = popLastBackup(scope) + if (!undoData) return + + scope.history.push(undoData) + + const tempScope = createTempScope(scope) + if (!tempScope) return + + updateGlobalScope(tempScope, ox, oy, scale) + forceResetNodesSet(true) + updateRestrictedElementsInScope() +} + +function saveGlobalScopePosition() { + return { + ox: globalScope.ox, + oy: globalScope.oy, + scale: globalScope.scale, + } +} + +function resetGlobalScopePosition() { + globalScope.ox = 0 + globalScope.oy = 0 +} + +function popLastBackup(scope: Scope): string | undefined { + return scope.backups.pop() +} + +function createTempScope(scope: Scope): Scope | undefined { + const tempScope = new Scope(scope.name) + if (scope.backups.length === 0) return tempScope + + try { + loadScope(tempScope, JSON.parse(scope.backups[scope.backups.length - 1])) + } catch (error) { + console.error('Failed to parse backup data:', error) + loading = false + return undefined + } + + tempScope.backups = scope.backups + tempScope.history = scope.history + tempScope.id = scope.id + tempScope.name = scope.name + tempScope.testbenchData = scope.testbenchData + + return tempScope +} + +function updateGlobalScope(tempScope: Scope, ox: number, oy: number, scale: number) { + scopeList[tempScope.id] = tempScope + globalScope = tempScope + globalScope.ox = ox + globalScope.oy = oy + globalScope.scale = scale + loading = false +} \ No newline at end of file