From: Aleksander Ɓukasz Date: Mon, 15 Jul 2013 13:41:36 +0000 (+0200) Subject: integration wip: documentCanvas - selecting vertically first text on start X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/c2f61b78499f7c1f4f722ef4a4b5514c95598505?ds=sidebyside integration wip: documentCanvas - selecting vertically first text on start --- diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 3778698..705ef2c 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -267,6 +267,32 @@ describe('Canvas', function() { expect(c.doc().children()[2].getText()).to.equal(' cat'); }); }); + + describe('getting vertically first text element', function() { + it('returns the first child if it\'s text element, ignores metadata', function() { + var c = canvas.fromXML('
authorAlice
has
a cat
'), + first = c.doc().getVerticallyFirstTextElement(); + + expect(first.sameNode(c.doc().children()[1])).to.be.true; + }); + + it('looks recursively inside node elements if they precede text element', function() { + var c = canvas.fromXML('\ +
\ +
\ +
\ + Alice\ +
\ +
\ + Some text\ +
'), + textAlice = c.doc().children()[0].children()[0].children()[0], + first = c.doc().getVerticallyFirstTextElement(); + + expect(textAlice).to.be.instanceOf(documentElement.DocumentTextElement); + expect(first.sameNode(textAlice)).to.be.true; + }); + }); }); describe('manipulation api', function() { diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 3a89bd1..7e5824b 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -80,6 +80,27 @@ $.extend(DocumentElement.prototype, { markAsCurrent: function() { this.canvas.markAsCurrent(this); + }, + + getVerticallyFirstTextElement: function() { + var toret; + this.children().some(function(child) { + if(!child.isVisible()) + return false; // continue + if(child instanceof DocumentTextElement) { + toret = child; + return true; // break + } else { + toret = child.getVerticallyFirstTextElement(); + if(toret) + return true; // break + } + }); + return toret; + }, + + isVisible: function() { + return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata'; } }); diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index bde08d3..1c3242a 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -22,6 +22,7 @@ return function(sandbox) { canvasWrapper.onShow = function() { if(!shownAlready) { shownAlready = true; + canvas.setCurrentElement(canvas.doc().getVerticallyFirstTextElement()); } else { canvas.setCursorPosition(cursorPosition); this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);