-
Notifications
You must be signed in to change notification settings - Fork 152
JS to TS: simulator/src/setup.ts #437
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
Open
nickhil-verma
wants to merge
4
commits into
CircuitVerse:main
Choose a base branch
from
nickhil-verma:setup.ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2ef15d2
feat(circuit): add labels for circuit elements
nickhil-verma d23d0b6
fix: extra pop up after creating new project #399
nickhil-verma 42aef0f
fix: extra pop up after creating new project #399
nickhil-verma a7484f5
fix: js to ts setup.ts
nickhil-verma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,178 @@ | ||
/* eslint-disable import/no-cycle */ | ||
/* eslint-disable no-restricted-syntax */ | ||
/* eslint-disable guard-for-in */ | ||
import { generateId, showMessage } from './utils'; | ||
import { backgroundArea } from './backgroundArea'; | ||
import plotArea from './plotArea'; | ||
import { simulationArea } from './simulationArea'; | ||
import { dots } from './canvasApi'; | ||
import { update, updateSimulationSet, updateCanvasSet } from './engine'; | ||
import { setupUI } from './ux'; | ||
import startMainListeners from './listeners'; | ||
import { newCircuit } from './circuit'; | ||
import load from './data/load'; | ||
import save from './data/save'; | ||
import { showTourGuide } from './tutorials'; | ||
import setupModules from './moduleSetup'; | ||
import 'codemirror/lib/codemirror.css'; | ||
import 'codemirror/addon/hint/show-hint.css'; | ||
import 'codemirror/mode/javascript/javascript'; // verilog.js from codemirror is not working because array prototype is changed. | ||
import 'codemirror/addon/edit/closebrackets'; | ||
import 'codemirror/addon/hint/anyword-hint'; | ||
import 'codemirror/addon/hint/show-hint'; | ||
import { setupCodeMirrorEnvironment } from './Verilog2CV'; | ||
import '../vendor/jquery-ui.min.css'; | ||
import '../vendor/jquery-ui.min'; | ||
import { confirmSingleOption } from '#/components/helpers/confirmComponent/ConfirmComponent.vue'; | ||
import { getToken } from '#/pages/simulatorHandler.vue'; | ||
|
||
/** | ||
* to resize window and setup things it | ||
* sets up new width for the canvas variables. | ||
* Also redraws the grid. | ||
* @category setup | ||
*/ | ||
export function resetup(): void { | ||
const DPR = window.devicePixelRatio || 1; | ||
if (lightMode) { | ||
DPR = 1; | ||
} | ||
const width = document.getElementById('simulationArea')?.clientWidth! * DPR; | ||
let height; | ||
if (!embed) { | ||
height = | ||
(document.body.clientHeight - | ||
document.getElementById('toolbar')?.clientHeight!) * | ||
DPR; | ||
} else { | ||
height = document.getElementById('simulation')?.clientHeight! * DPR; | ||
} | ||
// setup simulationArea and backgroundArea variables used to make changes to canvas. | ||
backgroundArea.setup(); | ||
simulationArea.setup(); | ||
// redraw grid | ||
dots(); | ||
document.getElementById('backgroundArea')!.style.height = | ||
height / DPR + 100 + 'px'; | ||
document.getElementById('backgroundArea')!.style.width = | ||
width / DPR + 100 + 'px'; | ||
document.getElementById('canvasArea')!.style.height = height / DPR + 'px'; | ||
simulationArea.canvas.width = width; | ||
simulationArea.canvas.height = height; | ||
backgroundArea.canvas.width = width + 100 * DPR; | ||
backgroundArea.canvas.height = height + 100 * DPR; | ||
if (!embed) { | ||
plotArea.setup(); | ||
} | ||
updateCanvasSet(true); | ||
update(); // INEFFICIENT, needs to be deprecated | ||
simulationArea.prevScale = 0; | ||
dots(); | ||
} | ||
|
||
window.onresize = resetup; // listener | ||
window.onorientationchange = resetup; // listener | ||
|
||
// for mobiles | ||
window.addEventListener('orientationchange', resetup); // listener | ||
|
||
/** | ||
* function to setup environment variables like projectId and DPR | ||
* @category setup | ||
*/ | ||
function setupEnvironment(): void { | ||
setupModules(); | ||
const projectId = generateId(); | ||
(window as any).projectId = projectId; | ||
updateSimulationSet(true); | ||
newCircuit('Main'); | ||
(window as any).data = {}; | ||
resetup(); | ||
setupCodeMirrorEnvironment(); | ||
} | ||
|
||
/** | ||
* Fetches project data from API and loads it into the simulator. | ||
* @param {number} projectId The ID of the project to fetch data for | ||
* @category setup | ||
*/ | ||
async function fetchProjectData(projectId: number): Promise<void> { | ||
try { | ||
const response = await fetch( | ||
`/api/v1/projects/${projectId}/circuit_data`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Token ${getToken('cvt')}`, | ||
}, | ||
} | ||
); | ||
if (response.ok) { | ||
const data = await response.json(); | ||
await load(data); | ||
await simulationArea.changeClockTime(data.timePeriod || 500); | ||
($('.loadingIcon') as any).fadeOut(); | ||
} else { | ||
throw new Error('API call failed'); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
confirmSingleOption('Error: Could not load.'); | ||
($('.loadingIcon') as any).fadeOut(); | ||
} | ||
} | ||
|
||
/** | ||
* Load project data immediately when available. | ||
* Improvement to eliminate delay caused by setTimeout in previous implementation revert if issues arise. | ||
* @category setup | ||
*/ | ||
async function loadProjectData(): Promise<void> { | ||
(window as any).logixProjectId = (window as any).logixProjectId ?? 0; | ||
if ((window as any).logixProjectId !== 0) { | ||
($('.loadingIcon') as any).fadeIn(); | ||
await fetchProjectData((window as any).logixProjectId); | ||
} else if (localStorage.getItem('recover_login') && (window as any).isUserLoggedIn) { | ||
// Restore unsaved data and save | ||
const data = JSON.parse(localStorage.getItem('recover_login')!); | ||
await load(data); | ||
localStorage.removeItem('recover'); | ||
localStorage.removeItem('recover_login'); | ||
await save(); | ||
} else if (localStorage.getItem('recover')) { | ||
// Restore unsaved data which didn't get saved due to error | ||
showMessage( | ||
"We have detected that you did not save your last work. Don't worry we have recovered them. Access them using Project->Recover" | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Show tour guide if it hasn't been completed yet. | ||
* The tour is shown after a delay of 2 seconds. | ||
* @category setup | ||
*/ | ||
function showTour(): void { | ||
if (!localStorage.tutorials_tour_done && !embed) { | ||
setTimeout(() => { | ||
showTourGuide(); | ||
}, 2000); | ||
} | ||
} | ||
|
||
/** | ||
* The first function to be called to setup the whole simulator. | ||
* This function sets up the simulator environment, the UI, the listeners, | ||
* loads the project data, and shows the tour guide. | ||
* @category setup | ||
*/ | ||
export function setup(): void { | ||
setupEnvironment(); | ||
if (!embed) { | ||
setupUI(); | ||
startMainListeners(); | ||
} | ||
loadProjectData(); | ||
showTour(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent constant re-assignment of
DPR
.Currently,
DPR
is declared as a constant, but re-assigned inside theif (lightMode)
conditional, which will throw an error in TypeScript. If you need to overrideDPR
, switch to using alet
variable or create another variable for the adjusted value.Proposed fix:
📝 Committable suggestion
🧰 Tools
🪛 Biome (1.9.4)
[error] 38-38: Can't assign DPR because it's a constant
This is where the variable is defined as constant
Unsafe fix: Replace const with let if you assign it to a new value.
(lint/correctness/noConstAssign)