integration wip: documentCanvas - selecting vertically first text on start
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 15 Jul 2013 13:41:36 +0000 (15:41 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 15 Jul 2013 13:41:36 +0000 (15:41 +0200)
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js
modules/documentCanvas/documentCanvas.js

index 3778698..705ef2c 100644 (file)
@@ -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('<section><metadata><dc:author>author</dc:author></metadata>Alice<div>has</div>a cat</section>'),
+                        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('\
+                            <section>\
+                                <div>\
+                                    <div>\
+                                        Alice\
+                                    </div>\
+                                </div>\
+                                Some text\
+                            </section>'),
+                        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() {
index 3a89bd1..7e5824b 100644 (file)
@@ -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';
     }
 });
 
index bde08d3..1c3242a 100644 (file)
@@ -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);