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(!cursor.isSelecting()) {
49 var added = element.after({tag: 'block'});
50 added.append({text:''});
51 canvas.setCurrentElement(added, {caretTo: 'start'});
55 if(!(element.parent().parent())) {
56 return false; // top level element is unsplittable
59 var elements = position.element.split({offset: position.offset}),
64 if(position.offsetAtBeginning)
65 newEmpty = elements.first;
66 else if(position.offsetAtEnd)
67 newEmpty = elements.second;
70 goto = newEmpty.append(documentElement.DocumentTextElement.create({text: ''}, this));
73 goto = elements.second;
74 gotoOptions = {caretTo: 'start'};
77 canvas.setCurrentElement(goto, gotoOptions);
83 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
84 keydown: function(event, canvas) {
85 var position = canvas.getCursor().getPosition(),
86 element = position.element;
87 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
88 var direction, caretTo;
89 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
96 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
97 canvas.setCurrentElement(el, {caretTo: caretTo});
100 keyup: function(event, canvas) {
101 var element = canvas.getCursor().getPosition().element,
106 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
113 element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
115 canvas.setCurrentElement(element, {caretTo: caretTo});
120 var selectsWholeTextElement = function(cursor) {
121 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd)
126 handlers.push({key: KEYS.X,
127 keydown: function(event, canvas) {
128 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor()))
129 event.preventDefault();
133 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
134 keydown: function(event, canvas) {
135 var cursor = canvas.getCursor(),
136 position = canvas.getCursor().getPosition(),
137 element = position.element;
139 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
140 event.preventDefault();
144 var cursorAtOperationEdge = position.offsetAtBeginning;
145 if(event.which === KEYS.DELETE) {
146 cursorAtOperationEdge = position.offsetAtEnd;
149 var willDeleteWholeText = function() {
150 return element.getText().length === 1 || selectsWholeTextElement(cursor);
153 if(willDeleteWholeText()) {
154 event.preventDefault();
157 else if(element.isEmpty()) {
159 var direction = 'above',
162 if(event.which === KEYS.DELETE) {
167 event.preventDefault();
169 var parent = element.parent(),
170 grandParent = parent ? parent.parent() : null,
172 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
173 if(grandParent && grandParent.children().length === 1) {
174 goto = grandParent.append({text: ''});
176 goto = element.getNearestTextElement(direction);
180 goto = element.getNearestTextElement(direction);
183 canvas.setCurrentElement(goto, {caretTo: caretTo});
184 canvas.publisher('contentChanged');
186 else if(cursorAtOperationEdge) {
188 event.preventDefault();