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: ''});
93 gotoOptions = {caretTo: 'start'};
96 canvas.setCurrentElement(utils.findCanvasElement(goto), gotoOptions);
102 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
103 keydown: function(event, canvas) {
104 var position = canvas.getCursor().getPosition(),
105 element = position.element;
106 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
107 var direction, caretTo;
108 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
115 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
116 canvas.setCurrentElement(el, {caretTo: caretTo});
119 keyup: function(event, canvas) {
120 var element = canvas.getCursor().getPosition().element,
125 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
132 element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
134 canvas.setCurrentElement(element, {caretTo: caretTo});
139 var selectsWholeTextElement = function(cursor) {
140 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd)
145 handlers.push({key: KEYS.X,
146 keydown: function(event, canvas) {
147 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor()))
148 event.preventDefault();
152 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
153 keydown: function(event, canvas) {
154 var cursor = canvas.getCursor(),
155 position = canvas.getCursor().getPosition(),
156 element = position.element;
158 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
159 event.preventDefault();
163 var cursorAtOperationEdge = position.offsetAtBeginning;
164 if(event.which === KEYS.DELETE) {
165 cursorAtOperationEdge = position.offsetAtEnd;
168 var willDeleteWholeText = function() {
169 return element.getText().length === 1 || selectsWholeTextElement(cursor);
172 if(willDeleteWholeText()) {
173 event.preventDefault();
176 else if(element.isEmpty()) {
178 var direction = 'above',
181 if(event.which === KEYS.DELETE) {
186 event.preventDefault();
188 var parent = element.parent(),
189 grandParent = parent ? parent.parent() : null,
191 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
192 if(grandParent && grandParent.children().length === 1) {
193 goto = grandParent.data('wlxmlNode').append({text: ''});
195 goto = element.getNearestTextElement(direction);
197 parent.data('wlxmlNode').detach();
199 goto = element.getNearestTextElement(direction);
200 element.data('wlxmlNode').detach();
202 canvas.setCurrentElement(goto, {caretTo: caretTo});
203 canvas.publisher('contentChanged');
205 else if(cursorAtOperationEdge) {
207 event.preventDefault();