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 added = currentElement.data('wlxmlNode').after({
54 tag: currentElement.getWlxmlTag() || 'div',
55 attrs: {'class': currentElement.getWlxmlClass() || 'p'}
57 added.append({text:''});
58 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
63 if(!cursor.isSelecting()) {
65 if(element instanceof documentElement.DocumentTextElement) {
66 element = element.parent();
68 added = element.data('wlxmlNode').after(
69 {tagName: element.getWlxmlTag() || 'div', attrs: {'class': element.getWlxmlClass() || 'p'}}
71 added.append({text: ''});
72 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
76 if(!(element.parent().parent())) {
77 return false; // top level element is unsplittable
80 //var nodes = position.element.data('wlxmlNode').split({offset: position.offset}),
81 // var nodes = position.element.data('wlxmlNode').transform('split', {offset: position.offset}),
86 // if(position.offsetAtBeginning)
87 // newEmpty = nodes.first;
88 // else if(position.offsetAtEnd)
89 // newEmpty = nodes.second;
92 // //goto = newEmpty.append({text: ''});
95 // goto = nodes.second;
96 // gotoOptions = {caretTo: 'start'};
99 var result = position.element.data('wlxmlNode').breakContent({offset: position.offset}),
101 if(result.emptyText) {
102 goto = result.createdEmpty;
105 goto = result.second;
106 gotoOptions = {caretTo: 'start'};
109 canvas.setCurrentElement(utils.findCanvasElement(goto), gotoOptions);
115 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
116 keydown: function(event, canvas) {
117 var position = canvas.getCursor().getPosition(),
118 element = position.element;
119 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
120 var direction, caretTo;
121 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
128 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
129 canvas.setCurrentElement(el, {caretTo: caretTo});
132 keyup: function(event, canvas) {
133 var element = canvas.getCursor().getPosition().element,
138 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
146 element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
148 canvas.setCurrentElement(element, {caretTo: caretTo});
153 var selectsWholeTextElement = function(cursor) {
154 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd) {
160 handlers.push({key: KEYS.X,
161 keydown: function(event, canvas) {
162 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor())) {
163 event.preventDefault();
168 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
169 keydown: function(event, canvas) {
170 var cursor = canvas.getCursor(),
171 position = canvas.getCursor().getPosition(),
172 element = position.element;
174 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
175 event.preventDefault();
179 var cursorAtOperationEdge = position.offsetAtBeginning;
180 if(event.which === KEYS.DELETE) {
181 cursorAtOperationEdge = position.offsetAtEnd;
184 var willDeleteWholeText = function() {
185 return element.getText().length === 1 || selectsWholeTextElement(cursor);
188 if(willDeleteWholeText()) {
189 event.preventDefault();
192 else if(element.isEmpty()) {
194 var direction = 'above',
197 if(event.which === KEYS.DELETE) {
202 event.preventDefault();
204 var parent = element.parent(),
205 grandParent = parent ? parent.parent() : null,
207 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
208 if(grandParent && grandParent.children().length === 1) {
209 goto = grandParent.data('wlxmlNode').append({text: ''});
211 goto = element.getNearestTextElement(direction);
213 parent.data('wlxmlNode').detach();
215 goto = element.getNearestTextElement(direction);
216 element.data('wlxmlNode').detach();
218 canvas.setCurrentElement(goto, {caretTo: caretTo});
219 canvas.publisher('contentChanged');
221 else if(cursorAtOperationEdge) {
223 event.preventDefault();