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.after({tag: currentElement.getWlxmlTag() || 'div', klass: currentElement.getWlxmlClass() || 'p'});
51 added.append({text:''});
52 canvas.setCurrentElement(added, {caretTo: 'start'});
57 if(!cursor.isSelecting()) {
59 var added = element.after({tag: element.getWlxmlTag() || 'div', klass: element.getWlxmlClass() || 'p'});
60 added.append({text:''});
61 canvas.setCurrentElement(added, {caretTo: 'start'});
65 if(!(element.parent().parent())) {
66 return false; // top level element is unsplittable
69 var elements = position.element.split({offset: position.offset}),
74 if(position.offsetAtBeginning)
75 newEmpty = elements.first;
76 else if(position.offsetAtEnd)
77 newEmpty = elements.second;
80 goto = newEmpty.append(documentElement.DocumentTextElement.create({text: ''}, this));
83 goto = elements.second;
84 gotoOptions = {caretTo: 'start'};
87 canvas.setCurrentElement(goto, gotoOptions);
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) {
106 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
107 canvas.setCurrentElement(el, {caretTo: caretTo});
110 keyup: function(event, canvas) {
111 var element = canvas.getCursor().getPosition().element,
116 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
123 element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
125 canvas.setCurrentElement(element, {caretTo: caretTo});
130 var selectsWholeTextElement = function(cursor) {
131 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd)
136 handlers.push({key: KEYS.X,
137 keydown: function(event, canvas) {
138 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor()))
139 event.preventDefault();
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;
149 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
150 event.preventDefault();
154 var cursorAtOperationEdge = position.offsetAtBeginning;
155 if(event.which === KEYS.DELETE) {
156 cursorAtOperationEdge = position.offsetAtEnd;
159 var willDeleteWholeText = function() {
160 return element.getText().length === 1 || selectsWholeTextElement(cursor);
163 if(willDeleteWholeText()) {
164 event.preventDefault();
167 else if(element.isEmpty()) {
169 var direction = 'above',
172 if(event.which === KEYS.DELETE) {
177 event.preventDefault();
179 var parent = element.parent(),
180 grandParent = parent ? parent.parent() : null,
182 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
183 if(grandParent && grandParent.children().length === 1) {
184 goto = grandParent.append({text: ''});
186 goto = element.getNearestTextElement(direction);
190 goto = element.getNearestTextElement(direction);
193 canvas.setCurrentElement(goto, {caretTo: caretTo});
194 canvas.publisher('contentChanged');
196 else if(cursorAtOperationEdge) {
198 event.preventDefault();