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/06/22 23:27:33 UTC
[incubator-annotator] 02/06: Move createTextQuoteSelector to
@annotator/dom
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 9b3e8ce974e3921946901b7d038952a089021562
Author: Randall Leeds <ra...@apache.org>
AuthorDate: Sat Jun 22 13:35:57 2019 -0700
Move createTextQuoteSelector to @annotator/dom
Move the createTextQuoteSelector function to the @annotator/dom package.
Augment it to support Node and anything that String can convert.
Delete the @annotator/text package.
---
demo/search.js | 2 +-
packages/dom/package.json | 1 -
packages/dom/src/text/quote.js | 36 ++++++++++++++++++++++++++++++++++--
packages/text/.npmignore | 2 --
packages/text/package.json | 24 ------------------------
packages/text/src/index.js | 41 -----------------------------------------
6 files changed, 35 insertions(+), 71 deletions(-)
diff --git a/demo/search.js b/demo/search.js
index 150058f..1415af1 100644
--- a/demo/search.js
+++ b/demo/search.js
@@ -15,7 +15,7 @@
import { makeRefinable } from '@annotator/selector';
import { createRangeSelectorCreator } from '@annotator/range';
-import { createTextQuoteSelector } from '@annotator/text';
+import { createTextQuoteSelector } from '@annotator/dom';
const createSelector = makeRefinable(selector => {
const selectorCreator = {
diff --git a/packages/dom/package.json b/packages/dom/package.json
index 1d6dff7..95e288b 100644
--- a/packages/dom/package.json
+++ b/packages/dom/package.json
@@ -12,7 +12,6 @@
"main": "lib/index.js",
"module": "esm/index.js",
"dependencies": {
- "@annotator/text": "^0.0.0",
"@babel/runtime-corejs3": "^7.4.0",
"core-js": "3",
"range-normalize": "^1.1.1"
diff --git a/packages/dom/src/text/quote.js b/packages/dom/src/text/quote.js
index 87c7926..1ba8ef7 100644
--- a/packages/dom/src/text/quote.js
+++ b/packages/dom/src/text/quote.js
@@ -16,7 +16,39 @@
/* global Range */
import normalizeRange from 'range-normalize';
-import { createTextQuoteSelector } from '@annotator/text';
+
+function textContent(scope) {
+ return typeof scope === 'string'
+ ? scope
+ : scope instanceof Object && 'textContent' in scope
+ ? scope.textContent
+ : String(scope);
+}
+
+export function createTextQuoteSelector(selector) {
+ return async function* matchAll(scope) {
+ const text = textContent(scope);
+
+ const prefix = selector.prefix || '';
+ const suffix = selector.suffix || '';
+ const pattern = prefix + selector.exact + suffix;
+
+ let fromIndex = -1;
+
+ while (true) {
+ const matchIndex = text.indexOf(pattern, fromIndex + 1);
+ if (matchIndex == -1) return;
+
+ const result = [selector.exact];
+ result.index = matchIndex + prefix.length;
+ result.input = text;
+
+ yield result;
+
+ fromIndex = matchIndex;
+ }
+ };
+}
export async function describeTextQuoteByRange({ range, context }) {
// Shrink range to fit in context, if needed.
@@ -50,7 +82,7 @@ export async function describeTextQuoteByRange({ range, context }) {
const rangeIndex = range.startOffset;
const rangeEndIndex = range.endOffset;
- const matches = createTextQuoteSelector(selector)(contextText);
+ const matches = createTextQuoteSelector(selector)(context);
const minSuffixes = [];
const minPrefixes = [];
for await (let match of matches) {
diff --git a/packages/text/.npmignore b/packages/text/.npmignore
deleted file mode 100644
index 281df39..0000000
--- a/packages/text/.npmignore
+++ /dev/null
@@ -1,2 +0,0 @@
-src
-test
diff --git a/packages/text/package.json b/packages/text/package.json
deleted file mode 100644
index f25ec1a..0000000
--- a/packages/text/package.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "name": "@annotator/text",
- "version": "0.0.0",
- "description": "Utilities for annotation of plain text documents.",
- "homepage": "https://annotator.apache.org",
- "repository": {
- "type": "git",
- "url": "https://github.com/apache/incubator-annotator.git"
- },
- "license": "Apache-2.0",
- "author": "Apache Software Foundation",
- "main": "lib/index.js",
- "module": "esm/index.js",
- "dependencies": {
- "@babel/runtime-corejs3": "^7.4.0",
- "core-js": "3"
- },
- "engines": {
- "node": ">=6.0.0"
- },
- "publishConfig": {
- "access": "public"
- }
-}
diff --git a/packages/text/src/index.js b/packages/text/src/index.js
deleted file mode 100644
index ce0f671..0000000
--- a/packages/text/src/index.js
+++ /dev/null
@@ -1,41 +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.
- */
-
-export function createTextQuoteSelector(selector) {
- return async function* matchAll(scope) {
- const prefix = selector.prefix || '';
- const suffix = selector.suffix || '';
- const pattern = prefix + selector.exact + suffix;
- let lastIndex = 0;
- let next = () => scope.indexOf(pattern, lastIndex);
- let match = next();
- while (match !== -1) {
- let result = [selector.exact];
- result.index = match + prefix.length;
- result.input = scope;
- yield result;
- lastIndex = match + 1;
- match = next();
- }
- };
-}
-
-export function describeTextQuote({ scope, startIndex, endIndex }) {
- const exact = scope.substring(startIndex, endIndex);
- return {
- type: 'TextQuoteSelector',
- exact,
- };
-}