2 'modules/documentCanvas/canvas/documentElement',
3 'modules/documentCanvas/canvas/utils'
4 ], function(documentElement, utils) {
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);
29 var handles = function(handler, event) {
30 if(handler.key === event.which) {
33 if(handler.keys && handler.keys.indexOf(event.which) !== -1) {
42 handlers.push({key: KEYS.ENTER,
43 keydown: function(event, canvas) {
44 event.preventDefault();
45 var cursor = canvas.getCursor(),
46 position = cursor.getPosition(),
47 element = position.element,
50 if(Object.keys(cursor.getPosition()).length === 0) {
51 var currentElement = canvas.getCurrentNodeElement();
53 canvas.wlxmlDocument.startTransaction();
54 added = currentElement.data('wlxmlNode').after({
55 tag: currentElement.getWlxmlTag() || 'div',
56 attrs: {'class': currentElement.getWlxmlClass() || 'p'}
58 added.append({text:''});
59 canvas.wlxmlDocument.endTransaction();
60 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
65 if(!cursor.isSelecting()) {
67 if(element instanceof documentElement.DocumentTextElement) {
68 element = element.parent();
71 canvas.wlxmlDocument.startTransaction();
72 added = element.data('wlxmlNode').after(
73 {tagName: element.getWlxmlTag() || 'div', attrs: {'class': element.getWlxmlClass() || 'p'}}
75 added.append({text: ''});
76 canvas.wlxmlDocument.endTransaction();
77 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
81 if(!(element.parent().parent())) {
82 return false; // top level element is unsplittable
85 //var nodes = position.element.data('wlxmlNode').split({offset: position.offset}),
86 // var nodes = position.element.data('wlxmlNode').transform('split', {offset: position.offset}),
91 // if(position.offsetAtBeginning)
92 // newEmpty = nodes.first;
93 // else if(position.offsetAtEnd)
94 // newEmpty = nodes.second;
97 // //goto = newEmpty.append({text: ''});
100 // goto = nodes.second;
101 // gotoOptions = {caretTo: 'start'};
104 var result = position.element.data('wlxmlNode').breakContent({offset: position.offset}),
106 if(result.emptyText) {
107 goto = result.emptyText;
110 goto = result.second;
111 gotoOptions = {caretTo: 'start'};
114 canvas.setCurrentElement(utils.findCanvasElement(goto), gotoOptions);
120 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
121 keydown: function(event, canvas) {
122 var position = canvas.getCursor().getPosition(),
123 element = position.element;
124 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
125 var direction, caretTo;
126 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
133 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
134 canvas.setCurrentElement(el, {caretTo: caretTo});
137 keyup: function(event, canvas) {
138 var element = canvas.getCursor().getPosition().element,
143 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
151 element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
153 canvas.setCurrentElement(element, {caretTo: caretTo});
158 var selectsWholeTextElement = function(cursor) {
159 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd) {
165 handlers.push({key: KEYS.X,
166 keydown: function(event, canvas) {
167 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor())) {
168 event.preventDefault();
173 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
174 keydown: function(event, canvas) {
175 var cursor = canvas.getCursor(),
176 position = canvas.getCursor().getPosition(),
177 element = position.element,
178 node = element.data('wlxmlNode');
180 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
181 event.preventDefault();
185 var cursorAtOperationEdge = position.offsetAtBeginning;
186 if(event.which === KEYS.DELETE) {
187 cursorAtOperationEdge = position.offsetAtEnd;
190 var willDeleteWholeText = function() {
191 return element.getText().length === 1 || selectsWholeTextElement(cursor);
194 canvas.wlxmlDocument.startTransaction();
196 if(willDeleteWholeText()) {
197 event.preventDefault();
200 else if(element.isEmpty()) {
202 var direction = 'above',
205 if(event.which === KEYS.DELETE) {
210 event.preventDefault();
212 var parent = element.parent(),
213 grandParent = parent ? parent.parent() : null,
215 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
216 if(grandParent && grandParent.children().length === 1) {
217 goto = grandParent.data('wlxmlNode').append({text: ''});
219 goto = element.getNearestTextElement(direction);
221 parent.data('wlxmlNode').detach();
223 goto = element.getNearestTextElement(direction);
224 element.data('wlxmlNode').detach();
226 canvas.setCurrentElement(goto, {caretTo: caretTo});
227 canvas.publisher('contentChanged');
229 else if(cursorAtOperationEdge) {
231 event.preventDefault();
233 canvas.wlxmlDocument.endTransaction();