@scoriiu/fenshot 0.1.5 → 0.1.7
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 +25 -2
- package/dist/detect.d.ts +24 -0
- package/dist/detect.js +167 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/reader/Library.d.ts +11 -0
- package/dist/reader/Library.js +74 -0
- package/dist/reader/PageView.d.ts +35 -0
- package/dist/reader/PageView.js +80 -0
- package/dist/reader/ReadApp.d.ts +9 -0
- package/dist/reader/ReadApp.js +122 -0
- package/dist/reader/Reader.d.ts +8 -0
- package/dist/reader/Reader.js +117 -0
- package/dist/reader/ReaderBar.d.ts +30 -0
- package/dist/reader/ReaderBar.js +36 -0
- package/dist/reader/SideGrip.d.ts +12 -0
- package/dist/reader/SideGrip.js +66 -0
- package/dist/reader/find.d.ts +42 -0
- package/dist/reader/find.js +279 -0
- package/dist/reader/host.d.ts +90 -0
- package/dist/reader/host.js +66 -0
- package/dist/reader/index.d.ts +3 -0
- package/dist/reader/index.js +3 -0
- package/dist/reader/pdf.d.ts +46 -0
- package/dist/reader/pdf.js +130 -0
- package/dist/reader/reader.css +1084 -0
- package/dist/reader/store.d.ts +63 -0
- package/dist/reader/store.js +103 -0
- package/dist/reader/useDiagrams.d.ts +18 -0
- package/dist/reader/useDiagrams.js +146 -0
- package/dist/reader/usePages.d.ts +18 -0
- package/dist/reader/usePages.js +148 -0
- package/dist/recognize.d.ts +8 -0
- package/dist/recognize.js +59 -20
- package/package.json +29 -7
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Live demo: **[fenshot.com](https://fenshot.com/)**
|
|
|
10
10
|
|
|
11
11
|
Chessboard recognition that runs entirely in the browser: finds the board in any screenshot (chess.com, Lichess, book diagrams, reddit lightboxes), reads the position with a CNN tile classifier, and returns a FEN with per-tile confidence. No server, no account, nothing leaves the page.
|
|
12
12
|
|
|
13
|
-
Extracted from the position-import feature of [kibitzo.com](https://kibitzo.com
|
|
13
|
+
Extracted from the position-import feature of [kibitzo.com](https://kibitzo.com/?utm_source=fenshot&utm_medium=npm&utm_campaign=readme), where it runs in production.
|
|
14
14
|
|
|
15
15
|
## Install
|
|
16
16
|
|
|
@@ -87,6 +87,29 @@ Compose a full analyzable FEN from a bare placement. Castling rights are inferre
|
|
|
87
87
|
|
|
88
88
|
`findChessboardCorners`, `snapCorners`, `extractTiles`, `rgbaToGray`, `probsToPlacement`, `flipPlacement` are all exported for custom pipelines (e.g. Node with a raster library instead of canvas).
|
|
89
89
|
|
|
90
|
+
## A chess-book reader, for React
|
|
91
|
+
|
|
92
|
+
`@scoriiu/fenshot/reader` opens a chess book (PDF) in the browser, finds and reads every diagram in it, and puts
|
|
93
|
+
the one the reader clicks on a board beside the page. The book stays on the device (IndexedDB). It runs
|
|
94
|
+
[fenshot.com/read](https://fenshot.com/read/) and [kibitzo.com/read](https://kibitzo.com/read); each site gives
|
|
95
|
+
its own words and board through `ReaderHost`. It needs `react` and `pdfjs-dist` (optional peers: the recognizer
|
|
96
|
+
alone does not).
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { createRecognizer } from "@scoriiu/fenshot";
|
|
100
|
+
import { BookReader, englishWords, type ReaderHost } from "@scoriiu/fenshot/reader";
|
|
101
|
+
import "@scoriiu/fenshot/reader.css";
|
|
102
|
+
|
|
103
|
+
const host: ReaderHost = {
|
|
104
|
+
recognizer: createRecognizer({ modelUrl, wasmPaths }),
|
|
105
|
+
pdf: { workerSrc: "/pdfjs/pdf.worker.min.mjs", assets: "/pdfjs/" }, // pdfjs-dist's worker and its wasm/, cmaps/, standard_fonts/, iccs/
|
|
106
|
+
t: englishWords,
|
|
107
|
+
BoardPanel: MyBoard, // ({ picked: { page, diagram }, onClose }) => your board
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
<BookReader host={host} />;
|
|
111
|
+
```
|
|
112
|
+
|
|
90
113
|
## How it works, and why it reads book diagrams
|
|
91
114
|
|
|
92
115
|
1. **Detection** is a TypeScript port of `chessboard_finder.py` from [Elucidation/tensorflow_chessbot](https://github.com/Elucidation/tensorflow_chessbot) (MIT): board edges produce evenly spaced gradient peaks; find the 7-line arithmetic sequence, pick the sub-grid that best matches an ideal checkerboard. One measured deviation: the reference's scale-dependent noise pre-gate is removed, it rejected page-wide screenshots whose board spans a small part of the frame, and against our fixture corpus it rejected nothing the sequence search did not already reject.
|
|
@@ -108,7 +131,7 @@ The model is reproducible, not just downloadable: the full training pipeline (as
|
|
|
108
131
|
|
|
109
132
|
- Board detection algorithm: [Elucidation/tensorflow_chessbot](https://github.com/Elucidation/tensorflow_chessbot) (MIT).
|
|
110
133
|
- Piece set and board theme assets used as training input: lichess ([lila](https://github.com/lichess-org/lila), free licenses). The corpus also includes other sites' themes (including chess.com) so the recognizer reads their screenshots too; those assets are training input only and are never redistributed, only trained weights ship.
|
|
111
|
-
- Built and maintained by [kibitzo.com](https://kibitzo.com
|
|
134
|
+
- Built and maintained by [kibitzo.com](https://kibitzo.com/?utm_source=fenshot&utm_medium=npm&utm_campaign=readme).
|
|
112
135
|
|
|
113
136
|
## License
|
|
114
137
|
|
package/dist/detect.d.ts
CHANGED
|
@@ -44,4 +44,28 @@ export interface GrayImage {
|
|
|
44
44
|
* the alignment with higher mean confidence (recognize.ts).
|
|
45
45
|
*/
|
|
46
46
|
export declare function snapCorners(img: GrayImage, box: BoardCorners): BoardCorners;
|
|
47
|
+
/**
|
|
48
|
+
* How much a box looks like a chessboard, whatever stands on it: the
|
|
49
|
+
* Pearson correlation (-1..1) between the 64 tile backgrounds and the
|
|
50
|
+
* chessboard colouring. A box on a real board scores high in any theme;
|
|
51
|
+
* a box that hangs off the board, or sits at the wrong scale, mixes page
|
|
52
|
+
* background into the tiles and scores low. Patches average over a few
|
|
53
|
+
* pixels, so hatched dark squares read as their mean grey.
|
|
54
|
+
*/
|
|
55
|
+
export declare function boardness(img: GrayImage, box: BoardCorners): number;
|
|
56
|
+
/**
|
|
57
|
+
* Second look: boards the plain search misses or misplaces because the
|
|
58
|
+
* board itself is full of edges. Hatched dark squares (printed diagrams,
|
|
59
|
+
* chess.com's newspaper board) put more gradient energy inside the
|
|
60
|
+
* squares than on the grid lines, and marbled or low-contrast boards can
|
|
61
|
+
* slide the grid one tile diagonally, which keeps the colouring and so
|
|
62
|
+
* slips past repairParity. On a blurred copy the hatching melts to grey
|
|
63
|
+
* and the grid lines dominate again.
|
|
64
|
+
*
|
|
65
|
+
* Returns the boxes found on blurred copies, each with its grid-snapped
|
|
66
|
+
* variant, that are square and look like a board, without duplicates and
|
|
67
|
+
* without anything in `known`. They are CANDIDATES: the caller lets the
|
|
68
|
+
* tile classifier choose (recognize.ts).
|
|
69
|
+
*/
|
|
70
|
+
export declare function findBlurredBoardCandidates(img: GrayImage, known?: BoardCorners[]): BoardCorners[];
|
|
47
71
|
export declare function findChessboardCorners(img: GrayImage): BoardCorners | null;
|
package/dist/detect.js
CHANGED
|
@@ -376,6 +376,173 @@ function repairParity(img, box) {
|
|
|
376
376
|
}
|
|
377
377
|
return best;
|
|
378
378
|
}
|
|
379
|
+
/** Box blur (edges clamped), two separable passes. */
|
|
380
|
+
function boxBlur(img, radius) {
|
|
381
|
+
const { width: w, height: h, data } = img;
|
|
382
|
+
const span = 2 * radius + 1;
|
|
383
|
+
const tmp = new Float32Array(w * h);
|
|
384
|
+
const out = new Float32Array(w * h);
|
|
385
|
+
for (let y = 0; y < h; y++) {
|
|
386
|
+
const row = y * w;
|
|
387
|
+
let acc = 0;
|
|
388
|
+
for (let x = -radius; x <= radius; x++)
|
|
389
|
+
acc += data[row + Math.min(w - 1, Math.max(0, x))];
|
|
390
|
+
for (let x = 0; x < w; x++) {
|
|
391
|
+
tmp[row + x] = acc / span;
|
|
392
|
+
acc += data[row + Math.min(w - 1, x + radius + 1)] - data[row + Math.max(0, x - radius)];
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
for (let x = 0; x < w; x++) {
|
|
396
|
+
let acc = 0;
|
|
397
|
+
for (let y = -radius; y <= radius; y++)
|
|
398
|
+
acc += tmp[Math.min(h - 1, Math.max(0, y)) * w + x];
|
|
399
|
+
for (let y = 0; y < h; y++) {
|
|
400
|
+
out[y * w + x] = acc / span;
|
|
401
|
+
acc += tmp[Math.min(h - 1, y + radius + 1) * w + x] - tmp[Math.max(0, y - radius) * w + x];
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return { data: out, width: w, height: h };
|
|
405
|
+
}
|
|
406
|
+
/** Where in a tile its background shows: near the four corners, which
|
|
407
|
+
* pieces (drawn centered) do not reach. */
|
|
408
|
+
const BACKGROUND_SAMPLES = [
|
|
409
|
+
[0.16, 0.16],
|
|
410
|
+
[0.84, 0.16],
|
|
411
|
+
[0.16, 0.84],
|
|
412
|
+
[0.84, 0.84],
|
|
413
|
+
];
|
|
414
|
+
/** Background brightness of one tile: the median of small patches at its
|
|
415
|
+
* corners. Null when the tile lies mostly outside the image. */
|
|
416
|
+
function tileBackground(img, x, y, tw, th, patch) {
|
|
417
|
+
const vals = [];
|
|
418
|
+
for (const [fx, fy] of BACKGROUND_SAMPLES) {
|
|
419
|
+
const cx = Math.round(x + fx * tw);
|
|
420
|
+
const cy = Math.round(y + fy * th);
|
|
421
|
+
let sum = 0;
|
|
422
|
+
let n = 0;
|
|
423
|
+
for (let py = Math.max(0, cy - patch); py <= Math.min(img.height - 1, cy + patch); py++) {
|
|
424
|
+
for (let px = Math.max(0, cx - patch); px <= Math.min(img.width - 1, cx + patch); px++) {
|
|
425
|
+
sum += img.data[py * img.width + px];
|
|
426
|
+
n++;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (n)
|
|
430
|
+
vals.push(sum / n);
|
|
431
|
+
}
|
|
432
|
+
return vals.length < 2 ? null : median(vals);
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* How much a box looks like a chessboard, whatever stands on it: the
|
|
436
|
+
* Pearson correlation (-1..1) between the 64 tile backgrounds and the
|
|
437
|
+
* chessboard colouring. A box on a real board scores high in any theme;
|
|
438
|
+
* a box that hangs off the board, or sits at the wrong scale, mixes page
|
|
439
|
+
* background into the tiles and scores low. Patches average over a few
|
|
440
|
+
* pixels, so hatched dark squares read as their mean grey.
|
|
441
|
+
*/
|
|
442
|
+
export function boardness(img, box) {
|
|
443
|
+
const tw = (box.x1 - box.x0) / 8;
|
|
444
|
+
const th = (box.y1 - box.y0) / 8;
|
|
445
|
+
if (tw < 6 || th < 6)
|
|
446
|
+
return -1;
|
|
447
|
+
const patch = Math.max(1, Math.round(tw / 10));
|
|
448
|
+
const backgrounds = [];
|
|
449
|
+
const colours = [];
|
|
450
|
+
for (let r = 0; r < 8; r++) {
|
|
451
|
+
for (let c = 0; c < 8; c++) {
|
|
452
|
+
const bg = tileBackground(img, box.x0 + c * tw, box.y0 + r * th, tw, th, patch);
|
|
453
|
+
if (bg === null)
|
|
454
|
+
continue;
|
|
455
|
+
backgrounds.push(bg);
|
|
456
|
+
colours.push((r + c) % 2 === 0 ? 1 : -1);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (backgrounds.length < 48)
|
|
460
|
+
return -1;
|
|
461
|
+
const meanBg = backgrounds.reduce((a, b) => a + b, 0) / backgrounds.length;
|
|
462
|
+
const meanCol = colours.reduce((a, b) => a + b, 0) / colours.length;
|
|
463
|
+
let cov = 0;
|
|
464
|
+
let varBg = 0;
|
|
465
|
+
let varCol = 0;
|
|
466
|
+
for (let i = 0; i < backgrounds.length; i++) {
|
|
467
|
+
cov += (backgrounds[i] - meanBg) * (colours[i] - meanCol);
|
|
468
|
+
varBg += (backgrounds[i] - meanBg) ** 2;
|
|
469
|
+
varCol += (colours[i] - meanCol) ** 2;
|
|
470
|
+
}
|
|
471
|
+
return varBg > 0 && varCol > 0 ? cov / Math.sqrt(varBg * varCol) : 0;
|
|
472
|
+
}
|
|
473
|
+
/** Blur radii for the second look. The right one depends on the hatch
|
|
474
|
+
* period, which depends on scale: measured on the benchmark, 1 to 4 px
|
|
475
|
+
* between them find hatched boards from 110 to 200 dpi and chess.com's
|
|
476
|
+
* newspaper board; 6 px added nothing. */
|
|
477
|
+
const BLUR_RADII = [1, 2, 3, 4];
|
|
478
|
+
/** Above this size the second look runs at half resolution: tiles are
|
|
479
|
+
* large enough to stand the coarser grid, and a page with no board on it
|
|
480
|
+
* (the extension's whole-tab scan) costs a quarter as much. Halving is
|
|
481
|
+
* itself a 2x2 blur, so the radii shrink with it. */
|
|
482
|
+
const HALF_RES_ABOVE = 1000;
|
|
483
|
+
const HALF_RES_BLUR_RADII = [0, 1, 2];
|
|
484
|
+
/** A second-look box must be square within this fraction... */
|
|
485
|
+
const MAX_ASPECT_ERROR = 0.08;
|
|
486
|
+
/** ...and look like a board at least this much (boardness). */
|
|
487
|
+
const MIN_BOARDNESS = 0.5;
|
|
488
|
+
function sameBox(a, b) {
|
|
489
|
+
return (Math.abs(a.x0 - b.x0) <= 1 && Math.abs(a.y0 - b.y0) <= 1 && Math.abs(a.x1 - b.x1) <= 1 && Math.abs(a.y1 - b.y1) <= 1);
|
|
490
|
+
}
|
|
491
|
+
/** Half-size copy, each pixel the mean of a 2x2 block. */
|
|
492
|
+
function halve(img) {
|
|
493
|
+
const w = img.width >> 1;
|
|
494
|
+
const h = img.height >> 1;
|
|
495
|
+
const out = new Float32Array(w * h);
|
|
496
|
+
for (let y = 0; y < h; y++) {
|
|
497
|
+
const top = 2 * y * img.width;
|
|
498
|
+
const bottom = top + img.width;
|
|
499
|
+
for (let x = 0; x < w; x++) {
|
|
500
|
+
out[y * w + x] = (img.data[top + 2 * x] + img.data[top + 2 * x + 1] + img.data[bottom + 2 * x] + img.data[bottom + 2 * x + 1]) / 4;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return { data: out, width: w, height: h };
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Second look: boards the plain search misses or misplaces because the
|
|
507
|
+
* board itself is full of edges. Hatched dark squares (printed diagrams,
|
|
508
|
+
* chess.com's newspaper board) put more gradient energy inside the
|
|
509
|
+
* squares than on the grid lines, and marbled or low-contrast boards can
|
|
510
|
+
* slide the grid one tile diagonally, which keeps the colouring and so
|
|
511
|
+
* slips past repairParity. On a blurred copy the hatching melts to grey
|
|
512
|
+
* and the grid lines dominate again.
|
|
513
|
+
*
|
|
514
|
+
* Returns the boxes found on blurred copies, each with its grid-snapped
|
|
515
|
+
* variant, that are square and look like a board, without duplicates and
|
|
516
|
+
* without anything in `known`. They are CANDIDATES: the caller lets the
|
|
517
|
+
* tile classifier choose (recognize.ts).
|
|
518
|
+
*/
|
|
519
|
+
export function findBlurredBoardCandidates(img, known = []) {
|
|
520
|
+
const found = [];
|
|
521
|
+
const consider = (box) => {
|
|
522
|
+
const w = box.x1 - box.x0;
|
|
523
|
+
const h = box.y1 - box.y0;
|
|
524
|
+
if (w <= 0 || Math.abs(w - h) / w > MAX_ASPECT_ERROR)
|
|
525
|
+
return;
|
|
526
|
+
if (known.some((k) => sameBox(k, box)) || found.some((f) => sameBox(f, box)))
|
|
527
|
+
return;
|
|
528
|
+
if (boardness(img, box) < MIN_BOARDNESS)
|
|
529
|
+
return;
|
|
530
|
+
found.push(box);
|
|
531
|
+
};
|
|
532
|
+
const half = Math.max(img.width, img.height) > HALF_RES_ABOVE;
|
|
533
|
+
const base = half ? halve(img) : img;
|
|
534
|
+
const scale = half ? 2 : 1;
|
|
535
|
+
const up = (b) => ({ x0: b.x0 * scale, y0: b.y0 * scale, x1: b.x1 * scale, y1: b.y1 * scale });
|
|
536
|
+
for (const radius of half ? HALF_RES_BLUR_RADII : BLUR_RADII) {
|
|
537
|
+
const blurred = radius > 0 ? boxBlur(base, radius) : base;
|
|
538
|
+
const box = findChessboardCorners(blurred);
|
|
539
|
+
if (!box)
|
|
540
|
+
continue;
|
|
541
|
+
consider(up(box));
|
|
542
|
+
consider(up(snapCorners(blurred, box)));
|
|
543
|
+
}
|
|
544
|
+
return found;
|
|
545
|
+
}
|
|
379
546
|
export function findChessboardCorners(img) {
|
|
380
547
|
const gradY = gradientRows(img);
|
|
381
548
|
const gradX = gradientCols(img);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createRecognizer, recognizeGray, CONFIDENCE_FLOOR, type Recognizer, type RecognizerOptions, type BoardScanResult, type TileClassifier, } from "./recognize";
|
|
2
|
-
export { findChessboardCorners, snapCorners, type BoardCorners, type GrayImage, } from "./detect";
|
|
2
|
+
export { findChessboardCorners, findBlurredBoardCandidates, boardness, snapCorners, type BoardCorners, type GrayImage, } from "./detect";
|
|
3
3
|
export { extractBoardImage, boardToTiles, extractTiles, rgbaToGray } from "./tiles";
|
|
4
4
|
export { probsToPlacement, flipPlacement, resolveOrientation, type RecognitionResult, } from "./fen";
|
|
5
5
|
export { inferCastling, placementToFen } from "./compose";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createRecognizer, recognizeGray, CONFIDENCE_FLOOR, } from "./recognize";
|
|
2
|
-
export { findChessboardCorners, snapCorners, } from "./detect";
|
|
2
|
+
export { findChessboardCorners, findBlurredBoardCandidates, boardness, snapCorners, } from "./detect";
|
|
3
3
|
export { extractBoardImage, boardToTiles, extractTiles, rgbaToGray } from "./tiles";
|
|
4
4
|
export { probsToPlacement, flipPlacement, resolveOrientation, } from "./fen";
|
|
5
5
|
export { inferCastling, placementToFen } from "./compose";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Book } from "./store";
|
|
2
|
+
export declare function Library({ books, counts, busy, error, onFile, onOpen, onRename, onRemove }: {
|
|
3
|
+
books: Book[];
|
|
4
|
+
counts: Map<string, number>;
|
|
5
|
+
busy: boolean;
|
|
6
|
+
error: string | null;
|
|
7
|
+
onFile(file: File): void;
|
|
8
|
+
onOpen(book: Book): void;
|
|
9
|
+
onRename(book: Book, name: string): void;
|
|
10
|
+
onRemove(book: Book): void;
|
|
11
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
import { useReaderHost, useT } from "./host";
|
|
4
|
+
import { fileNameOf, sizeText } from "./store";
|
|
5
|
+
/**
|
|
6
|
+
* The reader's first screen: the door for a new book, the books kept on this device as a list, the one opened last
|
|
7
|
+
* first (each row: the first page, the name, how many diagrams, how far the reader got, when he last opened it,
|
|
8
|
+
* and the way back in), and what the reader does.
|
|
9
|
+
*/
|
|
10
|
+
const COVER_ICON = "M5 4.5A1.5 1.5 0 0 1 6.5 3H19v15H6.5A1.5 1.5 0 0 0 5 19.5zM5 19.5A1.5 1.5 0 0 0 6.5 21H19";
|
|
11
|
+
/** When a book was last opened, in the page's language: "today", "yesterday", "3 Dec", "3 Dec 2025". */
|
|
12
|
+
function openedOn(at) {
|
|
13
|
+
const lang = document.documentElement.lang || "en";
|
|
14
|
+
const day = (t) => { const d = new Date(t); return new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime(); };
|
|
15
|
+
const ago = Math.round((day(Date.now()) - day(at)) / 86400000);
|
|
16
|
+
if (ago <= 1) {
|
|
17
|
+
const words = new Intl.RelativeTimeFormat(lang, { numeric: "auto" }).format(-ago, "day");
|
|
18
|
+
return words.charAt(0).toLocaleUpperCase(lang) + words.slice(1);
|
|
19
|
+
}
|
|
20
|
+
const sameYear = new Date(at).getFullYear() === new Date().getFullYear();
|
|
21
|
+
return new Intl.DateTimeFormat(lang, { day: "numeric", month: "short", ...(sameYear ? {} : { year: "numeric" }) }).format(at);
|
|
22
|
+
}
|
|
23
|
+
/** Rename and Remove, behind one button: a row shows one action, the one most wanted. */
|
|
24
|
+
function RowMenu({ onRename, onRemove }) {
|
|
25
|
+
const t = useT();
|
|
26
|
+
const [open, setOpen] = useState(false);
|
|
27
|
+
const holder = useRef(null);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!open)
|
|
30
|
+
return;
|
|
31
|
+
const away = (e) => { if (!holder.current?.contains(e.target))
|
|
32
|
+
setOpen(false); };
|
|
33
|
+
const key = (e) => { if (e.key === "Escape")
|
|
34
|
+
setOpen(false); };
|
|
35
|
+
window.addEventListener("pointerdown", away);
|
|
36
|
+
window.addEventListener("keydown", key);
|
|
37
|
+
return () => { window.removeEventListener("pointerdown", away); window.removeEventListener("keydown", key); };
|
|
38
|
+
}, [open]);
|
|
39
|
+
const pick = (run) => () => { setOpen(false); run(); };
|
|
40
|
+
return (_jsxs("div", { className: "lib-menu", ref: holder, children: [_jsx("button", { type: "button", className: "rd-icon", onClick: () => setOpen(!open), "aria-expanded": open, "aria-label": t("readMore"), title: t("readMore"), "data-testid": "read-book-menu", children: _jsxs("svg", { viewBox: "0 0 24 24", fill: "currentColor", "aria-hidden": true, children: [_jsx("circle", { cx: "5", cy: "12", r: "1.7" }), _jsx("circle", { cx: "12", cy: "12", r: "1.7" }), _jsx("circle", { cx: "19", cy: "12", r: "1.7" })] }) }), open && (_jsxs("ul", { className: "lib-menu-list", role: "menu", children: [_jsx("li", { children: _jsx("button", { type: "button", role: "menuitem", onClick: pick(onRename), "data-testid": "read-rename", children: t("readRename") }) }), _jsx("li", { children: _jsx("button", { type: "button", role: "menuitem", className: "lib-danger", onClick: pick(onRemove), "data-testid": "read-remove", children: t("readRemove") }) })] }))] }));
|
|
41
|
+
}
|
|
42
|
+
function BookRow({ book, diagrams, onOpen, onRename, onRemove }) {
|
|
43
|
+
const t = useT();
|
|
44
|
+
const [naming, setNaming] = useState(false);
|
|
45
|
+
const [name, setName] = useState(book.name);
|
|
46
|
+
const share = book.pages > 1 ? Math.round(((book.page - 1) / (book.pages - 1)) * 100) : 0;
|
|
47
|
+
const save = () => {
|
|
48
|
+
const next = name.trim();
|
|
49
|
+
if (next && next !== book.name)
|
|
50
|
+
onRename(next);
|
|
51
|
+
else
|
|
52
|
+
setName(book.name);
|
|
53
|
+
setNaming(false);
|
|
54
|
+
};
|
|
55
|
+
return (_jsxs("li", { className: "lib-row", "data-testid": "read-book", children: [_jsx("button", { type: "button", className: "lib-cover", onClick: onOpen, "aria-label": book.name, tabIndex: -1, children: book.cover ? _jsx("img", { src: book.cover, alt: "" }) : _jsx("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.4", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true, children: _jsx("path", { d: COVER_ICON }) }) }), _jsxs("div", { className: "lib-about", children: [naming ? (_jsx("form", { className: "lib-rename", onSubmit: (e) => { e.preventDefault(); save(); }, children: _jsx("input", { autoFocus: true, value: name, maxLength: 120, onChange: (e) => setName(e.target.value), onBlur: save, onKeyDown: (e) => { if (e.key === "Escape") {
|
|
56
|
+
setName(book.name);
|
|
57
|
+
setNaming(false);
|
|
58
|
+
} }, "aria-label": t("readRename"), "data-testid": "read-rename-input" }) })) : (_jsx("button", { type: "button", className: "lib-name", onClick: onOpen, title: book.renamed ? fileNameOf(book) : book.name, children: book.name })), _jsxs("p", { className: "lib-meta", children: [diagrams > 0 && _jsxs(_Fragment, { children: [_jsx("b", { children: t("readDiagramsShort", { n: diagrams }) }), _jsx("span", { "aria-hidden": true, children: " \u00B7 " })] }), t("readPageOf", { page: book.page, pages: book.pages }), _jsxs("span", { className: "lib-size", children: [_jsx("span", { "aria-hidden": true, children: " \u00B7 " }), sizeText(book.size)] }), _jsxs("span", { className: "lib-when-inline", "aria-hidden": true, children: [" \u00B7 ", openedOn(book.opened)] })] }), _jsx("div", { className: "lib-progress", role: "progressbar", "aria-valuenow": share, "aria-valuemin": 0, "aria-valuemax": 100, children: _jsx("span", { style: { width: `${share}%` } }) })] }), _jsx("time", { className: "lib-when", dateTime: new Date(book.opened).toISOString(), title: new Date(book.opened).toLocaleString(), "data-testid": "read-book-when", children: openedOn(book.opened) }), _jsx("button", { type: "button", className: "rd-btn rd-btn-primary rd-btn-quiet lib-open", onClick: onOpen, "data-testid": "read-continue", children: book.page > 1 ? t("readOpenAt", { page: book.page }) : t("readOpen") }), _jsx(RowMenu, { onRename: () => { setName(book.name); setNaming(true); }, onRemove: () => { if (window.confirm(t("readRemoveAsk")))
|
|
59
|
+
onRemove(); } })] }));
|
|
60
|
+
}
|
|
61
|
+
export function Library({ books, counts, busy, error, onFile, onOpen, onRename, onRemove }) {
|
|
62
|
+
const t = useT();
|
|
63
|
+
const { emptyLibrary } = useReaderHost();
|
|
64
|
+
const input = useRef(null);
|
|
65
|
+
const [over, setOver] = useState(false);
|
|
66
|
+
const points = [
|
|
67
|
+
{ title: "readWhy1", sub: "readWhy1Sub", icon: "M4 6h16M4 12h10M4 18h6M17 15l2 2 3-4" },
|
|
68
|
+
{ title: "readWhy2", sub: "readWhy2Sub", icon: "M5 3h14v14H5zM5 10h14M12 3v14M8 21h8" },
|
|
69
|
+
{ title: "readWhy3", sub: "readWhy3Sub", icon: "M12 3l7 3v5c0 4.5-3 8-7 10-4-2-7-5.5-7-10V6zM9 12l2 2 4-4" },
|
|
70
|
+
];
|
|
71
|
+
return (_jsxs("main", { className: "lib", children: [_jsxs("div", { className: `rd-drop lib-drop ${over ? "rd-drop-active" : ""} ${busy ? "rd-drop-busy" : ""}`, onDragOver: (e) => { e.preventDefault(); setOver(true); }, onDragLeave: () => setOver(false), onDrop: (e) => { e.preventDefault(); setOver(false); const f = e.dataTransfer.files[0]; if (f)
|
|
72
|
+
onFile(f); }, onClick: () => input.current?.click(), "data-testid": "read-drop", children: [busy ? (_jsxs("div", { className: "rd-status", children: [_jsx("div", { className: "rd-spinner" }), t("readOpening")] })) : (_jsxs(_Fragment, { children: [_jsx("div", { className: "rd-drop-title", children: books.length ? t("readAddBook") : t("readDropTitle") }), _jsxs("button", { type: "button", className: "rd-btn rd-btn-primary lib-choose", onClick: (e) => { e.stopPropagation(); input.current?.click(); }, "data-testid": "read-choose", children: [_jsxs("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true, children: [_jsx("path", { d: "M14 3H6a1 1 0 0 0-1 1v16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V8z" }), _jsx("path", { d: "M14 3v5h5M12 18v-6M9 15l3-3 3 3" })] }), t("readChoose")] }), _jsx("div", { className: "rd-drop-sub", children: t("readDropSub") }), _jsx("div", { className: "rd-drop-hint", children: t("readPrivate") }), error && _jsx("div", { className: "rd-error", children: error })] })), _jsx("input", { ref: input, type: "file", accept: "application/pdf,.pdf", hidden: true, onChange: (e) => { const f = e.target.files?.[0]; if (f)
|
|
73
|
+
onFile(f); e.target.value = ""; } })] }), books.length === 0 && emptyLibrary, books.length > 0 && (_jsxs("section", { className: "lib-section", children: [_jsxs("div", { className: "lib-head", children: [_jsx("h2", { children: t("readShelf") }), _jsx("span", { children: books.length === 1 ? t("readShelfOne") : t("readShelfCount", { n: books.length }) })] }), _jsx("ul", { className: "lib-list", children: books.map((b) => _jsx(BookRow, { book: b, diagrams: counts.get(b.id) ?? 0, onOpen: () => onOpen(b), onRename: (name) => onRename(b, name), onRemove: () => onRemove(b) }, b.id)) })] })), _jsx("ul", { className: "lib-points", children: points.map((point) => (_jsxs("li", { children: [_jsx("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.8", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true, children: _jsx("path", { d: point.icon }) }), _jsx("p", { className: "lib-point-title", children: t(point.title) }), _jsx("p", { className: "lib-point-sub", children: t(point.sub) })] }, point.title))) })] }));
|
|
74
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Doc } from "./pdf";
|
|
2
|
+
import type { Diagram } from "./store";
|
|
3
|
+
/** A point of a page, in fractions of its width and height. */
|
|
4
|
+
export type Point = {
|
|
5
|
+
page: number;
|
|
6
|
+
fx: number;
|
|
7
|
+
fy: number;
|
|
8
|
+
};
|
|
9
|
+
/** A box on a page, in the same fractions. */
|
|
10
|
+
export type Box = {
|
|
11
|
+
x0: number;
|
|
12
|
+
y0: number;
|
|
13
|
+
x1: number;
|
|
14
|
+
y1: number;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* One page of the book: its picture when it is near the screen (`live`), the diagrams found on it as buttons, the
|
|
18
|
+
* reader's double-click (or two quick taps) told as a point of the page, and the box he draws around a diagram
|
|
19
|
+
* that was not found (while `selecting`, or with Shift held).
|
|
20
|
+
*/
|
|
21
|
+
export declare const PageView: import("react").NamedExoticComponent<{
|
|
22
|
+
doc: Doc;
|
|
23
|
+
n: number;
|
|
24
|
+
width: number;
|
|
25
|
+
ratio0: number;
|
|
26
|
+
live: boolean;
|
|
27
|
+
found: Diagram[] | undefined;
|
|
28
|
+
picked: Diagram | null;
|
|
29
|
+
busy: Point | null;
|
|
30
|
+
selecting: boolean;
|
|
31
|
+
onPoint(p: Point): void;
|
|
32
|
+
onDiagram(n: number, d: Diagram): void;
|
|
33
|
+
onBox(n: number, box: Box): void;
|
|
34
|
+
holder(n: number, el: HTMLDivElement | null): void;
|
|
35
|
+
}>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { memo, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { drawPage, ratioOf } from "./pdf";
|
|
4
|
+
/** A box smaller than this on screen was a slip of the hand, not a diagram. */
|
|
5
|
+
const BOX_MIN = 28;
|
|
6
|
+
/**
|
|
7
|
+
* One page of the book: its picture when it is near the screen (`live`), the diagrams found on it as buttons, the
|
|
8
|
+
* reader's double-click (or two quick taps) told as a point of the page, and the box he draws around a diagram
|
|
9
|
+
* that was not found (while `selecting`, or with Shift held).
|
|
10
|
+
*/
|
|
11
|
+
export const PageView = memo(function PageView({ doc, n, width, ratio0, live, found, picked, busy, selecting, onPoint, onDiagram, onBox, holder }) {
|
|
12
|
+
const [drawn, setDrawn] = useState(null);
|
|
13
|
+
const canvas = useRef(null);
|
|
14
|
+
const [ratio, setRatio] = useState(ratio0);
|
|
15
|
+
const tap = useRef(null);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (!live)
|
|
18
|
+
return;
|
|
19
|
+
let gone = false;
|
|
20
|
+
void ratioOf(doc, n).then((r) => { if (!gone)
|
|
21
|
+
setRatio(r); });
|
|
22
|
+
return () => { gone = true; };
|
|
23
|
+
}, [doc, n, live]);
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
const el = canvas.current;
|
|
26
|
+
if (!live || !el || width < 50)
|
|
27
|
+
return;
|
|
28
|
+
let task = null, gone = false;
|
|
29
|
+
const px = Math.min(2400, Math.round(width * Math.min(2, window.devicePixelRatio || 1)));
|
|
30
|
+
void drawPage(doc, n, px, el).then((t) => { t.promise.catch(() => { }); if (gone)
|
|
31
|
+
t.cancel();
|
|
32
|
+
else
|
|
33
|
+
task = t; });
|
|
34
|
+
return () => { gone = true; task?.cancel(); };
|
|
35
|
+
}, [doc, n, live, width]);
|
|
36
|
+
const point = (clientX, clientY, el) => {
|
|
37
|
+
const r = el.getBoundingClientRect();
|
|
38
|
+
return { page: n, fx: (clientX - r.left) / r.width, fy: (clientY - r.top) / r.height };
|
|
39
|
+
};
|
|
40
|
+
/** A phone has no double-click: two taps close in time and place are one. */
|
|
41
|
+
const tapped = (e) => {
|
|
42
|
+
if (e.pointerType !== "touch" || selecting)
|
|
43
|
+
return;
|
|
44
|
+
const now = performance.now(), was = tap.current;
|
|
45
|
+
if (was && now - was.at < 340 && Math.hypot(e.clientX - was.x, e.clientY - was.y) < 32) {
|
|
46
|
+
tap.current = null;
|
|
47
|
+
onPoint(point(e.clientX, e.clientY, e.currentTarget));
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
tap.current = { at: now, x: e.clientX, y: e.clientY };
|
|
51
|
+
};
|
|
52
|
+
/** A box drawn from where the pointer went down to where it is let go. */
|
|
53
|
+
const draw = (down) => {
|
|
54
|
+
if (down.button > 0 || !(selecting || down.shiftKey))
|
|
55
|
+
return;
|
|
56
|
+
const el = down.currentTarget, from = point(down.clientX, down.clientY, el);
|
|
57
|
+
down.preventDefault();
|
|
58
|
+
el.setPointerCapture(down.pointerId);
|
|
59
|
+
const boxTo = (e) => {
|
|
60
|
+
const to = point(e.clientX, e.clientY, el), hold = (v) => Math.min(1, Math.max(0, v));
|
|
61
|
+
return { x0: hold(Math.min(from.fx, to.fx)), y0: hold(Math.min(from.fy, to.fy)), x1: hold(Math.max(from.fx, to.fx)), y1: hold(Math.max(from.fy, to.fy)) };
|
|
62
|
+
};
|
|
63
|
+
const move = (e) => setDrawn(boxTo(e));
|
|
64
|
+
const end = (e) => {
|
|
65
|
+
el.removeEventListener("pointermove", move);
|
|
66
|
+
el.removeEventListener("pointerup", end);
|
|
67
|
+
el.removeEventListener("pointercancel", end);
|
|
68
|
+
setDrawn(null);
|
|
69
|
+
const b = boxTo(e), r = el.getBoundingClientRect();
|
|
70
|
+
if (e.type === "pointerup" && (b.x1 - b.x0) * r.width >= BOX_MIN && (b.y1 - b.y0) * r.height >= BOX_MIN)
|
|
71
|
+
onBox(n, b);
|
|
72
|
+
};
|
|
73
|
+
el.addEventListener("pointermove", move);
|
|
74
|
+
el.addEventListener("pointerup", end);
|
|
75
|
+
el.addEventListener("pointercancel", end);
|
|
76
|
+
};
|
|
77
|
+
const box = (d) => ({ left: `${d.x0 * 100}%`, top: `${d.y0 * 100}%`, width: `${(d.x1 - d.x0) * 100}%`, height: `${(d.y1 - d.y0) * 100}%` });
|
|
78
|
+
return (_jsxs("div", { className: "rd-page", ref: (el) => holder(n, el), "data-page": n, style: { width, height: Math.round(width * ratio) }, onDoubleClick: (e) => { if (!selecting)
|
|
79
|
+
onPoint(point(e.clientX, e.clientY, e.currentTarget)); }, onPointerUp: tapped, onPointerDown: draw, children: [live && _jsx("canvas", { ref: canvas }), (found ?? []).map((d, i) => (_jsx("button", { type: "button", className: `rd-diagram ${picked === d ? "on" : ""}`, style: box(d), "aria-label": `Diagram ${i + 1}`, "data-testid": "read-diagram", onClick: (e) => { e.stopPropagation(); onDiagram(n, d); }, onDoubleClick: (e) => e.stopPropagation() }, i))), picked && !(found ?? []).includes(picked) && _jsx("span", { className: "rd-diagram on", style: box(picked) }), drawn && _jsx("span", { className: "rd-marquee", style: box(drawn) }), busy?.page === n && _jsx("span", { className: "rd-busy", style: { left: `${busy.fx * 100}%`, top: `${busy.fy * 100}%` } }), _jsx("span", { className: "rd-page-no", children: n })] }));
|
|
80
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ReaderHost } from "./host";
|
|
2
|
+
/**
|
|
3
|
+
* A chess book read in the browser (founder 2026-12-05): the library, or a book open; a PDF dropped anywhere on
|
|
4
|
+
* the page opens it. The address keeps the open book (#<id>), so a reload stays in it. The site around it is the
|
|
5
|
+
* host's (ReaderHost): fenshot.com/read and kibitzo.com/read are this same reader.
|
|
6
|
+
*/
|
|
7
|
+
export declare function BookReader({ host }: {
|
|
8
|
+
host: ReaderHost;
|
|
9
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { ReaderHostProvider, useReaderHost, useT, useTrack } from "./host";
|
|
4
|
+
import { Library } from "./Library";
|
|
5
|
+
import { addBook, diagramCounts, fileOf, listBooks, removeBook, saveBook, takeIncoming } from "./store";
|
|
6
|
+
import { configurePdf, coverOf, openPdf } from "./pdf";
|
|
7
|
+
import { Reader } from "./Reader";
|
|
8
|
+
/**
|
|
9
|
+
* A chess book read in the browser (founder 2026-12-05): the library, or a book open; a PDF dropped anywhere on
|
|
10
|
+
* the page opens it. The address keeps the open book (#<id>), so a reload stays in it. The site around it is the
|
|
11
|
+
* host's (ReaderHost): fenshot.com/read and kibitzo.com/read are this same reader.
|
|
12
|
+
*/
|
|
13
|
+
export function BookReader({ host }) {
|
|
14
|
+
configurePdf(host.pdf);
|
|
15
|
+
return _jsx(ReaderHostProvider, { value: host, children: _jsx(ReadApp, {}) });
|
|
16
|
+
}
|
|
17
|
+
function ReadApp() {
|
|
18
|
+
const t = useT();
|
|
19
|
+
const count = useTrack();
|
|
20
|
+
const { header } = useReaderHost();
|
|
21
|
+
const [books, setBooks] = useState([]);
|
|
22
|
+
const [open, setOpen] = useState(null);
|
|
23
|
+
const [busy, setBusy] = useState(false);
|
|
24
|
+
const [error, setError] = useState(null);
|
|
25
|
+
const [counts, setCounts] = useState(new Map());
|
|
26
|
+
const refresh = useCallback(() => {
|
|
27
|
+
void diagramCounts().then(setCounts).catch(() => setCounts(new Map()));
|
|
28
|
+
return listBooks().then(setBooks).catch(() => setBooks([]));
|
|
29
|
+
}, []);
|
|
30
|
+
const openBook = useCallback(async (book) => {
|
|
31
|
+
setBusy(true);
|
|
32
|
+
setError(null);
|
|
33
|
+
try {
|
|
34
|
+
const file = await fileOf(book.id);
|
|
35
|
+
if (!file)
|
|
36
|
+
throw new Error("gone");
|
|
37
|
+
const doc = await openPdf(file);
|
|
38
|
+
const now = { ...book, pages: doc.numPages, page: Math.min(book.page, doc.numPages), opened: Date.now() };
|
|
39
|
+
await saveBook(now);
|
|
40
|
+
history.replaceState(null, "", `#${encodeURIComponent(book.id)}`);
|
|
41
|
+
setOpen({ book: now, doc });
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
setError(t("readCantOpen"));
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
setBusy(false);
|
|
48
|
+
}
|
|
49
|
+
}, [t]);
|
|
50
|
+
const openFile = useCallback(async (file) => {
|
|
51
|
+
if (file.type !== "application/pdf" && !/\.pdf$/i.test(file.name)) {
|
|
52
|
+
setError(t("readNotPdf"));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
setBusy(true);
|
|
56
|
+
setError(null);
|
|
57
|
+
try {
|
|
58
|
+
const doc = await openPdf(file);
|
|
59
|
+
const book = await addBook(file, doc.numPages, { cover: await coverOf(doc) });
|
|
60
|
+
count("read-open", { pages: doc.numPages });
|
|
61
|
+
history.replaceState(null, "", `#${encodeURIComponent(book.id)}`);
|
|
62
|
+
setOpen({ book: { ...book, opened: Date.now() }, doc });
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
setError(t("readCantOpen"));
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
setBusy(false);
|
|
69
|
+
}
|
|
70
|
+
}, [t]);
|
|
71
|
+
// Once, on arrival: the library, and the book the address names.
|
|
72
|
+
const arrived = useRef(false);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (arrived.current)
|
|
75
|
+
return;
|
|
76
|
+
arrived.current = true;
|
|
77
|
+
void listBooks().then(async (all) => {
|
|
78
|
+
setBooks(all);
|
|
79
|
+
void diagramCounts().then(setCounts).catch(() => { });
|
|
80
|
+
const id = decodeURIComponent(location.hash.slice(1));
|
|
81
|
+
if (id === "incoming") {
|
|
82
|
+
const file = await takeIncoming();
|
|
83
|
+
if (file)
|
|
84
|
+
return void openFile(file);
|
|
85
|
+
}
|
|
86
|
+
const book = id ? all.find((b) => b.id === id) : undefined;
|
|
87
|
+
if (book)
|
|
88
|
+
void openBook(book);
|
|
89
|
+
}).catch(() => setBooks([]));
|
|
90
|
+
}, [openBook, openFile]);
|
|
91
|
+
// A PDF dropped anywhere on the page, in the library or over a book.
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
const over = (e) => { if (e.dataTransfer?.types.includes("Files"))
|
|
94
|
+
e.preventDefault(); };
|
|
95
|
+
const drop = (e) => { const f = e.dataTransfer?.files[0]; if (f) {
|
|
96
|
+
e.preventDefault();
|
|
97
|
+
void openFile(f);
|
|
98
|
+
} };
|
|
99
|
+
window.addEventListener("dragover", over);
|
|
100
|
+
window.addEventListener("drop", drop);
|
|
101
|
+
return () => { window.removeEventListener("dragover", over); window.removeEventListener("drop", drop); };
|
|
102
|
+
}, [openFile]);
|
|
103
|
+
const toLibrary = useCallback(() => {
|
|
104
|
+
void open?.doc.loadingTask.destroy();
|
|
105
|
+
history.replaceState(null, "", location.pathname + location.search);
|
|
106
|
+
setOpen(null);
|
|
107
|
+
void refresh();
|
|
108
|
+
}, [open, refresh]);
|
|
109
|
+
const progress = useCallback((page) => {
|
|
110
|
+
setOpen((now) => {
|
|
111
|
+
if (!now || now.book.page === page)
|
|
112
|
+
return now;
|
|
113
|
+
const book = { ...now.book, page, opened: Date.now() };
|
|
114
|
+
void saveBook(book);
|
|
115
|
+
return { ...now, book };
|
|
116
|
+
});
|
|
117
|
+
}, []);
|
|
118
|
+
useEffect(() => { document.body.classList.toggle("reading", open !== null); }, [open]);
|
|
119
|
+
if (open)
|
|
120
|
+
return _jsx(Reader, { book: open.book, doc: open.doc, onLibrary: toLibrary, onProgress: progress }, open.book.id);
|
|
121
|
+
return (_jsxs("div", { className: "fsr-library", children: [header, _jsx(Library, { books: books, counts: counts, busy: busy, error: error, onFile: (f) => void openFile(f), onOpen: (b) => void openBook(b), onRename: (b, name) => void saveBook({ ...b, name, renamed: true }).then(refresh), onRemove: (b) => void removeBook(b.id).then(refresh) })] }));
|
|
122
|
+
}
|