国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
design
Setting processing
Apply theme
Define functions
Tools and libraries I use
Home Web Front-end CSS Tutorial How I Created a Code Beautifier in Two Days

How I Created a Code Beautifier in Two Days

Apr 19, 2025 am 10:19 AM

How I Created a Code Beautifier in Two Days

Recently, I designed a wireframe diagram of a code beautification tool. The next day, I decided to turn it into a real tool. The entire project was completed in less than two days.

I've been thinking about building a new code beautification tool. The idea isn't unique, but every time I use someone else's tool, I find myself reapplying the same settings over and over again and avoiding ads every time . ???

I wanted a simple and easy-to-use tool without any hassle, so last week I took some paper and started sketching a wireframe. I really like to draw wireframes by hand. Designed with pencil and paper, my brain works better than staring at the screen.

I was immediately inspired after drawing the wireframe. The next day, I took time out of my daily work and turned it into reality. ????

View results

design

I know I want the code editor to be the focus of the tool, so I created a slim menu bar at the top for control modes (e.g. HTML, CSS, JavaScript) and settings. I also added an "About" button.

The editor itself takes up most of the screen, but it blends well with the background so you hardly notice it. Instead of using instructions to waste space, I used a placeholder that disappears when you start typing.

At the bottom, I created a status bar that displays real-time statistics about the code, including the current mode, indentation settings, line count, number of characters, and document size in bytes. There are Clear and Clean and Copy buttons on the right side of the status bar. In the middle is the logo showing off my own service.

I don't think many developers will write code on their phones, but I still want this tool to run on mobile devices. In addition to the commonly used responsive techniques, I also had to monitor the window size and adjust the tab position when the screen became too narrow.

I use flexbox and viewport units for vertical resizing. This is actually easy to do except for a little iOS problem. Here is a pen showing the basic wireframe. Note how the text area stretches to fill in the unused space between the title and the footer.

If you look at the JavaScript tab, you will see iOS problems and solutions. I'm not sure how to detect such a feature, so now it's just a simple device check.

Setting processing

I want the most commonly used settings to be easy to access, but also expose advanced settings for each mode. To do this, I made the settings button into a pop-up with links to more advanced settings. After changing the settings, the UI will be updated immediately and the settings will be saved to localStorage for a long time.

I'm using Vue.js here. Each setting is mapped to a data property, and when one of them changes, the UI updates (if needed) and I call saveSettings(). It's roughly like this.

 function saveSettings() {
  const settings = {};

  // settingsToStore is an array of attribute names that will be persisted // "this" refers to the current Vue model settingsToStore.map(key => settings[key] = this[key]);
  localStorage.setItem('settings', JSON.stringify(settings));
}

Each setting is a data property that is synchronized with localStorage. This is a rather primitive way of state storage, so I may update the application later to use state management libraries such as Vuex.

To restore settings, I have a restoreSettings() function that runs when the application starts.

 function restoreSettings() {
  const json = localStorage.getItem('settings');

  if (json) {
    try {
      const settings = JSON.parse(json);

      Object.keys(settings).forEach(key => {
        if (settingsToStore.includes(key)) {
          this[key] = settings[key];
        }
      });
    } catch (err) {
      window.alert('Error loading previous settings');
    }
  }
}

This function gets settings from localStorage and applies them one by one, making sure only valid settings in settingsToStore are imported.

The Advanced Settings link opens a dialog with each mode tab. Despite over 30 settings in total, everything is organized and accessible so users don’t feel overwhelmed.

Apply theme

Dark mode is very popular these days, so it is enabled by default. For those who like it, there is also a bright color theme. The entire UI will change except for popups and dialogs.

I've considered using prefers-color-scheme , which happened to be in Firefox 67 recently, but I decided to switch buttons might be better. The browser's support for color theme preference queries is not very good, and the developers are weird. (For example, I use macOS with a bright theme, but my text editor is dark.)

Define functions

It's easy to come up with a feature point. It is difficult to limit the features of the initial version. Here are the most relevant features I posted right away:

  • Beautify HTML, CSS and JavaScript code
  • Syntax highlighting with label/bracket matching
  • Paste or drag and drop a file to load the code
  • Automatically detect indentation preferences based on pasted code or drag-and-drop files
  • Bright and dark themes
  • One-click cleaning and copying
  • Keyboard shortcuts
  • Most JS Beautify options are configurable
  • Settings are stored in localStorage indefinitely
  • Minimal UI, no ads (just an inconspicuous publicity for my own service)?

I also added some easter eggs for fun. Try refreshing the page, exploring shortcuts, and sharing it on Facebook or Twitter to find them. ?

Tools and libraries I use

I really like Vue.js. It might be a bit too much for this project, but the Vue CLI allows me to start building with all the latest tools with a simple command.

 vue create beautiful-code

I didn't have to waste any time building scaffolding, which helped me build this tool quickly. In addition, Vue is very convenient in real-time statistics, changing themes, switching settings, etc. I've used various Element UI components such as buttons, form elements, popups, and dialogs.

The editor is powered by CodeMirror and uses custom styles. This is a well-supported and great project that I highly recommend for in-browser code editing.

The library that does all the beautification is called JS Beautify, which handles JavaScript, HTML, and CSS. JS Beautify runs on the client side, so this application doesn't actually have a backend – your browser does all the work!

JS Beautify is very easy to use. Install it using npm install js-beautify and run your code through the corresponding function.

 import beauty from 'js-beautify';

const code = 'Your code here';
const settings = {
  // Your settings here
};

// HTML
const html = beautiful.html(code, settings)

// CSS
const css = beautiful.css(code, settings)

// JavaScript
const js = beautiful.js(code, settings)

Each function returns a string containing the beautified code. You can change the output method of each language by passing in your own settings.

I've been asked several times about Prettier, which is a similar tool, so it's worth mentioning that I chose JS Beautify because it's not too arbitrary and more configurable. If the demand is large enough, I would consider adding an option to switch between JS Beautify and Prettier.

I've used these libraries before, so integration is actually very easy. ?

This project is thanks to my application Surreal CMS. If you are looking for an excellent CMS for static websites, check it out – it’s free for personal, educational and nonprofit sites!

Oh, if you want to know which editor I'm using...it's Visual Studio Code. ????

The above is the detailed content of How I Created a Code Beautifier in Two Days. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is 'render-blocking CSS'? What is 'render-blocking CSS'? Jun 24, 2025 am 12:42 AM

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

External vs. Internal CSS: What's the Best Approach? External vs. Internal CSS: What's the Best Approach? Jun 20, 2025 am 12:45 AM

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

Does my CSS must be on lower case? Does my CSS must be on lower case? Jun 19, 2025 am 12:29 AM

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

CSS Case Sensitivity: Understanding What Matters CSS Case Sensitivity: Understanding What Matters Jun 20, 2025 am 12:09 AM

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

What is Autoprefixer and how does it work? What is Autoprefixer and how does it work? Jul 02, 2025 am 01:15 AM

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

What are CSS counters? What are CSS counters? Jun 19, 2025 am 12:34 AM

CSScounterscanautomaticallynumbersectionsandlists.1)Usecounter-resettoinitialize,counter-incrementtoincrease,andcounter()orcounters()todisplayvalues.2)CombinewithJavaScriptfordynamiccontenttoensureaccurateupdates.

CSS: When Does Case Matter (and When Doesn't)? CSS: When Does Case Matter (and When Doesn't)? Jun 19, 2025 am 12:27 AM

In CSS, selector and attribute names are case-sensitive, while values, named colors, URLs, and custom attributes are case-sensitive. 1. The selector and attribute names are case-insensitive, such as background-color and background-Color are the same. 2. The hexadecimal color in the value is case-sensitive, but the named color is case-sensitive, such as red and Red is invalid. 3. URLs are case sensitive and may cause file loading problems. 4. Custom properties (variables) are case sensitive, and you need to pay attention to the consistency of case when using them.

What is the conic-gradient() function? What is the conic-gradient() function? Jul 01, 2025 am 01:16 AM

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin

See all articles