Create your own advanced WYSIWYG editor [part 2]

javascript & Ajax discussions and tutorials of javascript developing

Create your own advanced WYSIWYG editor [part 2]

Postby Designer » Sun Sep 14, 2008 8:07 pm

Create your own advanced WYSIWYG editor [Part 2]
Expanding upon what we have learned in Part 1, this tutorial will go into adding the ability to format text, attach images, add URLs, and change font colours within our WYSIWYG editor. Please feel free to ask questions and post suggestions to this tutorial.

Let's Begin

Adding additional formatting functions
Revisiting part 1 we are able to bold our text using the following code:
Code: Select all
function bold()
    {
    Editor.execCommand('bold', false, null);
    }

Now the common question is what about all the other possible text formats such as italic and underline as well as editor functions to undo or redo a command. We are able to use the pre-defined execCommand function on our Editor element to apply these operations. Please observe the following sample function that will save us time by allowing us to access all operations through the argument.
Code: Select all
function dowithtext(text) {
    Editor.execCommand(text,false,null);
}

One Function with One Argument vs. Several Functions no Arguments

How do we use this function
Code: Select all
<a href=#" onClick=dowithtext('bold')">Add Bold</a>


In our coding we will use the following buttons in our WYSIWYG editor:
Code: Select all
<img src=images/undo.gif" onClick=dowithtext('Undo')" />
<img src=images/redo.gif" onClick=dowithtext('Redo')" />
<img src=images/bold.gif" onClick=dowithtext('bold')" />
<img src=images/italic.gif" onClick=dowithtext('italic')" />
<img src=images/underline.gif" onClick=dowithtext('underline')" />

You can see the example below as to how our editor will look in a browser:

Please feel free to use different buttons, text links, etc... in your editor.

Adding a URL
We will now go over how to add a URL to our WYSIWYG editor. We will be creating a new function that will prompt the user for the URL using the prompt function and again we'll use the execCommand to create the hyperlink in our Editor element. Please check out the following code:
Code: Select all
    function createURL() { 
	 var szURL=prompt("Enter a URL:", "http://");
	    if ((szURL != null) && (szURL != "")) {
	 Editor.execCommand("CreateLink",false,szURL);
		}
	 }


We can deploy this function through the same method we used for the buttons above. Please see the screenshot found later on in this article to see how our button appears.

Add an Image
We will use the same algorithm for the URL function to create the image in our Editor element. At this time we do not provide a method for users to upload an image; therefore, this is going to allow the user to provide the URL to the image. Please see the following code:
Code: Select all
function insertimage() { 
	   imagePath=prompt('Enter Image URL:', 'http://');
	   if ((imagePath != null) && (imagePath != "")) {
	   Editor.execCommand('InsertImage', false, imagePath);
    }
}


Buttons for URL and Image Functions
Please see the following code that we'll use for our buttons:
Code: Select all
<img src=images/link.gif" onClick=createURL()" />
<img src=images/insertimage.gif" onClick=insertimage()" />


Choosing a Font Colour / Add a Colour Pallet
We will now be creating the function that will allow us to apply a color to our font within the Editor element. Please download the following compressed attachment (tar.gz - Unzip through WinRar for Win32 users):
images.tar.gz
Colour Picker with JavaScript and Images Attached. Will need to extract with WinRar for Windows users. Linux users can use "tar -zxvf <filename>" through a Terminal.
(20.01 KiB) Downloaded 140 times

Please include the JavaScript file from the compressed attachment into the Editor document:
Code: Select all
<script language=JavaScript" src=jscolor.js"></script>

We can use this library with the following code:
Code: Select all
<input onChange=Colour(this.value)" />

This element will call upon the Colour function each time we change our focus within the element such as moving the mouse to a different colour will give us the code for that colour. You can see the last screenshot of this part that includes the Colour Picker, Add URL, and Add Image:


Whew! Part 2 has been completed. Please see the following code for what we have up till now:
Code: Select all
    <html>
    <head>
	  <script language="JavaScript">
	  window.onload=function() {
	  Editor=document.getElementById('box').contentWindow.document;
	  Editor.designMode= "on";
	  }
	  function dowithtext(text)
	   {
		Editor.execCommand(text, false, null);
	   }
		  function createURL() {
	    var szURL=prompt("Enter a URL:", "http://");
		  if ((szURL != null) && (szURL != "")) {
		Editor.execCommand("CreateLink",false,szURL);
		  }
	    }
	   function insertimage() {
		imagePath=prompt('Enter Image URL:', 'http://');
		if ((imagePath != null) && (imagePath != "")) {
		Editor.execCommand('InsertImage', false, imagePath);
	   }
    }
	   function Colour(colour) { Editor.execCommand("color",false, colour); }
	  </script>
    </head>
    <body>
    <img src="images/undo.gif" onClick="dowithtext('Undo')" />
    <img src="images/redo.gif" onClick="dowithtext('Redo')" />
    <img src="images/bold.gif" onClick="dowithtext('bold')" />
    <img src="images/italic.gif" onClick="dowithtext('italic')" />
    <img src="images/underline.gif" onClick="dowithtext('underline')" />
    <img src="images/link.gif" onClick="createURL()" />
    <img src="images/insertimage.gif" onClick="insertimage()" />
    <script language="JavaScript" src="jscolor.js"></script>
    <input onChange="Colour(this.value)" />
    <br/>
    <iframe width="500" scrolling="auto" height="120" id="box"></iframe>
    </body>
    </html>


Please stay tuned for Part 3 of our Create your own advanced WYSIWYG editor tutorial series. We will go into advanced text formatting, discuss browser-compatibility issues with the colour picker, and connecting our editor with the PHP backend.

Thank you all for your time!

Support spread the tutorial :

Digg it
http://digg.com/programming/Create_your ... G_editor_2



Stumble it
http://almsamim.stumbleupon.com/



http://delicious.com/almsamim
Last edited by Designer on Sun Dec 07, 2008 8:39 am, edited 11 times in total.
Reason: making CreateURL and insertimage cross-browser
Almsamim.com
Developing for a better web
User avatar
Designer
Member
Member
Global Moderator
Global Moderator
 
Mood: Inspired
Posts: 506
Joined: Thu Sep 11, 2008 11:52 pm
Location: Faculty of Medicine
skills: Programming & Design
Gender: male
phpBB knowledge: Intermediate

Postby funky_fresh33 » Sun Sep 14, 2008 11:48 pm

please check out the parameter called "InsertHtml" it doesn't exist in Command Identifiers

the whole example is good but plz solve this problem i mentioned it

i can't add URL or even Images | i'm using IE 7 :mrgreen:
Think-Dare-Share
PHP absolute beginner
funky_fresh33
New
New
 
Posts: 9
Joined: Sun Sep 14, 2008 3:19 pm
skills: ..
Gender: male
phpBB knowledge: None

Re:

Postby Designer » Mon Sep 15, 2008 12:38 am

funky_fresh33 wrote:please check out the parameter called "InsertHtml" it doesn't exist in Command Identifiers

the whole example is good but plz solve this problem i mentioned it

i can't add URL or even Images | i'm using IE 7 :mrgreen:

thanks i solved the problem , ;)
it was from using inserthtml
so i used only createLink and InsertImage instead , and it worked well on IE6 , FF2 ,3

Code: Select all
    function createURL() { 
	  var szURL = prompt("Enter a URL:", "http://");
   	  if ((szURL != null) && (szURL != "")) {
      Editor.execCommand("CreateLink",false,szURL);
		  }
	  } 
    function insertimage() { 
		imagePath = prompt('Enter Image URL:', 'http://');
		if ((imagePath != null) && (imagePath != "")) {
		Editor.execCommand('InsertImage', false, imagePath);
    }
}
Almsamim.com
Developing for a better web
User avatar
Designer
Member
Member
Global Moderator
Global Moderator
 
Mood: Inspired
Posts: 506
Joined: Thu Sep 11, 2008 11:52 pm
Location: Faculty of Medicine
skills: Programming & Design
Gender: male
phpBB knowledge: Intermediate

Re: Re:

Postby funky_fresh33 » Mon Sep 15, 2008 8:52 am

admin wrote:thanks i solved the problem , ;)
it was from using inserthtml
so i used only createLink and InsertImage instead , and it worked well on IE6 , FF2 ,3

Code: Select all
    function createURL() { 
	  var szURL = prompt("Enter a URL:", "http://");
   	  if ((szURL != null) && (szURL != "")) {
      Editor.execCommand("CreateLink",false,szURL);
		  }
	  } 
    function insertimage() { 
		imagePath = prompt('Enter Image URL:', 'http://');
		if ((imagePath != null) && (imagePath != "")) {
		Editor.execCommand('InsertImage', false, imagePath);
    }
}


it works now

lol :lol:
Think-Dare-Share
PHP absolute beginner
funky_fresh33
New
New
 
Posts: 9
Joined: Sun Sep 14, 2008 3:19 pm
skills: ..
Gender: male
phpBB knowledge: None

Re: Create your own advanced WYSIWYG editor [part 2]

Postby necer_cheniki » Wed Sep 17, 2008 3:36 pm

very very cool
I applied the first two parts tutorial ,and every think works correctly :mrgreen:
This is a mazing Editor thank you for the tutorials .
but there is a question witch floor me : where is execCommand Method it is pre-defined in our browsers or what ?
Last edited by necer_cheniki on Wed Sep 17, 2008 3:39 pm, edited 1 time in total.
necer_cheniki
New
New
 
Posts: 14
Joined: Tue Sep 16, 2008 12:12 pm

Postby Designer » Wed Sep 17, 2008 3:38 pm

execCommand is pre-defined in nearly all browsers but there is diffrence in commands
Almsamim.com
Developing for a better web
User avatar
Designer
Member
Member
Global Moderator
Global Moderator
 
Mood: Inspired
Posts: 506
Joined: Thu Sep 11, 2008 11:52 pm
Location: Faculty of Medicine
skills: Programming & Design
Gender: male
phpBB knowledge: Intermediate

Re: Create your own advanced WYSIWYG editor [part 2]

Postby necer_cheniki » Wed Sep 17, 2008 3:45 pm

Good,tank you very much ,
Now I have to complete the 3rd Part :D
necer_cheniki
New
New
 
Posts: 14
Joined: Tue Sep 16, 2008 12:12 pm


Return to Javascript & Ajax

Who is online

Users browsing this forum: No registered users and 0 guests

cron