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}),
82 if(position.offsetAtBeginning)
83 newEmpty = nodes.first;
84 else if(position.offsetAtEnd)
85 newEmpty = nodes.second;
88 goto = newEmpty.append({text: ''});
92 gotoOptions = {caretTo: 'start'};
95 canvas.setCurrentElement(utils.findCanvasElement(goto), gotoOptions);
101 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
102 keydown: function(event, canvas) {
103 var position = canvas.getCursor().getPosition(),
104 element = position.element;
105 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
106 var direction, caretTo;
107 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
114 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
115 canvas.setCurrentElement(el, {caretTo: caretTo});
118 keyup: function(event, canvas) {
119 var element = canvas.getCursor().getPosition().element,
124 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
131 element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
133 canvas.setCurrentElement(element, {caretTo: caretTo});
138 var selectsWholeTextElement = function(cursor) {
139 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd)
144 handlers.push({key: KEYS.X,
145 keydown: function(event, canvas) {
146 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor()))
147 event.preventDefault();
151 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
152 keydown: function(event, canvas) {
153 var cursor = canvas.getCursor(),
154 position = canvas.getCursor().getPosition(),
155 element = position.element;
157 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
158 event.preventDefault();
162 var cursorAtOperationEdge = position.offsetAtBeginning;
163 if(event.which === KEYS.DELETE) {
164 cursorAtOperationEdge = position.offsetAtEnd;
167 var willDeleteWholeText = function() {
168 return element.getText().length === 1 || selectsWholeTextElement(cursor);
171 if(willDeleteWholeText()) {
172 event.preventDefault();
175 else if(element.isEmpty()) {
177 var direction = 'above',
180 if(event.which === KEYS.DELETE) {
185 event.preventDefault();
187 var parent = element.parent(),
188 grandParent = parent ? parent.parent() : null,
190 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
191 if(grandParent && grandParent.children().length === 1) {
192 goto = grandParent.append({text: ''});
194 goto = element.getNearestTextElement(direction);
198 goto = element.getNearestTextElement(direction);
201 canvas.setCurrentElement(goto, {caretTo: caretTo});
202 canvas.publisher('contentChanged');
204 else if(cursorAtOperationEdge) {
206 event.preventDefault();