Skip to content

Commit ac9a470

Browse files
Js to TS : simulator/src/data/redo.ts (#418)
* redo.ts * add
1 parent a7a6c5c commit ac9a470

File tree

2 files changed

+76
-47
lines changed

2 files changed

+76
-47
lines changed

Diff for: src/simulator/src/data/redo.js

-47
This file was deleted.

Diff for: src/simulator/src/data/redo.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-disable import/no-cycle */
2+
import { layoutModeGet } from '../layoutMode';
3+
import Scope, { scopeList } from '../circuit';
4+
import { loadScope } from './load';
5+
import { updateRestrictedElementsInScope } from '../restrictedElementDiv';
6+
import { forceResetNodesSet } from '../engine';
7+
8+
// Extend the Scope type to include missing properties
9+
interface ExtendedScope extends Scope {
10+
testbenchData: any;
11+
ox: number;
12+
oy: number;
13+
scale: number;
14+
history: string[];
15+
backups: string[];
16+
name: string;
17+
id: string | number;
18+
}
19+
20+
// Type declarations for global variables
21+
declare var globalScope: ExtendedScope;
22+
declare var loading: boolean;
23+
24+
/**
25+
* Function to restore copy from backup
26+
* @param {ExtendedScope=} scope - The circuit on which redo is called
27+
* @category data
28+
*/
29+
export default function redo(scope: ExtendedScope = globalScope): void {
30+
if (layoutModeGet()) return;
31+
if (scope.history.length === 0) return;
32+
33+
// Store the current view state
34+
const backupOx: number = globalScope.ox;
35+
const backupOy: number = globalScope.oy;
36+
const backupScale: number = globalScope.scale;
37+
38+
// Reset the view position
39+
globalScope.ox = 0;
40+
globalScope.oy = 0;
41+
42+
// Create a temporary scope
43+
const tempScope: ExtendedScope = new Scope(scope.name) as ExtendedScope;
44+
loading = true;
45+
46+
// Get the redo data and update history
47+
const redoData: string = scope.history.pop()!;
48+
scope.backups.push(redoData);
49+
50+
// Load the scope data
51+
loadScope(tempScope, JSON.parse(redoData));
52+
53+
// Transfer persistent properties
54+
tempScope.backups = scope.backups;
55+
tempScope.history = scope.history;
56+
tempScope.id = scope.id;
57+
tempScope.name = scope.name;
58+
tempScope.testbenchData = scope.testbenchData;
59+
60+
// Update the scope list
61+
scopeList[scope.id] = tempScope;
62+
63+
// Update global scope
64+
globalScope = tempScope;
65+
66+
// Restore view state
67+
globalScope.ox = backupOx;
68+
globalScope.oy = backupOy;
69+
globalScope.scale = backupScale;
70+
71+
loading = false;
72+
forceResetNodesSet(true);
73+
74+
// Update restricted elements
75+
updateRestrictedElementsInScope();
76+
}

0 commit comments

Comments
 (0)