Skip to content

Commit

Permalink
JS to TS : simulator/src/data/undo.ts (CircuitVerse#421)
Browse files Browse the repository at this point in the history
* add

* remove

* fix null

* resolve 2

* reslove cognitive complexity

---------

Co-authored-by: Arnabdaz <[email protected]>
  • Loading branch information
ThatDeparted2061 and Arnabdaz authored Feb 10, 2025
1 parent c810760 commit 1ea73f2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 51 deletions.
51 changes: 0 additions & 51 deletions src/simulator/src/data/undo.js

This file was deleted.

82 changes: 82 additions & 0 deletions src/simulator/src/data/undo.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1ea73f2

Please sign in to comment.