make sure to run all tests
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / keyboard.js
1 define([
2 'modules/documentCanvas/canvas/documentElement',
3 'modules/documentCanvas/canvas/utils'
4 ], function(documentElement, utils) {
5     
6 'use strict';
7
8
9 var KEYS = {
10     ENTER: 13,
11     ARROW_LEFT: 37,
12     ARROW_UP: 38,
13     ARROW_RIGHT: 39,
14     ARROW_DOWN: 40,
15     BACKSPACE: 8,
16     DELETE: 46,
17     X: 88
18 };
19
20 var handleKey = function(event, canvas) {
21     handlers.some(function(handler) {
22         if(handles(handler, event) && handler[event.type]) {
23             handler[event.type](event, canvas);
24             return true;
25         }
26     });
27 };
28
29 var handles = function(handler, event) {
30     if(handler.key === event.which)
31         return true;
32     if(handler.keys && handler.keys.indexOf(event.which) !== -1)
33         return true;
34     return false;
35 };
36
37 var handlers = [];
38
39
40 handlers.push({key: KEYS.ENTER,
41     keydown: function(event, canvas) {
42         event.preventDefault();
43         var cursor = canvas.getCursor(),
44             position = cursor.getPosition(),
45             element = position.element;
46
47         if(Object.keys(cursor.getPosition()).length === 0) {
48             var currentElement = canvas.getCurrentNodeElement();
49             if(currentElement) {
50                 var added = currentElement.after({tag: currentElement.getWlxmlTag() || 'div', klass: currentElement.getWlxmlClass() || 'p'});
51                 added.append({text:''});
52                 canvas.setCurrentElement(added, {caretTo: 'start'});
53             }
54             return;
55         }
56
57         if(!cursor.isSelecting()) {
58             if(event.ctrlKey) {
59                 var added = element.after({tag: element.getWlxmlTag() || 'div', klass: element.getWlxmlClass() || 'p'});
60                 added.append({text:''});
61                 canvas.setCurrentElement(added, {caretTo: 'start'});
62
63             } else {
64
65                 if(!(element.parent().parent())) {
66                     return false; // top level element is unsplittable
67                 }
68
69                 var elements = position.element.split({offset: position.offset}),
70                     newEmpty,
71                     goto,
72                     gotoOptions;
73
74                 if(position.offsetAtBeginning)
75                     newEmpty = elements.first;
76                 else if(position.offsetAtEnd)
77                     newEmpty = elements.second;
78                 
79                 if(newEmpty) {
80                     goto = newEmpty.append(documentElement.DocumentTextElement.create({text: ''}, this));
81                     gotoOptions = {};
82                 } else {
83                     goto = elements.second;
84                     gotoOptions = {caretTo: 'start'};
85                 }
86
87                 canvas.setCurrentElement(goto, gotoOptions);
88             }
89         }
90     }
91 });
92
93 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
94     keydown: function(event, canvas) {
95         var position = canvas.getCursor().getPosition(),
96             element = position.element;
97         if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
98             var direction, caretTo;
99             if(event.which === KEYS.ARROW_LEFT  || event.which === KEYS.ARROW_UP) {
100                 direction = 'above';
101                 caretTo = 'end';
102             } else {
103                 direction = 'below';
104                 caretTo = 'start';
105             }
106             var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
107             canvas.setCurrentElement(el, {caretTo: caretTo});
108         }
109     },
110     keyup: function(event, canvas) {
111         var element = canvas.getCursor().getPosition().element,
112             caretTo = false;
113         if(!element) {
114             // Chrome hack
115             var direction;
116             if(event.which === KEYS.ARROW_LEFT  || event.which === KEYS.ARROW_UP) {
117                 direction = 'above';
118                 caretTo = 'end';
119             } else {
120                 direction = 'below';
121                 caretTo = 'start';
122             }
123             element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
124         }
125         canvas.setCurrentElement(element, {caretTo: caretTo});
126     }
127 });
128
129
130 var selectsWholeTextElement = function(cursor) {
131     if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd)
132         return true;
133     return false;
134 }
135
136 handlers.push({key: KEYS.X,
137     keydown: function(event, canvas) {
138         if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor()))
139             event.preventDefault();
140     }
141 });
142
143 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
144     keydown: function(event, canvas) {
145         var cursor = canvas.getCursor(),
146             position = canvas.getCursor().getPosition(),
147             element = position.element;
148
149         if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
150             event.preventDefault();
151             return;
152         }
153             
154         var cursorAtOperationEdge = position.offsetAtBeginning;
155         if(event.which === KEYS.DELETE) {
156             cursorAtOperationEdge = position.offsetAtEnd;
157         }
158
159         var willDeleteWholeText = function() {
160             return element.getText().length === 1 || selectsWholeTextElement(cursor);
161         }
162
163         if(willDeleteWholeText()) {
164             event.preventDefault();
165             element.setText('');
166         }
167         else if(element.isEmpty()) {
168
169             var direction = 'above',
170                 caretTo = 'end';
171                 
172             if(event.which === KEYS.DELETE) {
173                 direction = 'below';
174                 caretTo = 'start';
175             }
176
177             event.preventDefault();
178
179             var parent = element.parent(),
180                 grandParent = parent ? parent.parent() : null,
181                 goto;
182             if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
183                 if(grandParent && grandParent.children().length === 1) {
184                     goto = grandParent.append({text: ''});
185                 } else {
186                     goto = element.getNearestTextElement(direction);
187                 }
188                 parent.detach();
189             } else {
190                 goto = element.getNearestTextElement(direction);
191                 element.detach();
192             }
193             canvas.setCurrentElement(goto, {caretTo: caretTo});
194             canvas.publisher('contentChanged');
195         }
196         else if(cursorAtOperationEdge) {
197             // todo
198             event.preventDefault();
199         }
200     }
201 });
202
203 return {
204     handleKey: handleKey
205 };
206
207 });