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({key: KEYS.X,
183 keydown: function(event, canvas) {
184 if(event.ctrlKey && selectsWholeTextElement(canvas.getCursor())) {
185 event.preventDefault();
190 handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
191 keydown: function(event, canvas) {
192 var cursor = canvas.getCursor(),
193 position = canvas.getCursor().getPosition(),
194 element = position.element,
195 node = element ? element.wlxmlNode : null,
200 if(!element || !node) {
204 if(event.which === KEYS.DELETE) {
209 if(cursor.isSelecting()) {
210 event.preventDefault();
211 var start = cursor.getSelectionStart(),
212 end = cursor.getSelectionEnd();
214 if(direction === 'above') {
215 if(start.offsetAtBeginning) {
216 goto = canvas.getNearestTextElement('above', start.element);
219 goto = start.element;
220 caretTo = start.offset;
223 if(end.offsetAtEnd) {
224 goto = canvas.getNearestTextElement('below', start.element);
232 canvas.wlxmlDocument.deleteText({
234 node: start.element.wlxmlNode,
238 node: end.element.wlxmlNode,
243 canvas.setCurrentElement(goto, {caretTo: caretTo});
248 var cursorAtOperationEdge = position.offsetAtBeginning;
249 if(event.which === KEYS.DELETE) {
250 cursorAtOperationEdge = position.offsetAtEnd;
253 var willDeleteWholeText = function() {
254 return element.getText().length === 1 || selectsWholeTextElement(cursor);
257 canvas.wlxmlDocument.transaction(function() {
258 if(willDeleteWholeText()) {
259 event.preventDefault();
262 else if(cursorAtOperationEdge) {
263 if(direction === 'below') {
264 element = canvas.getNearestTextElement(direction, element);
266 if(element && element.wlxmlNode.getIndex() === 0) {
267 goto = element.wlxmlNode.parent().moveUp();
269 canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
272 event.preventDefault();
276 description: gettext('Remove text')
282 var handleKeyEvent = function(e, s) {
283 keyEventHandlers.some(function(handler) {
284 if(handler.applies(e, s)) {
290 // todo: whileRemoveWholetext
291 var keyEventHandlers = [
293 applies: function(e, s) {
294 return e.key === KEYS.ARROW_UP && s.type === 'caret';
296 run: function(e, s) {
298 var caretRect = window.getSelection().getRangeAt(0).getClientRects()[0],
299 frameRects = s.element.dom[0].getClientRects(),
300 caretTop = caretRect.bottom - caretRect.height,
301 position, target,rect, scrolled;
304 if((frameRects[0].bottom === caretRect.bottom) || (caretRect.left < frameRects[0].left)) {
306 s.canvas.rootWrapper.find('[document-text-element]').each(function() {
307 var test = getLastRectAbove(this, caretTop);
316 scrolled = scroll('top', target);
317 position = utils.caretPositionFromPoint(caretRect.left, rect.bottom - 1 - scrolled);
318 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
324 applies: function(e, s) {
325 return e.key === KEYS.ARROW_DOWN && s.type === 'caret';
327 run: function(e, s) {
329 var caretRect = window.getSelection().getRangeAt(0).getClientRects()[0],
330 frameRects = s.element.dom[0].getClientRects(),
331 lastRect = frameRects[frameRects.length-1],
332 position, target,rect, scrolled;
334 if(lastRect.bottom === caretRect.bottom || (caretRect.left > lastRect.left + lastRect.width)) {
336 s.canvas.rootWrapper.find('[document-text-element]').each(function() {
337 var test = getFirstRectBelow(this, caretRect.bottom);
345 scrolled = scroll('bottom', target);
346 position = utils.caretPositionFromPoint(caretRect.left, rect.top +1 - scrolled);
347 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
353 applies: function(e, s) {
354 return e.key === KEYS.ARROW_LEFT && s.type === 'caret';
356 run: function(e, s) {
362 prev = s.canvas.getPreviousTextElement(s.element);
364 scroll('top', prev.dom[0]);
365 s.canvas.setCurrentElement(s.canvas.getDocumentElement(prev.dom.contents()[0]), {caretTo: 'end'});
371 applies: function(e, s) {
372 return e.key === KEYS.ARROW_RIGHT && s.type === 'caret';
374 run: function(e, s) {
379 next = s.canvas.getNextTextElement(s.element);
381 scroll('bottom', next.dom[0]);
382 s.canvas.setCurrentElement(s.canvas.getDocumentElement(next.dom.contents()[0]), {caretTo: 0});
385 var secondToLast = (s.offset === s.element.wlxmlNode.getText().length -1);
387 // Only Flying Spaghetti Monster knows why this is need for FF (for versions at least 26 to 31)
389 s.canvas.setCurrentElement(s.element, {caretTo: 'end'});
395 applies: function(e, s) {
396 return s.type === 'caret' &&
397 s.element.wlxmlNode.parent().is({tagName: 'span'}) &&
398 s.element.wlxmlNode.getText().length === 1 &&
400 (e.key === KEYS.BACKSPACE);
402 run: function(e, s) {
404 prevTextNode = s.element.canvas.getPreviousTextElement(s.element).wlxmlNode;
406 s.element.wlxmlNode.parent().detach(params);
407 s.canvas.setCurrentElement(
408 (params.ret && params.ret.mergedTo) || prevTextNode,
409 {caretTo: params.ret ? params.ret.previousLen : (prevTextNode ? prevTextNode.getText().length : 0)});
413 applies: function(e, s) {
414 return s.type === 'caret' && (
415 (s.isAtBeginning() && e.key === KEYS.BACKSPACE) ||
416 (s.isAtEnd() && e.key === KEYS.DELETE)
420 var direction, caretTo, cursorAtOperationEdge, goto, element;
422 if(e.key === KEYS.BACKSPACE) {
425 cursorAtOperationEdge = s.isAtBeginning();
431 cursorAtOperationEdge = s.isAtEnd();
432 element = cursorAtOperationEdge && s.canvas.getNearestTextElement(direction, s.element);
435 if(!cursorAtOperationEdge || !element) {
441 s.canvas.wlxmlDocument.transaction(function() {
442 if(element.wlxmlNode.getIndex() === 0) {
443 goto = element.wlxmlNode.parent().moveUp();
445 goto = element.wlxmlNode.moveUp();
448 s.canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
452 description: gettext('Remove text')
459 applies: function(e,s) {
460 return s.type === 'caret' && s.element.getText().length === 1 && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE);
464 e.element.wlxmlNode.setText('');
465 s.canvas.setCurrentElement(s.element, {caretTo: 0});
470 applies: function(e, s) {
471 return s.type === 'textSelection' && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE);
473 run: function(e, s) {
474 var direction = 'above',
478 if(e.key === KEYS.DELETE) {
485 if(s.startsAtBeginning && s.endsAtEnd && s.startElement.sameNode(s.endElement)) {
486 goto = s.startElement;
487 caretTo = s.startOffset;
488 } else if(direction === 'above') {
489 if(s.startsAtBeginning()) {
490 goto = s.canvas.getNearestTextElement('above', s.startElement);
493 goto = s.startElement;
494 caretTo = s.startOffset;
498 goto = s.canvas.getNearestTextElement('below', s.startElement);
506 var doc = s.canvas.wlxmlDocument;
507 doc.transaction(function() {
511 node: s.startElement.wlxmlNode,
512 offset: s.startOffset
515 node: s.endElement.wlxmlNode,
521 success: function() {
523 s.canvas.setCurrentElement(goto, {caretTo: caretTo});
531 applies: function(e, s) {
532 return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement();
534 run: function(e, s) {
535 var result, goto, gotoOptions;
538 s.canvas.wlxmlDocument.transaction(function() {
539 result = s.element.wlxmlNode.breakContent({offset: s.offset});
542 description: gettext('Splitting text'),
543 fragment: s.toDocumentFragment()
547 if(result.emptyText) {
548 goto = result.emptyText;
551 goto = result.second;
552 gotoOptions = {caretTo: 'start'};
555 s.canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
561 handleKey: handleKey,
562 handleKeyEvent: handleKeyEvent,