You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ra...@apache.org on 2019/07/01 07:11:18 UTC

[incubator-annotator] branch master updated (43ea385 -> 69cc30c)

This is an automated email from the ASF dual-hosted git repository.

randall pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git.


    from 43ea385  Demo copy and style refresh
     new 7136d41  Move demo code into a single file
     new 69cc30c  Harmonize create and describe

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 demo/index.js                  | 39 +++++++++++++++++++++++++++-----------
 demo/mark.js                   | 25 ------------------------
 demo/search.js                 | 43 ------------------------------------------
 packages/dom/src/text-quote.js | 10 ++++++----
 4 files changed, 34 insertions(+), 83 deletions(-)
 delete mode 100644 demo/mark.js
 delete mode 100644 demo/search.js


[incubator-annotator] 02/02: Harmonize create and describe

Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

randall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git

commit 69cc30c1fb8985d1f274a61d4f088f4aa47cee93
Author: Randall Leeds <ra...@apache.org>
AuthorDate: Mon Jul 1 00:10:45 2019 -0700

    Harmonize create and describe
---
 demo/index.js                  | 12 ++++++------
 packages/dom/src/text-quote.js | 10 ++++++----
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/demo/index.js b/demo/index.js
index c8fe1b5..d65e87e 100644
--- a/demo/index.js
+++ b/demo/index.js
@@ -22,7 +22,7 @@ import {
 import {
   createRangeSelectorCreator,
   createTextQuoteSelector,
-  describeTextQuoteByRange as describeRange,
+  describeTextQuote,
 } from '@annotator/dom';
 import { makeRefinable } from '@annotator/selector';
 import highlightRange from 'dom-highlight-range';
@@ -70,13 +70,13 @@ async function describeSelection() {
   if (selection.isCollapsed) return;
 
   const range = selection.getRangeAt(0);
-  const context = document.createRange();
-  context.selectNodeContents(selectable);
+  const scope = document.createRange();
+  scope.selectNodeContents(selectable);
 
-  if (!context.isPointInRange(range.startContainer, range.startOffset)) return;
-  if (!context.isPointInRange(range.endContainer, range.endOffset)) return;
+  if (!scope.isPointInRange(range.startContainer, range.startOffset)) return;
+  if (!scope.isPointInRange(range.endContainer, range.endOffset)) return;
 
-  return describeRange({ range, context });
+  return describeTextQuote(range, scope);
 }
 
 async function onSelectionChange() {
diff --git a/packages/dom/src/text-quote.js b/packages/dom/src/text-quote.js
index bc56fa1..9a7f28c 100644
--- a/packages/dom/src/text-quote.js
+++ b/packages/dom/src/text-quote.js
@@ -117,9 +117,11 @@ export function createTextQuoteSelector(selector) {
   };
 }
 
-export async function describeTextQuoteByRange({ range, context }) {
-  const root = context.commonAncestorContainer;
-  const text = context.toString();
+export async function describeTextQuote(range, scope = null) {
+  scope = rangeFromScope(scope || ownerDocument(range).documentElement);
+
+  const root = scope.commonAncestorContainer;
+  const text = scope.toString();
 
   const exact = range.toString();
   const selector = createTextQuoteSelector({ exact });
@@ -135,7 +137,7 @@ export async function describeTextQuoteByRange({ range, context }) {
 
   const affixLengthPairs = [[0, 0]];
 
-  for await (const match of selector(context)) {
+  for await (const match of selector(scope)) {
     const matchIter = createNodeIterator(root, SHOW_TEXT);
 
     const matchStartNode = firstTextNodeInRange(match);


[incubator-annotator] 01/02: Move demo code into a single file

Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

randall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git

commit 7136d413581568498268ea5d51d8f32ae8ce72d7
Author: Randall Leeds <ra...@apache.org>
AuthorDate: Sun Jun 30 23:43:23 2019 -0700

    Move demo code into a single file
---
 demo/index.js  | 29 +++++++++++++++++++++++------
 demo/mark.js   | 25 -------------------------
 demo/search.js | 43 -------------------------------------------
 3 files changed, 23 insertions(+), 74 deletions(-)

diff --git a/demo/index.js b/demo/index.js
index 78f0d23..c8fe1b5 100644
--- a/demo/index.js
+++ b/demo/index.js
@@ -19,15 +19,31 @@ import {
   parse as parseFragment,
   stringify as stringifyFragment,
 } from '@annotator/fragment-identifier';
-import { describeTextQuoteByRange as describeRange } from '@annotator/dom';
-
-import { mark } from './mark.js';
-import { search } from './search.js';
+import {
+  createRangeSelectorCreator,
+  createTextQuoteSelector,
+  describeTextQuoteByRange as describeRange,
+} from '@annotator/dom';
+import { makeRefinable } from '@annotator/selector';
+import highlightRange from 'dom-highlight-range';
 
 function clear() {
   corpus.innerHTML = selectable.innerHTML;
 }
 
+const createSelector = makeRefinable(selector => {
+  const selectorCreator = {
+    TextQuoteSelector: createTextQuoteSelector,
+    RangeSelector: createRangeSelectorCreator(createSelector),
+  }[selector.type];
+
+  if (selectorCreator == null) {
+    throw new Error(`Unsupported selector type: ${selector.type}`);
+  }
+
+  return selectorCreator(selector);
+});
+
 const refresh = async () => {
   clear();
 
@@ -35,14 +51,15 @@ const refresh = async () => {
   if (!identifier) return;
 
   const { selector } = parseFragment(identifier);
+  const matchAll = createSelector(selector);
   const ranges = [];
 
-  for await (const range of search(corpus, selector)) {
+  for await (const range of matchAll(corpus)) {
     ranges.push(range);
   }
 
   for (const range of ranges) {
-    mark(range);
+    highlightRange(range, 'highlighted');
   }
 
   parsed.innerText = JSON.stringify(selector, null, 2);
diff --git a/demo/mark.js b/demo/mark.js
deleted file mode 100644
index 5ffbc2d..0000000
--- a/demo/mark.js
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * @license
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-/**
- * Surround the contents of the given range with a mark tag.
- * @param {Range} range
- */
-
-import highlightRange from 'dom-highlight-range';
-
-export function mark(range) {
-  highlightRange(range, 'highlighted');
-}
diff --git a/demo/search.js b/demo/search.js
deleted file mode 100644
index e5145a3..0000000
--- a/demo/search.js
+++ /dev/null
@@ -1,43 +0,0 @@
-/**
- * @license
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-import { makeRefinable } from '@annotator/selector';
-import {
-  createRangeSelectorCreator,
-  createTextQuoteSelector,
-} from '@annotator/dom';
-
-const createSelector = makeRefinable(selector => {
-  const selectorCreator = {
-    TextQuoteSelector: createTextQuoteSelector,
-    RangeSelector: createRangeSelectorCreator(createSelector),
-  }[selector.type];
-
-  if (selectorCreator == null) {
-    throw new Error(`Unsupported selector type: ${selector.type}`);
-  }
-
-  return selectorCreator(selector);
-});
-
-/**
- * Locate a selector.
- * @param {Node} root node
- * @param {Selector} selector
- * @return {Range}
- */
-export async function* search(root, selector) {
-  yield* createSelector(selector)(root);
-}