2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('editor-br', function(Y) {
13 * Plugin for Editor to normalize BR's.
15 * @submodule editor-br
18 * Plugin for Editor to normalize BR's.
19 * @class Plugin.EditorBR
25 var EditorBR = function() {
26 EditorBR.superclass.constructor.apply(this, arguments);
27 }, HOST = 'host', LI = 'li';
30 Y.extend(EditorBR, Y.Base, {
32 * Frame keyDown handler that normalizes BR's when pressing ENTER.
36 _onKeyDown: function(e) {
41 if (e.keyCode == 13) {
42 var host = this.get(HOST), inst = host.getInstance(),
43 sel = new inst.Selection(),
48 if (!sel.anchorNode || (!sel.anchorNode.test(LI) && !sel.anchorNode.ancestor(LI))) {
49 sel._selection.pasteHTML('<br>');
50 sel._selection.collapse(false);
51 sel._selection.select();
56 if (!sel.anchorNode.test(LI) && !sel.anchorNode.ancestor(LI)) {
57 host.frame._execCommand('insertlinebreak', null);
65 * Adds listeners for keydown in IE and Webkit. Also fires insertbeonreturn for supporting browsers.
67 * @method _afterEditorReady
69 _afterEditorReady: function() {
70 var inst = this.get(HOST).getInstance();
72 inst.config.doc.execCommand('insertbronreturn', null, true);
75 if (Y.UA.ie || Y.UA.webkit) {
76 inst.on('keydown', Y.bind(this._onKeyDown, this), inst.config.doc);
80 * Adds a nodeChange listener only for FF, in the event of a backspace or delete, it creates an empy textNode
81 * inserts it into the DOM after the e.changedNode, then removes it. Causing FF to redraw the content.
83 * @method _onNodeChange
84 * @param {Event} e The nodeChange event.
86 _onNodeChange: function(e) {
87 switch (e.changedType) {
89 case 'backspace-down':
92 * This forced FF to redraw the content on backspace.
93 * On some occasions FF will leave a cursor residue after content has been deleted.
94 * Dropping in the empty textnode and then removing it causes FF to redraw and
95 * remove the "ghost cursors"
97 var inst = this.get(HOST).getInstance();
98 var d = e.changedNode;
99 var t = inst.config.doc.createTextNode(' ');
105 initializer: function() {
106 var host = this.get(HOST);
107 if (host.editorPara) {
108 Y.error('Can not plug EditorBR and EditorPara at the same time.');
111 host.after('ready', Y.bind(this._afterEditorReady, this));
113 host.on('nodeChange', Y.bind(this._onNodeChange, this));
136 Y.namespace('Plugin');
138 Y.Plugin.EditorBR = EditorBR;
142 }, '3.3.0' ,{requires:['node'], skinnable:false});