From 6b16ebe9d02c4acf056b21a49cde0fd5e6363397 Mon Sep 17 00:00:00 2001 From: Fred Chasen Date: Thu, 20 Jul 2017 15:57:58 -0400 Subject: [PATCH] Tidy up Locations --- examples/locations.html | 3 +-- src/locations.js | 33 +++++++++++++++++++------------ src/managers/default/index.js | 37 ----------------------------------- 3 files changed, 21 insertions(+), 52 deletions(-) diff --git a/examples/locations.html b/examples/locations.html index a7d516c..84c0ee3 100644 --- a/examples/locations.html +++ b/examples/locations.html @@ -69,7 +69,6 @@ rendition.on("keyup", keyListener); document.addEventListener("keyup", keyListener, false); - book.ready.then(function(){ // Load in stored locations from json or local storage @@ -81,7 +80,7 @@ // Or generate the locations on the fly // Can pass an option number of chars to break sections by // default is 150 chars - return book.locations.generate(600); + return book.locations.generate(1600); } }) .then(function(locations){ diff --git a/src/locations.js b/src/locations.js index 4f2dcf3..ad6c30a 100644 --- a/src/locations.js +++ b/src/locations.js @@ -119,15 +119,31 @@ class Locations { pos = len; } + while (pos < len) { - // counter = this.break; - pos += dist; + dist = _break - counter; + + if (counter === 0) { + // Start new range + pos += 1; + range = this.createRange(); + range.startContainer = node; + range.startOffset = pos; + } + + // pos += dist; + // Gone over - if(pos >= len){ + if(pos + dist >= len){ // Continue counter for next node - counter = len - (pos - _break); + counter += len - pos; + // break + pos = len; // At End } else { + // Advance pos + pos += dist; + // End the previous range range.endContainer = node; range.endOffset = pos; @@ -135,14 +151,6 @@ class Locations { let cfi = new EpubCFI(range, cfiBase).toString(); locations.push(cfi); counter = 0; - - // Start new range - pos += 1; - range = this.createRange(); - range.startContainer = node; - range.startOffset = pos; - - dist = _break; } } prev = node; @@ -154,7 +162,6 @@ class Locations { if (range && range.startContainer && prev) { range.endContainer = prev; range.endOffset = prev.length; - // cfi = section.cfiFromRange(range); let cfi = new EpubCFI(range, cfiBase).toString(); locations.push(cfi); counter = 0; diff --git a/src/managers/default/index.js b/src/managers/default/index.js index 266888b..ef6bffb 100644 --- a/src/managers/default/index.js +++ b/src/managers/default/index.js @@ -539,43 +539,6 @@ class DefaultViewManager { }); return sections; - - /* - if(visible.length === 1) { - startA = container.left - visible[0].position().left; - endA = startA + this.layout.spreadWidth + this.layout.gap; - - pageLeft = this.mapping.page(visible[0].contents, visible[0].section.cfiBase, startA, endA) - return { - index : visible[0].section.index, - href : visible[0].section.href, - start: pageLeft.start, - end: pageLeft.end - }; - } - - if(visible.length > 1) { - last = visible.length - 1; - - // Left Col - startA = container.left - visible[0].position().left; - endA = startA + this.layout.columnWidth + this.layout.gap / 2; - - // Right Col - startB = container.left + this.layout.spreadWidth - visible[last].position().left; - endB = startB + this.layout.columnWidth + this.layout.gap / 2; - - pageLeft = this.mapping.page(visible[0].contents, visible[0].section.cfiBase, startA, endA); - pageRight = this.mapping.page(visible[last].contents, visible[last].section.cfiBase, startB, endB); - - return { - index : visible[last].section.index, - href : visible[last].section.href, - start: pageLeft.start, - end: pageRight.end - }; - } - */ } isVisible(view, offsetPrev, offsetNext, _container){