Background Image
Web
Development
Tools
Tool...
49 Projects
Web Development Project
Web
Development
Project
Project: Rich Text Editor WYSIWYG
Creator:Graphix
Project type:Games
Created on:2010-10-10 21:33:04
Last edit:2010-11-16 19:10:16
Download:
Basic concept

For a long time, I thought that making a rich text editor was very difficult as there was few documentation and tutorials on how to do make one. Then I got a question by a user on DaniWeb on how to make one, and I decided to create my own project.

Almost 99% of all rich text editors on the web are based on the principle of and inline frame, which is set to be editable. The user can edit this frame, but you need to provide buttons that triggers the events to bold text, add links, underline etc.
Interface

The interface of the rich text editor we are going to make is simple and will have the following things:

  • Bold button

  • Underline button

  • Italic button

  • Align center button

  • Add link button


Programming


We first start off with making our interface, the HTML is kept simple and clean. It has all the above menu elements. The element with the frame_holder id is where the iframe will be loaded using javascript (see next step):

Code Snippet (Syntax highlighted) - Toggle Wrap

<form action='' method='post' onsubmit='SaveRichTextEdit(); return true;'>

<!-- The menu bar //-->

<input type='button' value='Bold' onclick='FontEdit("bold");' />
<input type='button' value='Underline' onclick='FontEdit("underline");' />
<input type='button' value='Italic' onclick='FontEdit("italic");' />
<input type='button' value='Align center' onclick='FontEdit("justifycenter");' />
<input type='button' value='Add link' onclick='AddLink()' />

<!-- This is where the actual content (in HTML) of the rich text editor is stored: //-->

<input type='hidden' name='rich_text_area_content' id='rich_text_area_content' value='' />

<!-- The edit frame, that the user uses to edit //-->

<div id='frame_holder'></div>

</form>


Now we start off with making the function that load the frame into the frame holder:

Code Snippet (Syntax highlighted) - Toggle Wrap


function LoadRichTextEditor() {

/* Retrieving the frame holder: */
var Frame_Holder = document.getElementById('frame_holder');

/* Creating iFrame element: */
var Frame = document.createElement("iframe");

/* Setting the name and id of the frame */
Frame.name = Frame.id = "RichEditArea";

/* Loading the frame into the frame holder: */
Frame_Holder.appendChild(Frame);

/* Now for the most important part: setting the design mode on for the frame: */

/* Setting MTE_Frame design mode active */
RichEditArea.document.designMode="on";

/* Writing the iFrame content */
RichEditArea.document.open();
RichEditArea.document.write('<html><head><style type="text/css">body{ font-family:Arial; font-size:13px; }</style></head><body>');
RichEditArea.document.write('This is some text</body></html>');
RichEditArea.document.close();

/* Focussing the rich edit area when the function is done: */
RichEditArea.focus();

}

/* Triggering the load function when the page is loaded: */

window.onload=function(){

LoadRichTextEditor();

}



That was the hardest part! Now we only need to write the function that handles the commands (bold, italic etc...):

Code Snippet (Syntax highlighted) - Toggle Wrap

function FontEdit( Variable, Value ) {

/* Executing command: */
RichEditArea.document.execCommand(Variable,"",Value);


/* Focussing the rich text editor */
RichEditArea.focus();

}


Then the last function, to add a link into the rich edit area:

Code Snippet (Syntax highlighted) - Toggle Wrap

function AddLink() {
var link = prompt('Enter the URL:');

if (link == null) {
link = 'http://www.webdevproject.com';
}

FontEdit('CreateLink',link);

}


That was about it, but there are two more things I want to explain that you are probably wondering:

How can I send the value of the rich text area with the form?

Well, you need to create a function, that is triggered when the form is submitted. That function reads the content of the rich text area and sets that as value for an hidden input in your form, for example:

Code Snippet (Syntax highlighted) - Toggle Wrap

function SaveRichTextEdit() {

/* Retrieving content: */
var content = RichEditArea.document.body.innerHTML;

/* Cleaning out all tags (< and >) */
content = content.replace(/</g,"&lt;").replace(/>/g,"&gt;");

/* Saving content: */
document.getElementById('rich_text_area_content').value = content;

}


How can I load text into the rich text area?

In the LoadRichTextEditor() function, you need to change the content of the <body></body> tag within the frame, into whatever you want (watch out for double and single quotes!)
Using this code

Extending this code



You can also extend this code, by adding more rich edit options. Here is a full list of all ExecCommand variables:

All command identifiers without an argument that do something with the current selection:


  • Bold - Toggles the current selection between bold and unbold

  • Underline - Toggles the current selection between underlined and not underlined

  • Italic - Toggles the current selection between italic and nonitalic

  • StrikeThrough - Toggles the current selection between striked through and not striked through

  • Subscript - Toggles the current selection between subscript and non subscript

  • Superscript - Toggles the current selection between superscript and non superscript

  • Indent - Indents the current selection

  • Outdent - Outdents the current selection

  • JustifyLeft - Aligns the current selection to the left

  • JustifyRight - Aligns the current selection to the right

  • JustifyCenter - Aligns the current selection to the center

  • Copy - Copies the current selection to the clipboard

  • Paste - Pastes the content of the clipboard

  • Cut - Copies the current selection to the clipboard and then deletes it

  • Delete - Deletes the current selection

  • AbsolutePosition - Sets the current selection absolutely positioned



All command identifiers with an argument that do something with the current selection:


  • CreateLink - Converts the current selection to a link, of which the href is the second argument

  • FontName - Converts the current selection into an font family, which is declared as the second argument

  • FontSize - Converts the current selection into an font size, which is declared as the second argument

  • ForeColor - Converts the fore color of the current selection into the color declared as the second argument

  • BackColor - Converts the background color of the current selection into the color declared as the second argument



All command identifiers with an argument that insert something around the current selection:


  • InsertOrderedList - Toggles the current selection between an ordered list and a normal text format, the second argument declared is the id

  • InsertUnorderedList - Toggles the current selection between an unordered list and a normal text format, the second argument declared is the id

  • InsertButton - Inserts a button around the current selection, the second argument declared is the id

  • InsertFieldset - Inserts a fieldset around the current selection, the second argument declared is the id

  • InsertIFrame - Inserts a iFrame around the current selection, the second argument declared is the id

  • InsertImage - Inserts a image around the current selection, the second argument declared is the source (e.g. http://www.example.com/images/img1.png)

  • InsertInputButton - Inserts a input button around the current selection, the second argument declared is the id

  • InsertInputCheckbox - Inserts a checkbox around the current selection, the second argument declared is the id

  • InsertInputFileUpload - Inserts a file upload input around the current selection

  • InsertFieldset - Inserts a fieldset around the current selection

  • InsertHorizontalRule - Inserts a horizontal rule around the current selection



All command identifiers that changes that do not fit in the above catagories:


  • 2D-Position - Allows absolutely positioned elements to be moved by dragging

  • LiveResize - Causes the rich text editor to update an element's appearance continuously during a resizing or moving operation, rather than updating only at the completion of the move or resize.

  • OverWrite - Toggles the cursor style from insert to regular

  • Print - Opens the standard print dialog to print the content of the rich text editor

  • Open - Opens the standard open dialog to open a file from the users computer in the rich text editor

  • SaveAs - Opens the standard open dialog to save the content of the rich text editor file onto the users computer

  • SelectAll - Selects the entire rich text editor content

  • Unselect - Unselects the current selection

  • MultipleSelection - Allows the user to select more than one selection using SHIFT or CTRL keys



Download



You can download the full sourcecode here:
http://www.webdevproject.com/zips/RichTextEditor.zip
Comments
Latest news
New project: Math Quiz
2010-11-09 18:07:48
Read more...

New project: Custom Messagebox
2010-11-06 19:10:50
Read more...

New project: Visitor Counter
2010-11-01 21:39:04
Read more...

New project: Image Editor
2010-11-01 21:37:27
Read more...

New project: Image Generator
2010-11-01 21:33:05
Read more...

Projects Contact © 2010 WDP - All rights reserved Terms and Conditions Log in