forked from CircuitVerse/cv-frontend-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JS to TS : simulator/src/data/undo.ts (CircuitVerse#421)
* add * remove * fix null * resolve 2 * reslove cognitive complexity --------- Co-authored-by: Arnabdaz <[email protected]>
- Loading branch information
1 parent
c810760
commit 1ea73f2
Showing
2 changed files
with
82 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |