@nypl/web-reader 5.0.0 → 5.0.1

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/dist/index.d.mts CHANGED
@@ -35,9 +35,9 @@ interface Locations {
35
35
 
36
36
  declare const ReadiumWebpubContext = "http://readium.org/webpub/default.jsonld";
37
37
  declare const IS_DEV: boolean;
38
- declare const HEADER_HEIGHT = 48;
39
- declare const CHROME_HEIGHT = 48;
40
- declare const DEFAULT_HEIGHT = "calc(100vh - 48px)";
38
+ declare const HEADER_HEIGHT = 49;
39
+ declare const CHROME_HEIGHT = 49;
40
+ declare const DEFAULT_HEIGHT = "calc(100vh - 49px)";
41
41
  declare const DEFAULT_SHOULD_GROW_WHEN_SCROLLING = true;
42
42
  declare const READER_MARGIN = 16;
43
43
  declare const DEFAULT_SETTINGS: ReaderSettings;
@@ -70,6 +70,7 @@ declare const FONT_DETAILS: {
70
70
  fontWeight: string;
71
71
  };
72
72
  };
73
+ declare const IN_VIEW_DELAY_MS = 150;
73
74
  declare const LOCAL_STORAGE_SETTINGS_KEY = "web-reader-settings";
74
75
  declare const LOCAL_STORAGE_LOCATIONS_KEY = "web-reader-locations";
75
76
  declare const MAIN_CONTENT_ID = "mainContent";
@@ -546,4 +547,4 @@ type WebReaderProps = UseWebReaderArguments<string | Uint8Array>;
546
547
  declare const WebReaderWithoutBoundary: FC<WebReaderProps>;
547
548
  declare const WebReader: FC<WebReaderProps>;
548
549
 
549
- export { CHROME_HEIGHT, DEFAULT_FIT_MODE, DEFAULT_FONT_HEIGHT, DEFAULT_FONT_WIDTH, DEFAULT_HEIGHT, DEFAULT_SETTINGS, DEFAULT_SHOULD_GROW_WHEN_SCROLLING, FONT_DETAILS, HEADER_HEIGHT, IS_DEV, LOCAL_STORAGE_LOCATIONS_KEY, LOCAL_STORAGE_SETTINGS_KEY, MAIN_CONTENT_ID, READER_MARGIN, type ReadiumLink, ReadiumWebpubContext, type WebReaderProps, WebReaderWithoutBoundary, type WebpubManifest, addTocToManifest, clearWebReaderLocalStorage, WebReader as default, getTheme, useColorModeValue, useHtmlReader, usePdfReader, useWebReader };
550
+ export { CHROME_HEIGHT, DEFAULT_FIT_MODE, DEFAULT_FONT_HEIGHT, DEFAULT_FONT_WIDTH, DEFAULT_HEIGHT, DEFAULT_SETTINGS, DEFAULT_SHOULD_GROW_WHEN_SCROLLING, FONT_DETAILS, HEADER_HEIGHT, IN_VIEW_DELAY_MS, IS_DEV, LOCAL_STORAGE_LOCATIONS_KEY, LOCAL_STORAGE_SETTINGS_KEY, MAIN_CONTENT_ID, READER_MARGIN, type ReadiumLink, ReadiumWebpubContext, type WebReaderProps, WebReaderWithoutBoundary, type WebpubManifest, addTocToManifest, clearWebReaderLocalStorage, WebReader as default, getTheme, useColorModeValue, useHtmlReader, usePdfReader, useWebReader };
package/dist/index.d.ts CHANGED
@@ -35,9 +35,9 @@ interface Locations {
35
35
 
36
36
  declare const ReadiumWebpubContext = "http://readium.org/webpub/default.jsonld";
37
37
  declare const IS_DEV: boolean;
38
- declare const HEADER_HEIGHT = 48;
39
- declare const CHROME_HEIGHT = 48;
40
- declare const DEFAULT_HEIGHT = "calc(100vh - 48px)";
38
+ declare const HEADER_HEIGHT = 49;
39
+ declare const CHROME_HEIGHT = 49;
40
+ declare const DEFAULT_HEIGHT = "calc(100vh - 49px)";
41
41
  declare const DEFAULT_SHOULD_GROW_WHEN_SCROLLING = true;
42
42
  declare const READER_MARGIN = 16;
43
43
  declare const DEFAULT_SETTINGS: ReaderSettings;
@@ -70,6 +70,7 @@ declare const FONT_DETAILS: {
70
70
  fontWeight: string;
71
71
  };
72
72
  };
73
+ declare const IN_VIEW_DELAY_MS = 150;
73
74
  declare const LOCAL_STORAGE_SETTINGS_KEY = "web-reader-settings";
74
75
  declare const LOCAL_STORAGE_LOCATIONS_KEY = "web-reader-locations";
75
76
  declare const MAIN_CONTENT_ID = "mainContent";
@@ -546,4 +547,4 @@ type WebReaderProps = UseWebReaderArguments<string | Uint8Array>;
546
547
  declare const WebReaderWithoutBoundary: FC<WebReaderProps>;
547
548
  declare const WebReader: FC<WebReaderProps>;
548
549
 
549
- export { CHROME_HEIGHT, DEFAULT_FIT_MODE, DEFAULT_FONT_HEIGHT, DEFAULT_FONT_WIDTH, DEFAULT_HEIGHT, DEFAULT_SETTINGS, DEFAULT_SHOULD_GROW_WHEN_SCROLLING, FONT_DETAILS, HEADER_HEIGHT, IS_DEV, LOCAL_STORAGE_LOCATIONS_KEY, LOCAL_STORAGE_SETTINGS_KEY, MAIN_CONTENT_ID, READER_MARGIN, type ReadiumLink, ReadiumWebpubContext, type WebReaderProps, WebReaderWithoutBoundary, type WebpubManifest, addTocToManifest, clearWebReaderLocalStorage, WebReader as default, getTheme, useColorModeValue, useHtmlReader, usePdfReader, useWebReader };
550
+ export { CHROME_HEIGHT, DEFAULT_FIT_MODE, DEFAULT_FONT_HEIGHT, DEFAULT_FONT_WIDTH, DEFAULT_HEIGHT, DEFAULT_SETTINGS, DEFAULT_SHOULD_GROW_WHEN_SCROLLING, FONT_DETAILS, HEADER_HEIGHT, IN_VIEW_DELAY_MS, IS_DEV, LOCAL_STORAGE_LOCATIONS_KEY, LOCAL_STORAGE_SETTINGS_KEY, MAIN_CONTENT_ID, READER_MARGIN, type ReadiumLink, ReadiumWebpubContext, type WebReaderProps, WebReaderWithoutBoundary, type WebpubManifest, addTocToManifest, clearWebReaderLocalStorage, WebReader as default, getTheme, useColorModeValue, useHtmlReader, usePdfReader, useWebReader };
package/dist/index.js CHANGED
@@ -814,6 +814,7 @@ __export(src_exports, {
814
814
  DEFAULT_SHOULD_GROW_WHEN_SCROLLING: () => DEFAULT_SHOULD_GROW_WHEN_SCROLLING,
815
815
  FONT_DETAILS: () => FONT_DETAILS,
816
816
  HEADER_HEIGHT: () => HEADER_HEIGHT,
817
+ IN_VIEW_DELAY_MS: () => IN_VIEW_DELAY_MS,
817
818
  IS_DEV: () => IS_DEV,
818
819
  LOCAL_STORAGE_LOCATIONS_KEY: () => LOCAL_STORAGE_LOCATIONS_KEY,
819
820
  LOCAL_STORAGE_SETTINGS_KEY: () => LOCAL_STORAGE_SETTINGS_KEY,
@@ -1389,7 +1390,7 @@ var React29 = __toESM(require("react"));
1389
1390
  // src/constants.ts
1390
1391
  var ReadiumWebpubContext = "http://readium.org/webpub/default.jsonld";
1391
1392
  var IS_DEV = process.env.NODE_ENV === "development";
1392
- var HEADER_HEIGHT = 48;
1393
+ var HEADER_HEIGHT = 49;
1393
1394
  var CHROME_HEIGHT = HEADER_HEIGHT;
1394
1395
  var DEFAULT_HEIGHT = `calc(100vh - ${CHROME_HEIGHT}px)`;
1395
1396
  var DEFAULT_SHOULD_GROW_WHEN_SCROLLING = true;
@@ -1429,6 +1430,7 @@ var FONT_DETAILS = {
1429
1430
  fontWeight: "regular"
1430
1431
  }
1431
1432
  };
1433
+ var IN_VIEW_DELAY_MS = 150;
1432
1434
  var LOCAL_STORAGE_SETTINGS_KEY = "web-reader-settings";
1433
1435
  var LOCAL_STORAGE_LOCATIONS_KEY = "web-reader-locations";
1434
1436
  var MAIN_CONTENT_ID = "mainContent";
@@ -7928,6 +7930,7 @@ var ScrollPage = ({
7928
7930
  width,
7929
7931
  height,
7930
7932
  onLoadSuccess,
7933
+ onPageRef,
7931
7934
  placeholderHeight,
7932
7935
  placeholderWidth,
7933
7936
  allowInView,
@@ -7947,8 +7950,9 @@ var ScrollPage = ({
7947
7950
  (el) => {
7948
7951
  if (typeof loadRef === "function") loadRef(el);
7949
7952
  if (typeof visibilityRef === "function") visibilityRef(el);
7953
+ if (onPageRef) onPageRef(pageNumber, el);
7950
7954
  },
7951
- [loadRef, visibilityRef]
7955
+ [loadRef, onPageRef, pageNumber, visibilityRef]
7952
7956
  );
7953
7957
  const handleLoadSuccess = import_react98.default.useCallback(
7954
7958
  (page) => {
@@ -8057,7 +8061,24 @@ function usePdfReader(args) {
8057
8061
  const isFetching = !state2.resource;
8058
8062
  const isParsed = typeof state2.numPages === "number";
8059
8063
  const [containerRef, containerSize] = useMeasure();
8060
- const [pageHeight, setPageHeight] = React50.useState(0);
8064
+ const documentContainerRef = React50.useRef(null);
8065
+ const pageRefs = React50.useRef(/* @__PURE__ */ new Map());
8066
+ const setPageRef = React50.useCallback(
8067
+ (pageNumber, element) => {
8068
+ if (element) {
8069
+ pageRefs.current.set(pageNumber, element);
8070
+ return;
8071
+ }
8072
+ pageRefs.current.delete(pageNumber);
8073
+ },
8074
+ []
8075
+ );
8076
+ const scrollState = React50.useRef({
8077
+ ratios: /* @__PURE__ */ new Map(),
8078
+ lastVisiblePage: state2.pageNumber,
8079
+ isInViewUpdate: false,
8080
+ lastProgrammaticNavAt: 0
8081
+ });
8061
8082
  React50.useEffect(() => {
8062
8083
  if (!webpubManifestUrl || !manifest) {
8063
8084
  return dispatch({ type: "ARGS_CHANGED", args: void 0 });
@@ -8130,13 +8151,6 @@ function usePdfReader(args) {
8130
8151
  var _a2;
8131
8152
  resizePage(containerSize, state2.fitMode, (_a2 = state2.rotation) != null ? _a2 : 0, state2.scale);
8132
8153
  }, [containerSize, resizePage, state2.fitMode, state2.rotation, state2.scale]);
8133
- React50.useEffect(() => {
8134
- if (pageHeight === 0 && state2.pdfWidth && state2.pdfHeight) {
8135
- const aspectRatio = state2.pdfHeight / state2.pdfWidth;
8136
- const initialPageHeight = (containerSize.width - READER_MARGIN) * aspectRatio;
8137
- setPageHeight(Math.round(initialPageHeight));
8138
- }
8139
- }, [state2.pdfWidth, state2.pdfHeight, containerSize.width, pageHeight]);
8140
8154
  React50.useEffect(() => {
8141
8155
  var _a2, _b2;
8142
8156
  const isFirstResource = state2.resourceIndex === 0;
@@ -8159,25 +8173,40 @@ function usePdfReader(args) {
8159
8173
  ]);
8160
8174
  React50.useEffect(() => {
8161
8175
  var _a2;
8162
- if (!((_a2 = state2.settings) == null ? void 0 : _a2.isScrolling)) return;
8163
- if (!state2.rendered) return;
8176
+ if (!((_a2 = state2.settings) == null ? void 0 : _a2.isScrolling) || !state2.rendered) return;
8164
8177
  if (scrollState.current.isInViewUpdate) {
8165
8178
  scrollState.current.isInViewUpdate = false;
8166
8179
  return;
8167
8180
  }
8168
- process.nextTick(() => {
8169
- const page = document.querySelector(
8170
- `[data-page-number="${state2.pageNumber}"]`
8171
- );
8172
- page == null ? void 0 : page.scrollIntoView();
8173
- });
8181
+ const documentContainer = documentContainerRef.current;
8182
+ const pageRef = pageRefs.current.get(state2.pageNumber);
8183
+ if (documentContainer && pageRef) {
8184
+ const containerRect = documentContainer.getBoundingClientRect();
8185
+ const pageRect = pageRef.getBoundingClientRect();
8186
+ documentContainer.scrollTo({
8187
+ top: documentContainer.scrollTop + (pageRect.top - containerRect.top)
8188
+ });
8189
+ }
8174
8190
  }, [state2.pageNumber, (_c = state2.settings) == null ? void 0 : _c.isScrolling, state2.rendered]);
8191
+ const beginProgrammaticNavigation = React50.useCallback(
8192
+ (pendingPage) => {
8193
+ const currentScrollState = scrollState.current;
8194
+ currentScrollState.lastVisiblePage = pendingPage;
8195
+ currentScrollState.lastProgrammaticNavAt = Date.now();
8196
+ currentScrollState.ratios.clear();
8197
+ },
8198
+ []
8199
+ );
8175
8200
  const goForward = React50.useCallback(() => __async(null, null, function* () {
8201
+ beginProgrammaticNavigation(
8202
+ state2.numPages ? Math.min(state2.pageNumber + 1, state2.numPages) : state2.pageNumber + 1
8203
+ );
8176
8204
  dispatch({ type: "GO_FORWARD" });
8177
- }), []);
8205
+ }), [beginProgrammaticNavigation, state2.numPages, state2.pageNumber]);
8178
8206
  const goBackward = React50.useCallback(() => __async(null, null, function* () {
8207
+ beginProgrammaticNavigation(Math.max(1, state2.pageNumber - 1));
8179
8208
  dispatch({ type: "GO_BACKWARD" });
8180
- }), []);
8209
+ }), [beginProgrammaticNavigation, state2.pageNumber]);
8181
8210
  const setScroll = React50.useCallback(
8182
8211
  (val) => __async(null, null, function* () {
8183
8212
  const isScrolling = val === "scrolling";
@@ -8203,42 +8232,38 @@ function usePdfReader(args) {
8203
8232
  const goToPage = React50.useCallback((href) => __async(null, null, function* () {
8204
8233
  dispatch({ type: "GO_TO_HREF", href });
8205
8234
  }), []);
8206
- const goToPageNumber = React50.useCallback((page) => {
8207
- dispatch({ type: "GO_TO_PAGE", page });
8208
- }, []);
8235
+ const goToPageNumber = React50.useCallback(
8236
+ (page) => {
8237
+ beginProgrammaticNavigation(page);
8238
+ dispatch({ type: "GO_TO_PAGE", page });
8239
+ },
8240
+ [beginProgrammaticNavigation]
8241
+ );
8209
8242
  const setFitMode = React50.useCallback((mode) => {
8210
8243
  dispatch({ type: "SET_FIT_MODE", fitMode: mode });
8211
8244
  }, []);
8212
- const scrollState = React50.useRef({
8213
- ratios: /* @__PURE__ */ new Map(),
8214
- lastVisiblepage: state2.pageNumber,
8215
- hasScrolled: false,
8216
- isInViewUpdate: false
8217
- });
8218
8245
  const onInView = React50.useCallback(
8219
8246
  (pageNum, ratio) => {
8220
8247
  var _a2;
8221
8248
  const currentScrollState = scrollState.current;
8222
- if (!((_a2 = state2.settings) == null ? void 0 : _a2.isScrolling)) return;
8223
- currentScrollState.ratios.set(pageNum, ratio);
8224
- if (!currentScrollState.hasScrolled && state2.pageNumber === 1) {
8225
- const container = document.querySelector(
8226
- `#${MAIN_CONTENT_ID} .react-pdf__Document`
8227
- );
8228
- if (container && container.scrollTop > 0)
8229
- currentScrollState.hasScrolled = true;
8230
- else return;
8249
+ if (!((_a2 = state2.settings) == null ? void 0 : _a2.isScrolling) || Date.now() - currentScrollState.lastProgrammaticNavAt < IN_VIEW_DELAY_MS)
8250
+ return;
8251
+ if (ratio <= 0) {
8252
+ currentScrollState.ratios.delete(pageNum);
8253
+ return;
8231
8254
  }
8232
- let mostVisiblePage = currentScrollState.lastVisiblepage;
8255
+ currentScrollState.ratios.set(pageNum, ratio);
8256
+ let mostVisiblePage = currentScrollState.lastVisiblePage;
8233
8257
  let maxRatio = -1;
8234
- currentScrollState.ratios.forEach((r, p) => {
8258
+ for (const [p, r] of currentScrollState.ratios) {
8235
8259
  if (r > maxRatio) {
8236
8260
  maxRatio = r;
8237
8261
  mostVisiblePage = p;
8238
8262
  }
8239
- });
8240
- if (mostVisiblePage !== currentScrollState.lastVisiblepage) {
8241
- currentScrollState.lastVisiblepage = mostVisiblePage;
8263
+ if (r > 0.8) break;
8264
+ }
8265
+ if (mostVisiblePage !== currentScrollState.lastVisiblePage) {
8266
+ currentScrollState.lastVisiblePage = mostVisiblePage;
8242
8267
  if (state2.pageNumber !== mostVisiblePage) {
8243
8268
  currentScrollState.isInViewUpdate = true;
8244
8269
  dispatch({ type: "PAGE_IN_VIEW", page: mostVisiblePage });
@@ -8305,7 +8330,7 @@ function usePdfReader(args) {
8305
8330
  tabIndex: -1,
8306
8331
  id: MAIN_CONTENT_ID,
8307
8332
  ref: containerRef,
8308
- height: pageHeight,
8333
+ height,
8309
8334
  sx: {
8310
8335
  ".react-pdf__Document": {
8311
8336
  width: "100%",
@@ -8324,7 +8349,8 @@ function usePdfReader(args) {
8324
8349
  {
8325
8350
  file: state2.resource,
8326
8351
  onLoadSuccess: onDocumentLoadSuccess,
8327
- onLoadError: onDocumentLoadError
8352
+ onLoadError: onDocumentLoadError,
8353
+ inputRef: documentContainerRef
8328
8354
  },
8329
8355
  isParsed && state2.numPages && /* @__PURE__ */ React50.createElement(React50.Fragment, null, state2.settings.isScrolling && Array.from(new Array(state2.numPages), (_, index) => {
8330
8356
  var _a2;
@@ -8342,7 +8368,8 @@ function usePdfReader(args) {
8342
8368
  allowInView: state2.rendered,
8343
8369
  onInView,
8344
8370
  fitMode: state2.fitMode,
8345
- rotate: (_a2 = state2.rotation) != null ? _a2 : 0
8371
+ rotate: (_a2 = state2.rotation) != null ? _a2 : 0,
8372
+ onPageRef: setPageRef
8346
8373
  }
8347
8374
  );
8348
8375
  }), !state2.settings.isScrolling && /* @__PURE__ */ React50.createElement(
@@ -8564,6 +8591,7 @@ var src_default = WebReader;
8564
8591
  DEFAULT_SHOULD_GROW_WHEN_SCROLLING,
8565
8592
  FONT_DETAILS,
8566
8593
  HEADER_HEIGHT,
8594
+ IN_VIEW_DELAY_MS,
8567
8595
  IS_DEV,
8568
8596
  LOCAL_STORAGE_LOCATIONS_KEY,
8569
8597
  LOCAL_STORAGE_SETTINGS_KEY,