3 'modules/documentCanvas/canvas/documentElement',
4 'modules/documentCanvas/canvas/utils'
5 ], function($, documentElement, utils) {
21 var handleKey = function(event, canvas) {
22 handlers.some(function(handler) {
23 if(handles(handler, event) && handler[event.type]) {
24 handler[event.type](event, canvas);
30 var handles = function(handler, event) {
31 if(handler.key === event.which) {
34 if(handler.keys && handler.keys.indexOf(event.which) !== -1) {
41 var scroll = function(place, textElement) {
42 var rect = textElement.getBoundingClientRect(),
43 scroll = $('#rng-module-documentCanvas-contentWrapper'),
44 border = rect.bottom - (place === 'top' ? rect.height : 0) - scroll.offset().top + scroll[0].scrollTop,
45 visible = scroll[0].scrollTop + {top: 0, bottom: scroll.height()}[place],
49 if(place === 'top' && (border - padding < visible)) {
50 toScroll = border - visible - padding;
51 } else if(place === 'bottom' && (border + padding > visible)) {
52 toScroll = border - visible + padding;
55 scroll[0].scrollTop = scroll[0].scrollTop + toScroll;
60 var getLastRectAbove = function(node, y) {
61 var rects = node.getClientRects(),
64 while((rect = rects[idx])) {
75 var getFirstRectBelow = function(node, y) {
76 var rects = node.getClientRects(),
79 while((rect = rects[idx])) {
92 handlers.push({key: KEYS.ENTER,
93 keydown: function(event, canvas) {
94 event.preventDefault();
95 var cursor = canvas.getCursor(),
96 position = cursor.getPosition(),
97 element = position.element;
99 if(Object.keys(cursor.getPosition()).length === 0) {
100 var currentElement = canvas.getCurrentNodeElement();
101 if(currentElement && !currentElement.wlxmlNode.isRoot()) {
102 canvas.wlxmlDocument.transaction(function() {
103 var added = currentElement.wlxmlNode.after({
104 tagName: currentElement.wlxmlNode.getTagName() || 'div',
105 attrs: {'class': currentElement.wlxmlNode.getClass() || 'p'}
107 added.append({text:''});
111 description: gettext('Splitting text')
113 success: function(ret) {
114 canvas.setCurrentElement(utils.getElementForNode(ret), {caretTo: 'start'});
122 if(!cursor.isSelecting()) {
124 if(element instanceof documentElement.DocumentTextElement) {
125 element = element.parent();
128 canvas.wlxmlDocument.transaction(function() {
129 var added = element.wlxmlNode.after(
130 {tagName: element.wlxmlNode.getTagName() || 'div', attrs: {'class': element.wlxmlNode.getClass() || 'p'}}
132 added.append({text: ''});
136 description: gettext('Splitting text')
138 success: function(ret) {
139 canvas.setCurrentElement(utils.getElementForNode(ret), {caretTo: 'start'});
145 if(!(element.parent().parent())) {
146 return false; // top level element is unsplittable
149 var node = position.element.wlxmlNode,
150 result, goto, gotoOptions;
152 node.document.transaction(function() {
153 result = position.element.wlxmlNode.breakContent({offset: position.offset});
156 description: gettext('Splitting text')
160 if(result.emptyText) {
161 goto = result.emptyText;
164 goto = result.second;
165 gotoOptions = {caretTo: 'start'};
168 canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
175 var selectsWholeTextElement = function(cursor) {
176 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd) {
182 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
183 keydown: function(event, canvas) {
184 var cursor = canvas.getCursor(),
185 position = canvas.getCursor().getPosition(),
186 element = position.element,
187 node = element ? element.wlxmlNode : null,
192 if(!element || !node) {
196 if(event.which === KEYS.DELETE) {
201 if(cursor.isSelecting()) {
202 event.preventDefault();
203 var start = cursor.getSelectionStart(),
204 end = cursor.getSelectionEnd();
206 if(direction === 'above') {
207 if(start.offsetAtBeginning) {
208 goto = canvas.getNearestTextElement('above', start.element);
211 goto = start.element;
212 caretTo = start.offset;
215 if(end.offsetAtEnd) {
216 goto = canvas.getNearestTextElement('below', start.element);
224 canvas.wlxmlDocument.deleteText({
226 node: start.element.wlxmlNode,
230 node: end.element.wlxmlNode,
235 canvas.setCurrentElement(goto, {caretTo: caretTo});
240 var cursorAtOperationEdge = position.offsetAtBeginning;
241 if(event.which === KEYS.DELETE) {
242 cursorAtOperationEdge = position.offsetAtEnd;
245 var willDeleteWholeText = function() {
246 return element.getText().length === 1 || selectsWholeTextElement(cursor);
249 canvas.wlxmlDocument.transaction(function() {
250 if(willDeleteWholeText()) {
251 event.preventDefault();
254 else if(cursorAtOperationEdge) {
255 if(direction === 'below') {
256 element = canvas.getNearestTextElement(direction, element);
258 if(element && element.wlxmlNode.getIndex() === 0) {
259 goto = element.wlxmlNode.parent().moveUp();
261 canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
264 event.preventDefault();
268 description: gettext('Remove text')
274 var handleKeyEvent = function(e, s) {
275 keyEventHandlers.some(function(handler) {
276 if(handler.applies(e, s)) {
282 // todo: whileRemoveWholetext
283 var keyEventHandlers = [
285 applies: function(e, s) {
288 s.type === 'textSelection' &&
289 s.startsAtBeginning() &&
298 applies: function(e, s) {
299 return e.key === KEYS.ARROW_UP && s.type === 'caret';
301 run: function(e, s) {
303 var caretRect = window.getSelection().getRangeAt(0).getClientRects()[0],
304 frameRects = s.element.dom[0].getClientRects(),
305 caretTop = caretRect.bottom - caretRect.height,
306 position, target,rect, scrolled;
309 if((frameRects[0].bottom === caretRect.bottom) || (caretRect.left < frameRects[0].left)) {
311 s.canvas.rootWrapper.find('[document-text-element]').each(function() {
312 var test = getLastRectAbove(this, caretTop);
321 scrolled = scroll('top', target);
322 position = utils.caretPositionFromPoint(caretRect.left, rect.bottom - 1 - scrolled);
323 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
327 scrolled = scroll('top', target);
328 var left = caretRect.left;
329 if(left > rect.left + rect.width) {
330 left = rect.left + rect.width;
331 } else if(left < rect.left ) {
334 position = utils.caretPositionFromPoint(left, rect.bottom - 1 - scrolled);
335 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
340 applies: function(e, s) {
341 return e.key === KEYS.ARROW_DOWN && s.type === 'caret';
343 run: function(e, s) {
345 var caretRect = window.getSelection().getRangeAt(0).getClientRects()[0],
346 frameRects = s.element.dom[0].getClientRects(),
347 lastRect = frameRects[frameRects.length-1],
348 position, target,rect, scrolled;
350 if(lastRect.bottom === caretRect.bottom || (caretRect.left > lastRect.left + lastRect.width)) {
352 s.canvas.rootWrapper.find('[document-text-element]').each(function() {
353 var test = getFirstRectBelow(this, caretRect.bottom);
361 scrolled = scroll('bottom', target);
362 position = utils.caretPositionFromPoint(caretRect.left, rect.top +1 - scrolled);
363 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
367 scrolled = scroll('bottom', target);
368 var left = caretRect.left;
369 if(left > rect.left + rect.width) {
370 left = rect.left + rect.width;
371 } else if(left < rect.left ) {
374 position = utils.caretPositionFromPoint(left, rect.top +1 - scrolled);
375 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
380 applies: function(e, s) {
381 return e.key === KEYS.ARROW_LEFT && s.type === 'caret';
383 run: function(e, s) {
389 prev = s.canvas.getPreviousTextElement(s.element);
391 scroll('top', prev.dom[0]);
392 s.canvas.setCurrentElement(s.canvas.getDocumentElement(prev.dom.contents()[0]), {caretTo: 'end'});
398 applies: function(e, s) {
399 return e.key === KEYS.ARROW_RIGHT && s.type === 'caret';
401 run: function(e, s) {
406 next = s.canvas.getNextTextElement(s.element);
408 scroll('bottom', next.dom[0]);
409 s.canvas.setCurrentElement(s.canvas.getDocumentElement(next.dom.contents()[0]), {caretTo: 0});
412 var secondToLast = (s.offset === s.element.wlxmlNode.getText().length -1);
414 // Only Flying Spaghetti Monster knows why this is need for FF (for versions at least 26 to 31)
416 s.canvas.setCurrentElement(s.element, {caretTo: 'end'});
422 applies: function(e, s) {
423 return s.type === 'caret' &&
424 s.element.wlxmlNode.parent().is({tagName: 'span'}) &&
425 s.element.wlxmlNode.getText().length === 1 &&
427 (e.key === KEYS.BACKSPACE);
429 run: function(e, s) {
431 prevTextNode = s.element.canvas.getPreviousTextElement(s.element).wlxmlNode;
433 s.element.wlxmlNode.parent().detach(params);
434 s.canvas.setCurrentElement(
435 (params.ret && params.ret.mergedTo) || prevTextNode,
436 {caretTo: params.ret ? params.ret.previousLen : (prevTextNode ? prevTextNode.getText().length : 0)});
440 applies: function(e, s) {
441 return s.type === 'caret' && (
442 (s.isAtBeginning() && e.key === KEYS.BACKSPACE) ||
443 (s.isAtEnd() && e.key === KEYS.DELETE)
447 var direction, caretTo, cursorAtOperationEdge, goto, element;
449 if(e.key === KEYS.BACKSPACE) {
452 cursorAtOperationEdge = s.isAtBeginning();
458 cursorAtOperationEdge = s.isAtEnd();
459 element = cursorAtOperationEdge && s.canvas.getNearestTextElement(direction, s.element);
462 if(!cursorAtOperationEdge || !element) {
468 s.canvas.wlxmlDocument.transaction(function() {
469 if(element.wlxmlNode.getIndex() === 0) {
470 goto = element.wlxmlNode.parent().moveUp();
472 goto = element.wlxmlNode.moveUp();
475 s.canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
479 description: gettext('Remove text')
486 applies: function(e,s) {
487 return s.type === 'caret' && s.element.getText().length === 1 && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE);
491 e.element.wlxmlNode.setText('');
492 s.canvas.setCurrentElement(s.element, {caretTo: 0});
497 applies: function(e, s) {
498 return s.type === 'textSelection' && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE);
500 run: function(e, s) {
501 var direction = 'above',
505 if(e.key === KEYS.DELETE) {
512 if(s.startsAtBeginning && s.endsAtEnd && s.startElement.sameNode(s.endElement)) {
513 goto = s.startElement;
514 caretTo = s.startOffset;
515 } else if(direction === 'above') {
516 if(s.startsAtBeginning()) {
517 goto = s.canvas.getNearestTextElement('above', s.startElement);
520 goto = s.startElement;
521 caretTo = s.startOffset;
525 goto = s.canvas.getNearestTextElement('below', s.startElement);
533 var doc = s.canvas.wlxmlDocument;
534 doc.transaction(function() {
538 node: s.startElement.wlxmlNode,
539 offset: s.startOffset
542 node: s.endElement.wlxmlNode,
548 success: function() {
550 s.canvas.setCurrentElement(goto, {caretTo: caretTo});
558 applies: function(e, s) {
559 return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement();
561 run: function(e, s) {
562 var result, goto, gotoOptions;
565 s.canvas.wlxmlDocument.transaction(function() {
566 result = s.element.wlxmlNode.breakContent({offset: s.offset});
569 description: gettext('Splitting text'),
570 fragment: s.toDocumentFragment()
574 if(result.emptyText) {
575 goto = result.emptyText;
578 goto = result.second;
579 gotoOptions = {caretTo: 'start'};
582 s.canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
588 handleKey: handleKey,
589 handleKeyEvent: handleKeyEvent,