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 if(element instanceof documentElement.DocumentTextElement) {
60 element = element.parent();
62 var added = element.data('wlxmlNode').after(
63 {tagName: element.getWlxmlTag() || 'div', attrs: {'class': element.getWlxmlClass() || 'p'}}
65 added.append({text: ''});
66 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
70 if(!(element.parent().parent())) {
71 return false; // top level element is unsplittable
74 var elements = position.element.split({offset: position.offset}),
79 if(position.offsetAtBeginning)
80 newEmpty = elements.first;
81 else if(position.offsetAtEnd)
82 newEmpty = elements.second;
85 goto = newEmpty.append(documentElement.DocumentTextElement.create({text: ''}, this));
88 goto = elements.second;
89 gotoOptions = {caretTo: 'start'};
92 canvas.setCurrentElement(goto, gotoOptions);
98 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
99 keydown: function(event, canvas) {
100 var position = canvas.getCursor().getPosition(),
101 element = position.element;
102 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
103 var direction, caretTo;
104 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
111 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
112 canvas.setCurrentElement(el, {caretTo: caretTo});
115 keyup: function(event, canvas) {
116 var element = canvas.getCursor().getPosition().element,
121 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
128 element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
130 canvas.setCurrentElement(element, {caretTo: caretTo});
135 var selectsWholeTextElement = function(cursor) {
136 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd)
141 handlers.push({key: KEYS.X,
142 keydown: function(event, canvas) {
143 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor()))
144 event.preventDefault();
148 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
149 keydown: function(event, canvas) {
150 var cursor = canvas.getCursor(),
151 position = canvas.getCursor().getPosition(),
152 element = position.element;
154 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
155 event.preventDefault();
159 var cursorAtOperationEdge = position.offsetAtBeginning;
160 if(event.which === KEYS.DELETE) {
161 cursorAtOperationEdge = position.offsetAtEnd;
164 var willDeleteWholeText = function() {
165 return element.getText().length === 1 || selectsWholeTextElement(cursor);
168 if(willDeleteWholeText()) {
169 event.preventDefault();
172 else if(element.isEmpty()) {
174 var direction = 'above',
177 if(event.which === KEYS.DELETE) {
182 event.preventDefault();
184 var parent = element.parent(),
185 grandParent = parent ? parent.parent() : null,
187 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
188 if(grandParent && grandParent.children().length === 1) {
189 goto = grandParent.append({text: ''});
191 goto = element.getNearestTextElement(direction);
195 goto = element.getNearestTextElement(direction);
198 canvas.setCurrentElement(goto, {caretTo: caretTo});
199 canvas.publisher('contentChanged');
201 else if(cursorAtOperationEdge) {
203 event.preventDefault();