@readium/helpers 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -3
- package/dist/index.js +1 -1
- package/dist/textFragmentDirective.js +1 -0
- package/dist/vendor/text-fragments-polyfill/textFragmentGenerator.js +1 -0
- package/dist/vendor/text-fragments-polyfill/textFragmentMatcher.js +1 -0
- package/package.json +6 -6
- package/src/index.ts +3 -0
- package/src/textFragmentDirective.ts +44 -0
- package/src/vendor/text-fragments-polyfill/LICENSE +201 -0
- package/src/vendor/text-fragments-polyfill/README.MD +8 -0
- package/src/vendor/text-fragments-polyfill/textFragmentGenerator.ts +1744 -0
- package/src/vendor/text-fragments-polyfill/textFragmentMatcher.ts +883 -0
- package/types/src/index.d.ts +3 -0
- package/types/src/textFragmentDirective.d.ts +7 -0
- package/types/src/vendor/text-fragments-polyfill/textFragmentGenerator.d.ts +60 -0
- package/types/src/vendor/text-fragments-polyfill/textFragmentMatcher.d.ts +71 -0
package/types/src/index.d.ts
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2020 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import type { TextFragment } from './textFragmentMatcher.ts';
|
|
17
|
+
/**
|
|
18
|
+
* Allows overriding the max runtime to specify a different interval. Fragment
|
|
19
|
+
* generation will halt and throw an error after this amount of time.
|
|
20
|
+
* @param newTimeoutDurationMs - the desired timeout length, in ms.
|
|
21
|
+
*/
|
|
22
|
+
export declare const setFragmentGenerationTimeout: (newTimeoutDurationMs: number | null) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Enum indicating the success, or failure reason, of generateFragment.
|
|
25
|
+
*/
|
|
26
|
+
export declare const GenerateFragmentStatus: {
|
|
27
|
+
readonly SUCCESS: 0;
|
|
28
|
+
readonly INVALID_SELECTION: 1;
|
|
29
|
+
readonly AMBIGUOUS: 2;
|
|
30
|
+
readonly TIMEOUT: 3;
|
|
31
|
+
readonly EXECUTION_FAILED: 4;
|
|
32
|
+
};
|
|
33
|
+
export type GenerateFragmentStatusValue = typeof GenerateFragmentStatus[keyof typeof GenerateFragmentStatus];
|
|
34
|
+
export interface GenerateFragmentResult {
|
|
35
|
+
status: GenerateFragmentStatusValue;
|
|
36
|
+
fragment?: TextFragment;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Attempts to generate a fragment, suitable for formatting and including in a
|
|
40
|
+
* URL, which will highlight the given selection upon opening.
|
|
41
|
+
* @param selection - a Selection object, the result of window.getSelection
|
|
42
|
+
* @param startTime - the time when generation began, for timeout purposes.
|
|
43
|
+
* Defaults to current timestamp.
|
|
44
|
+
*/
|
|
45
|
+
export declare const generateFragment: (selection: Selection, startTime?: number) => GenerateFragmentResult;
|
|
46
|
+
/**
|
|
47
|
+
* Attampts to generate a fragment using a given range. @see {@link generateFragment}
|
|
48
|
+
* @param startTime - the time when generation began, for timeout purposes.
|
|
49
|
+
* Defaults to current timestamp.
|
|
50
|
+
*/
|
|
51
|
+
export declare const generateFragmentFromRange: (range: Range, startTime?: number) => GenerateFragmentResult;
|
|
52
|
+
/**
|
|
53
|
+
* Checks whether fragment generation can be attempted for a given range. This
|
|
54
|
+
* checks a handful of simple conditions: the range must be nonempty, not inside
|
|
55
|
+
* an <input>, etc. A true return is not a guarantee that fragment generation
|
|
56
|
+
* will succeed; instead, this is a way to quickly rule out generation in cases
|
|
57
|
+
* where a failure is predictable.
|
|
58
|
+
* @return true if fragment generation may proceed; false otherwise.
|
|
59
|
+
*/
|
|
60
|
+
export declare const isValidRangeForFragmentGeneration: (range: Range) => boolean;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2020 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export interface TextFragment {
|
|
17
|
+
textStart: string;
|
|
18
|
+
textEnd?: string;
|
|
19
|
+
prefix?: string;
|
|
20
|
+
suffix?: string;
|
|
21
|
+
}
|
|
22
|
+
interface BoundaryPoint {
|
|
23
|
+
node: Node;
|
|
24
|
+
offset: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Searches the document for a given text fragment.
|
|
28
|
+
*
|
|
29
|
+
* @param textFragment - Text Fragment to highlight.
|
|
30
|
+
* @param documentToProcess - document where to extract and mark fragments in.
|
|
31
|
+
* @param root - the root element where to extract and mark fragments in.
|
|
32
|
+
* @return Zero or more ranges within the document corresponding to the
|
|
33
|
+
* fragment. If the fragment corresponds to more than one location in the
|
|
34
|
+
* document (i.e., is ambiguous) then the first two matches will be
|
|
35
|
+
* returned (regardless of how many more matches there may be in the
|
|
36
|
+
* document).
|
|
37
|
+
*/
|
|
38
|
+
export declare const processTextFragmentDirective: (textFragment: TextFragment, documentToProcess: Document, root?: Node) => Range[];
|
|
39
|
+
/**
|
|
40
|
+
* Should not be referenced except in the /test directory.
|
|
41
|
+
*/
|
|
42
|
+
export declare const forTesting: {
|
|
43
|
+
advanceRangeStartPastOffset: (range: Range, node: Node, offset: number) => void;
|
|
44
|
+
advanceRangeStartToNonWhitespace: (range: Range) => void;
|
|
45
|
+
findRangeFromNodeList: (query: string, range: Range, textNodes: Text[], segmenter?: Intl.Segmenter) => Range | undefined;
|
|
46
|
+
findTextInRange: (query: string, range: Range) => Range | undefined;
|
|
47
|
+
getBoundaryPointAtIndex: (index: number, textNodes: Text[], isEnd: boolean) => BoundaryPoint | undefined;
|
|
48
|
+
isWordBounded: (text: string, startPos: number, length: number, segmenter?: Intl.Segmenter) => boolean;
|
|
49
|
+
makeNewSegmenter: (doc?: Document) => Intl.Segmenter | undefined;
|
|
50
|
+
normalizeString: (str?: string) => string;
|
|
51
|
+
forwardTraverse: (walker: TreeWalker, finishedSubtrees: Set<Node>) => Node | null;
|
|
52
|
+
backwardTraverse: (walker: TreeWalker, finishedSubtrees: Set<Node>) => Node | null;
|
|
53
|
+
getAllTextNodes: (root: Node, range?: Range) => Text[][];
|
|
54
|
+
acceptTextNodeIfVisibleInRange: (node: Node, range?: Range) => number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Should only be used by other files in this directory.
|
|
58
|
+
*/
|
|
59
|
+
export declare const internal: {
|
|
60
|
+
BLOCK_ELEMENTS: string[];
|
|
61
|
+
BOUNDARY_CHARS: RegExp;
|
|
62
|
+
NON_BOUNDARY_CHARS: RegExp;
|
|
63
|
+
acceptNodeIfVisibleInRange: (node: Node, range?: Range) => number;
|
|
64
|
+
normalizeString: (str?: string) => string;
|
|
65
|
+
makeNewSegmenter: (doc?: Document) => Intl.Segmenter | undefined;
|
|
66
|
+
forwardTraverse: (walker: TreeWalker, finishedSubtrees: Set<Node>) => Node | null;
|
|
67
|
+
backwardTraverse: (walker: TreeWalker, finishedSubtrees: Set<Node>) => Node | null;
|
|
68
|
+
makeTextNodeWalker: (range: Range) => TreeWalker;
|
|
69
|
+
isNodeVisible: (node: Node) => boolean;
|
|
70
|
+
};
|
|
71
|
+
export {};
|