Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete

Rate this content
Bookmark

Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?


In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.

Hussien Khayoon
Hussien Khayoon
Kahvi Patel
Kahvi Patel
86 min
30 Nov, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Workshop covers various topics related to software development, including using the CodeMirror library to create custom editors, exploring CodeMirror setup and themes, adding multiple editors and custom themes, implementing linting and autocomplete features, and adding custom autocompletion in CodeMirror. It also discusses the Hydrogen and Oxygen tools for creating custom storefronts in Shopify, introduces the Polaris Viz UI library, and shares experiences and backgrounds of team members. The Workshop emphasizes the complexity of React code, the challenges and benefits of using TypeScript, and the importance of collaboration and communication with maintainers in open source projects.

1. Introduction to CodeMirror Library

Short description:

In this part, we will discuss CodeMirror, a powerful library that allows you to create your own editor. We will cover the basic setup, adding themes, linting, and auto-complete features. The library is widely used at Shopify and we had to upgrade it to meet our specific needs. Despite the thorough documentation, we had to navigate through examples to implement linting and custom auto-complete. We will be using ReactCodeMirror, a helpful library that simplifies the usage of CodeMirror. Let's dive into the coding process and follow the instructions.

This text is the first part: "Course 3. Developing in a Web browser"... I'm going to explain how to install React playoffs..." I'm Hussain, I'm a staff developer here at Shopify. I mostly focus on the front end. I've been using React for Seven years, closely. I mean, when it came out, let's talk about CodeMirror.

So what are we doing with CodeMirror? Today, the agenda is, we're going to talk about the library itself. We're going to talk about some basic setup. We're going to add our own themes and use additional themes that they have. Add our own linting. So, the ability to catch some errors. Adding a general auto-complete, something more complex of an auto-complete in JavaScript, and we're going to do more custom auto-complete.

I will be using JavaScript just because I think it's more accessible. Not everybody uses TypeScript. I wanted to just make it more general. Also, it saves a lot of time here and all the errors we might get. So, why are we talking about this? CodeMirror itself is a very, very, very powerful library. It allows you to create your own editor out of the box, and it has a lot of features and a lot of documentation. How we landed on this problem was at Shopify, we have a few places across our app that merchants use. Different across different code bases that other developers use in our docs, where we want to show a code editor, whether you can edit it or not, in different languages, adding certain syntax. Across the app, and Shopify has been around for a long time, I've only been there for a year, we noticed that CodeMirror was the main one being used. Also, it wasn't the most up-to-date version, so part of what I did was had the learn CodeMirror and add a new feature where merchants are able to add their own code, essentially, add their own code and fire off events in those codes. What that means is a merchant can say, after I make a sale, please send Facebook the message about my sale, meaning tell them that I sold something, this particular product at this particular price with these parameters. We fire off that event for you, but you put in the code that you want to fire off because you want to send it to Facebook anywhere else. So we had to have a coding environment and that's just one of the use cases that we had it at.

So I think a lot of what we do here as developers, because you can create your own, you can write your own CodeMirror, you can create your own editor and that's cooler, so to speak, but generally, we find ourselves if there's something out there that's better, maybe it's not the best thing that you should do, and we all know that and we all have to make those difficult decisions about should we use this? Should we make our own? And something like this that already existed in the code base that other teams were using, it was a good idea to use this. But the problem is it wasn't up to date. We had to upgrade it. And I think that leads me to the point about why this is so important is because I found myself, I I've been developing for about 10 years. I found the most important, the thing I'm doing the most is not writing new, cool stuff and cutting edge innovation. Generally speaking, I'm giving libraries on the front end or backend, but let's just say front end, and I have to, what I call wrangle them. I have to make them work for what I want it to do. At the end of the day, it's nice when the library just works. But rarely, is that the case because business means code and business makes code complicated. So, um, what I've had to do with CodeMirror and what our team had to do was essentially make it work for our use case. And it was an environment where you see documentation like this and you get very excited because it's, it's very thorough, but documentation, what I learned is very nice. But the problem is when it's very thorough, it doesn't mean it's very easy to find out to do what you want. So we had to do a linting. We had to do autocomplete. We had to add our own, but it wasn't very apparent from the examples, but the documentation was there. So let's get through here and let's start coding here. But I just thought that introduction would be important. So one thing to know here is we're using ReactCodeMirror. This just makes using CodeMirror a lot easier. You can make your own wrappers on what our teams do, but this library here is quite useful. It essentially just makes things into its own packages and you'll see that we have a lot of packages here and that's just the architecture they decided to do to allow you and give you more flexibility. They give some basic examples of what you can do. They support multiple languages, so it's easy to use out of the box when you're using React. So let's follow the instructions here. One thing that's cool about StackBlitz is that you don't actually need to install these libraries directly, so you should do Yarn add.".

2. Exploring CodeMirror Setup in StackBlitz

Short description:

In this part, we explore the basic setup of CodeMirror in StackBlitz. We discuss the speed and architecture of StackBlitz, highlighting its use of WebAssembly for fast execution. The basic setup of the code editor allows control over height, provides autocomplete, collapsing, and line-gutter features. We also touch on the power of extensions in CodeMirror and the challenges of navigating the extensive documentation. Additionally, we mention the ability to add TSX True and the limitations of adding divs within JavaScript.

So you'll see here, if you're following along with StackBlitz, I'm going to take this code here with the basic setup and I'm just going to paste it in here. And I'm just going to change this to app.js. If it works, if it doesn't work, we'll revert it back.

So as you can see here, it's already asking me to I have the 10-D list here, so I can't see a lot of what I'm doing. Okay. It's going to ask me to install these parameters. Pretty cool. I don't know if you noticed, but StackBlitz is actually pretty, pretty fast compared to a lot of the stuff here. And a lot of the other kind of environments, I think Code Sandbox particularly is what I used. And what's really interesting about their architecture is everything I think is written in, correct me if I'm wrong, Covey is in WebAssembly, and just loaded on your browser instead of like, you know going back to your code, sorry, going to the cloud every time you update something and asking a server for things. So as far as I know, that's what they do. And it's very, very fast if you use it. So I don't know if you notice that or if you're a user, I just found out about this pretty recently from a call.

Yeah, there's an interesting difference between how they execute code on CodeSandbox versus in StackBlitz. I don't know the details of it, but I do know that StackBlitz is local, which is awesome. Yeah, so, thanks for that, Covey. So as you can see here, we have a basic setup of a code editor. And you see, we didn't really need to do much here, but there's some important nuances. The first one here is, we can control height. Pretty easy thing here. You can make this 250 if you want. You'll see that it increased in size. You see here, we have initial kind of value. And my mouse is going crazy. And another thing that you can do here is, you can see you have some things out of the box, like autocomplete. For example, for of loop, we have it right here. We could do an if statement. It's going to do an if block out of the box. There's no linting errors or anything like that out of the box. But you can see here, with the basic setup, you can even do some collapsing, which is really nice. You have this, what's called a line-gutter or the gutter, which allows you to show the lines. And they have different colors for highlighting and everything like that. Also, let's see here. You can do a trycatch block if you want. And it's pretty nice out of the box. So what makes this possible is you have, you're plugged into these language kind of plugins or packages. And JavaScript allows you to essentially make this editor specifically for JavaScript. And you call it as a function, and it's inside the extensions. The extension is the most powerful thing in Codemirror, and what I love about it is, everything, every feature you want to add, you can do through extensions. Now, the problem with it, I found is that the documentation is extensive. But it's also confusing because a lot of times what they do is they'll use their own kind of terminology in other objects and just say like, oh, it provides this and facets. And what I found with that is like, I'm, you know, it's a bit overwhelming and you got to like dig into more to understand what's really going on. I don't know about you, but I like to follow examples. So, and that's how I was trying to do things. So a lot of it was trying to find things and do things online. And with the JavaScript extension, you can also add, I think, TSX True in here. And I think you, it'll give you the ability to add things like divs, but I think they have to be inside some JavaScript here I can't remember here. OK. Let me see, is it in here? OK, doesn't seem to be working, but that's OK. That's how demos go.

Watch more workshops on topic

Testing Web Applications Using Cypress
TestJS Summit - January, 2021TestJS Summit - January, 2021
173 min
Testing Web Applications Using Cypress
WorkshopFree
Gleb Bahmutov
Gleb Bahmutov
This workshop will teach you the basics of writing useful end-to-end tests using Cypress Test Runner.
We will cover writing tests, covering every application feature, structuring tests, intercepting network requests, and setting up the backend data.
Anyone who knows JavaScript programming language and has NPM installed would be able to follow along.
Build a powerful DataGrid in few hours with Ag Grid
React Summit US 2023React Summit US 2023
96 min
Build a powerful DataGrid in few hours with Ag Grid
WorkshopFree
Mike Ryan
Mike Ryan
Does your React app need to efficiently display lots (and lots) of data in a grid? Do your users want to be able to search, sort, filter, and edit data? AG Grid is the best JavaScript grid in the world and is packed with features, highly performant, and extensible. In this workshop, you’ll learn how to get started with AG Grid, how we can enable sorting and filtering of data in the grid, cell rendering, and more. You will walk away from this free 3-hour workshop equipped with the knowledge for implementing AG Grid into your React application.
We all know that rolling our own grid solution is not easy, and let's be honest, is not something that we should be working on. We are focused on building a product and driving forward innovation. In this workshop, you'll see just how easy it is to get started with AG Grid.
Prerequisites: Basic React and JavaScript
Workshop level: Beginner
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
Build a Powerful Datagrid With AG Grid
React Summit 2024React Summit 2024
168 min
Build a Powerful Datagrid With AG Grid
WorkshopFree
Brian Love
Brian Love
Does your React app need to efficiently display lots (and lots) of data in a grid? Do your users want to be able to search, sort, filter, and edit data? AG Grid is the best JavaScript grid in the world and is packed with features, highly performant, and extensible. In this workshop, you’ll learn how to get started with AG Grid, how we can enable sorting and filtering of data in the grid, cell rendering, and more. You will walk away from this free 3-hour workshop equipped with the knowledge for implementing AG Grid into your React application.
JavaScript-based full-text search with Orama everywhere
Node Congress 2023Node Congress 2023
49 min
JavaScript-based full-text search with Orama everywhere
Workshop
Michele Riva
Michele Riva
In this workshop, we will see how to adopt Orama, a powerful full-text search engine written entirely in JavaScript, to make search available wherever JavaScript runs. We will learn when, how, and why deploying it on a serverless function could be a great idea, and when it would be better to keep it directly on the browser. Forget APIs, complex configurations, etc: Orama will make it easy to integrate search on projects of any scale.
Getting Started with AG Grid and React – The Best Javascript Grid in the World
React Advanced Conference 2022React Advanced Conference 2022
140 min
Getting Started with AG Grid and React – The Best Javascript Grid in the World
WorkshopFree
Brian Love
Brian Love
Does your React app need to efficiently display lots (and lots) of data in a grid? Do your users want to be able to search, sort, filter, and edit data? AG Grid is the best JavaScript grid in the world and is packed with features, highly performant, and extensible. In this workshop, you’ll learn how to get started with AG Grid, how we can enable sorting and filtering of data in the grid, cell rendering, and more. You will walk away from this free 3-hour workshop equipped with the knowledge for implementing AG Grid into your React application.
We all know that rolling our own grid solution is not easy, and let's be honest, is not something that we should be working on. We are focused on building a product and driving forward innovation. In this workshop, you'll see just how easy it is to get started with AG Grid.
Prerequisites: Basic React and JavaScript
Workshop level: Beginner

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Vite: Rethinking Frontend Tooling
JSNation Live 2021JSNation Live 2021
31 min
Vite: Rethinking Frontend Tooling
Top Content
Vite is a new build tool that intends to provide a leaner, faster, and more friction-less workflow for building modern web apps. This talk will dive into the project's background, rationale, technical details and design decisions: what problem does it solve, what makes it fast, and how does it fit into the JS tooling landscape.
Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
Do you have a large product built by many teams? Are you struggling to release often? Did your frontend turn into a massive unmaintainable monolith? If, like me, you’ve answered yes to any of those questions, this talk is for you! I’ll show you exactly how you can build a micro frontend architecture with Remix to solve those challenges.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
React provides a contract to developers- uphold certain rules, and React can efficiently and correctly update the UI. In this talk we'll explore these rules in depth, understanding the reasoning behind them and how they unlock new directions such as automatic memoization. 
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
Remix is a web framework that gives you the simple mental model of a Multi-Page App (MPA) but the power and capabilities of a Single-Page App (SPA). One of the big challenges of SPAs is network management resulting in a great deal of indirection and buggy code. This is especially noticeable in application state which Remix completely eliminates, but it's also an issue in individual components that communicate with a single-purpose backend endpoint (like a combobox search for example).
In this talk, Kent will demonstrate how Remix enables you to build complex UI components that are connected to a backend in the simplest and most powerful way you've ever seen. Leaving you time to chill with your family or whatever else you do for fun.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
JavaScript in the browser runs many times faster than it did two decades ago. And that happened because the browser vendors spent that time working on intensive performance optimizations in their JavaScript engines.Because of this optimization work, JavaScript is now running in many places besides the browser. But there are still some environments where the JS engines can’t apply those optimizations in the right way to make things fast.We’re working to solve this, beginning a whole new wave of JavaScript optimization work. We’re improving JavaScript performance for entirely different environments, where different rules apply. And this is possible because of WebAssembly. In this talk, I'll explain how this all works and what's coming next.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
As developers, we spend much of our time debugging apps - often code we didn't even write. Sadly, few developers have ever been taught how to approach debugging - it's something most of us learn through painful experience.  The good news is you _can_ learn how to debug effectively, and there's several key techniques and tools you can use for debugging JS and React apps.