From: Aleksander Ɓukasz Date: Thu, 3 Apr 2014 08:59:31 +0000 (+0200) Subject: refactoring: move getNearest/Next/PreviousTextElement out of DocumentElment X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/4a1c68596333765b36f36a9cf88942f90c20efab?ds=sidebyside;hp=39625b37b8cbd892677f7e8bf3ab5f8b065a1fd0 refactoring: move getNearest/Next/PreviousTextElement out of DocumentElment --- diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index 0ec22a1..a43799c 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -233,7 +233,7 @@ $.extend(Canvas.prototype, Backbone.Events, { this.eventBus.on('elementToggled', function(toggle, element) { if(!toggle) { - canvas.setCurrentElement(element.getPreviousTextElement()); + canvas.setCurrentElement(canvas.getPreviousTextElement(element)); } }); }, @@ -270,6 +270,20 @@ $.extend(Canvas.prototype, Backbone.Events, { } }, + getPreviousTextElement: function(relativeToElement, includeInvisible) { + return this.getNearestTextElement('above', relativeToElement, includeInvisible); + }, + + getNextTextElement: function(relativeToElement, includeInvisible) { + return this.getNearestTextElement('below', relativeToElement, includeInvisible); + }, + + getNearestTextElement: function(direction, relativeToElement, includeInvisible) { + includeInvisible = includeInvisible !== undefined ? includeInvisible : false; + var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible'); + return this.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, relativeToElement.dom()[0])); + }, + contains: function(element) { return element.dom().parents().index(this.wrapper) !== -1; }, diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index 1da7b3e..7b7f7f9 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -51,20 +51,6 @@ $.extend(DocumentElement.prototype, { return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0]; }, - getPreviousTextElement: function(includeInvisible) { - return this.getNearestTextElement('above', includeInvisible); - }, - - getNextTextElement: function(includeInvisible) { - return this.getNearestTextElement('below', includeInvisible); - }, - - getNearestTextElement: function(direction, includeInvisible) { - includeInvisible = includeInvisible !== undefined ? includeInvisible : false; - var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible'); - return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0])); - }, - trigger: function() { //this.canvas.bus.trigger() } diff --git a/src/editor/modules/documentCanvas/canvas/keyboard.js b/src/editor/modules/documentCanvas/canvas/keyboard.js index 4cf3d77..f0439d6 100644 --- a/src/editor/modules/documentCanvas/canvas/keyboard.js +++ b/src/editor/modules/documentCanvas/canvas/keyboard.js @@ -226,7 +226,7 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE], if(direction === 'above') { if(start.offsetAtBeginning) { - goto = start.element.getNearestTextElement('above'); + goto = canvas.getNearestTextElement('above', start.element); caretTo = 'end'; } else { goto = start.element; @@ -234,7 +234,7 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE], } } else { if(end.offsetAtEnd) { - goto = start.element.getNearestTextElement('below'); + goto = canvas.getNearestTextElement('below', start.element); caretTo = 'start'; } else { goto = end.element; @@ -283,18 +283,18 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE], if(grandParent && grandParent.children().length === 1) { goto = grandParent.wlxmlNode.append({text: ''}); } else { - goto = element.getNearestTextElement(direction); + goto = canvas.getNearestTextElement(direction, element); } parent.wlxmlNode.detach(); } else { - goto = element.getNearestTextElement(direction); + goto = canvas.getNearestTextElement(direction, element); element.wlxmlNode.detach(); } canvas.setCurrentElement(goto, {caretTo: caretTo}); } else if(cursorAtOperationEdge) { if(direction === 'below') { - element = element.getNearestTextElement(direction); + element = canvas.getNearestTextElement(direction, element); } if(element) { goto = element.wlxmlNode.mergeContentUp();