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