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)
32 if(handler.keys && handler.keys.indexOf(event.which) !== -1)
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;
47 if(Object.keys(cursor.getPosition()).length === 0) {
48 var currentElement = canvas.getCurrentNodeElement();
50 var added = currentElement.data('wlxmlNode').after({
51 tag: currentElement.getWlxmlTag() || 'div',
52 attrs: {'class': currentElement.getWlxmlClass() || 'p'}
54 added.append({text:''});
55 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
60 if(!cursor.isSelecting()) {
62 if(element instanceof documentElement.DocumentTextElement) {
63 element = element.parent();
65 var added = element.data('wlxmlNode').after(
66 {tagName: element.getWlxmlTag() || 'div', attrs: {'class': element.getWlxmlClass() || 'p'}}
68 added.append({text: ''});
69 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
73 if(!(element.parent().parent())) {
74 return false; // top level element is unsplittable
77 //var nodes = position.element.data('wlxmlNode').split({offset: position.offset}),
78 // var nodes = position.element.data('wlxmlNode').transform('split', {offset: position.offset}),
83 // if(position.offsetAtBeginning)
84 // newEmpty = nodes.first;
85 // else if(position.offsetAtEnd)
86 // newEmpty = nodes.second;
89 // //goto = newEmpty.append({text: ''});
92 // goto = nodes.second;
93 // gotoOptions = {caretTo: 'start'};
96 var result = position.element.data('wlxmlNode').breakContent({offset: position.offset}),
98 if(result.emptyText) {
99 goto = result.createdEmpty;
102 goto = result.second;
103 gotoOptions = {caretTo: 'start'};
106 canvas.setCurrentElement(utils.findCanvasElement(goto), gotoOptions);
112 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
113 keydown: function(event, canvas) {
114 var position = canvas.getCursor().getPosition(),
115 element = position.element;
116 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
117 var direction, caretTo;
118 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
125 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
126 canvas.setCurrentElement(el, {caretTo: caretTo});
129 keyup: function(event, canvas) {
130 var element = canvas.getCursor().getPosition().element,
135 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
142 element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
144 canvas.setCurrentElement(element, {caretTo: caretTo});
149 var selectsWholeTextElement = function(cursor) {
150 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd)
155 handlers.push({key: KEYS.X,
156 keydown: function(event, canvas) {
157 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor()))
158 event.preventDefault();
162 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
163 keydown: function(event, canvas) {
164 var cursor = canvas.getCursor(),
165 position = canvas.getCursor().getPosition(),
166 element = position.element;
168 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
169 event.preventDefault();
173 var cursorAtOperationEdge = position.offsetAtBeginning;
174 if(event.which === KEYS.DELETE) {
175 cursorAtOperationEdge = position.offsetAtEnd;
178 var willDeleteWholeText = function() {
179 return element.getText().length === 1 || selectsWholeTextElement(cursor);
182 if(willDeleteWholeText()) {
183 event.preventDefault();
186 else if(element.isEmpty()) {
188 var direction = 'above',
191 if(event.which === KEYS.DELETE) {
196 event.preventDefault();
198 var parent = element.parent(),
199 grandParent = parent ? parent.parent() : null,
201 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
202 if(grandParent && grandParent.children().length === 1) {
203 goto = grandParent.data('wlxmlNode').append({text: ''});
205 goto = element.getNearestTextElement(direction);
207 parent.data('wlxmlNode').detach();
209 goto = element.getNearestTextElement(direction);
210 element.data('wlxmlNode').detach();
212 canvas.setCurrentElement(goto, {caretTo: caretTo});
213 canvas.publisher('contentChanged');
215 else if(cursorAtOperationEdge) {
217 event.preventDefault();