@embedpdf/plugin-scroll 1.0.0 → 1.0.2

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.ts CHANGED
@@ -26,6 +26,7 @@ interface VirtualItem {
26
26
 
27
27
  interface ScrollState extends ScrollMetrics {
28
28
  virtualItems: VirtualItem[];
29
+ totalPages: number;
29
30
  totalContentSize: {
30
31
  width: number;
31
32
  height: number;
@@ -108,11 +109,17 @@ interface ScrollToPageOptions {
108
109
  behavior?: ScrollBehavior;
109
110
  center?: boolean;
110
111
  }
112
+ interface PageChangePayload {
113
+ pageNumber: number;
114
+ totalPages: number;
115
+ }
111
116
  interface ScrollCapability {
112
117
  onScrollerData: EventHook<ScrollerLayout>;
113
118
  onStateChange: EventHook<ScrollState>;
114
119
  onScroll: EventHook<ScrollMetrics>;
115
- onPageChange: EventHook<number>;
120
+ getCurrentPage(): number;
121
+ getTotalPages(): number;
122
+ onPageChange: EventHook<PageChangePayload>;
116
123
  onLayoutChange: EventHook<LayoutChangePayload>;
117
124
  scrollToPage(options: ScrollToPageOptions): void;
118
125
  scrollToNextPage(behavior?: ScrollBehavior): void;
@@ -127,6 +134,7 @@ interface ScrollCapability {
127
134
 
128
135
  declare const UPDATE_SCROLL_STATE = "UPDATE_SCROLL_STATE";
129
136
  declare const SET_DESIRED_SCROLL_POSITION = "SET_DESIRED_SCROLL_POSITION";
137
+ declare const UPDATE_TOTAL_PAGES = "UPDATE_TOTAL_PAGES";
130
138
  interface UpdateScrollStateAction extends Action {
131
139
  type: typeof UPDATE_SCROLL_STATE;
132
140
  payload: Partial<ScrollState>;
@@ -138,7 +146,11 @@ interface SetDesiredScrollPositionAction extends Action {
138
146
  y: number;
139
147
  };
140
148
  }
141
- type ScrollAction = UpdateScrollStateAction | SetDesiredScrollPositionAction;
149
+ interface UpdateTotalPagesAction extends Action {
150
+ type: typeof UPDATE_TOTAL_PAGES;
151
+ payload: number;
152
+ }
153
+ type ScrollAction = UpdateScrollStateAction | SetDesiredScrollPositionAction | UpdateTotalPagesAction;
142
154
 
143
155
  declare class ScrollPlugin extends BasePlugin<ScrollPluginConfig, ScrollCapability, ScrollState, ScrollAction> {
144
156
  readonly id: string;
@@ -185,4 +197,4 @@ declare const manifest: PluginManifest<ScrollPluginConfig>;
185
197
 
186
198
  declare const ScrollPluginPackage: PluginPackage<ScrollPlugin, ScrollPluginConfig, ScrollState, ScrollAction>;
187
199
 
188
- export { type LayoutChangePayload, type PageLayout, type PageVisibilityMetrics, SCROLL_PLUGIN_ID, type ScrollCapability, type ScrollMetrics, ScrollPlugin, type ScrollPluginConfig, ScrollPluginPackage, type ScrollState, ScrollStrategy, type ScrollStrategyInterface, type ScrollToPageOptions, type ScrollerLayout, type VirtualItem, manifest };
200
+ export { type LayoutChangePayload, type PageChangePayload, type PageLayout, type PageVisibilityMetrics, SCROLL_PLUGIN_ID, type ScrollCapability, type ScrollMetrics, ScrollPlugin, type ScrollPluginConfig, ScrollPluginPackage, type ScrollState, ScrollStrategy, type ScrollStrategyInterface, type ScrollToPageOptions, type ScrollerLayout, type VirtualItem, manifest };
package/dist/index.js CHANGED
@@ -5,7 +5,6 @@ import {
5
5
  SET_PAGES,
6
6
  SET_ROTATION,
7
7
  createBehaviorEmitter,
8
- createEmitter,
9
8
  getPagesWithRotatedSize
10
9
  } from "@embedpdf/core";
11
10
 
@@ -406,9 +405,13 @@ var HorizontalScrollStrategy = class extends BaseScrollStrategy {
406
405
  // src/lib/actions.ts
407
406
  var UPDATE_SCROLL_STATE = "UPDATE_SCROLL_STATE";
408
407
  var SET_DESIRED_SCROLL_POSITION = "SET_DESIRED_SCROLL_POSITION";
408
+ var UPDATE_TOTAL_PAGES = "UPDATE_TOTAL_PAGES";
409
409
  function updateScrollState(payload) {
410
410
  return { type: UPDATE_SCROLL_STATE, payload };
411
411
  }
412
+ function updateTotalPages(payload) {
413
+ return { type: UPDATE_TOTAL_PAGES, payload };
414
+ }
412
415
 
413
416
  // src/lib/selectors.ts
414
417
  var getScrollerLayout = (state, scale) => {
@@ -449,7 +452,7 @@ var ScrollPlugin = class extends BasePlugin {
449
452
  this.scroll$ = createBehaviorEmitter();
450
453
  this.state$ = createBehaviorEmitter();
451
454
  this.scrollerLayout$ = createBehaviorEmitter();
452
- this.pageChange$ = createEmitter();
455
+ this.pageChange$ = createBehaviorEmitter();
453
456
  this.viewport = this.registry.getPlugin("viewport").provides();
454
457
  this.strategyConfig = {
455
458
  pageGap: this.config?.pageGap ?? 10,
@@ -464,10 +467,12 @@ var ScrollPlugin = class extends BasePlugin {
464
467
  mode: "throttle",
465
468
  wait: 250
466
469
  });
467
- this.coreStore.onAction(
468
- SET_DOCUMENT,
469
- (_action, state) => this.refreshAll(getPagesWithRotatedSize(state.core), this.viewport.getMetrics())
470
- );
470
+ this.coreStore.onAction(SET_DOCUMENT, (_action, state) => {
471
+ const totalPages = state.core.pages.length;
472
+ this.dispatch(updateTotalPages(totalPages));
473
+ this.pageChange$.emit({ pageNumber: this.currentPage, totalPages });
474
+ this.refreshAll(getPagesWithRotatedSize(state.core), this.viewport.getMetrics());
475
+ });
471
476
  this.coreStore.onAction(
472
477
  SET_ROTATION,
473
478
  (_action, state) => this.refreshAll(getPagesWithRotatedSize(state.core), this.viewport.getMetrics())
@@ -498,7 +503,7 @@ var ScrollPlugin = class extends BasePlugin {
498
503
  this.scroll$.emit(emit.metrics);
499
504
  if (emit.metrics.currentPage !== this.currentPage) {
500
505
  this.currentPage = emit.metrics.currentPage;
501
- this.pageChange$.emit(this.currentPage);
506
+ this.pageChange$.emit({ pageNumber: this.currentPage, totalPages: this.state.totalPages });
502
507
  }
503
508
  }
504
509
  this.scrollerLayout$.emit(this.getScrollerLayoutFromState());
@@ -559,6 +564,8 @@ var ScrollPlugin = class extends BasePlugin {
559
564
  onScroll: this.scroll$.on,
560
565
  onPageChange: this.pageChange$.on,
561
566
  onScrollerData: this.scrollerLayout$.on,
567
+ getCurrentPage: () => this.currentPage,
568
+ getTotalPages: () => this.state.totalPages,
562
569
  scrollToPage: (options) => {
563
570
  const { pageNumber, behavior = "smooth", pageCoordinates, center = false } = options;
564
571
  const virtualItems = this.getVirtualItemsFromState();
@@ -677,6 +684,7 @@ var defaultScrollMetrics = {
677
684
  };
678
685
  var initialState = (coreState, config) => ({
679
686
  virtualItems: [],
687
+ totalPages: coreState.pages.length,
680
688
  totalContentSize: { width: 0, height: 0 },
681
689
  desiredScrollPosition: { x: 0, y: 0 },
682
690
  strategy: config.strategy ?? "vertical" /* Vertical */,
@@ -686,6 +694,8 @@ var initialState = (coreState, config) => ({
686
694
  });
687
695
  var scrollReducer = (state, action) => {
688
696
  switch (action.type) {
697
+ case UPDATE_TOTAL_PAGES:
698
+ return { ...state, totalPages: action.payload };
689
699
  case SET_SCALE:
690
700
  return { ...state, scale: action.payload };
691
701
  case UPDATE_SCROLL_STATE: