You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@annotator.apache.org by GitBox <gi...@apache.org> on 2020/03/26 20:12:43 UTC

[GitHub] [incubator-annotator] Treora commented on a change in pull request #62: Use highlighter in demo

Treora commented on a change in pull request #62: Use highlighter in demo
URL: https://github.com/apache/incubator-annotator/pull/62#discussion_r398861577
 
 

 ##########
 File path: demo/index.js
 ##########
 @@ -28,62 +28,18 @@ import {
   createRangeSelectorCreator,
   createTextQuoteSelector,
   describeTextQuote,
+  highlightRange,
 } from '@annotator/dom';
 import { makeRefinable } from '@annotator/selector';
 
-function clear() {
-  corpus.innerHTML = selectable.innerHTML;
-}
-
-function highlight(range) {
-  for (const node of textNodes(range)) {
-    const mark = document.createElement('mark');
-    const markRange = document.createRange();
-    markRange.selectNode(node);
-    markRange.surroundContents(mark);
-  }
-}
-
-function textNodes(range) {
-  const nodes = [];
-
-  if (range.collapsed) return nodes;
-
-  let startNode = range.startContainer;
-  let startOffset = range.startOffset;
-
-  if (startNode.nodeType === 3) {
-    if (startOffset > 0 && startOffset < startNode.length) {
-      startNode = startNode.splitText(startOffset);
-      startOffset = 0;
-    }
-  }
-
-  let endNode = range.endContainer;
-  let endOffset = range.endOffset;
-
-  if (endNode.nodeType === 3) {
-    if (endOffset > 0 && endOffset < endNode.length) {
-      endNode = endNode.splitText(endOffset);
-      endOffset = 0;
-    }
-  }
+const cleanupFunctions = [];
 
-  const walker = document.createTreeWalker(document.documentElement);
-  walker.currentNode = startNode;
-
-  while (walker.currentNode !== endNode) {
-    if (walker.currentNode.nodeType === 3) {
-      nodes.push(walker.currentNode);
-    }
-    walker.nextNode();
-  }
-
-  if (endNode.nodeType === 3 && endOffset > 0) {
-    nodes.push(endNode);
+function cleanup() {
+  let removeHighlight;
+  while (removeHighlight = cleanupFunctions.shift()) {
+    removeHighlight();
   }
-
-  return nodes;
+  corpus.normalize();
 
 Review comment:
   @tilgovi I just added this `normalize()` for now, so the demo keeps working despite the anchoring bug that made the fourth example fail.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services