editor-para.js revision c30005d1ab01ecd568065cec0254c82fd88cd587
2223N/AYUI.add('editor-para', function(Y) {
2223N/A
2223N/A
2223N/A /**
2223N/A * Plugin for Editor to paragraph auto wrapping and correction.
2223N/A * @module editor
2223N/A * @submodule editor-para
2223N/A */
2223N/A /**
2223N/A * Plugin for Editor to paragraph auto wrapping and correction.
2223N/A * @class Plugin.EditorPara
2223N/A * @extends Base
2223N/A * @constructor
2223N/A */
2223N/A
2223N/A
2223N/A var EditorPara = function() {
2223N/A EditorPara.superclass.constructor.apply(this, arguments);
2223N/A }, HOST = 'host', BODY = 'body', NODE_CHANGE = 'nodeChange',
2223N/A FIRST_P = BODY + ' > p';
2223N/A
2223N/A Y.extend(EditorPara, Y.Base, {
2223N/A /**
2223N/A * Utility method to create an empty paragraph when the document is empty.
2223N/A * @private
2223N/A * @method _fixFirstPara
2223N/A */
2223N/A _fixFirstPara: function() {
2223N/A var host = this.get(HOST), inst = host.getInstance(), sel;
2223N/A inst.one('body').setContent('<p>' + inst.Selection.CURSOR + '</p>');
2976N/A sel = new inst.Selection();
2223N/A sel.focusCursor(true, false);
2223N/A },
/**
* nodeChange handler to handle fixing an empty document.
* @private
* @method _onNodeChange
*/
_onNodeChange: function(e) {
var host = this.get(HOST), inst = host.getInstance();
switch (e.changedType) {
case 'keydown':
if (inst.config.doc.childNodes.length < 2) {
var cont = inst.config.doc.body.innerHTML;
if (cont && cont.length < 5 && cont.toLowerCase() == '<br>') {
this._fixFirstPara();
}
}
break;
case 'backspace-up':
case 'backspace-down':
case 'delete-up':
if (!Y.UA.ie) {
var ps = inst.all(FIRST_P), br, item, html;
item = inst.one(BODY);
if (ps.item(0)) {
item = ps.item(0);
}
br = item.one('br');
if (br) {
br.removeAttribute('id');
br.removeAttribute('class');
}
html = item.get('innerHTML');
if (inst.Selection.getText(item) === '' && !item.test('p')) {
this._fixFirstPara();
} else if (item.test('p') && (html.length === 0) || (html == '<span><br></span>')) {
e.changedEvent.frameEvent.halt();
}
}
break;
}
},
/**
* Performs a block element filter when the Editor is first ready
* @private
* @method _afterEditorReady
*/
_afterEditorReady: function() {
var host = this.get(HOST), inst = host.getInstance();
if (inst) {
inst.Selection.filterBlocks();
}
},
/**
* Performs a block element filter when the Editor after an content change
* @private
* @method _afterContentChange
*/
_afterContentChange: function() {
var host = this.get(HOST), inst = host.getInstance();
if (inst && inst.Selection) {
inst.Selection.filterBlocks();
}
},
/**
* Performs block/paste filtering after paste.
* @private
* @method _afterPaste
*/
_afterPaste: function() {
var host = this.get(HOST), inst = host.getInstance(),
sel = new inst.Selection();
Y.later(50, host, function() {
inst.Selection.filterBlocks();
});
},
initializer: function() {
var host = this.get(HOST);
host.on(NODE_CHANGE, Y.bind(this._onNodeChange, this));
host.after('ready', Y.bind(this._afterEditorReady, this));
host.after('contentChange', Y.bind(this._afterContentChange, this));
if (Y.Env.webkit) {
host.after('dom:paste', Y.bind(this._afterPaste, this));
}
}
}, {
/**
* editorPara
* @static
* @property NAME
*/
NAME: 'editorPara',
/**
* editorPara
* @static
* @property NS
*/
NS: 'editorPara',
ATTRS: {
host: {
value: false
}
}
});
Y.namespace('Plugin');
Y.Plugin.EditorPara = EditorPara;
}, '1.0.0', {requires: ['editor-base', 'selection']});