Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS to TS : simulator/src/data/undo.ts #421

Merged
merged 7 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading