-
Notifications
You must be signed in to change notification settings - Fork 152
JS to TS : simulator/src/tutorial.ts #438
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
base: main
Are you sure you want to change the base?
Changes from all commits
2ef15d2
d23d0b6
42aef0f
a7484f5
a8f036b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||||||||||
} | ||||||||||||
Comment on lines
+36
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix const reassignment of DPR. The DPR variable is declared as const but reassigned, which is incorrect: - const DPR = window.devicePixelRatio || 1;
- if (lightMode) {
- DPR = 1;
- }
+ let DPR = lightMode ? 1 : (window.devicePixelRatio || 1); 📝 Committable suggestion
Suggested change
🧰 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) |
||||||||||||
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; | ||||||||||||
} | ||||||||||||
Comment on lines
+40
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add null checks for DOM elements. Replace non-null assertions with proper null checks: - const width = document.getElementById('simulationArea')?.clientWidth! * DPR;
+ const simulationArea = document.getElementById('simulationArea');
+ if (!simulationArea) {
+ console.error('Simulation area not found');
+ return;
+ }
+ const width = simulationArea.clientWidth * 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); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
Comment on lines
+156
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add cleanup for setTimeout. The setTimeout should be cleaned up to prevent memory leaks: function showTour(): void {
if (!localStorage.tutorials_tour_done && !embed) {
- setTimeout(() => {
+ const timeoutId = setTimeout(() => {
showTourGuide();
}, 2000);
+ // Add cleanup method to clear timeout
+ return () => clearTimeout(timeoutId);
}
}
|
||||||||||||
|
||||||||||||
/** | ||||||||||||
* 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(); | ||||||||||||
} |
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.
💡 Codebase verification
🛠️ Refactor suggestion
Reconsider removing the page leave warning
While there is a recovery mechanism in place, removing the
onbeforeunload
handler eliminates the primary way to prevent accidental data loss. The current recovery system only helps after the fact, but doesn't prevent users from accidentally navigating away with unsaved changes.onbeforeunload
handler to maintain better user experience🔗 Analysis chain
Remove commented code.
Instead of keeping commented code, consider removing it entirely and preserving it in version control history if needed.
Verify impact on user experience.
The removal of the
onbeforeunload
handler means users won't be warned about unsaved changes when leaving the page. Please confirm if this is intentional and if there are alternative measures to prevent data loss.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 93
Script:
Length of output: 743
Script:
Length of output: 4640