This example demonstrates how to toggle from a plain text field to the Rich Text Editor with a simple button click.
Setting up the Editor's HTML is done by creating a textarea
control on the page.
1 | <button type="button" id="toggleEditor">Toggle Editor</button> |
2 | <form method="post" action="#" id="form1"> |
3 | <textarea id="editor" name="editor" rows="20" cols="75"> |
4 | This is some more test text.<br>This is some more test text.<br>This is some more test text.<br> |
5 | This is some more test text.<br>This is some more test text.<br>This is some more test text. |
6 | </textarea> |
7 | </form> |
view plain | print | ? |
Once the textarea
is on the page, then initialize the Editor like this:
1 | (function() { |
2 | //Setup some private variables |
3 | var Dom = YAHOO.util.Dom, |
4 | Event = YAHOO.util.Event; |
5 | |
6 | //The Editor config |
7 | var myConfig = { |
8 | height: '300px', |
9 | width: '600px', |
10 | animate: true, |
11 | dompath: true, |
12 | focusAtStart: true |
13 | }; |
14 | |
15 | //Now let's load the Editor.. |
16 | var myEditor = new YAHOO.widget.Editor('editor', myConfig); |
17 | myEditor.render(); |
18 | })(); |
view plain | print | ? |
Now we will create a Button
control and attach a click
event to it.
From the click event we will determine the state of the editor (either "on" or "off"). Then we will choose to either hide or show it,
Note: It is not recommended to set the editor to display: none
. This causes certain browsers to lose the editor, you should use visibility: hidden
or top, left
(to move it out of the viewable area).
Before showing or hiding the Editor, we need to clean up the HTML we are using.
Switching from the Editor to the textarea, we need to strip the HTML from our output and replace all <br>
's with line breaks. This code snippet will handle this for our example. Your implementation may need a stronger approach.
Note: You "could" add a button to the toolbar and have it execute the toggle. Then remove these few lines from the example and you will have a source editor.
1 | //From the Editor to the textarea |
2 | var stripHTML = /<\S[^><]*>/g; |
3 | myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); |
4 | |
5 | //From the textarea to the Editor |
6 | myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); |
view plain | print | ? |
First we must call myEditor.saveHTML()
. This method will clean up the HTML in the Editor and return it to the textarea.
Once it is in the textarea, we will process it to remove all of the HTML and replace the <br>
's with line breaks.
Now using YAHOO.util.Dom we will set top
, left
and position: absolute
(this will keep the Editor from taking up page space) on the Editor elements containers firstChild (which is the container that holds the Rich Text Editor). Then we will set the textarea to hidden
.
1 | myEditor.saveHTML(); |
2 | var stripHTML = /<\S[^><]*>/g; |
3 | myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); |
4 | |
5 | var fc = myEditor.get('element').previousSibling, |
6 | el = myEditor.get('element'); |
7 | |
8 | Dom.setStyle(fc, 'position', 'absolute'); |
9 | Dom.setStyle(fc, 'top', '-9999px'); |
10 | Dom.setStyle(fc, 'left', '-9999px'); |
11 | myEditor.get('element_cont').removeClass('yui-editor-container'); |
12 | Dom.setStyle(el, 'visibility', 'visible'); |
13 | Dom.setStyle(el, 'top', ''); |
14 | Dom.setStyle(el, 'left', ''); |
15 | Dom.setStyle(el, 'position', 'static'); |
view plain | print | ? |
Using YAHOO.util.Dom we will set top: 0
, left: 0
and position: static
(to put the Editor back in the page) on the Editor elements containers firstChild (which is the container that holds the Rich Text Editor). Then we will set the textarea to visible
.
Then we call the Editor method _setDesignMode('on')
to re-enable designMode since it may have been lost with the visibility change.
Now we call the Editor method setEditorHTML()
passing it the value of the textarea with the line breaks all converted back to <br>
's.
1 | var fc = myEditor.get('element').previousSibling, |
2 | el = myEditor.get('element'); |
3 | |
4 | Dom.setStyle(fc, 'position', 'static'); |
5 | Dom.setStyle(fc, 'top', '0'); |
6 | Dom.setStyle(fc, 'left', '0'); |
7 | Dom.setStyle(el, 'visibility', 'hidden'); |
8 | Dom.setStyle(el, 'top', '-9999px'); |
9 | Dom.setStyle(el, 'left', '-9999px'); |
10 | Dom.setStyle(el, 'position', 'absolute'); |
11 | myEditor.get('element_cont').addClass('yui-editor-container'); |
12 | YAHOO.log('Reset designMode on the Editor', 'info', 'example'); |
13 | myEditor._setDesignMode('on'); |
14 | YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example'); |
15 | myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); |
view plain | print | ? |
1 | (function() { |
2 | var Dom = YAHOO.util.Dom, |
3 | Event = YAHOO.util.Event; |
4 | |
5 | YAHOO.log('Create Button Control (#toggleEditor)', 'info', 'example'); |
6 | var _button = new YAHOO.widget.Button('toggleEditor'); |
7 | _button.addClass('toggleEditor'); |
8 | |
9 | var myConfig = { |
10 | height: '300px', |
11 | width: '600px', |
12 | animate: true, |
13 | dompath: true, |
14 | focusAtStart: true |
15 | }; |
16 | |
17 | var state = 'on'; |
18 | YAHOO.log('Set state to on..', 'info', 'example'); |
19 | |
20 | YAHOO.log('Create the Editor..', 'info', 'example'); |
21 | var myEditor = new YAHOO.widget.Editor('editor', myConfig); |
22 | myEditor.render(); |
23 | |
24 | _button.on('click', function(ev) { |
25 | Event.stopEvent(ev); |
26 | if (state == 'on') { |
27 | YAHOO.log('state is on, so turn off', 'info', 'example'); |
28 | state = 'off'; |
29 | myEditor.saveHTML(); |
30 | YAHOO.log('Save the Editors HTML', 'info', 'example'); |
31 | var stripHTML = /<\S[^><]*>/g; |
32 | myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); |
33 | YAHOO.log('Strip the HTML markup from the string.', 'info', 'example'); |
34 | YAHOO.log('Set Editor container to position: absolute, top: -9999px, left: -9999px. Set textarea visible', 'info', 'example'); |
35 | |
36 | var fc = myEditor.get('element').previousSibling, |
37 | el = myEditor.get('element'); |
38 | |
39 | Dom.setStyle(fc, 'position', 'absolute'); |
40 | Dom.setStyle(fc, 'top', '-9999px'); |
41 | Dom.setStyle(fc, 'left', '-9999px'); |
42 | myEditor.get('element_cont').removeClass('yui-editor-container'); |
43 | Dom.setStyle(el, 'visibility', 'visible'); |
44 | Dom.setStyle(el, 'top', ''); |
45 | Dom.setStyle(el, 'left', ''); |
46 | Dom.setStyle(el, 'position', 'static'); |
47 | } else { |
48 | YAHOO.log('state is off, so turn on', 'info', 'example'); |
49 | state = 'on'; |
50 | YAHOO.log('Set Editor container to position: static, top: 0, left: 0. Set textarea to hidden', 'info', 'example'); |
51 | |
52 | var fc = myEditor.get('element').previousSibling, |
53 | el = myEditor.get('element'); |
54 | |
55 | Dom.setStyle(fc, 'position', 'static'); |
56 | Dom.setStyle(fc, 'top', '0'); |
57 | Dom.setStyle(fc, 'left', '0'); |
58 | Dom.setStyle(el, 'visibility', 'hidden'); |
59 | Dom.setStyle(el, 'top', '-9999px'); |
60 | Dom.setStyle(el, 'left', '-9999px'); |
61 | Dom.setStyle(el, 'position', 'absolute'); |
62 | myEditor.get('element_cont').addClass('yui-editor-container'); |
63 | YAHOO.log('Reset designMode on the Editor', 'info', 'example'); |
64 | myEditor._setDesignMode('on'); |
65 | YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example'); |
66 | myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); |
67 | } |
68 | }); |
69 | })(); |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 1156ms (+0) 5:14:57 PM:
example
Create the Editor..
INFO 1156ms (+3) 5:14:57 PM:
example
Set state to on..
INFO 1153ms (+1153) 5:14:57 PM:
example
Create Button Control (#toggleEditor)
INFO 0ms (+0) 5:14:56 PM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings