Skip to content

Commit 3ae9dc7

Browse files
authored
Merge branch 'CircuitVerse:main' into main
2 parents 5e544c0 + 1ea73f2 commit 3ae9dc7

File tree

4 files changed

+121
-56
lines changed

4 files changed

+121
-56
lines changed

src/simulator/src/app.js renamed to src/simulator/src/app.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { setup } from './setup'
1+
import { setup } from './setup';
2+
import { JsConfig } from './types/app.types'
23

34
document.addEventListener('DOMContentLoaded', () => {
4-
setup()
5-
var js = {
5+
setup();
6+
const js: JsConfig = {
67
devices: {
78
dev0: {
89
type: 'Input',
@@ -206,5 +207,5 @@ document.addEventListener('DOMContentLoaded', () => {
206207
},
207208
],
208209
subcircuits: {},
209-
}
210-
})
210+
};
211+
});

src/simulator/src/data/undo.js

-51
This file was deleted.

src/simulator/src/data/undo.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
// Declare global variables
9+
declare let globalScope: Scope
10+
declare let loading: boolean
11+
12+
/**
13+
* Function to restore copy from backup
14+
* @param scope - The circuit on which undo is called
15+
* @category data
16+
*/
17+
export default function undo(scope: Scope = globalScope): void {
18+
if (layoutModeGet() || scope.backups.length < 2) return
19+
20+
const { ox, oy, scale } = saveGlobalScopePosition()
21+
resetGlobalScopePosition()
22+
23+
loading = true
24+
const undoData = popLastBackup(scope)
25+
if (!undoData) return
26+
27+
scope.history.push(undoData)
28+
29+
const tempScope = createTempScope(scope)
30+
if (!tempScope) return
31+
32+
updateGlobalScope(tempScope, ox, oy, scale)
33+
forceResetNodesSet(true)
34+
updateRestrictedElementsInScope()
35+
}
36+
37+
function saveGlobalScopePosition() {
38+
return {
39+
ox: globalScope.ox,
40+
oy: globalScope.oy,
41+
scale: globalScope.scale,
42+
}
43+
}
44+
45+
function resetGlobalScopePosition() {
46+
globalScope.ox = 0
47+
globalScope.oy = 0
48+
}
49+
50+
function popLastBackup(scope: Scope): string | undefined {
51+
return scope.backups.pop()
52+
}
53+
54+
function createTempScope(scope: Scope): Scope | undefined {
55+
const tempScope = new Scope(scope.name)
56+
if (scope.backups.length === 0) return tempScope
57+
58+
try {
59+
loadScope(tempScope, JSON.parse(scope.backups[scope.backups.length - 1]))
60+
} catch (error) {
61+
console.error('Failed to parse backup data:', error)
62+
loading = false
63+
return undefined
64+
}
65+
66+
tempScope.backups = scope.backups
67+
tempScope.history = scope.history
68+
tempScope.id = scope.id
69+
tempScope.name = scope.name
70+
tempScope.testbenchData = scope.testbenchData
71+
72+
return tempScope
73+
}
74+
75+
function updateGlobalScope(tempScope: Scope, ox: number, oy: number, scale: number) {
76+
scopeList[tempScope.id] = tempScope
77+
globalScope = tempScope
78+
globalScope.ox = ox
79+
globalScope.oy = oy
80+
globalScope.scale = scale
81+
loading = false
82+
}

src/simulator/src/types/app.types.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
interface Device {
2+
type: string;
3+
net?: string;
4+
order?: number;
5+
bits: number;
6+
label?: string;
7+
abits?: number;
8+
words?: number;
9+
offset?: number;
10+
rdports?: Array<{ clock_polarity?: boolean }>;
11+
wrports?: Array<{ clock_polarity?: boolean }>;
12+
memdata?: Array<number | string>;
13+
}
14+
15+
interface Connector {
16+
to: {
17+
id: string;
18+
port: string;
19+
};
20+
from: {
21+
id: string;
22+
port: string;
23+
};
24+
name: string;
25+
}
26+
27+
export interface JsConfig {
28+
devices: {
29+
[key: string]: Device;
30+
};
31+
connectors: Connector[];
32+
subcircuits: Record<string, unknown>;
33+
}

0 commit comments

Comments
 (0)