cf969586b4e653fcf919c3d865ee00c834cda7b7
[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             // on it list, or ctrl pressed.
48             if (!e.ctrlKey) {
49                 sel.insertNode('br', 'after'); 
50             } else {
51                 // only do this if we have ctrl key..
52                 var br = doc.createElement('br');
53                 br.className = 'clear';
54                 br.setAttribute('style', 'clear:all');
55                 sel.insertNode(br, 'after'); 
56             }
57             
58          
59             this.core.undoManager.addEvent();
60             this.core.fireEditorEvent(e);
61             return false;
62         }
63         
64         // deal with <li> insetion
65         if (pli.innerText.trim() == '' &&
66             pli.previousSibling &&
67             pli.previousSibling.nodeName == 'LI' &&
68             pli.previousSibling.innerText.trim() ==  '') {
69             pli.parentNode.removeChild(pli.previousSibling);
70             sel.cursorAfter(pc);
71             this.core.undoManager.addEvent();
72             this.core.fireEditorEvent(e);
73             return false;
74         }
75     
76         var li = doc.createElement('LI');
77         li.innerHTML = '&nbsp;';
78         if (!pli || !pli.firstSibling) {
79             pc.appendChild(li);
80         } else {
81             pli.parentNode.insertBefore(li, pli.firstSibling);
82         }
83         sel.cursorText (li.firstChild);
84       
85         this.core.undoManager.addEvent();
86         this.core.fireEditorEvent(e);
87
88         return false;
89         
90     
91         
92         
93          
94     }
95 };
96