Skip to content

Commit

Permalink
#3
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Jun 19, 2015
1 parent dd31c7e commit 9c682bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
15 changes: 13 additions & 2 deletions src/main/Rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ class Rendering {
* ``
* @param {string} startPath
* @param {string} endPath
* @returns {*}
* @returns {Range}
*/
renderWithPath(startPath, endPath) {
const startContainer = this._deserializePath(startPath);
Expand All @@ -678,12 +678,23 @@ class Rendering {
throw 'Could not find start- and/or end-container in document';
}

/**
* Renders a result (that returned from `renderWithRange`)
* @param result
* @returns {Range}
*/
renderWithResult(result) {
return this.renderWithPath(
`${result.startContainerPath};${result.startOffset}`,
`${result.endContainerPath};${result.endOffset}`);
}


/**
* Prepares a selection with a range object
* @param {Range} range
* @param {boolean} [withoutResult] optional do calculate a result, the selection would not be serializable
* @returns {*}
* @returns {Object}
*/
renderWithRange(range, withoutResult) {
return this._renderWithElements(range.startContainer, range.endContainer,
Expand Down
7 changes: 4 additions & 3 deletions src/main/modules/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ document.addEventListener("DOMContentLoaded", function () {
if (autoMarkText.parentNode.nextSibling) {
let nextText = autoMarkText.parentNode.nextSibling.childNodes[0];
setTimeout(function () {
render(nextText, ++c, length);
render(nextText, ++c, length);
}, speed);
}
};
Expand All @@ -46,9 +46,9 @@ document.addEventListener("DOMContentLoaded", function () {
marker.renderWithPath(range.startContainerPath + ";" + range.startOffset,
range.endContainerPath + ";" + range.endOffset);
} catch (e) {
console.warn("Could not render:", range);
console.warn("Could not render:", range, e);
localStorage.setItem(STORAGE_KEY, JSON.stringify([]));
throw 'Cleared local storage because of a rendering issue, the page might have been changed ;)';
console.error('Cleared local storage because of a rendering issue, the page might have been changed ;)');
}

});
Expand All @@ -71,6 +71,7 @@ document.addEventListener("DOMContentLoaded", function () {
var selection = document.getSelection(), renderer = new Marklib.Rendering(document),
result = renderer.renderWithRange(selection.getRangeAt(0));
selection.removeAllRanges();
console.info("stored:", result);
savedRanges.push(result);
localStorage.setItem(STORAGE_KEY, JSON.stringify(savedRanges));
} catch (e) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,19 @@ class Util {
static calcIndex(node) {
let calculatedIndex = 0,
foundWrapper = false;
const nodes = node.childNodes, length = nodes.length;
const nodes = node.parentNode.childNodes, length = nodes.length;
for (let thisIndex = 0; thisIndex < length; thisIndex++) {
const el = nodes[thisIndex];
if (el === node) {
return false;
break;
}
const maybeIndexOfOriginal = el.getAttribute(ATTR_DATA_ORIGINAL_INDEX);
const isOriginal = maybeIndexOfOriginal !== undefined;
const maybeIndexOfOriginal = el.getAttribute ? el.getAttribute(ATTR_DATA_ORIGINAL_INDEX) : null;
const isOriginal = maybeIndexOfOriginal !== null;
// Important: do not include pseudo elements
if (el !== node && (el.nodeType !== Node.TEXT_NODE || isOriginal) && !el.hasAttribute(DATA_PSEUDO)) {
if ((el.nodeType !== Node.TEXT_NODE || isOriginal)) {
if (isOriginal) {
calculatedIndex = parseInt(maybeIndexOfOriginal);
calculatedIndex
= parseInt(maybeIndexOfOriginal) + 1; // + 1 because the textnode itself must be counted too
foundWrapper = true;
} else {
calculatedIndex++;
Expand Down

0 comments on commit 9c682bb

Please sign in to comment.