Whether you're creating your own personal web site, a site for a medium sized business, or a site for a company intranet, the content management system (CMS) is one of the most important aspects of the overall design. In this article Mitchell shows us how to build an awesome browser based HTML editor using only HTML and JavaScript. This article is a must read!
Building a WYSIWYG HTML Editor Part 1/2 - Building our HTML editor (Page 4 of 7 )
Over the next couple of pages we're going to create our own browser based WYSIWYG HTML editor. I won't look at the entire source code for the editor, but I will go into detail about the most important points. The single HTML file and required images are available as part of the support material for this article. You should download it before continuing.
Firstly, we want to make our HTML editor boths look and feels like a "normal" HTML editor, such as Front Page 2000. Using style sheets and events, we can create a set of "buttons" that mimic those found in Windows applications. Our HTML editor will contain several buttons to bold text, insert images, etc. Here's how the image that makes up the bold button looks:
It has a class attribute of "butClass", which is defined in the <head> part of our HTML document:
.butClass
{
border: 1px solid;
border-color: #D6D3CE;
}
It also responds to five events, which will be fired by the browser: onMouseOver, onMouseOut, onMouseDown, onMouseUp and onClick. The contents of each of these attributes points to a JavaScript function that is defined at the top of the file. For example, when a user moves his/her mouse over a button, the selOn function is called, passing in the image as an object ("this" represents the control for which the event has been raised):
function selOn(ctrl)
{
ctrl.style.borderColor = '#000000';
ctrl.style.backgroundColor = '#B5BED6';
ctrl.style.cursor = 'hand';
}
The selOn function sets the border color of the image to black, its background color to "FrontPage blue", and the mouse cursor to a hand.
Each button in our HTML editor responds to the same five events in exactly the same way. The only difference is that when the onClick event is fired (the user clicks on a button), then each button will call a different JavaScript function, thus performing a different task.
Here's how the buttons will look at different stages:
Besides buttons, our HTML editor also contains an inline frame (<iframe>) and drop down lists to set the font type and size, as well as various heading sizes (<h1>, <h2>, etc). If you've downloaded the support material, you will see that our HTML editor looks like this in IE:
It’s amazing what you can do with a little bit of CSS and some positioning isn't it? When each button is clicked, its onClick event is fired. Here's the code for the doBold() JavaScript function, which is called when the bold "button" is clicked on:
function doBold()
{
iView.document.execCommand('bold', false, null);
}
As you can see, we've used the same execCommand that we used earlier to test bolding some text. In fact, most of our JavaScript functions only contain one line, which is the call to the inline frame's execCommand method.
The code for the italic and underline buttons is fairly similar to the bold buttons code, so let's look at the code for the center align function, doCenter():
One again, a simple call to execCommand, passing in the command "justifycenter". To actually test the justifycenter command, click on the inline frame, type in some text, select it, and click the center align button, like this:
The insertorderedlist and insertunorderedlist commands can be passed to execCommand to toggle whether or not the selected text is part of either an ordered or unordered list respectively. When the user clicks on the bulleted (unsorted) list button, the doBulList() JavaScript function is called. It looks like this:
Here's a screen shot from me playing around with an unordered list. I've also changed the colors of the foreground and background, which we will look at next: