@seamless-co/pagemark 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.md +25 -2
- package/behaviours/behaviour.d.ts +43 -0
- package/behaviours/behaviour.js +84 -0
- package/behaviours/comparison.d.ts +62 -0
- package/behaviours/comparison.js +129 -0
- package/behaviours/document-viewer.d.ts +9 -0
- package/behaviours/document-viewer.js +9 -0
- package/behaviours/hidden.d.ts +24 -0
- package/behaviours/hidden.js +38 -0
- package/behaviours/hover.d.ts +34 -0
- package/behaviours/hover.js +49 -0
- package/behaviours/index.d.ts +24 -0
- package/behaviours/index.js +14 -0
- package/behaviours/linked-viewers.d.ts +14 -0
- package/behaviours/linked-viewers.js +25 -0
- package/behaviours/links.d.ts +4 -0
- package/behaviours/links.js +4 -0
- package/behaviours/marks.d.ts +64 -0
- package/behaviours/marks.js +33 -0
- package/behaviours/quotes.d.ts +7 -0
- package/behaviours/quotes.js +9 -0
- package/behaviours/region.d.ts +27 -0
- package/behaviours/region.js +40 -0
- package/behaviours/search.d.ts +38 -0
- package/behaviours/search.js +63 -0
- package/behaviours/selection.d.ts +30 -0
- package/behaviours/selection.js +41 -0
- package/behaviours/text.d.ts +8 -0
- package/behaviours/text.js +24 -0
- package/behaviours/viewer-state.d.ts +36 -0
- package/behaviours/viewer-state.js +58 -0
- package/browser/blob-pieces.d.ts +9 -0
- package/browser/blob-pieces.js +50 -0
- package/browser/browser-source.d.ts +25 -0
- package/browser/browser-source.js +62 -0
- package/browser/index.d.ts +2 -0
- package/browser/index.js +1 -0
- package/browser/local-pdf.d.ts +27 -0
- package/browser/local-pdf.js +61 -0
- package/browser/worker-engine.d.ts +21 -0
- package/browser/worker-engine.js +1 -0
- package/browser/worker.d.ts +1 -0
- package/browser/worker.js +51 -0
- package/core/compare.d.ts +55 -0
- package/core/compare.js +350 -0
- package/core/find.d.ts +12 -0
- package/core/find.js +177 -0
- package/core/hidden.d.ts +12 -0
- package/core/hidden.js +26 -0
- package/core/index.d.ts +11 -0
- package/core/index.js +6 -0
- package/core/links.d.ts +7 -0
- package/core/links.js +19 -0
- package/core/locate.d.ts +21 -0
- package/core/locate.js +140 -0
- package/core/paragraphs.d.ts +37 -0
- package/core/paragraphs.js +141 -0
- package/core/types.d.ts +89 -0
- package/core/types.js +1 -0
- package/document/document.d.ts +83 -0
- package/document/document.js +215 -0
- package/document/handover-source.d.ts +18 -0
- package/document/handover-source.js +39 -0
- package/document/index.d.ts +10 -0
- package/document/index.js +5 -0
- package/document/reading-source.d.ts +7 -0
- package/document/reading-source.js +16 -0
- package/document/server-source.d.ts +10 -0
- package/document/server-source.js +23 -0
- package/document/source.d.ts +21 -0
- package/document/source.js +1 -0
- package/document/split-source.d.ts +9 -0
- package/document/split-source.js +25 -0
- package/document/store.d.ts +12 -0
- package/document/store.js +21 -0
- package/engine/engine-thread.d.ts +10 -0
- package/engine/engine-thread.js +1 -0
- package/engine/index.d.ts +2 -0
- package/engine/index.js +1 -0
- package/engine/pdfium.d.ts +16 -0
- package/engine/pdfium.js +222 -0
- package/engine/remote.d.ts +51 -0
- package/engine/remote.js +109 -0
- package/package.json +65 -4
- package/react/hooks.d.ts +128 -0
- package/react/hooks.js +129 -0
- package/react/index.d.ts +3 -0
- package/react/index.js +2 -0
- package/react/viewer.d.ts +48 -0
- package/react/viewer.js +127 -0
- package/reader/assemble.d.ts +4 -0
- package/reader/assemble.js +29 -0
- package/reader/index.d.ts +7 -0
- package/reader/index.js +5 -0
- package/reader/layer-page.d.ts +30 -0
- package/reader/layer-page.js +40 -0
- package/reader/layer-words.d.ts +4 -0
- package/reader/layer-words.js +102 -0
- package/reader/letter-edges.d.ts +13 -0
- package/reader/letter-edges.js +60 -0
- package/reader/links.d.ts +7 -0
- package/reader/links.js +22 -0
- package/reader/seams.d.ts +92 -0
- package/reader/seams.js +3 -0
- package/reader/seen.d.ts +16 -0
- package/reader/seen.js +67 -0
- package/reader/trust.d.ts +9 -0
- package/reader/trust.js +34 -0
- package/viewer/attachments.d.ts +11 -0
- package/viewer/attachments.js +104 -0
- package/viewer/dom.d.ts +14 -0
- package/viewer/dom.js +38 -0
- package/viewer/events.d.ts +17 -0
- package/viewer/events.js +20 -0
- package/viewer/index.d.ts +17 -0
- package/viewer/index.js +8 -0
- package/viewer/links.d.ts +16 -0
- package/viewer/links.js +59 -0
- package/viewer/marks.d.ts +24 -0
- package/viewer/marks.js +171 -0
- package/viewer/page-layers.d.ts +13 -0
- package/viewer/page-layers.js +33 -0
- package/viewer/pictures.d.ts +24 -0
- package/viewer/pictures.js +293 -0
- package/viewer/pointer.d.ts +21 -0
- package/viewer/pointer.js +244 -0
- package/viewer/stage.d.ts +21 -0
- package/viewer/stage.js +31 -0
- package/viewer/text-between.d.ts +13 -0
- package/viewer/text-between.js +28 -0
- package/viewer/text.d.ts +20 -0
- package/viewer/text.js +180 -0
- package/viewer/tile-grid.d.ts +20 -0
- package/viewer/tile-grid.js +36 -0
- package/viewer/tiles.d.ts +15 -0
- package/viewer/tiles.js +190 -0
- package/viewer/turn.d.ts +18 -0
- package/viewer/turn.js +40 -0
- package/viewer/types.d.ts +238 -0
- package/viewer/types.js +1 -0
- package/viewer/view-over.d.ts +21 -0
- package/viewer/view-over.js +191 -0
- package/viewer/view.d.ts +59 -0
- package/viewer/view.js +1 -0
- package/viewer/viewer.css +301 -0
- package/viewer/viewer.d.ts +11 -0
- package/viewer/viewer.js +104 -0
- package/viewer/viewport.d.ts +37 -0
- package/viewer/viewport.js +429 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Change, HiddenRun, Match, ParagraphChange } from "../core/index.js";
|
|
2
|
+
import type { ViewerMark } from "../viewer/index.js";
|
|
3
|
+
import type { Side } from "./comparison.js";
|
|
4
|
+
import type { LocatedQuote } from "./quotes.js";
|
|
5
|
+
/**
|
|
6
|
+
* NOTE: a mark that carries what it stands for, as every mark made here does. A viewer hands a mark back on a press
|
|
7
|
+
* and on a hover, so the match or the change behind it is read from the mark itself.
|
|
8
|
+
* INVARIANT: a mark's id is an identity and nothing more. Nothing builds one to say what a mark is, and nothing reads
|
|
9
|
+
* one to find out.
|
|
10
|
+
*/
|
|
11
|
+
export type Marked<Of> = ViewerMark<Of> & {
|
|
12
|
+
of: Of;
|
|
13
|
+
};
|
|
14
|
+
/** NOTE: `order` is the match's place among the matches it was made with, and `group` the name they were given. */
|
|
15
|
+
export type MatchMark = Marked<{
|
|
16
|
+
kind: "match";
|
|
17
|
+
group: string;
|
|
18
|
+
order: number;
|
|
19
|
+
match: Match;
|
|
20
|
+
}>;
|
|
21
|
+
/** NOTE: `quote` is the quote's place in the list, `order` the match's place among that quote's matches. */
|
|
22
|
+
export type QuoteMark = Marked<{
|
|
23
|
+
kind: "quote";
|
|
24
|
+
quote: number;
|
|
25
|
+
order: number;
|
|
26
|
+
text: string;
|
|
27
|
+
match: Match;
|
|
28
|
+
}>;
|
|
29
|
+
export type HiddenMark = Marked<{
|
|
30
|
+
kind: "hidden";
|
|
31
|
+
order: number;
|
|
32
|
+
run: HiddenRun;
|
|
33
|
+
}>;
|
|
34
|
+
/** NOTE: `order` is the change's place among the changes, `span` which of its runs of words on that side. */
|
|
35
|
+
export type ChangeMark = Marked<{
|
|
36
|
+
kind: "change";
|
|
37
|
+
order: number;
|
|
38
|
+
span: number;
|
|
39
|
+
side: Side;
|
|
40
|
+
change: Change;
|
|
41
|
+
}>;
|
|
42
|
+
export type ParagraphMark = Marked<{
|
|
43
|
+
kind: "paragraph";
|
|
44
|
+
order: number;
|
|
45
|
+
side: Side;
|
|
46
|
+
change: ParagraphChange;
|
|
47
|
+
}>;
|
|
48
|
+
/** NOTE: everything a mark made here can stand for, told apart by `kind`. */
|
|
49
|
+
export type MarkOf = (MatchMark | QuoteMark | HiddenMark | ChangeMark | ParagraphMark)["of"];
|
|
50
|
+
/** NOTE: the marks of every place a quote occurs. `group` names them apart from other marks in the same viewer. */
|
|
51
|
+
export declare function matchMarks(matches: ReadonlyArray<Match>, group: string, tone?: string): MatchMark[];
|
|
52
|
+
/** NOTE: the marks of a list of quotes, as `locateQuotes` gives it. Each says which quote it is and which of that quote's places. */
|
|
53
|
+
export declare function quoteMarks(located: ReadonlyArray<LocatedQuote>, tone?: string): QuoteMark[];
|
|
54
|
+
/**
|
|
55
|
+
* NOTE: the first of `marks` that stands for a thing of `kind`, or null: how a press or a hover is traced back to a
|
|
56
|
+
* match or a change. A mark that stands for nothing, or for something of an application's own, is passed over.
|
|
57
|
+
*/
|
|
58
|
+
export declare function markOfKind<Kind extends MarkOf["kind"]>(marks: ReadonlyArray<ViewerMark>, kind: Kind): Marked<Extract<MarkOf, {
|
|
59
|
+
kind: Kind;
|
|
60
|
+
}>> | null;
|
|
61
|
+
/** NOTE: the ids of every mark that stands for the thing at `order`: what to hand a viewer's focus to light the whole of it. */
|
|
62
|
+
export declare function markIds(marks: ReadonlyArray<Marked<{
|
|
63
|
+
order: number;
|
|
64
|
+
}>>, order: number): string[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const NEXT_PAGE = "-next";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: one mark for a match, or two for one that runs over a page break, the second on the page after. Both carry
|
|
4
|
+
* the same thing. The first alone carries the label, so a reader stepping through marks stops once at each match.
|
|
5
|
+
*/
|
|
6
|
+
function marksOf(match, id, tone, of, label) {
|
|
7
|
+
const first = { id, page: match.page, rects: match.rects, tone, label, of };
|
|
8
|
+
return match.continued ? [first, { id: `${id}${NEXT_PAGE}`, page: match.continued.page, rects: match.continued.rects, tone, of }] : [first];
|
|
9
|
+
}
|
|
10
|
+
/** NOTE: the marks of every place a quote occurs. `group` names them apart from other marks in the same viewer. */
|
|
11
|
+
export function matchMarks(matches, group, tone) {
|
|
12
|
+
return matches.flatMap((match, order) => marksOf(match, `${group}-${order}`, tone, { kind: "match", group, order, match }, `Match ${order + 1} of ${matches.length}`));
|
|
13
|
+
}
|
|
14
|
+
/** NOTE: the marks of a list of quotes, as `locateQuotes` gives it. Each says which quote it is and which of that quote's places. */
|
|
15
|
+
export function quoteMarks(located, tone = "quote") {
|
|
16
|
+
return located.flatMap((entry, quote) => entry.matches.flatMap((match, order) => marksOf(match, `quote-${quote}-${order}`, tone, { kind: "quote", quote, order, text: entry.quote, match }, `Quote "${entry.quote}", place ${order + 1} of ${entry.matches.length}`)));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* NOTE: the first of `marks` that stands for a thing of `kind`, or null: how a press or a hover is traced back to a
|
|
20
|
+
* match or a change. A mark that stands for nothing, or for something of an application's own, is passed over.
|
|
21
|
+
*/
|
|
22
|
+
export function markOfKind(marks, kind) {
|
|
23
|
+
for (const mark of marks) {
|
|
24
|
+
const of = mark.of;
|
|
25
|
+
if (typeof of === "object" && of !== null && "kind" in of && of.kind === kind)
|
|
26
|
+
return mark;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
/** NOTE: the ids of every mark that stands for the thing at `order`: what to hand a viewer's focus to light the whole of it. */
|
|
31
|
+
export function markIds(marks, order) {
|
|
32
|
+
return marks.filter((mark) => mark.of.order === order).map((mark) => mark.id);
|
|
33
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Match, type PageReading } from "../core/index.js";
|
|
2
|
+
export type LocatedQuote = {
|
|
3
|
+
quote: string;
|
|
4
|
+
matches: Match[];
|
|
5
|
+
};
|
|
6
|
+
/** NOTE: a list of quotes, one per line of text, each with every place it occurs. */
|
|
7
|
+
export declare function locateQuotes(pages: ReadonlyArray<PageReading>, text: string): LocatedQuote[];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { findQuote } from "../core/index.js";
|
|
2
|
+
/** NOTE: a list of quotes, one per line of text, each with every place it occurs. */
|
|
3
|
+
export function locateQuotes(pages, text) {
|
|
4
|
+
return text
|
|
5
|
+
.split("\n")
|
|
6
|
+
.map((line) => line.trim())
|
|
7
|
+
.filter((line) => line !== "")
|
|
8
|
+
.map((quote) => ({ quote, matches: findQuote(pages, quote) }));
|
|
9
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Box, type PageReading, type RegionReading } from "../core/index.js";
|
|
2
|
+
import type { PagemarkDocument } from "../document/index.js";
|
|
3
|
+
import type { ViewerDrag, ViewerMark } from "../viewer/index.js";
|
|
4
|
+
import { Behaviour } from "./behaviour.js";
|
|
5
|
+
/** NOTE: a rectangle dragged on one page; `isFinal` once the drag has ended. */
|
|
6
|
+
export type Region = {
|
|
7
|
+
page: number;
|
|
8
|
+
box: Box;
|
|
9
|
+
isFinal: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare function regionFromDrag(drag: ViewerDrag): Region;
|
|
12
|
+
export declare function regionReading(pages: ReadonlyArray<PageReading>, region: Region | null): RegionReading | null;
|
|
13
|
+
/** NOTE: the box itself, and over it the words it caught. */
|
|
14
|
+
export declare function regionMarks(region: Region | null, read: RegionReading | null): ViewerMark[];
|
|
15
|
+
export type BoxSelectionState = {
|
|
16
|
+
region: Region | null;
|
|
17
|
+
read: RegionReading | null;
|
|
18
|
+
marks: ViewerMark[];
|
|
19
|
+
};
|
|
20
|
+
/** NOTE: a box a reader drags on a page, and the words inside it, whatever their order on the page. Hand `onDrag` to the viewer. */
|
|
21
|
+
export declare class BoxSelection extends Behaviour<BoxSelectionState> {
|
|
22
|
+
#private;
|
|
23
|
+
constructor(document: PagemarkDocument | null);
|
|
24
|
+
readonly onDrag: (drag: ViewerDrag) => void;
|
|
25
|
+
readonly clear: () => void;
|
|
26
|
+
protected follow(state: BoxSelectionState): BoxSelectionState;
|
|
27
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { readRegion } from "../core/index.js";
|
|
2
|
+
import { Behaviour } from "./behaviour.js";
|
|
3
|
+
export function regionFromDrag(drag) {
|
|
4
|
+
const box = {
|
|
5
|
+
x: Math.min(drag.from.x, drag.to.x),
|
|
6
|
+
y: Math.min(drag.from.y, drag.to.y),
|
|
7
|
+
width: Math.abs(drag.to.x - drag.from.x),
|
|
8
|
+
height: Math.abs(drag.to.y - drag.from.y),
|
|
9
|
+
};
|
|
10
|
+
return { page: drag.page, box, isFinal: drag.phase === "end" };
|
|
11
|
+
}
|
|
12
|
+
export function regionReading(pages, region) {
|
|
13
|
+
const page = region ? pages[region.page] : undefined;
|
|
14
|
+
return region && page ? readRegion(page, region.box) : null;
|
|
15
|
+
}
|
|
16
|
+
/** NOTE: the box itself, and over it the words it caught. */
|
|
17
|
+
export function regionMarks(region, read) {
|
|
18
|
+
if (!region)
|
|
19
|
+
return [];
|
|
20
|
+
const box = { id: "region", page: region.page, rects: [region.box], tone: "region" };
|
|
21
|
+
return read ? [box, { id: "region-words", page: region.page, rects: read.rects, tone: "selection" }] : [box];
|
|
22
|
+
}
|
|
23
|
+
const NO_BOX = { region: null, read: null, marks: [] };
|
|
24
|
+
/** NOTE: a box a reader drags on a page, and the words inside it, whatever their order on the page. Hand `onDrag` to the viewer. */
|
|
25
|
+
export class BoxSelection extends Behaviour {
|
|
26
|
+
constructor(document) {
|
|
27
|
+
super(document, NO_BOX);
|
|
28
|
+
}
|
|
29
|
+
onDrag = (drag) => this.change(this.#of(regionFromDrag(drag)));
|
|
30
|
+
clear = () => this.change(NO_BOX);
|
|
31
|
+
follow(state) {
|
|
32
|
+
return this.#of(state.region);
|
|
33
|
+
}
|
|
34
|
+
#of(region) {
|
|
35
|
+
if (!region)
|
|
36
|
+
return NO_BOX;
|
|
37
|
+
const read = regionReading(this.pages, region);
|
|
38
|
+
return { region, read, marks: regionMarks(region, read) };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Match, type PageReading } from "../core/index.js";
|
|
2
|
+
import type { PagemarkDocument } from "../document/index.js";
|
|
3
|
+
import { Behaviour } from "./behaviour.js";
|
|
4
|
+
import { type MatchMark } from "./marks.js";
|
|
5
|
+
export type QuoteSearch = {
|
|
6
|
+
matches: Match[];
|
|
7
|
+
marks: MatchMark[];
|
|
8
|
+
/** NOTE: which match is in focus, counted from 0, or -1 when there is none. */
|
|
9
|
+
focused: number;
|
|
10
|
+
/** NOTE: the ids of every mark of the match in focus: two for a match that runs over a page break, none when there is no match. */
|
|
11
|
+
focusedMarkIds: string[];
|
|
12
|
+
};
|
|
13
|
+
/** NOTE: every place a quote occurs, as marks, with the one in focus held inside the range of what was found. */
|
|
14
|
+
export declare function quoteSearch(pages: ReadonlyArray<PageReading>, quote: string, focusIndex: number): QuoteSearch;
|
|
15
|
+
/** NOTE: the next or previous match, going round from the last to the first. */
|
|
16
|
+
export declare function stepFocus(current: number, by: number, count: number): number;
|
|
17
|
+
export type SearchState = QuoteSearch & {
|
|
18
|
+
quote: string;
|
|
19
|
+
/** NOTE: true while the quote is still being looked for in pages whose words have not arrived, so "not found" is not yet the answer. */
|
|
20
|
+
isPartial: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* NOTE: looks for a quote in a document and steps through the places it occurs. What is found in the words already
|
|
24
|
+
* here is shown at once; setting a quote fetches the rest, and `isPartial` says while they are on their way.
|
|
25
|
+
*/
|
|
26
|
+
export declare class Search extends Behaviour<SearchState, string> {
|
|
27
|
+
#private;
|
|
28
|
+
constructor(document: PagemarkDocument | null);
|
|
29
|
+
/** NOTE: takes the quote to look for. What the words already here hold is found at once, and the rest are fetched. */
|
|
30
|
+
readonly set: (quote: string) => void;
|
|
31
|
+
readonly stateFor: (quote: string) => SearchState;
|
|
32
|
+
readonly next: () => void;
|
|
33
|
+
readonly previous: () => void;
|
|
34
|
+
/** NOTE: puts one match in focus, held inside the range of what was found. Nothing is looked for again. */
|
|
35
|
+
readonly focus: (index: number) => void;
|
|
36
|
+
/** NOTE: with no quote, new words change nothing, so the state is kept and nobody is told as a document arrives. */
|
|
37
|
+
protected follow(state: SearchState): SearchState;
|
|
38
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { findQuote } from "../core/index.js";
|
|
2
|
+
import { Behaviour } from "./behaviour.js";
|
|
3
|
+
import { markIds, matchMarks } from "./marks.js";
|
|
4
|
+
const SEARCH_MARKS = "match";
|
|
5
|
+
/** NOTE: every place a quote occurs, as marks, with the one in focus held inside the range of what was found. */
|
|
6
|
+
export function quoteSearch(pages, quote, focusIndex) {
|
|
7
|
+
const matches = findQuote(pages, quote);
|
|
8
|
+
const marks = matchMarks(matches, SEARCH_MARKS);
|
|
9
|
+
const focused = matches.length === 0 ? -1 : Math.min(Math.max(focusIndex, 0), matches.length - 1);
|
|
10
|
+
return { matches, marks, focused, focusedMarkIds: markIds(marks, focused) };
|
|
11
|
+
}
|
|
12
|
+
/** NOTE: the next or previous match, going round from the last to the first. */
|
|
13
|
+
export function stepFocus(current, by, count) {
|
|
14
|
+
if (count === 0)
|
|
15
|
+
return 0;
|
|
16
|
+
return (Math.min(Math.max(current, 0), count - 1) + by + count) % count;
|
|
17
|
+
}
|
|
18
|
+
const NO_SEARCH = { quote: "", matches: [], marks: [], focused: -1, focusedMarkIds: [], isPartial: false };
|
|
19
|
+
/**
|
|
20
|
+
* NOTE: looks for a quote in a document and steps through the places it occurs. What is found in the words already
|
|
21
|
+
* here is shown at once; setting a quote fetches the rest, and `isPartial` says while they are on their way.
|
|
22
|
+
*/
|
|
23
|
+
export class Search extends Behaviour {
|
|
24
|
+
#foretold = null;
|
|
25
|
+
constructor(document) {
|
|
26
|
+
super(document, NO_SEARCH);
|
|
27
|
+
}
|
|
28
|
+
/** NOTE: takes the quote to look for. What the words already here hold is found at once, and the rest are fetched. */
|
|
29
|
+
set = (quote) => {
|
|
30
|
+
if (quote === this.get().quote)
|
|
31
|
+
return;
|
|
32
|
+
this.change(this.stateFor(quote));
|
|
33
|
+
if (quote.trim() !== "")
|
|
34
|
+
this.document?.whole().catch(() => undefined);
|
|
35
|
+
};
|
|
36
|
+
stateFor = (quote) => {
|
|
37
|
+
const state = this.get();
|
|
38
|
+
if (quote === state.quote)
|
|
39
|
+
return state;
|
|
40
|
+
const { pages, isComplete } = this;
|
|
41
|
+
const foretold = this.#foretold;
|
|
42
|
+
if (foretold && foretold.state.quote === quote && foretold.pages === pages && foretold.isComplete === isComplete)
|
|
43
|
+
return foretold.state;
|
|
44
|
+
this.#foretold = { pages, isComplete, state: this.#found(quote, 0) };
|
|
45
|
+
return this.#foretold.state;
|
|
46
|
+
};
|
|
47
|
+
next = () => this.focus(stepFocus(this.get().focused, 1, this.get().matches.length));
|
|
48
|
+
previous = () => this.focus(stepFocus(this.get().focused, -1, this.get().matches.length));
|
|
49
|
+
/** NOTE: puts one match in focus, held inside the range of what was found. Nothing is looked for again. */
|
|
50
|
+
focus = (index) => {
|
|
51
|
+
const state = this.get();
|
|
52
|
+
const focused = state.matches.length === 0 ? -1 : Math.min(Math.max(index, 0), state.matches.length - 1);
|
|
53
|
+
if (focused !== state.focused)
|
|
54
|
+
this.change({ ...state, focused, focusedMarkIds: markIds(state.marks, focused) });
|
|
55
|
+
};
|
|
56
|
+
/** NOTE: with no quote, new words change nothing, so the state is kept and nobody is told as a document arrives. */
|
|
57
|
+
follow(state) {
|
|
58
|
+
return state.quote.trim() === "" ? state : this.#found(state.quote, state.focused);
|
|
59
|
+
}
|
|
60
|
+
#found(quote, focus) {
|
|
61
|
+
return { ...quoteSearch(this.pages, quote, focus), quote, isPartial: quote.trim() !== "" && !this.isComplete };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type PageReading } from "../core/index.js";
|
|
2
|
+
import type { PagemarkDocument } from "../document/index.js";
|
|
3
|
+
import type { ViewerDrag, ViewerMark } from "../viewer/index.js";
|
|
4
|
+
import { Behaviour } from "./behaviour.js";
|
|
5
|
+
/** NOTE: a run of whole words on one page. */
|
|
6
|
+
export type WordRange = {
|
|
7
|
+
page: number;
|
|
8
|
+
first: number;
|
|
9
|
+
last: number;
|
|
10
|
+
};
|
|
11
|
+
/** NOTE: the words from the one nearest where a drag began to the one nearest where it is now, in reading order. */
|
|
12
|
+
export declare function selectionFromDrag(pages: ReadonlyArray<PageReading>, drag: ViewerDrag): WordRange | null;
|
|
13
|
+
export declare function selectionText(pages: ReadonlyArray<PageReading>, selection: WordRange | null): string;
|
|
14
|
+
export declare function selectionMarks(pages: ReadonlyArray<PageReading>, selection: WordRange | null): ViewerMark[];
|
|
15
|
+
export type WordSelectionState = {
|
|
16
|
+
range: WordRange | null;
|
|
17
|
+
text: string;
|
|
18
|
+
marks: ViewerMark[];
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* NOTE: the words a reader drags across, kept as a mark with their text, for an application that does something
|
|
22
|
+
* with them. Hand `onDrag` to the viewer. It is the application's own selection; the browser's is the viewer's text.
|
|
23
|
+
*/
|
|
24
|
+
export declare class WordSelection extends Behaviour<WordSelectionState> {
|
|
25
|
+
#private;
|
|
26
|
+
constructor(document: PagemarkDocument | null);
|
|
27
|
+
readonly onDrag: (drag: ViewerDrag) => void;
|
|
28
|
+
readonly clear: () => void;
|
|
29
|
+
protected follow(state: WordSelectionState): WordSelectionState;
|
|
30
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { nearestWord, wordRangeRects, wordRangeText } from "../core/index.js";
|
|
2
|
+
import { Behaviour } from "./behaviour.js";
|
|
3
|
+
/** NOTE: the words from the one nearest where a drag began to the one nearest where it is now, in reading order. */
|
|
4
|
+
export function selectionFromDrag(pages, drag) {
|
|
5
|
+
const page = pages[drag.page];
|
|
6
|
+
if (!page)
|
|
7
|
+
return null;
|
|
8
|
+
const from = nearestWord(page, drag.from.x, drag.from.y);
|
|
9
|
+
const to = nearestWord(page, drag.to.x, drag.to.y);
|
|
10
|
+
if (from === null || to === null)
|
|
11
|
+
return null;
|
|
12
|
+
return { page: drag.page, first: Math.min(from, to), last: Math.max(from, to) };
|
|
13
|
+
}
|
|
14
|
+
export function selectionText(pages, selection) {
|
|
15
|
+
const page = selection ? pages[selection.page] : undefined;
|
|
16
|
+
return selection && page ? wordRangeText(page, selection.first, selection.last) : "";
|
|
17
|
+
}
|
|
18
|
+
export function selectionMarks(pages, selection) {
|
|
19
|
+
const page = selection ? pages[selection.page] : undefined;
|
|
20
|
+
if (!selection || !page)
|
|
21
|
+
return [];
|
|
22
|
+
return [{ id: "selection", page: selection.page, rects: wordRangeRects(page, selection.first, selection.last), tone: "selection" }];
|
|
23
|
+
}
|
|
24
|
+
const NO_SELECTION = { range: null, text: "", marks: [] };
|
|
25
|
+
/**
|
|
26
|
+
* NOTE: the words a reader drags across, kept as a mark with their text, for an application that does something
|
|
27
|
+
* with them. Hand `onDrag` to the viewer. It is the application's own selection; the browser's is the viewer's text.
|
|
28
|
+
*/
|
|
29
|
+
export class WordSelection extends Behaviour {
|
|
30
|
+
constructor(document) {
|
|
31
|
+
super(document, NO_SELECTION);
|
|
32
|
+
}
|
|
33
|
+
onDrag = (drag) => this.change(this.#of(selectionFromDrag(this.pages, drag)));
|
|
34
|
+
clear = () => this.change(NO_SELECTION);
|
|
35
|
+
follow(state) {
|
|
36
|
+
return this.#of(state.range);
|
|
37
|
+
}
|
|
38
|
+
#of(range) {
|
|
39
|
+
return range ? { range, text: selectionText(this.pages, range), marks: selectionMarks(this.pages, range) } : NO_SELECTION;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PageReading } from "../core/index.js";
|
|
2
|
+
import type { ViewerTextLine } from "../viewer/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: every page's words as the lines a viewer lets a reader select, in page order. Words a reader cannot see are
|
|
5
|
+
* left out, since the text stands for what the page shows. A page keeps the same lines while it is the same page, so
|
|
6
|
+
* a viewer draws again only the pages whose words arrived or changed.
|
|
7
|
+
*/
|
|
8
|
+
export declare function textLines(pages: ReadonlyArray<PageReading>): ViewerTextLine[][];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const known = new WeakMap();
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: every page's words as the lines a viewer lets a reader select, in page order. Words a reader cannot see are
|
|
4
|
+
* left out, since the text stands for what the page shows. A page keeps the same lines while it is the same page, so
|
|
5
|
+
* a viewer draws again only the pages whose words arrived or changed.
|
|
6
|
+
*/
|
|
7
|
+
export function textLines(pages) {
|
|
8
|
+
return pages.map((page) => {
|
|
9
|
+
let lines = known.get(page);
|
|
10
|
+
if (!lines) {
|
|
11
|
+
lines = page.lines
|
|
12
|
+
.map((line) => ({
|
|
13
|
+
box: line.box,
|
|
14
|
+
words: page.words
|
|
15
|
+
.slice(line.firstWord, line.lastWord + 1)
|
|
16
|
+
.filter((word) => !word.hidden)
|
|
17
|
+
.map((word) => ({ text: word.text, box: word.box })),
|
|
18
|
+
}))
|
|
19
|
+
.filter((line) => line.words.length > 0);
|
|
20
|
+
known.set(page, lines);
|
|
21
|
+
}
|
|
22
|
+
return lines;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Readable } from "../document/index.js";
|
|
2
|
+
import type { Viewer, ViewerFit, ViewerRotation, ViewerSpread } from "../viewer/index.js";
|
|
3
|
+
/** NOTE: what a bar over a viewer shows. */
|
|
4
|
+
export type ViewerState = {
|
|
5
|
+
/** NOTE: screen pixels per page unit. */
|
|
6
|
+
zoom: number;
|
|
7
|
+
/** NOTE: what the zoom is held to while the pane changes size, or null after a zoom the reader chose. */
|
|
8
|
+
fit: ViewerFit | null;
|
|
9
|
+
/** NOTE: how far every page is shown turned, clockwise, in degrees. */
|
|
10
|
+
rotation: ViewerRotation;
|
|
11
|
+
/** NOTE: whether pages stand one to a row or two. */
|
|
12
|
+
spread: ViewerSpread;
|
|
13
|
+
/** NOTE: the page at the middle of the pane, counted from 0. Of two to a row, it is the left one. */
|
|
14
|
+
page: number;
|
|
15
|
+
pageCount: number;
|
|
16
|
+
/** NOTE: false once the zoom is as far in as the viewer goes, so a bar can stand its button down. */
|
|
17
|
+
canZoomIn: boolean;
|
|
18
|
+
/** NOTE: false once the zoom is as far out as the viewer goes. */
|
|
19
|
+
canZoomOut: boolean;
|
|
20
|
+
};
|
|
21
|
+
/** NOTE: the state to show while no viewer is built. */
|
|
22
|
+
export declare const NO_VIEWER: ViewerState;
|
|
23
|
+
/**
|
|
24
|
+
* NOTE: the part of a viewer its state is read from, so a test, or a caller's own viewer, can stand in for one. One
|
|
25
|
+
* that gives no `minZoom` or `maxZoom` has no end that way.
|
|
26
|
+
*/
|
|
27
|
+
export type WatchableViewer = Pick<Viewer, "zoom" | "fit" | "rotation" | "spread" | "pageInView" | "pageCount"> & Partial<Pick<Viewer, "minZoom" | "maxZoom">> & {
|
|
28
|
+
on(event: "zoom" | "pageInView", listener: () => void): () => void;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* NOTE: a viewer's zoom, fit, rotation, spread and page as one state that says when it changes. A viewer tells of them in two events; a
|
|
32
|
+
* bar shows them together, and a framework follows one state more easily than several events.
|
|
33
|
+
* NOTE: it holds nothing of its own. Asked, it reads the viewer, and gives the state it gave before while that is
|
|
34
|
+
* still true. It listens to the viewer only while something listens to it.
|
|
35
|
+
*/
|
|
36
|
+
export declare function viewerState(viewer: WatchableViewer): Readable<ViewerState>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** NOTE: the state to show while no viewer is built. */
|
|
2
|
+
export const NO_VIEWER = { zoom: 1, fit: null, rotation: 0, spread: "single", page: 0, pageCount: 0, canZoomIn: false, canZoomOut: false };
|
|
3
|
+
/** NOTE: a viewer's fit, rotation and spread change only as it zooms, so these two tell of everything in the state. */
|
|
4
|
+
const WATCHED = ["zoom", "pageInView"];
|
|
5
|
+
const KEYS = ["zoom", "fit", "rotation", "spread", "page", "pageCount", "canZoomIn", "canZoomOut"];
|
|
6
|
+
/**
|
|
7
|
+
* NOTE: a viewer's zoom, fit, rotation, spread and page as one state that says when it changes. A viewer tells of them in two events; a
|
|
8
|
+
* bar shows them together, and a framework follows one state more easily than several events.
|
|
9
|
+
* NOTE: it holds nothing of its own. Asked, it reads the viewer, and gives the state it gave before while that is
|
|
10
|
+
* still true. It listens to the viewer only while something listens to it.
|
|
11
|
+
*/
|
|
12
|
+
export function viewerState(viewer) {
|
|
13
|
+
const listeners = new Set();
|
|
14
|
+
let stops = [];
|
|
15
|
+
let known = null;
|
|
16
|
+
const get = () => {
|
|
17
|
+
const page = Math.max(0, viewer.pageInView);
|
|
18
|
+
const next = {
|
|
19
|
+
zoom: viewer.zoom,
|
|
20
|
+
fit: viewer.fit,
|
|
21
|
+
rotation: viewer.rotation,
|
|
22
|
+
spread: viewer.spread,
|
|
23
|
+
page,
|
|
24
|
+
pageCount: viewer.pageCount,
|
|
25
|
+
canZoomIn: viewer.maxZoom === undefined || viewer.zoom < viewer.maxZoom,
|
|
26
|
+
canZoomOut: viewer.minZoom === undefined || viewer.zoom > viewer.minZoom,
|
|
27
|
+
};
|
|
28
|
+
if (known && KEYS.every((key) => known?.[key] === next[key]))
|
|
29
|
+
return known;
|
|
30
|
+
known = next;
|
|
31
|
+
return known;
|
|
32
|
+
};
|
|
33
|
+
const tell = () => {
|
|
34
|
+
const before = known;
|
|
35
|
+
if (get() === before)
|
|
36
|
+
return;
|
|
37
|
+
for (const listener of [...listeners])
|
|
38
|
+
listener();
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
get,
|
|
42
|
+
subscribe(listener) {
|
|
43
|
+
if (listeners.size === 0) {
|
|
44
|
+
get();
|
|
45
|
+
stops = WATCHED.map((event) => viewer.on(event, tell));
|
|
46
|
+
}
|
|
47
|
+
listeners.add(listener);
|
|
48
|
+
return () => {
|
|
49
|
+
listeners.delete(listener);
|
|
50
|
+
if (listeners.size > 0)
|
|
51
|
+
return;
|
|
52
|
+
for (const stop of stops)
|
|
53
|
+
stop();
|
|
54
|
+
stops = [];
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FilePieces } from "../engine/index.js";
|
|
2
|
+
export type BlobPiecesOptions = {
|
|
3
|
+
/** NOTE: reads the bytes from `start` up to `end` at once; a test stands in for a worker's own reader. */
|
|
4
|
+
readSlice?: (blob: Blob, start: number, end: number) => Uint8Array;
|
|
5
|
+
pieceBytes?: number;
|
|
6
|
+
piecesKept?: number;
|
|
7
|
+
};
|
|
8
|
+
/** NOTE: a Blob as a file the engine reads a piece at a time. */
|
|
9
|
+
export declare function blobPieces(blob: Blob, options?: BlobPiecesOptions): FilePieces;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NOTE: how much of a file is read from its Blob at a time, and how many such pieces are kept. The engine asks for
|
|
3
|
+
* many small pieces, so each reaches the Blob seldom, and the file is never held whole.
|
|
4
|
+
*/
|
|
5
|
+
const PIECE_BYTES = 1 << 20;
|
|
6
|
+
const PIECES_KEPT = 8;
|
|
7
|
+
const readSliceAtOnce = (blob, start, end) => new Uint8Array(new FileReaderSync().readAsArrayBuffer(blob.slice(start, end)));
|
|
8
|
+
/** NOTE: a Blob as a file the engine reads a piece at a time. */
|
|
9
|
+
export function blobPieces(blob, options = {}) {
|
|
10
|
+
const readSlice = options.readSlice ?? readSliceAtOnce;
|
|
11
|
+
const pieceBytes = options.pieceBytes ?? PIECE_BYTES;
|
|
12
|
+
const piecesKept = options.piecesKept ?? PIECES_KEPT;
|
|
13
|
+
/** NOTE: oldest first, so the piece let go is the one asked for least lately. */
|
|
14
|
+
const kept = new Map();
|
|
15
|
+
const piece = (order) => {
|
|
16
|
+
const known = kept.get(order);
|
|
17
|
+
if (known) {
|
|
18
|
+
kept.delete(order);
|
|
19
|
+
kept.set(order, known);
|
|
20
|
+
return known;
|
|
21
|
+
}
|
|
22
|
+
const start = order * pieceBytes;
|
|
23
|
+
const read = readSlice(blob, start, Math.min(blob.size, start + pieceBytes));
|
|
24
|
+
kept.set(order, read);
|
|
25
|
+
const oldest = kept.keys().next().value;
|
|
26
|
+
if (kept.size > piecesKept && oldest !== undefined)
|
|
27
|
+
kept.delete(oldest);
|
|
28
|
+
return read;
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
size: blob.size,
|
|
32
|
+
read(offset, length) {
|
|
33
|
+
const end = Math.min(blob.size, offset + length);
|
|
34
|
+
if (offset >= end)
|
|
35
|
+
return new Uint8Array(0);
|
|
36
|
+
const first = Math.floor(offset / pieceBytes);
|
|
37
|
+
const last = Math.floor((end - 1) / pieceBytes);
|
|
38
|
+
if (first === last)
|
|
39
|
+
return piece(first).subarray(offset - first * pieceBytes, end - first * pieceBytes);
|
|
40
|
+
const joined = new Uint8Array(end - offset);
|
|
41
|
+
for (let order = first; order <= last; order += 1) {
|
|
42
|
+
const start = order * pieceBytes;
|
|
43
|
+
const from = Math.max(offset, start);
|
|
44
|
+
const to = Math.min(end, start + pieceBytes);
|
|
45
|
+
joined.set(piece(order).subarray(from - start, to - start), from - offset);
|
|
46
|
+
}
|
|
47
|
+
return joined;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DocumentSource } from "../document/index.js";
|
|
2
|
+
import { type LocalPdf, type OpenLocalPdfOptions } from "./local-pdf.js";
|
|
3
|
+
/** NOTE: a PDF as a browser has it: a file a reader chose, one fetched, or its bytes. */
|
|
4
|
+
export type BrowserFile = Blob | ArrayBuffer | Uint8Array;
|
|
5
|
+
export type BrowserSourceOptions = OpenLocalPdfOptions & {
|
|
6
|
+
/** NOTE: for a caller that shows or tells apart several documents. Left out, they are the file's own name when it has one. */
|
|
7
|
+
id?: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* NOTE: a source over a PDF that is opened when first asked for anything, and once. Whoever opens it is given, so
|
|
12
|
+
* the source can be tried with no engine.
|
|
13
|
+
* NOTE: its outline has every page's size and no words, since the sizes are known as soon as the file is open and
|
|
14
|
+
* a page is read only when its words are asked for.
|
|
15
|
+
*/
|
|
16
|
+
export declare function sourceFrom(open: () => Promise<LocalPdf>, document?: {
|
|
17
|
+
id?: string;
|
|
18
|
+
name?: string;
|
|
19
|
+
}): DocumentSource;
|
|
20
|
+
/**
|
|
21
|
+
* NOTE: a PDF read and drawn in the reader's own browser, with no server: its text layer gives the words, and the
|
|
22
|
+
* engine draws the pages. A page whose text cannot be trusted has no words and says why, since a browser has no OCR.
|
|
23
|
+
* `file` may be a function that gets the file, for one that should be fetched only if the source is ever used.
|
|
24
|
+
*/
|
|
25
|
+
export declare function browserSource(file: BrowserFile | (() => BrowserFile | Promise<BrowserFile>), options?: BrowserSourceOptions): DocumentSource;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { openLocalPdf } from "./local-pdf.js";
|
|
2
|
+
const UNNAMED = { id: "local", name: "document.pdf" };
|
|
3
|
+
/**
|
|
4
|
+
* NOTE: a source over a PDF that is opened when first asked for anything, and once. Whoever opens it is given, so
|
|
5
|
+
* the source can be tried with no engine.
|
|
6
|
+
* NOTE: its outline has every page's size and no words, since the sizes are known as soon as the file is open and
|
|
7
|
+
* a page is read only when its words are asked for.
|
|
8
|
+
*/
|
|
9
|
+
export function sourceFrom(open, document = {}) {
|
|
10
|
+
let opening = null;
|
|
11
|
+
let pdf = null;
|
|
12
|
+
let isClosed = false;
|
|
13
|
+
const opened = () => {
|
|
14
|
+
opening ??= open().then((made) => {
|
|
15
|
+
if (isClosed)
|
|
16
|
+
made.close();
|
|
17
|
+
else
|
|
18
|
+
pdf = made;
|
|
19
|
+
return made;
|
|
20
|
+
});
|
|
21
|
+
return opening;
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
outline: () => opened().then((made) => ({
|
|
25
|
+
...UNNAMED,
|
|
26
|
+
...document,
|
|
27
|
+
kind: "pdf",
|
|
28
|
+
status: "ready",
|
|
29
|
+
pageCount: made.pages.length,
|
|
30
|
+
pagesRead: made.pages.length,
|
|
31
|
+
pages: made.pages.map((size, index) => ({ index, ...size, source: "layer", text: "", words: [], lines: [] })),
|
|
32
|
+
})),
|
|
33
|
+
pages: (from, count) => opened().then((made) => {
|
|
34
|
+
const wanted = Array.from({ length: Math.max(0, Math.min(count, made.pages.length - from)) }, (_, order) => from + order);
|
|
35
|
+
return Promise.all(wanted.map((index) => made.read(index).then((answer) => answer.reading)));
|
|
36
|
+
}),
|
|
37
|
+
pageImage: (page, scale, signal) => opened().then((made) => made.imageUrl(page, scale, signal)),
|
|
38
|
+
tile: (page, scale, box, signal) => opened().then((made) => made.tileUrl(page, scale, box, signal)),
|
|
39
|
+
release: (url) => pdf?.releaseImage(url),
|
|
40
|
+
close() {
|
|
41
|
+
isClosed = true;
|
|
42
|
+
pdf?.close();
|
|
43
|
+
pdf = null;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** NOTE: bytes a caller holds become a Blob of their own, a copy, so the caller's stay theirs to change or let go. */
|
|
48
|
+
function blobOf(file) {
|
|
49
|
+
if (file instanceof Blob)
|
|
50
|
+
return file;
|
|
51
|
+
return new Blob([file]);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* NOTE: a PDF read and drawn in the reader's own browser, with no server: its text layer gives the words, and the
|
|
55
|
+
* engine draws the pages. A page whose text cannot be trusted has no words and says why, since a browser has no OCR.
|
|
56
|
+
* `file` may be a function that gets the file, for one that should be fetched only if the source is ever used.
|
|
57
|
+
*/
|
|
58
|
+
export function browserSource(file, options = {}) {
|
|
59
|
+
const named = typeof File !== "undefined" && file instanceof File ? { id: file.name, name: file.name } : {};
|
|
60
|
+
const document = { ...named, ...(options.id === undefined ? {} : { id: options.id }), ...(options.name === undefined ? {} : { name: options.name }) };
|
|
61
|
+
return sourceFrom(async () => openLocalPdf(blobOf(typeof file === "function" ? await file() : file), options), document);
|
|
62
|
+
}
|
package/browser/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { browserSource } from "./browser-source.js";
|