Writing a very simple JS editor

There are nice web editors out there which are ready to be used: you just download them and plug them in your page. I have used myself both CodeMirror and ACE in the past. For example I wrote a plugin for CodeMirror to support PlantUML. However there is an issue with these editors: they are difficult to extend and difficult to understand.

When I look at the code of these products I see something that I cannot easily understand, something I do not feel confident building upon.

Now, my philosophy is to build simple tools, that work, can be understood, combined and extended. So I wanted to try another approach, building my own simple web editor from scratch.

js web editor

Following the rule of putting your code where your mouth is, here comes the GitHub repo: https://github.com/ftomassetti/simple-web-editor

HTML

Let’s start slow, with some HTML:

What we have here?

  • jquery, of course
  • some CSS
  • a cool font from Google
  • a JS file with all of our code (wededitor.js)
  • a div (editor) and a span for our blinking editor

TypeScript

Now, we are going to use TypeScript with the hope that it can reduce a little bit the pain of using JavaScript. And also because I want to try it out. For people who have never used it before TypeScript is basically a superset of JavaScript which permits to optionally specify types. Types are used to check for errors and then forgot because in the end we generate JavaScript. You can use JavaScript libraries in TypeScript and when you want to do that you may want to import a description of all the types in that library. This is what we import in the first line.

Ok, let’s look at the code now. We have:

  • the Editor class
  • the function updateHTML
  • the wiring in $(document).ready(…)

The editor class

The editor class is where we do the heavy lifting. We store two things:

  1. the text contained in the editor
  2. the position of the caret within the text

TextBeforeCaret and TextAfterCaret obviously give us all the text before or after the caret (surprised?).

What generateHTML does? It generate the HTML code for the text placing a span to indicate the position of the caret: this element is the caret placeholder. Why we do not put the caret itself? Because the caret has a size, so if we would move it around inside the text we would cause all the text to move all the time. Instead we move the caret placeholder, which has zero size and then we use place the caret above the caret placeholder but at a different z-index. In this way we basically see the caret where we want to see it without moving the text left or right to leave place for the caret.

The remaining methods permit to:

  • insert a character
  • delete a character
  • move the caret left
  • move the caret right

The function updateHTML

The function updateHTML implement the trick we have seen for the caret:

First we update the content of the editor, then we find the position of the caret placeholder and then we move the blinking-cursor (a.k.a. the caret) just above the placeholder. We actually move it a little bit on the left because in this way it looks nicer.

The wiring

The wiring consist in attaching event handler to:

  • get when we type a character
  • get when we delete a character
  • get when we use the left and right arrow

We then just call methods from the Editor class.

Conclusions

Ok, we started with something simple: a very minimal editor where we can type, delete, and moving around using the arrow. This is not the most impressive editor ever seen. But it is simple and it works. We can build on top of that something sensible, that does what we need and it still understandable and extensible.

Download the guide with 68 resources on Creating Programming Languages

68resources

Receive the guide to your inbox to read it on all your devices when you have time

Powered by ConvertKit
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply