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) {
33 if(handler.keys && handler.keys.indexOf(event.which) !== -1) {
42 handlers.push({key: KEYS.ENTER,
43 keydown: function(event, canvas) {
44 event.preventDefault();
45 var cursor = canvas.getCursor(),
46 position = cursor.getPosition(),
47 element = position.element,
50 if(Object.keys(cursor.getPosition()).length === 0) {
51 var currentElement = canvas.getCurrentNodeElement();
53 canvas.wlxmlDocument.startTransaction(gettext('Splitting text'));
54 added = currentElement.wlxmlNode.after({
55 tagName: currentElement.getWlxmlTag() || 'div',
56 attrs: {'class': currentElement.getWlxmlClass() || 'p'}
58 added.append({text:''});
59 canvas.wlxmlDocument.endTransaction();
60 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
65 if(!cursor.isSelecting()) {
67 if(element instanceof documentElement.DocumentTextElement) {
68 element = element.parent();
71 canvas.wlxmlDocument.startTransaction(gettext('Splitting text'));
72 added = element.wlxmlNode.after(
73 {tagName: element.getWlxmlTag() || 'div', attrs: {'class': element.getWlxmlClass() || 'p'}}
75 added.append({text: ''});
76 canvas.wlxmlDocument.endTransaction();
77 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
81 if(!(element.parent().parent())) {
82 return false; // top level element is unsplittable
85 //var nodes = position.element.data('wlxmlNode').split({offset: position.offset}),
86 // var nodes = position.element.data('wlxmlNode').transform('split', {offset: position.offset}),
91 // if(position.offsetAtBeginning)
92 // newEmpty = nodes.first;
93 // else if(position.offsetAtEnd)
94 // newEmpty = nodes.second;
97 // //goto = newEmpty.append({text: ''});
100 // goto = nodes.second;
101 // gotoOptions = {caretTo: 'start'};
103 var node = position.element.wlxmlNode,
104 result, goto, gotoOptions;
106 node.document.transaction(function() {
107 result = position.element.wlxmlNode.breakContent({offset: position.offset});
108 }, this, gettext('Splitting text'));
110 if(result.emptyText) {
111 goto = result.emptyText;
114 goto = result.second;
115 gotoOptions = {caretTo: 'start'};
118 canvas.setCurrentElement(utils.findCanvasElement(goto), gotoOptions);
124 handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT],
125 keydown: function(event, canvas) {
126 var position = canvas.getCursor().getPosition(),
127 element = position.element;
128 if(element && (element instanceof documentElement.DocumentTextElement) && element.isEmpty()) {
129 var direction, caretTo;
130 if(event.which === KEYS.ARROW_LEFT || event.which === KEYS.ARROW_UP) {
137 var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
139 canvas.setCurrentElement(el, {caretTo: caretTo});
143 keyup: function(event, canvas) {
144 var element = canvas.getCursor().getPosition().element,
149 var moves = [{direction: 'above', caretTo: 'end'}, {direction: 'below', caretTo: 'start'}];
150 if(event.which === KEYS.ARROW_RIGHT || event.which === KEYS.ARROW_DOWN) {
153 moves.some(function(move) {
155 var targetNode = utils.nearestInDocumentOrder('[document-text-element]:visible', move.direction, window.getSelection().focusNode);
157 element = canvas.getDocumentElement(targetNode);
158 caretTo = move.caretTo;
159 return true; // break
164 canvas.setCurrentElement(element, {caretTo: caretTo});
170 var selectsWholeTextElement = function(cursor) {
171 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd) {
177 handlers.push({key: KEYS.X,
178 keydown: function(event, canvas) {
179 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor())) {
180 event.preventDefault();
185 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
186 keydown: function(event, canvas) {
187 var cursor = canvas.getCursor(),
188 position = canvas.getCursor().getPosition(),
189 element = position.element,
190 node = element ? element.wlxmlNode : null,
195 if(!element || !node) {
199 if(event.which === KEYS.DELETE) {
204 if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
205 event.preventDefault();
206 var start = cursor.getSelectionStart(),
207 end = cursor.getSelectionEnd();
209 if(direction === 'above') {
210 if(start.offsetAtBeginning) {
211 goto = start.element.getNearestTextElement('above');
214 goto = start.element;
215 caretTo = start.offset;
218 if(end.offsetAtEnd) {
219 goto = start.element.getNearestTextElement('below');
227 canvas.wlxmlDocument.deleteText({
229 node: start.element.wlxmlNode,
233 node: end.element.wlxmlNode,
238 canvas.setCurrentElement(goto, {caretTo: caretTo});
243 var cursorAtOperationEdge = position.offsetAtBeginning;
244 if(event.which === KEYS.DELETE) {
245 cursorAtOperationEdge = position.offsetAtEnd;
248 var willDeleteWholeText = function() {
249 return element.getText().length === 1 || selectsWholeTextElement(cursor);
252 canvas.wlxmlDocument.transaction(function() {
253 if(willDeleteWholeText()) {
254 event.preventDefault();
257 else if(element.isEmpty()) {
258 event.preventDefault();
259 var parent = element.parent(),
260 grandParent = parent ? parent.parent() : null;
261 if(!grandParent && parent.children().length === 1) {
264 if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
265 if(grandParent && grandParent.children().length === 1) {
266 goto = grandParent.wlxmlNode.append({text: ''});
268 goto = element.getNearestTextElement(direction);
270 parent.wlxmlNode.detach();
272 goto = element.getNearestTextElement(direction);
273 element.wlxmlNode.detach();
275 canvas.setCurrentElement(goto, {caretTo: caretTo});
277 else if(cursorAtOperationEdge) {
278 if(direction === 'below') {
279 element = element.getNearestTextElement(direction);
282 goto = element.wlxmlNode.mergeContentUp();
283 canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
285 event.preventDefault();