more styling
[roojs1] / Roo / htmleditor / KeyEnter.js
1
2 /**
3  * @class Roo.htmleditor.KeyEnter
4  * Handle Enter press..
5  * @cfg {Roo.HtmlEditorCore} core the editor.
6  * @constructor
7  * Create a new Filter.
8  * @param {Object} config Configuration options
9  */
10
11
12
13
14
15 Roo.htmleditor.KeyEnter = function(cfg) {
16     Roo.apply(this, cfg);
17     // this does not actually call walk as it's really just a abstract class
18  
19     Roo.get(this.core.doc.body).on('keypress', this.keypress, this);
20 }
21
22 //Roo.htmleditor.KeyEnter.i = 0;
23
24
25 Roo.htmleditor.KeyEnter.prototype = {
26     
27     core : false,
28     
29     keypress : function(e)
30     {
31         if (e.charCode != 13 && e.charCode != 10) {
32             Roo.log([e.charCode,e]);
33             return true;
34         }
35         e.preventDefault();
36         // https://stackoverflow.com/questions/18552336/prevent-contenteditable-adding-div-on-enter-chrome
37         var doc = this.core.doc;
38           //add a new line
39        
40     
41         var sel = this.core.getSelection();
42         var range = sel.getRangeAt(0);
43         var n = range.commonAncestorContainer;
44         var pc = range.closest([ 'ol', 'ul']);
45         var pli = range.closest('li');
46         if (!pc || e.ctrlKey) {
47             sel.insertNode('br', 'after'); 
48          
49             this.core.undoManager.addEvent();
50             this.core.fireEditorEvent(e);
51             return false;
52         }
53         
54         // deal with <li> insetion
55         if (pli.innerText.trim() == '' &&
56             pli.previousSibling &&
57             pli.previousSibling.nodeName == 'LI' &&
58             pli.previousSibling.innerText.trim() ==  '') {
59             pli.parentNode.removeChild(pli.previousSibling);
60             sel.cursorAfter(pc);
61             this.core.undoManager.addEvent();
62             this.core.fireEditorEvent(e);
63             return false;
64         }
65     
66         var li = doc.createElement('LI');
67         li.innerHTML = '&nbsp;';
68         if (!pli || !pli.firstSibling) {
69             pc.appendChild(li);
70         } else {
71             pli.parentNode.insertBefore(li, pli.firstSibling);
72         }
73         sel.cursorText (li.firstChild);
74       
75         this.core.undoManager.addEvent();
76         this.core.fireEditorEvent(e);
77
78         return false;
79         
80     
81         
82         
83          
84     }
85 };
86