Create your own advanced WYSIWYG editor [part 3]

javascript & Ajax discussions and tutorials of javascript developing

Create your own advanced WYSIWYG editor [part 3]

Postby Designer » Mon Sep 15, 2008 12:39 pm

Create your own advanced WYSIWYG editor [Part 3]
This is the third of the six part tutorial explaining how to create your own advanced WYSIWYG editor. In this chapter we will go into explaining how to handle problems with IE and our color problems; in addition, we will also add the justify to our editor, and we'll end with a hidden input element that we'll pass to our PHP backend.

Let's Begin

Colour problems with IE
Every almsamim (web developer) goes through the tribulations of creating a website that works with IE. The other browsers for the most part do not cause an issue as they'll follow the designated standards created. This blurb of the tutorial will face one of the IE problems relating to the colour picker as seen in the screenshot provided:
Thankfully IE has a proprietary condition that allows us to alternate between the code that works for other browsers to code that works specifically for IE. Please see the following example:
Code: Select all
<!--[if IE]>
<img src="images/color.gif" onClick="Colour(clr.value)" />
<![endif]-->

The IE end-user will now we able to choose the colour initially and then click on colour image to modify the font colour of our selected text as seen in the following screenshot:

Adding alignments to our editor
As we have done with the previous functions as seen in Part 2 we will just have to add the following buttons to our list of buttons defined:
Code: Select all
<img src=images/justifyleft.gif" onClick="dowithtext('justifyleft')" />
<img src="images/justifycenter.gif" onClick="dowithtext('justifycenter')" />
<img src="images/justifyright.gif" onClick="dowithtext('justifyright')" />


Adding an unlink button (an extra tip)
This was not mentioned in our introduction, but this is for you that read through the tutorial. The following sample of code will allow you to unlink URLs:
Code: Select all
<img src="images/unlink.gif" onClick="dowithtext('Unlink')" />
Now we have the following editor:
Please feel free to check out the following list of commands that you can create for your editor: List of Commands. If you have problems with the commands then reply to this thread and we'll help you solve your problem!

Submission of Text
Once we've completed our editing the most important part comes into action, submission. We need to be able to send the content that we've generated to a location to be saved. We will create a new function that will allow us to retrieve the content from our iframe:
Code: Select all
  function Gettext() {
	   var text=document.getElementById('text');
	    text.value=Editor.body.innerHTML;
  } 

In addition to the JavaScript added we will need to add a hidden element and a submit button to our HTML form.
Code: Select all
 <input type=hidden" />
<input type="submit" onClick="Gettext()" />



Accessing the text in PHP
If you are familiar with PHP then you know that we can access POST data easily. If you're not familiar follow this example:
Code: Select all
<?=$_POST[text]?>
In PHP we have an array, that stores all the data sent to the script, called $_POST. We will use the name of the hidden element to reference the appropriate data in the array hence the above example.

Recap on our Editor
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("forecolor",false, colour); }
  function Gettext() {
	   var text=document.getElementById('text');
	    text.value=Editor.body.innerHTML;
  }
	</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/unlink.gif" onClick="dowithtext('Unlink')" />
<img src="images/insertimage.gif" onClick="insertimage()" />
<script language="JavaScript" src="images/jscolor.js"></script>
<input onChange="Colour(this.value)" />
<!--[if IE]>
<img src="images/hv120x69.png" width="20" height="20" onClick="Colour(clr.value)" />
<![endif]-->
<img src="images/justifyleft.gif" onClick="dowithtext('justifyleft')" />
<img src="images/justifycenter.gif" onClick="dowithtext('justifycenter')" />
<img src="images/justifyright.gif" onClick="dowithtext('justifyright')" />
<br/>
<iframe width="500" id="box" name="box" height="120"></iframe>
<br />
<form action="php.php" method="post">
<input type="hidden" />
<input type="submit" onClick="Gettext()" />
</form>
</body>
</html> 
You can also download the code from the following attachment:
images.tar.gz
copywriting / formatting
(20.01 KiB) Downloaded 131 times


Thank you for your time and remember to reply if you have a question or suggestion.
[b]Stay tuned forPART 4!!

[/b]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
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 3]

Postby I am rage » Wed Sep 17, 2008 7:51 pm

This is a god sent according to my beliefe.
Thank you thank you.
If (spelling==false){
echo "sorry... I´m from Sweden :-* :roll: ";
}else{
echo"I can spell :-)";
}
I am rage
New
New
 
Posts: 7
Joined: Wed Sep 17, 2008 7:45 pm
Location: Sweden
skills: PHP , mysql

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

Postby Designer » Wed Sep 17, 2008 9:02 pm

I am rage wrote:This is a god sent according to my beliefe.
Thank you thank you.
If (spelling==false){
echo "sorry... I´m from Sweden :-* :roll: ";
}else{
echo"I can spell :-)";
}


i hope you enjoyed the tutorial
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 » Wed Sep 17, 2008 11:25 pm

nice and cool tutorial buddy

you are doing well, go on

yours,
funky_fresh33
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 3]

Postby I am rage » Fri Sep 19, 2008 8:46 pm

Hi again.
I cannot seem to get the colour schema to show..?
Is there a path that has to be written?
To be specific, in the colour picker 'box', when I click it the frame appears with the slider
but no colours to show or other .png. :?:
I am rage
New
New
 
Posts: 7
Joined: Wed Sep 17, 2008 7:45 pm
Location: Sweden
skills: PHP , mysql

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

Postby Designer » Fri Sep 19, 2008 11:18 pm

I am rage wrote:Hi again.
I cannot seem to get the colour schema to show..?
Is there a path that has to be written?
To be specific, in the colour picker 'box', when I click it the frame appears with the slider
but no colours to show or other .png. :?:

put on your javascript file this function
Code: Select all
// print colour palette in a table
function colorPalette(dirwidthheight)
{
    var 
000;
    var 
numberList = new Array(6);
    var 
color '';
    
numberList[0] = '00';
    
numberList[1] = '40';
    
numberList[2] = '80';
    
numberList[3] = 'BF';
    
numberList[4] = 'FF';
    
document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
    for (
05r++)
    {
        if (
dir == 'h')
        {
            
document.writeln('<tr>');
        }

        for (
05g++)
        {
            if (
dir == 'v')
            {
                
document.writeln('<tr>');
            }
            for (
05b++)
            {
                
color String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
                
document.write('<td bgcolor="#' color '" style="width: ' width 'px; height: ' height 'px;">');
                
document.write('<a href="#" onClick="Colour(\'#' color '\'); return false;"><img src="images/spacer.gif" width="' width '" height="' height '" alt="#' color '" title="#' color '" /></a>');
                
document.writeln('</td>');
            }
            if (
dir == 'v')
            {
                
document.writeln('</tr>');
            }
        }
        if (
dir == 'h')
        {
            
document.writeln('</tr>');
        }
    }
    
document.writeln('</table>');

this function print a table with colors
and then in your html file add
Code: Select all
<script language="JavaScript">
colorPalette('h', 15, 10);
</script>


then this color palette is very nice and cross-browser that will be disscused in part 4 ;)
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 3]

Postby I am rage » Sat Sep 20, 2008 9:03 am

You are the MASTER.
I´m such a newbe :D
Thanks.
I am rage
New
New
 
Posts: 7
Joined: Wed Sep 17, 2008 7:45 pm
Location: Sweden
skills: PHP , mysql

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

Postby Tyson » Wed Sep 24, 2008 2:47 pm

Hey good work so far. Just thought I'd mention that no color.gif image is included in the .tar file attached to this post. Keep it up!
Tyson
Tyson
New
New
 
Posts: 4
Joined: Wed Sep 24, 2008 2:44 pm

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

Postby Designer » Wed Sep 24, 2008 3:40 pm

Tyson wrote:Hey good work so far. Just thought I'd mention that no color.gif image is included in the .tar file attached to this post. Keep it up!

thanks tyson ;)
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 3]

Postby StevenJ » Mon Dec 08, 2008 3:48 pm

Hi,

anyone any idea how I can get the contents of the iFrame in PHP?


<?=$_POST [text]?>

this is not correct, you can't access the iframe contents through the $_POST array.

I am posting my form

<form id="InsertArticle" name="InsertArticle" method="POST" action="php.php">
<iframe width="500" height="120" id="Article_Title_Box" name="Article_Title_Box" ></iframe>
</form>


any idea how I can really access the contents of the iframe?
StevenJ
New
New
 
Posts: 3
Joined: Mon Dec 08, 2008 3:35 pm
skills: PHP, HTML, CSS, DHTML
Gender: male
phpBB knowledge: None

Next

Return to Javascript & Ajax

Who is online

Users browsing this forum: No registered users and 1 guest