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);
174 handlers.push({keys: [KEYS.ARROW_UP],
175 keydown: function(event, canvas) {
177 var element = canvas.getCursor().getPosition().element,
178 caretRect = window.getSelection().getRangeAt(0).getClientRects()[0],
179 frameRects = element.dom[0].getClientRects(),
180 caretTop = caretRect.bottom - caretRect.height,
181 position, target,rect, scrolled;
184 if((frameRects[0].bottom === caretRect.bottom) || (caretRect.left < frameRects[0].left)) {
185 event.preventDefault();
186 canvas.rootWrapper.find('[document-text-element]').each(function() {
187 var test = getLastRectAbove(this, caretTop);
196 scrolled = scroll('top', target);
197 position = utils.caretPositionFromPoint(caretRect.left, rect.bottom - 1 - scrolled);
198 canvas.setCurrentElement(canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
204 handlers.push({keys: [KEYS.ARROW_DOWN],
205 keydown: function(event, canvas) {
207 var element = canvas.getCursor().getPosition().element,
208 caretRect = window.getSelection().getRangeAt(0).getClientRects()[0],
209 frameRects = element.dom[0].getClientRects(),
210 lastRect = frameRects[frameRects.length-1],
211 position, target,rect, scrolled;
213 if(lastRect.bottom === caretRect.bottom || (caretRect.left > lastRect.left + lastRect.width)) {
214 event.preventDefault();
215 canvas.rootWrapper.find('[document-text-element]').each(function() {
216 var test = getFirstRectBelow(this, caretRect.bottom);
224 scrolled = scroll('bottom', target);
225 position = utils.caretPositionFromPoint(caretRect.left, rect.top +1 - scrolled);
226 canvas.setCurrentElement(canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
232 handlers.push({keys: [KEYS.ARROW_LEFT],
233 keydown: function(event, canvas) {
235 var position = canvas.getCursor().getPosition(),
236 element = position.element,
239 if(position.offset === 0) {
240 event.preventDefault();
241 prev = canvas.getPreviousTextElement(element);
243 scroll('top', prev.dom[0]);
244 canvas.setCurrentElement(canvas.getDocumentElement(prev.dom.contents()[0]), {caretTo: 'end'});
250 handlers.push({keys: [KEYS.ARROW_RIGHT],
251 keydown: function(event, canvas) {
253 var position = canvas.getCursor().getPosition(),
254 element = position.element,
256 if(position.offsetAtEnd) {
257 event.preventDefault();
258 next = canvas.getNextTextElement(element);
260 scroll('bottom', next.dom[0]);
261 canvas.setCurrentElement(canvas.getDocumentElement(next.dom.contents()[0]), {caretTo: 0});
264 var secondToLast = (position.offset === element.wlxmlNode.getText().length -1);
266 // Only Flying Spaghetti Monster knows why this is need for FF (for versions at least 26 to 31)
267 event.preventDefault();
268 canvas.setCurrentElement(element, {caretTo: 'end'});
275 var selectsWholeTextElement = function(cursor) {
276 if(cursor.isSelecting() && cursor.getSelectionStart().offsetAtBeginning && cursor.getSelectionEnd().offsetAtEnd) {
282 handlers.push({key: KEYS.X,
283 keydown: function(event, canvas) {
284 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor())) {
285 event.preventDefault();
290 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
291 keydown: function(event, canvas) {
292 var cursor = canvas.getCursor(),
293 position = canvas.getCursor().getPosition(),
294 element = position.element,
295 node = element ? element.wlxmlNode : null,
300 if(!element || !node) {
304 if(event.which === KEYS.DELETE) {
309 if(cursor.isSelecting()) {
310 event.preventDefault();
311 var start = cursor.getSelectionStart(),
312 end = cursor.getSelectionEnd();
314 if(direction === 'above') {
315 if(start.offsetAtBeginning) {
316 goto = canvas.getNearestTextElement('above', start.element);
319 goto = start.element;
320 caretTo = start.offset;
323 if(end.offsetAtEnd) {
324 goto = canvas.getNearestTextElement('below', start.element);
332 canvas.wlxmlDocument.deleteText({
334 node: start.element.wlxmlNode,
338 node: end.element.wlxmlNode,
343 canvas.setCurrentElement(goto, {caretTo: caretTo});
348 var cursorAtOperationEdge = position.offsetAtBeginning;
349 if(event.which === KEYS.DELETE) {
350 cursorAtOperationEdge = position.offsetAtEnd;
353 var willDeleteWholeText = function() {
354 return element.getText().length === 1 || selectsWholeTextElement(cursor);
357 canvas.wlxmlDocument.transaction(function() {
358 if(willDeleteWholeText()) {
359 event.preventDefault();
362 else if(cursorAtOperationEdge) {
363 if(direction === 'below') {
364 element = canvas.getNearestTextElement(direction, element);
366 if(element && element.wlxmlNode.getIndex() === 0) {
367 goto = element.wlxmlNode.parent().moveUp();
369 canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
372 event.preventDefault();
376 description: gettext('Remove text')
382 var handleKeyEvent = function(e, s) {
383 keyEventHandlers.some(function(handler) {
384 if(handler.applies(e, s)) {
390 // todo: whileRemoveWholetext
391 var keyEventHandlers = [
393 applies: function(e, s) {
394 return s.type === 'caret' &&
395 s.element.wlxmlNode.parent().is({tagName: 'span'}) &&
396 s.element.wlxmlNode.getText().length === 1 &&
398 (e.key === KEYS.BACKSPACE);
400 run: function(e, s) {
402 prevTextNode = s.element.canvas.getPreviousTextElement(s.element).wlxmlNode;
404 s.element.wlxmlNode.parent().detach(params);
405 s.canvas.setCurrentElement(
406 (params.ret && params.ret.mergedTo) || prevTextNode,
407 {caretTo: params.ret ? params.ret.previousLen : (prevTextNode ? prevTextNode.getText().length : 0)});
411 applies: function(e, s) {
412 return s.type === 'caret' && (
413 (s.isAtBeginning() && e.key === KEYS.BACKSPACE) ||
414 (s.isAtEnd() && e.key === KEYS.DELETE)
418 var direction, caretTo, cursorAtOperationEdge, goto, element;
420 if(e.key === KEYS.BACKSPACE) {
423 cursorAtOperationEdge = s.isAtBeginning();
429 cursorAtOperationEdge = s.isAtEnd();
430 element = cursorAtOperationEdge && s.canvas.getNearestTextElement(direction, s.element);
433 if(!cursorAtOperationEdge || !element) {
439 s.canvas.wlxmlDocument.transaction(function() {
440 if(element.wlxmlNode.getIndex() === 0) {
441 goto = element.wlxmlNode.parent().moveUp();
443 goto = element.wlxmlNode.moveUp();
446 s.canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
450 description: gettext('Remove text')
457 applies: function(e,s) {
458 return s.type === 'caret' && s.element.getText().length === 1 && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE);
462 e.element.wlxmlNode.setText('');
463 s.canvas.setCurrentElement(s.element, {caretTo: 0});
468 applies: function(e, s) {
469 return s.type === 'textSelection' && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE);
471 run: function(e, s) {
472 var direction = 'above',
476 if(e.key === KEYS.DELETE) {
483 if(s.startsAtBeginning && s.endsAtEnd && s.startElement.sameNode(s.endElement)) {
484 goto = s.startElement;
485 caretTo = s.startOffset;
486 } else if(direction === 'above') {
487 if(s.startsAtBeginning()) {
488 goto = s.canvas.getNearestTextElement('above', s.startElement);
491 goto = s.startElement;
492 caretTo = s.startOffset;
496 goto = s.canvas.getNearestTextElement('below', s.startElement);
504 var doc = s.canvas.wlxmlDocument;
505 doc.transaction(function() {
509 node: s.startElement.wlxmlNode,
510 offset: s.startOffset
513 node: s.endElement.wlxmlNode,
519 success: function() {
521 s.canvas.setCurrentElement(goto, {caretTo: caretTo});
529 applies: function(e, s) {
530 return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement();
532 run: function(e, s) {
533 var result, goto, gotoOptions;
536 s.canvas.wlxmlDocument.transaction(function() {
537 result = s.element.wlxmlNode.breakContent({offset: s.offset});
540 description: gettext('Splitting text'),
541 fragment: s.toDocumentFragment()
545 if(result.emptyText) {
546 goto = result.emptyText;
549 goto = result.second;
550 gotoOptions = {caretTo: 'start'};
553 s.canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
559 handleKey: handleKey,
560 handleKeyEvent: handleKeyEvent,