3 'modules/documentCanvas/canvas/utils'
4 ], function($, utils) {
20 var scroll = function(place, textElement) {
21 var rect = textElement.getBoundingClientRect(),
22 scroll = $('#rng-module-documentCanvas-contentWrapper'),
23 border = rect.bottom - (place === 'top' ? rect.height : 0) - scroll.offset().top + scroll[0].scrollTop,
24 visible = scroll[0].scrollTop + {top: 0, bottom: scroll.height()}[place],
28 if(place === 'top' && (border - padding < visible)) {
29 toScroll = border - visible - padding;
30 } else if(place === 'bottom' && (border + padding > visible)) {
31 toScroll = border - visible + padding;
34 scroll[0].scrollTop = scroll[0].scrollTop + toScroll;
39 var getLastRectAbove = function(node, y) {
40 var rects = node.getClientRects(),
43 while((rect = rects[idx])) {
54 var getFirstRectBelow = function(node, y) {
55 var rects = node.getClientRects(),
58 while((rect = rects[idx])) {
68 var handleKeyEvent = function(e, s) {
69 keyEventHandlers.some(function(handler) {
70 if(handler.applies(e, s)) {
76 // todo: whileRemoveWholetext
77 var keyEventHandlers = [
78 { // ctrl+x - prevented (?)
79 applies: function(e, s) {
82 s.type === 'textSelection' &&
83 s.startsAtBeginning() &&
92 applies: function(e, s) {
93 return e.key === KEYS.ARROW_UP && s.type === 'caret';
97 var caretRect = window.getSelection().getRangeAt(0).getClientRects()[0],
98 frameRects = s.element.dom[0].getClientRects(),
99 caretTop = caretRect.bottom - caretRect.height,
100 position, target,rect, scrolled;
103 if((frameRects[0].bottom === caretRect.bottom) || (caretRect.left < frameRects[0].left)) {
105 s.canvas.rootWrapper.find('[document-text-element]').each(function() {
106 var test = getLastRectAbove(this, caretTop);
115 scrolled = scroll('top', target);
116 position = utils.caretPositionFromPoint(caretRect.left, rect.bottom - 1 - scrolled);
117 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
121 scrolled = scroll('top', target);
122 var left = caretRect.left;
123 if(left > rect.left + rect.width) {
124 left = rect.left + rect.width;
125 } else if(left < rect.left ) {
128 position = utils.caretPositionFromPoint(left, rect.bottom - 1 - scrolled);
129 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
134 applies: function(e, s) {
135 return e.key === KEYS.ARROW_DOWN && s.type === 'caret';
137 run: function(e, s) {
139 var caretRect = window.getSelection().getRangeAt(0).getClientRects()[0],
140 frameRects = s.element.dom[0].getClientRects(),
141 lastRect = frameRects[frameRects.length-1],
142 position, target,rect, scrolled;
144 if(lastRect.bottom === caretRect.bottom || (caretRect.left > lastRect.left + lastRect.width)) {
146 s.canvas.rootWrapper.find('[document-text-element]').each(function() {
147 var test = getFirstRectBelow(this, caretRect.bottom);
155 scrolled = scroll('bottom', target);
156 position = utils.caretPositionFromPoint(caretRect.left, rect.top +1 - scrolled);
157 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
161 scrolled = scroll('bottom', target);
162 var left = caretRect.left;
163 if(left > rect.left + rect.width) {
164 left = rect.left + rect.width;
165 } else if(left < rect.left ) {
168 position = utils.caretPositionFromPoint(left, rect.top +1 - scrolled);
169 s.canvas.setCurrentElement(s.canvas.getDocumentElement(position.textNode), {caretTo: position.offset});
174 applies: function(e, s) {
175 return e.key === KEYS.ARROW_LEFT && s.type === 'caret';
177 run: function(e, s) {
183 prev = s.canvas.getPreviousTextElement(s.element);
185 scroll('top', prev.dom[0]);
186 s.canvas.setCurrentElement(s.canvas.getDocumentElement(prev.dom.contents()[0]), {caretTo: 'end'});
192 applies: function(e, s) {
193 return e.key === KEYS.ARROW_RIGHT && s.type === 'caret';
195 run: function(e, s) {
200 next = s.canvas.getNextTextElement(s.element);
202 scroll('bottom', next.dom[0]);
203 s.canvas.setCurrentElement(s.canvas.getDocumentElement(next.dom.contents()[0]), {caretTo: 0});
206 var secondToLast = (s.offset === s.element.wlxmlNode.getText().length -1);
208 // Only Flying Spaghetti Monster knows why this is need for FF (for versions at least 26 to 31)
210 s.canvas.setCurrentElement(s.element, {caretTo: 'end'});
215 { // backspace removing the last character in a span
216 applies: function(e, s) {
217 return s.type === 'caret' &&
218 s.element.wlxmlNode.parent().is({tagName: 'span'}) &&
219 s.element.wlxmlNode.getText().length === 1 &&
221 (e.key === KEYS.BACKSPACE);
223 run: function(e, s) {
225 prevTextNode = s.element.canvas.getPreviousTextElement(s.element).wlxmlNode;
227 s.element.wlxmlNode.parent().detach(params);
228 s.canvas.setCurrentElement(
229 (params.ret && params.ret.mergedTo) || prevTextNode,
230 {caretTo: params.ret ? params.ret.previousLen : (prevTextNode ? prevTextNode.getText().length : 0)});
233 { // backspace/delete through an edge (behaves weirdly at spans)
234 applies: function(e, s) {
235 return s.type === 'caret' && (
236 (s.isAtBeginning() && e.key === KEYS.BACKSPACE) ||
237 (s.isAtEnd() && e.key === KEYS.DELETE)
243 if(e.key === KEYS.BACKSPACE) {
247 element = s.canvas.getNearestTextElement('below', s.element);
255 var parent = element.wlxmlNode.parent();
256 if(element.wlxmlNode.getIndex() === 0 && parent.isContextRoot() && (!parent.is('item') || parent.getIndex() === 0)) {
257 // Don't even try to do anything at the edge of a context root, except for non-first items
258 // - this is a temporary solution until key events handling get refactored into something more sane.
264 s.canvas.wlxmlDocument.transaction(function() {
265 if(element.wlxmlNode.getIndex() === 0) {
266 goto = element.wlxmlNode.parent().moveUp();
268 goto = element.wlxmlNode.moveUp();
271 s.canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
275 description: gettext('Remove text')
281 { // backspace/delete last character in a node - why is it needed?
282 applies: function(e,s) {
283 return s.type === 'caret' && s.element.getText().length === 1 && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE);
287 s.element.wlxmlNode.setText('');
288 s.canvas.setCurrentElement(s.element, {caretTo: 0});
293 applies: function(e, s) {
294 return s.type === 'textSelection' && (e.key === KEYS.BACKSPACE || e.key === KEYS.DELETE);
296 run: function(e, s) {
297 var direction = 'above',
301 if(e.key === KEYS.DELETE) {
308 if(s.startsAtBeginning && s.endsAtEnd && s.startElement.sameNode(s.endElement)) {
309 goto = s.startElement;
310 caretTo = s.startOffset;
311 } else if(direction === 'above') {
312 if(s.startsAtBeginning()) {
313 goto = s.canvas.getNearestTextElement('above', s.startElement);
316 goto = s.startElement;
317 caretTo = s.startOffset;
321 goto = s.canvas.getNearestTextElement('below', s.startElement);
329 var doc = s.canvas.wlxmlDocument;
330 doc.transaction(function() {
334 node: s.startElement.wlxmlNode,
335 offset: s.startOffset
338 node: s.endElement.wlxmlNode,
344 success: function() {
346 s.canvas.setCurrentElement(goto, {caretTo: caretTo});
353 { // enter on an empty list item - creates paragraph after list
354 applies: function(e, s) {
355 var parent = s.element && s.element.wlxmlNode.parent(),
356 parentIsItem = parent && parent.is('item'),
357 itemIsOnList = parent && parent.parent() && parent.parent().is('list'),
358 onlyChild = parent.contents().length === 1;
359 return s.type === 'caret' && e.key === KEYS.ENTER && s.element.isEmpty() && onlyChild &&
360 parentIsItem && itemIsOnList;
362 run: function(e, s) {
363 var item = s.element.wlxmlNode.parent(),
364 list = item.parent();
366 s.canvas.wlxmlDocument.transaction(function() {
367 var p = list.after({tagName: 'div', attrs: {'class': 'p'}});
368 p.append({text: ''});
370 if(list.contents().length === 0) {
375 success: function(p) {
376 s.canvas.setCurrentElement(p);
381 { // enter - split node
382 applies: function(e, s) {
383 return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement();
385 run: function(e, s) {
386 var parent = s.element.parent(),
387 children = parent.children(),
388 result, goto, gotoOptions;
392 if(children.length === 1 && s.element.isEmpty()) {
396 s.canvas.wlxmlDocument.transaction(function() {
397 result = s.element.wlxmlNode.breakContent({offset: s.offset});
400 description: gettext('Splitting text'),
401 fragment: s.toDocumentFragment()
405 if(result.emptyText) {
406 goto = result.emptyText;
409 goto = result.second;
410 gotoOptions = {caretTo: 'start'};
413 s.canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
416 { // enter - new paragraph after image/video
417 applies: function (e, s) {
418 return s.type === 'nodeSelection' && e.key === KEYS.ENTER && !s.element.isRootElement();
420 run: function (e, s) {
421 var parent = s.element.parent(),
422 children = parent.children(),
423 result, goto, gotoOptions;
426 s.canvas.wlxmlDocument.transaction(function() {
427 result = s.element.wlxmlNode.insertNewNode();
430 description: gettext('Inserting node'),
431 fragment: s.toDocumentFragment()
435 s.canvas.setCurrentElement(utils.getElementForNode(result), {caretTo: 'start'});
441 handleKeyEvent: handleKeyEvent,