Created
September 8, 2011 20:20
-
-
Save timjb/1204563 to your computer and use it in GitHub Desktop.
Rich-Text-Editing with YUI3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>Proper with YUI – Test</title> | |
| </head> | |
| <body> | |
| <p id="buttons"> | |
| <button id="code">Code</button> | |
| <button id="em">Em</button> | |
| <button id="strong">Strong</button> | |
| </p> | |
| <div id="editor"></div> | |
| <script src="http://yui.yahooapis.com/3.4.0/build/yui/yui-min.js"></script> | |
| <script> | |
| YUI().use('editor', function (Y) { | |
| var editor = new Y.EditorBase({ | |
| content: 'Dies ist <em>ein</em> Test <strong>von</strong> YUI\'s <code>EditorBase</code>.', | |
| extracss: 'code { border: 1px solid #aaa; background: #eee; font-family: monospace; }' | |
| }); | |
| function wrapWith(tag) { | |
| return function () { | |
| var inst = this.getInstance(); | |
| var selection = new inst.Selection(); | |
| console.log('wrapWith: '+tag); | |
| if (!selection.isCollapsed) { | |
| selection.wrapContent(tag); | |
| } else { | |
| selection.insertContent('<'+tag+'></'+tag+'>'); | |
| } | |
| }; | |
| } | |
| Y.mix(Y.Plugin.ExecCommand.COMMANDS, { | |
| code: wrapWith('code'), | |
| em: wrapWith('em'), | |
| strong: wrapWith('strong') | |
| }); | |
| editor.render('#editor'); | |
| Y.delegate('click', function (event) { | |
| var command = event.target.get('id'); | |
| editor.execCommand(command); | |
| editor.focus(); | |
| }, '#buttons', 'button'); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment