-
Notifications
You must be signed in to change notification settings - Fork 0
3. main.js
Aizat edited this page May 8, 2025
·
1 revision
This file serves as the main entry point for the application's JavaScript code. It's responsible for orchestrating the various components of the application, including UI rendering, event handling, and the generation of the final Prettier configuration.
Key responsibilities of js/main.js include:
-
Importing Dependencies: It imports the
prettierOptionsarray fromjs/configData.js, providing access to the configuration options. It also imports UI manipulation functions (e.g.,renderConfigurator,toggleDarkMode,applySavedTheme) fromjs/ui.js. - DOM Element References: It retrieves references to key DOM elements, such as the download button, the textarea where the generated configuration is displayed, and the theme switch.
-
buildPrettierConfig()Function: This function is the core of the configuration generation process. It iterates over theprettierOptionsarray, reads the current values from the corresponding input elements in the UI, and constructs a Prettier configuration object. It only includes options that have been explicitly changed by the user (i.e., those that differ from their default values). -
displayGeneratedConfig()Function: This function takes the Prettier configuration object generated bybuildPrettierConfig()and displays it in thegeneratedConfigTextareain a human-readable JSON format (with indentation). It also enables or disables the download button based on whether the configuration is empty. -
downloadConfigFile()Function: This function handles the download of the generated Prettier configuration as a.prettierrc.jsonfile. It creates a Blob containing the JSON data, generates a temporary URL for the Blob, creates a temporary anchor element, triggers the download, and then cleans up the temporary resources. -
init()Function: This function initialises the application when the DOM is fully loaded. It performs the following tasks:- Applies any previously saved theme (light or dark mode).
- Renders the configuration cards into the UI using the
renderConfigurator()function fromjs/ui.js. - Displays the initial Prettier configuration (which may be empty or based on default values).
- Attaches event listeners to the download button (to trigger the download), the theme switch (to toggle dark mode), and the document (to listen for the custom
configChangeevent).
- Event Handling: It sets up event listeners to respond to user interactions, such as clicking the download button, toggling the theme switch, and changing the values of configuration options.
-
Application Entry Point: It uses
document.addEventListener('DOMContentLoaded', init)to ensure that theinit()function is executed only after the entire HTML document has been fully loaded and parsed.
In summary, js/main.js acts as the application's central controller, coordinating the UI, handling user input, and generating the final Prettier configuration file.