@embedpdf/plugin-viewport 1.0.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/dist/index.cjs +246 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +105 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +216 -0
- package/dist/index.js.map +1 -0
- package/dist/preact/index.cjs +124 -0
- package/dist/preact/index.cjs.map +1 -0
- package/dist/preact/index.d.cts +23 -0
- package/dist/preact/index.d.ts +23 -0
- package/dist/preact/index.js +95 -0
- package/dist/preact/index.js.map +1 -0
- package/dist/react/index.cjs +126 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +25 -0
- package/dist/react/index.d.ts +25 -0
- package/dist/react/index.js +96 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +63 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// src/lib/manifest.ts
|
|
2
|
+
var VIEWPORT_PLUGIN_ID = "viewport";
|
|
3
|
+
var manifest = {
|
|
4
|
+
id: VIEWPORT_PLUGIN_ID,
|
|
5
|
+
name: "Viewport Plugin",
|
|
6
|
+
version: "1.0.0",
|
|
7
|
+
provides: ["viewport"],
|
|
8
|
+
requires: [],
|
|
9
|
+
optional: [],
|
|
10
|
+
defaultConfig: {
|
|
11
|
+
enabled: true,
|
|
12
|
+
viewportGap: 10,
|
|
13
|
+
scrollEndDelay: 300
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// src/lib/actions.ts
|
|
18
|
+
var SET_VIEWPORT_METRICS = "SET_VIEWPORT_METRICS";
|
|
19
|
+
var SET_VIEWPORT_SCROLL_METRICS = "SET_VIEWPORT_SCROLL_METRICS";
|
|
20
|
+
var SET_VIEWPORT_GAP = "SET_VIEWPORT_GAP";
|
|
21
|
+
var SET_SCROLL_ACTIVITY = "SET_SCROLL_ACTIVITY";
|
|
22
|
+
function setViewportGap(viewportGap) {
|
|
23
|
+
return {
|
|
24
|
+
type: SET_VIEWPORT_GAP,
|
|
25
|
+
payload: viewportGap
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function setViewportMetrics(viewportMetrics) {
|
|
29
|
+
return {
|
|
30
|
+
type: SET_VIEWPORT_METRICS,
|
|
31
|
+
payload: viewportMetrics
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function setViewportScrollMetrics(scrollMetrics) {
|
|
35
|
+
return {
|
|
36
|
+
type: SET_VIEWPORT_SCROLL_METRICS,
|
|
37
|
+
payload: scrollMetrics
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function setScrollActivity(isScrolling) {
|
|
41
|
+
return { type: SET_SCROLL_ACTIVITY, payload: isScrolling };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/lib/reducer.ts
|
|
45
|
+
var initialState = {
|
|
46
|
+
viewportGap: 0,
|
|
47
|
+
viewportMetrics: {
|
|
48
|
+
width: 0,
|
|
49
|
+
height: 0,
|
|
50
|
+
scrollTop: 0,
|
|
51
|
+
scrollLeft: 0,
|
|
52
|
+
clientWidth: 0,
|
|
53
|
+
clientHeight: 0,
|
|
54
|
+
scrollWidth: 0,
|
|
55
|
+
scrollHeight: 0,
|
|
56
|
+
relativePosition: {
|
|
57
|
+
x: 0,
|
|
58
|
+
y: 0
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
isScrolling: false
|
|
62
|
+
};
|
|
63
|
+
var viewportReducer = (state = initialState, action) => {
|
|
64
|
+
switch (action.type) {
|
|
65
|
+
case SET_VIEWPORT_GAP:
|
|
66
|
+
return { ...state, viewportGap: action.payload };
|
|
67
|
+
case SET_VIEWPORT_METRICS:
|
|
68
|
+
return {
|
|
69
|
+
...state,
|
|
70
|
+
viewportMetrics: {
|
|
71
|
+
width: action.payload.width,
|
|
72
|
+
height: action.payload.height,
|
|
73
|
+
scrollTop: action.payload.scrollTop,
|
|
74
|
+
scrollLeft: action.payload.scrollLeft,
|
|
75
|
+
clientWidth: action.payload.clientWidth,
|
|
76
|
+
clientHeight: action.payload.clientHeight,
|
|
77
|
+
scrollWidth: action.payload.scrollWidth,
|
|
78
|
+
scrollHeight: action.payload.scrollHeight,
|
|
79
|
+
relativePosition: {
|
|
80
|
+
x: action.payload.scrollWidth <= action.payload.clientWidth ? 0 : action.payload.scrollLeft / (action.payload.scrollWidth - action.payload.clientWidth),
|
|
81
|
+
y: action.payload.scrollHeight <= action.payload.clientHeight ? 0 : action.payload.scrollTop / (action.payload.scrollHeight - action.payload.clientHeight)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
case SET_VIEWPORT_SCROLL_METRICS:
|
|
86
|
+
return {
|
|
87
|
+
...state,
|
|
88
|
+
viewportMetrics: {
|
|
89
|
+
...state.viewportMetrics,
|
|
90
|
+
scrollTop: action.payload.scrollTop,
|
|
91
|
+
scrollLeft: action.payload.scrollLeft
|
|
92
|
+
},
|
|
93
|
+
isScrolling: true
|
|
94
|
+
};
|
|
95
|
+
case SET_SCROLL_ACTIVITY:
|
|
96
|
+
return { ...state, isScrolling: action.payload };
|
|
97
|
+
default:
|
|
98
|
+
return state;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// src/lib/viewport-plugin.ts
|
|
103
|
+
import { BasePlugin, createEmitter, createBehaviorEmitter } from "@embedpdf/core";
|
|
104
|
+
var ViewportPlugin = class extends BasePlugin {
|
|
105
|
+
constructor(id, registry, config) {
|
|
106
|
+
super(id, registry);
|
|
107
|
+
this.id = id;
|
|
108
|
+
this.viewportMetrics$ = createBehaviorEmitter();
|
|
109
|
+
this.scrollMetrics$ = createBehaviorEmitter();
|
|
110
|
+
this.scrollReq$ = createEmitter();
|
|
111
|
+
this.scrollActivity$ = createBehaviorEmitter();
|
|
112
|
+
/* ------------------------------------------------------------------ */
|
|
113
|
+
/* “live rect” infrastructure */
|
|
114
|
+
/* ------------------------------------------------------------------ */
|
|
115
|
+
this.rectProvider = null;
|
|
116
|
+
if (config.viewportGap) {
|
|
117
|
+
this.dispatch(setViewportGap(config.viewportGap));
|
|
118
|
+
}
|
|
119
|
+
this.scrollEndDelay = config.scrollEndDelay || 300;
|
|
120
|
+
}
|
|
121
|
+
buildCapability() {
|
|
122
|
+
return {
|
|
123
|
+
getViewportGap: () => this.state.viewportGap,
|
|
124
|
+
getMetrics: () => this.state.viewportMetrics,
|
|
125
|
+
onScrollChange: this.scrollMetrics$.on,
|
|
126
|
+
onViewportChange: this.viewportMetrics$.on,
|
|
127
|
+
registerBoundingRectProvider: (fn) => {
|
|
128
|
+
this.rectProvider = fn;
|
|
129
|
+
},
|
|
130
|
+
getBoundingRect: () => this.rectProvider?.() ?? {
|
|
131
|
+
origin: { x: 0, y: 0 },
|
|
132
|
+
size: { width: 0, height: 0 }
|
|
133
|
+
},
|
|
134
|
+
setViewportMetrics: (viewportMetrics) => {
|
|
135
|
+
this.dispatch(setViewportMetrics(viewportMetrics));
|
|
136
|
+
},
|
|
137
|
+
setViewportScrollMetrics: this.setViewportScrollMetrics.bind(this),
|
|
138
|
+
scrollTo: (pos) => this.scrollTo(pos),
|
|
139
|
+
onScrollRequest: this.scrollReq$.on,
|
|
140
|
+
isScrolling: () => this.state.isScrolling,
|
|
141
|
+
onScrollActivity: this.scrollActivity$.on
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
bumpScrollActivity() {
|
|
145
|
+
if (this.scrollEndTimer) clearTimeout(this.scrollEndTimer);
|
|
146
|
+
this.scrollEndTimer = window.setTimeout(() => {
|
|
147
|
+
this.dispatch(setScrollActivity(false));
|
|
148
|
+
this.scrollEndTimer = void 0;
|
|
149
|
+
}, this.scrollEndDelay);
|
|
150
|
+
}
|
|
151
|
+
setViewportScrollMetrics(scrollMetrics) {
|
|
152
|
+
if (scrollMetrics.scrollTop !== this.state.viewportMetrics.scrollTop || scrollMetrics.scrollLeft !== this.state.viewportMetrics.scrollLeft) {
|
|
153
|
+
this.dispatch(setViewportScrollMetrics(scrollMetrics));
|
|
154
|
+
this.bumpScrollActivity();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
scrollTo(pos) {
|
|
158
|
+
const { x, y, center, behavior = "auto" } = pos;
|
|
159
|
+
if (center) {
|
|
160
|
+
const metrics = this.state.viewportMetrics;
|
|
161
|
+
const centeredX = x - metrics.clientWidth / 2;
|
|
162
|
+
const centeredY = y - metrics.clientHeight / 2;
|
|
163
|
+
this.scrollReq$.emit({
|
|
164
|
+
x: centeredX,
|
|
165
|
+
y: centeredY,
|
|
166
|
+
behavior
|
|
167
|
+
});
|
|
168
|
+
} else {
|
|
169
|
+
this.scrollReq$.emit({
|
|
170
|
+
x,
|
|
171
|
+
y,
|
|
172
|
+
behavior
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// Subscribe to store changes to notify onViewportChange
|
|
177
|
+
onStoreUpdated(prevState, newState) {
|
|
178
|
+
if (prevState !== newState) {
|
|
179
|
+
this.viewportMetrics$.emit(newState.viewportMetrics);
|
|
180
|
+
this.scrollMetrics$.emit({
|
|
181
|
+
scrollTop: newState.viewportMetrics.scrollTop,
|
|
182
|
+
scrollLeft: newState.viewportMetrics.scrollLeft
|
|
183
|
+
});
|
|
184
|
+
if (prevState.isScrolling !== newState.isScrolling) {
|
|
185
|
+
this.scrollActivity$.emit(newState.isScrolling);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async initialize(_config) {
|
|
190
|
+
}
|
|
191
|
+
async destroy() {
|
|
192
|
+
super.destroy();
|
|
193
|
+
this.viewportMetrics$.clear();
|
|
194
|
+
this.scrollMetrics$.clear();
|
|
195
|
+
this.scrollReq$.clear();
|
|
196
|
+
this.scrollActivity$.clear();
|
|
197
|
+
this.rectProvider = null;
|
|
198
|
+
if (this.scrollEndTimer) clearTimeout(this.scrollEndTimer);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
ViewportPlugin.id = "viewport";
|
|
202
|
+
|
|
203
|
+
// src/lib/index.ts
|
|
204
|
+
var ViewportPluginPackage = {
|
|
205
|
+
manifest,
|
|
206
|
+
create: (registry, _engine, config) => new ViewportPlugin(VIEWPORT_PLUGIN_ID, registry, config),
|
|
207
|
+
reducer: viewportReducer,
|
|
208
|
+
initialState
|
|
209
|
+
};
|
|
210
|
+
export {
|
|
211
|
+
VIEWPORT_PLUGIN_ID,
|
|
212
|
+
ViewportPlugin,
|
|
213
|
+
ViewportPluginPackage,
|
|
214
|
+
manifest
|
|
215
|
+
};
|
|
216
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/lib/manifest.ts","../src/lib/actions.ts","../src/lib/reducer.ts","../src/lib/viewport-plugin.ts","../src/lib/index.ts"],"sourcesContent":["import { PluginManifest } from '@embedpdf/core';\n\nimport { ViewportPluginConfig } from './types';\n\nexport const VIEWPORT_PLUGIN_ID = 'viewport';\n\nexport const manifest: PluginManifest<ViewportPluginConfig> = {\n id: VIEWPORT_PLUGIN_ID,\n name: 'Viewport Plugin',\n version: '1.0.0',\n provides: ['viewport'],\n requires: [],\n optional: [],\n defaultConfig: {\n enabled: true,\n viewportGap: 10,\n scrollEndDelay: 300,\n },\n};\n","import { Action } from '@embedpdf/core';\n\nimport { ViewportInputMetrics, ViewportScrollMetrics } from './types';\n\nexport const SET_VIEWPORT_METRICS = 'SET_VIEWPORT_METRICS';\nexport const SET_VIEWPORT_SCROLL_METRICS = 'SET_VIEWPORT_SCROLL_METRICS';\nexport const SET_VIEWPORT_GAP = 'SET_VIEWPORT_GAP';\nexport const SET_SCROLL_ACTIVITY = 'SET_SCROLL_ACTIVITY';\n\nexport interface SetViewportMetricsAction extends Action {\n type: typeof SET_VIEWPORT_METRICS;\n payload: ViewportInputMetrics;\n}\n\nexport interface SetViewportScrollMetricsAction extends Action {\n type: typeof SET_VIEWPORT_SCROLL_METRICS;\n payload: ViewportScrollMetrics;\n}\n\nexport interface SetViewportGapAction extends Action {\n type: typeof SET_VIEWPORT_GAP;\n payload: number;\n}\n\nexport interface SetScrollActivityAction extends Action {\n type: typeof SET_SCROLL_ACTIVITY;\n payload: boolean;\n}\n\nexport type ViewportAction =\n | SetViewportMetricsAction\n | SetViewportScrollMetricsAction\n | SetViewportGapAction\n | SetScrollActivityAction;\n\nexport function setViewportGap(viewportGap: number): SetViewportGapAction {\n return {\n type: SET_VIEWPORT_GAP,\n payload: viewportGap,\n };\n}\n\nexport function setViewportMetrics(\n viewportMetrics: ViewportInputMetrics,\n): SetViewportMetricsAction {\n return {\n type: SET_VIEWPORT_METRICS,\n payload: viewportMetrics,\n };\n}\n\nexport function setViewportScrollMetrics(\n scrollMetrics: ViewportScrollMetrics,\n): SetViewportScrollMetricsAction {\n return {\n type: SET_VIEWPORT_SCROLL_METRICS,\n payload: scrollMetrics,\n };\n}\n\nexport function setScrollActivity(isScrolling: boolean): SetScrollActivityAction {\n return { type: SET_SCROLL_ACTIVITY, payload: isScrolling };\n}\n","import { Reducer } from '@embedpdf/core';\n\nimport {\n SET_VIEWPORT_METRICS,\n SET_VIEWPORT_SCROLL_METRICS,\n SET_VIEWPORT_GAP,\n ViewportAction,\n SET_SCROLL_ACTIVITY,\n} from './actions';\nimport { ViewportState } from './types';\n\nexport const initialState: ViewportState = {\n viewportGap: 0,\n viewportMetrics: {\n width: 0,\n height: 0,\n scrollTop: 0,\n scrollLeft: 0,\n clientWidth: 0,\n clientHeight: 0,\n scrollWidth: 0,\n scrollHeight: 0,\n relativePosition: {\n x: 0,\n y: 0,\n },\n },\n isScrolling: false,\n};\n\nexport const viewportReducer: Reducer<ViewportState, ViewportAction> = (\n state = initialState,\n action,\n) => {\n switch (action.type) {\n case SET_VIEWPORT_GAP:\n return { ...state, viewportGap: action.payload };\n case SET_VIEWPORT_METRICS:\n return {\n ...state,\n viewportMetrics: {\n width: action.payload.width,\n height: action.payload.height,\n scrollTop: action.payload.scrollTop,\n scrollLeft: action.payload.scrollLeft,\n clientWidth: action.payload.clientWidth,\n clientHeight: action.payload.clientHeight,\n scrollWidth: action.payload.scrollWidth,\n scrollHeight: action.payload.scrollHeight,\n relativePosition: {\n x:\n action.payload.scrollWidth <= action.payload.clientWidth\n ? 0\n : action.payload.scrollLeft /\n (action.payload.scrollWidth - action.payload.clientWidth),\n y:\n action.payload.scrollHeight <= action.payload.clientHeight\n ? 0\n : action.payload.scrollTop /\n (action.payload.scrollHeight - action.payload.clientHeight),\n },\n },\n };\n case SET_VIEWPORT_SCROLL_METRICS:\n return {\n ...state,\n viewportMetrics: {\n ...state.viewportMetrics,\n scrollTop: action.payload.scrollTop,\n scrollLeft: action.payload.scrollLeft,\n },\n isScrolling: true,\n };\n case SET_SCROLL_ACTIVITY:\n return { ...state, isScrolling: action.payload };\n default:\n return state;\n }\n};\n","import { BasePlugin, PluginRegistry, createEmitter, createBehaviorEmitter } from '@embedpdf/core';\n\nimport {\n ViewportAction,\n setViewportMetrics,\n setViewportScrollMetrics,\n setViewportGap,\n setScrollActivity,\n} from './actions';\nimport {\n ViewportPluginConfig,\n ViewportState,\n ViewportCapability,\n ViewportMetrics,\n ViewportScrollMetrics,\n ViewportInputMetrics,\n ScrollToPayload,\n} from './types';\nimport { Rect } from '@embedpdf/models';\n\nexport class ViewportPlugin extends BasePlugin<\n ViewportPluginConfig,\n ViewportCapability,\n ViewportState,\n ViewportAction\n> {\n static readonly id = 'viewport' as const;\n\n private readonly viewportMetrics$ = createBehaviorEmitter<ViewportMetrics>();\n private readonly scrollMetrics$ = createBehaviorEmitter<ViewportScrollMetrics>();\n private readonly scrollReq$ = createEmitter<{\n x: number;\n y: number;\n behavior?: ScrollBehavior;\n }>();\n private readonly scrollActivity$ = createBehaviorEmitter<boolean>();\n\n /* ------------------------------------------------------------------ */\n /* “live rect” infrastructure */\n /* ------------------------------------------------------------------ */\n private rectProvider: (() => Rect) | null = null;\n\n private scrollEndTimer?: number;\n private readonly scrollEndDelay: number;\n\n constructor(\n public readonly id: string,\n registry: PluginRegistry,\n config: ViewportPluginConfig,\n ) {\n super(id, registry);\n\n if (config.viewportGap) {\n this.dispatch(setViewportGap(config.viewportGap));\n }\n\n this.scrollEndDelay = config.scrollEndDelay || 300;\n }\n\n protected buildCapability(): ViewportCapability {\n return {\n getViewportGap: () => this.state.viewportGap,\n getMetrics: () => this.state.viewportMetrics,\n onScrollChange: this.scrollMetrics$.on,\n onViewportChange: this.viewportMetrics$.on,\n registerBoundingRectProvider: (fn) => {\n this.rectProvider = fn;\n },\n getBoundingRect: (): Rect =>\n this.rectProvider?.() ?? {\n origin: { x: 0, y: 0 },\n size: { width: 0, height: 0 },\n },\n setViewportMetrics: (viewportMetrics: ViewportInputMetrics) => {\n this.dispatch(setViewportMetrics(viewportMetrics));\n },\n setViewportScrollMetrics: this.setViewportScrollMetrics.bind(this),\n scrollTo: (pos: ScrollToPayload) => this.scrollTo(pos),\n onScrollRequest: this.scrollReq$.on,\n isScrolling: () => this.state.isScrolling,\n onScrollActivity: this.scrollActivity$.on,\n };\n }\n\n private bumpScrollActivity() {\n if (this.scrollEndTimer) clearTimeout(this.scrollEndTimer);\n this.scrollEndTimer = window.setTimeout(() => {\n this.dispatch(setScrollActivity(false));\n this.scrollEndTimer = undefined;\n }, this.scrollEndDelay);\n }\n\n private setViewportScrollMetrics(scrollMetrics: ViewportScrollMetrics) {\n if (\n scrollMetrics.scrollTop !== this.state.viewportMetrics.scrollTop ||\n scrollMetrics.scrollLeft !== this.state.viewportMetrics.scrollLeft\n ) {\n this.dispatch(setViewportScrollMetrics(scrollMetrics));\n this.bumpScrollActivity();\n }\n }\n\n private scrollTo(pos: ScrollToPayload) {\n const { x, y, center, behavior = 'auto' } = pos;\n\n if (center) {\n const metrics = this.state.viewportMetrics;\n // Calculate the centered position by adding half the viewport dimensions\n const centeredX = x - metrics.clientWidth / 2;\n const centeredY = y - metrics.clientHeight / 2;\n\n this.scrollReq$.emit({\n x: centeredX,\n y: centeredY,\n behavior,\n });\n } else {\n this.scrollReq$.emit({\n x,\n y,\n behavior,\n });\n }\n }\n\n // Subscribe to store changes to notify onViewportChange\n override onStoreUpdated(prevState: ViewportState, newState: ViewportState): void {\n if (prevState !== newState) {\n this.viewportMetrics$.emit(newState.viewportMetrics);\n this.scrollMetrics$.emit({\n scrollTop: newState.viewportMetrics.scrollTop,\n scrollLeft: newState.viewportMetrics.scrollLeft,\n });\n if (prevState.isScrolling !== newState.isScrolling) {\n this.scrollActivity$.emit(newState.isScrolling);\n }\n }\n }\n\n async initialize(_config: ViewportPluginConfig) {\n // No initialization needed\n }\n\n async destroy(): Promise<void> {\n super.destroy();\n // Clear out any handlers\n this.viewportMetrics$.clear();\n this.scrollMetrics$.clear();\n this.scrollReq$.clear();\n this.scrollActivity$.clear();\n this.rectProvider = null;\n if (this.scrollEndTimer) clearTimeout(this.scrollEndTimer);\n }\n}\n","import { PluginPackage } from '@embedpdf/core';\n\nimport { ViewportAction } from './actions';\nimport { manifest, VIEWPORT_PLUGIN_ID } from './manifest';\nimport { viewportReducer, initialState } from './reducer';\nimport { ViewportPluginConfig, ViewportState } from './types';\nimport { ViewportPlugin } from './viewport-plugin';\n\nexport const ViewportPluginPackage: PluginPackage<\n ViewportPlugin,\n ViewportPluginConfig,\n ViewportState,\n ViewportAction\n> = {\n manifest,\n create: (registry, _engine, config) => new ViewportPlugin(VIEWPORT_PLUGIN_ID, registry, config),\n reducer: viewportReducer,\n initialState: initialState,\n};\n\nexport * from './viewport-plugin';\nexport * from './types';\nexport * from './manifest';\n"],"mappings":";AAIO,IAAM,qBAAqB;AAE3B,IAAM,WAAiD;AAAA,EAC5D,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,SAAS;AAAA,EACT,UAAU,CAAC,UAAU;AAAA,EACrB,UAAU,CAAC;AAAA,EACX,UAAU,CAAC;AAAA,EACX,eAAe;AAAA,IACb,SAAS;AAAA,IACT,aAAa;AAAA,IACb,gBAAgB;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;AAAA,EACF;AAAA;AAAA,EAGS,eAAe,WAA0B,UAA+B;AAC/E,QAAI,cAAc,UAAU;AAC1B,WAAK,iBAAiB,KAAK,SAAS,eAAe;AACnD,WAAK,eAAe,KAAK;AAAA,QACvB,WAAW,SAAS,gBAAgB;AAAA,QACpC,YAAY,SAAS,gBAAgB;AAAA,MACvC,CAAC;AACD,UAAI,UAAU,gBAAgB,SAAS,aAAa;AAClD,aAAK,gBAAgB,KAAK,SAAS,WAAW;AAAA,MAChD;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,WAAW,SAA+B;AAAA,EAEhD;AAAA,EAEA,MAAM,UAAyB;AAC7B,UAAM,QAAQ;AAEd,SAAK,iBAAiB,MAAM;AAC5B,SAAK,eAAe,MAAM;AAC1B,SAAK,WAAW,MAAM;AACtB,SAAK,gBAAgB,MAAM;AAC3B,SAAK,eAAe;AACpB,QAAI,KAAK,eAAgB,cAAa,KAAK,cAAc;AAAA,EAC3D;AACF;AArIa,eAMK,KAAK;;;AClBhB,IAAM,wBAKT;AAAA,EACF;AAAA,EACA,QAAQ,CAAC,UAAU,SAAS,WAAW,IAAI,eAAe,oBAAoB,UAAU,MAAM;AAAA,EAC9F,SAAS;AAAA,EACT;AACF;","names":[]}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/preact/index.ts
|
|
21
|
+
var preact_exports = {};
|
|
22
|
+
__export(preact_exports, {
|
|
23
|
+
Viewport: () => Viewport,
|
|
24
|
+
useViewport: () => useViewport,
|
|
25
|
+
useViewportCapability: () => useViewportCapability
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(preact_exports);
|
|
28
|
+
|
|
29
|
+
// src/preact/hooks/use-viewport.ts
|
|
30
|
+
var import_preact = require("@embedpdf/core/preact");
|
|
31
|
+
var import_plugin_viewport = require("@embedpdf/plugin-viewport");
|
|
32
|
+
var useViewport = () => (0, import_preact.usePlugin)(import_plugin_viewport.ViewportPlugin.id);
|
|
33
|
+
var useViewportCapability = () => (0, import_preact.useCapability)(import_plugin_viewport.ViewportPlugin.id);
|
|
34
|
+
|
|
35
|
+
// src/preact/components/viewport.tsx
|
|
36
|
+
var import_hooks2 = require("preact/hooks");
|
|
37
|
+
|
|
38
|
+
// src/preact/hooks/use-viewport-ref.ts
|
|
39
|
+
var import_hooks = require("preact/hooks");
|
|
40
|
+
function useViewportRef() {
|
|
41
|
+
const { provides: viewportProvides } = useViewportCapability();
|
|
42
|
+
const containerRef = (0, import_hooks.useRef)(null);
|
|
43
|
+
(0, import_hooks.useLayoutEffect)(() => {
|
|
44
|
+
if (!viewportProvides) return;
|
|
45
|
+
const container = containerRef.current;
|
|
46
|
+
if (!container) return;
|
|
47
|
+
const provideRect = () => {
|
|
48
|
+
const r = container.getBoundingClientRect();
|
|
49
|
+
return {
|
|
50
|
+
origin: { x: r.left, y: r.top },
|
|
51
|
+
size: { width: r.width, height: r.height }
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
viewportProvides.registerBoundingRectProvider(provideRect);
|
|
55
|
+
const onScroll = () => {
|
|
56
|
+
viewportProvides.setViewportScrollMetrics({
|
|
57
|
+
scrollTop: container.scrollTop,
|
|
58
|
+
scrollLeft: container.scrollLeft
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
container.addEventListener("scroll", onScroll);
|
|
62
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
63
|
+
const rect = container.getBoundingClientRect();
|
|
64
|
+
viewportProvides.setViewportMetrics({
|
|
65
|
+
width: container.offsetWidth,
|
|
66
|
+
height: container.offsetHeight,
|
|
67
|
+
clientWidth: container.clientWidth,
|
|
68
|
+
clientHeight: container.clientHeight,
|
|
69
|
+
scrollTop: container.scrollTop,
|
|
70
|
+
scrollLeft: container.scrollLeft,
|
|
71
|
+
scrollWidth: container.scrollWidth,
|
|
72
|
+
scrollHeight: container.scrollHeight
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
resizeObserver.observe(container);
|
|
76
|
+
const unsubscribeScrollRequest = viewportProvides.onScrollRequest(
|
|
77
|
+
({ x, y, behavior = "auto" }) => {
|
|
78
|
+
requestAnimationFrame(() => {
|
|
79
|
+
container.scrollTo({ left: x, top: y, behavior });
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
return () => {
|
|
84
|
+
viewportProvides.registerBoundingRectProvider(null);
|
|
85
|
+
container.removeEventListener("scroll", onScroll);
|
|
86
|
+
resizeObserver.disconnect();
|
|
87
|
+
unsubscribeScrollRequest();
|
|
88
|
+
};
|
|
89
|
+
}, [viewportProvides]);
|
|
90
|
+
return containerRef;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/preact/components/viewport.tsx
|
|
94
|
+
var import_jsx_runtime = require("preact/jsx-runtime");
|
|
95
|
+
function Viewport({ children, ...props }) {
|
|
96
|
+
const [viewportGap, setViewportGap] = (0, import_hooks2.useState)(0);
|
|
97
|
+
const viewportRef = useViewportRef();
|
|
98
|
+
const { provides: viewportProvides } = useViewportCapability();
|
|
99
|
+
(0, import_hooks2.useEffect)(() => {
|
|
100
|
+
if (viewportProvides) {
|
|
101
|
+
setViewportGap(viewportProvides.getViewportGap());
|
|
102
|
+
}
|
|
103
|
+
}, [viewportProvides]);
|
|
104
|
+
const { style, ...restProps } = props;
|
|
105
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
106
|
+
"div",
|
|
107
|
+
{
|
|
108
|
+
...restProps,
|
|
109
|
+
ref: viewportRef,
|
|
110
|
+
style: {
|
|
111
|
+
...typeof style === "object" ? style : {},
|
|
112
|
+
padding: `${viewportGap}px`
|
|
113
|
+
},
|
|
114
|
+
children
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
Viewport,
|
|
121
|
+
useViewport,
|
|
122
|
+
useViewportCapability
|
|
123
|
+
});
|
|
124
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/preact/index.ts","../../src/preact/hooks/use-viewport.ts","../../src/preact/components/viewport.tsx","../../src/preact/hooks/use-viewport-ref.ts"],"sourcesContent":["export * from './hooks';\nexport * from './components';\n","import { useCapability, usePlugin } from '@embedpdf/core/preact';\nimport { ViewportPlugin } from '@embedpdf/plugin-viewport';\n\nexport const useViewport = () => usePlugin<ViewportPlugin>(ViewportPlugin.id);\nexport const useViewportCapability = () => useCapability<ViewportPlugin>(ViewportPlugin.id);\n","/** @jsxImportSource preact */\nimport { ComponentChildren, JSX } from 'preact';\nimport { useEffect, useState } from 'preact/hooks';\n\nimport { useViewportCapability } from '../hooks';\nimport { useViewportRef } from '../hooks/use-viewport-ref';\n\ntype ViewportProps = JSX.HTMLAttributes<HTMLDivElement> & {\n children: ComponentChildren;\n};\n\nexport function Viewport({ children, ...props }: ViewportProps) {\n const [viewportGap, setViewportGap] = useState(0);\n const viewportRef = useViewportRef();\n const { provides: viewportProvides } = useViewportCapability();\n\n useEffect(() => {\n if (viewportProvides) {\n setViewportGap(viewportProvides.getViewportGap());\n }\n }, [viewportProvides]);\n\n const { style, ...restProps } = props;\n return (\n <div\n {...restProps}\n ref={viewportRef}\n style={{\n ...(typeof style === 'object' ? style : {}),\n padding: `${viewportGap}px`,\n }}\n >\n {children}\n </div>\n );\n}\n","import { useLayoutEffect, useRef } from 'preact/hooks';\n\nimport { useViewportCapability } from './use-viewport';\nimport { Rect } from '@embedpdf/models';\n\nexport function useViewportRef() {\n const { provides: viewportProvides } = useViewportCapability();\n const containerRef = useRef<HTMLDivElement>(null);\n\n useLayoutEffect(() => {\n if (!viewportProvides) return;\n\n const container = containerRef.current;\n if (!container) return;\n\n /* ---------- live rect provider --------------------------------- */\n const provideRect = (): Rect => {\n const r = container.getBoundingClientRect();\n return {\n origin: { x: r.left, y: r.top },\n size: { width: r.width, height: r.height },\n };\n };\n viewportProvides.registerBoundingRectProvider(provideRect);\n\n // Example: On scroll, call setMetrics\n const onScroll = () => {\n viewportProvides.setViewportScrollMetrics({\n scrollTop: container.scrollTop,\n scrollLeft: container.scrollLeft,\n });\n };\n container.addEventListener('scroll', onScroll);\n\n // Example: On resize, call setMetrics\n const resizeObserver = new ResizeObserver(() => {\n const rect = container.getBoundingClientRect();\n\n viewportProvides.setViewportMetrics({\n width: container.offsetWidth,\n height: container.offsetHeight,\n clientWidth: container.clientWidth,\n clientHeight: container.clientHeight,\n scrollTop: container.scrollTop,\n scrollLeft: container.scrollLeft,\n scrollWidth: container.scrollWidth,\n scrollHeight: container.scrollHeight,\n });\n });\n resizeObserver.observe(container);\n\n const unsubscribeScrollRequest = viewportProvides.onScrollRequest(\n ({ x, y, behavior = 'auto' }) => {\n requestAnimationFrame(() => {\n container.scrollTo({ left: x, top: y, behavior });\n });\n },\n );\n\n // Cleanup\n return () => {\n viewportProvides.registerBoundingRectProvider(null);\n container.removeEventListener('scroll', onScroll);\n resizeObserver.disconnect();\n unsubscribeScrollRequest();\n };\n }, [viewportProvides]);\n\n // Return the ref so your React code can attach it to a div\n return containerRef;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyC;AACzC,6BAA+B;AAExB,IAAM,cAAc,UAAM,yBAA0B,sCAAe,EAAE;AACrE,IAAM,wBAAwB,UAAM,6BAA8B,sCAAe,EAAE;;;ACF1F,IAAAA,gBAAoC;;;ACFpC,mBAAwC;AAKjC,SAAS,iBAAiB;AAC/B,QAAM,EAAE,UAAU,iBAAiB,IAAI,sBAAsB;AAC7D,QAAM,mBAAe,qBAAuB,IAAI;AAEhD,oCAAgB,MAAM;AACpB,QAAI,CAAC,iBAAkB;AAEvB,UAAM,YAAY,aAAa;AAC/B,QAAI,CAAC,UAAW;AAGhB,UAAM,cAAc,MAAY;AAC9B,YAAM,IAAI,UAAU,sBAAsB;AAC1C,aAAO;AAAA,QACL,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,EAAE,IAAI;AAAA,QAC9B,MAAM,EAAE,OAAO,EAAE,OAAO,QAAQ,EAAE,OAAO;AAAA,MAC3C;AAAA,IACF;AACA,qBAAiB,6BAA6B,WAAW;AAGzD,UAAM,WAAW,MAAM;AACrB,uBAAiB,yBAAyB;AAAA,QACxC,WAAW,UAAU;AAAA,QACrB,YAAY,UAAU;AAAA,MACxB,CAAC;AAAA,IACH;AACA,cAAU,iBAAiB,UAAU,QAAQ;AAG7C,UAAM,iBAAiB,IAAI,eAAe,MAAM;AAC9C,YAAM,OAAO,UAAU,sBAAsB;AAE7C,uBAAiB,mBAAmB;AAAA,QAClC,OAAO,UAAU;AAAA,QACjB,QAAQ,UAAU;AAAA,QAClB,aAAa,UAAU;AAAA,QACvB,cAAc,UAAU;AAAA,QACxB,WAAW,UAAU;AAAA,QACrB,YAAY,UAAU;AAAA,QACtB,aAAa,UAAU;AAAA,QACvB,cAAc,UAAU;AAAA,MAC1B,CAAC;AAAA,IACH,CAAC;AACD,mBAAe,QAAQ,SAAS;AAEhC,UAAM,2BAA2B,iBAAiB;AAAA,MAChD,CAAC,EAAE,GAAG,GAAG,WAAW,OAAO,MAAM;AAC/B,8BAAsB,MAAM;AAC1B,oBAAU,SAAS,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,CAAC;AAAA,QAClD,CAAC;AAAA,MACH;AAAA,IACF;AAGA,WAAO,MAAM;AACX,uBAAiB,6BAA6B,IAAI;AAClD,gBAAU,oBAAoB,UAAU,QAAQ;AAChD,qBAAe,WAAW;AAC1B,+BAAyB;AAAA,IAC3B;AAAA,EACF,GAAG,CAAC,gBAAgB,CAAC;AAGrB,SAAO;AACT;;;AD9CI;AAbG,SAAS,SAAS,EAAE,UAAU,GAAG,MAAM,GAAkB;AAC9D,QAAM,CAAC,aAAa,cAAc,QAAI,wBAAS,CAAC;AAChD,QAAM,cAAc,eAAe;AACnC,QAAM,EAAE,UAAU,iBAAiB,IAAI,sBAAsB;AAE7D,+BAAU,MAAM;AACd,QAAI,kBAAkB;AACpB,qBAAe,iBAAiB,eAAe,CAAC;AAAA,IAClD;AAAA,EACF,GAAG,CAAC,gBAAgB,CAAC;AAErB,QAAM,EAAE,OAAO,GAAG,UAAU,IAAI;AAChC,SACE;AAAA,IAAC;AAAA;AAAA,MACE,GAAG;AAAA,MACJ,KAAK;AAAA,MACL,OAAO;AAAA,QACL,GAAI,OAAO,UAAU,WAAW,QAAQ,CAAC;AAAA,QACzC,SAAS,GAAG,WAAW;AAAA,MACzB;AAAA,MAEC;AAAA;AAAA,EACH;AAEJ;","names":["import_hooks"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as _embedpdf_plugin_viewport from '@embedpdf/plugin-viewport';
|
|
2
|
+
import { ViewportPlugin } from '@embedpdf/plugin-viewport';
|
|
3
|
+
import { JSX, ComponentChildren } from 'preact';
|
|
4
|
+
|
|
5
|
+
declare const useViewport: () => {
|
|
6
|
+
plugin: ViewportPlugin | null;
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
ready: Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
declare const useViewportCapability: () => {
|
|
11
|
+
provides: Readonly<_embedpdf_plugin_viewport.ViewportCapability> | null;
|
|
12
|
+
isLoading: boolean;
|
|
13
|
+
ready: Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** @jsxImportSource preact */
|
|
17
|
+
|
|
18
|
+
type ViewportProps = JSX.HTMLAttributes<HTMLDivElement> & {
|
|
19
|
+
children: ComponentChildren;
|
|
20
|
+
};
|
|
21
|
+
declare function Viewport({ children, ...props }: ViewportProps): JSX.Element;
|
|
22
|
+
|
|
23
|
+
export { Viewport, useViewport, useViewportCapability };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as _embedpdf_plugin_viewport from '@embedpdf/plugin-viewport';
|
|
2
|
+
import { ViewportPlugin } from '@embedpdf/plugin-viewport';
|
|
3
|
+
import { JSX, ComponentChildren } from 'preact';
|
|
4
|
+
|
|
5
|
+
declare const useViewport: () => {
|
|
6
|
+
plugin: ViewportPlugin | null;
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
ready: Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
declare const useViewportCapability: () => {
|
|
11
|
+
provides: Readonly<_embedpdf_plugin_viewport.ViewportCapability> | null;
|
|
12
|
+
isLoading: boolean;
|
|
13
|
+
ready: Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** @jsxImportSource preact */
|
|
17
|
+
|
|
18
|
+
type ViewportProps = JSX.HTMLAttributes<HTMLDivElement> & {
|
|
19
|
+
children: ComponentChildren;
|
|
20
|
+
};
|
|
21
|
+
declare function Viewport({ children, ...props }: ViewportProps): JSX.Element;
|
|
22
|
+
|
|
23
|
+
export { Viewport, useViewport, useViewportCapability };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/preact/hooks/use-viewport.ts
|
|
2
|
+
import { useCapability, usePlugin } from "@embedpdf/core/preact";
|
|
3
|
+
import { ViewportPlugin } from "@embedpdf/plugin-viewport";
|
|
4
|
+
var useViewport = () => usePlugin(ViewportPlugin.id);
|
|
5
|
+
var useViewportCapability = () => useCapability(ViewportPlugin.id);
|
|
6
|
+
|
|
7
|
+
// src/preact/components/viewport.tsx
|
|
8
|
+
import { useEffect, useState } from "preact/hooks";
|
|
9
|
+
|
|
10
|
+
// src/preact/hooks/use-viewport-ref.ts
|
|
11
|
+
import { useLayoutEffect, useRef } from "preact/hooks";
|
|
12
|
+
function useViewportRef() {
|
|
13
|
+
const { provides: viewportProvides } = useViewportCapability();
|
|
14
|
+
const containerRef = useRef(null);
|
|
15
|
+
useLayoutEffect(() => {
|
|
16
|
+
if (!viewportProvides) return;
|
|
17
|
+
const container = containerRef.current;
|
|
18
|
+
if (!container) return;
|
|
19
|
+
const provideRect = () => {
|
|
20
|
+
const r = container.getBoundingClientRect();
|
|
21
|
+
return {
|
|
22
|
+
origin: { x: r.left, y: r.top },
|
|
23
|
+
size: { width: r.width, height: r.height }
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
viewportProvides.registerBoundingRectProvider(provideRect);
|
|
27
|
+
const onScroll = () => {
|
|
28
|
+
viewportProvides.setViewportScrollMetrics({
|
|
29
|
+
scrollTop: container.scrollTop,
|
|
30
|
+
scrollLeft: container.scrollLeft
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
container.addEventListener("scroll", onScroll);
|
|
34
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
35
|
+
const rect = container.getBoundingClientRect();
|
|
36
|
+
viewportProvides.setViewportMetrics({
|
|
37
|
+
width: container.offsetWidth,
|
|
38
|
+
height: container.offsetHeight,
|
|
39
|
+
clientWidth: container.clientWidth,
|
|
40
|
+
clientHeight: container.clientHeight,
|
|
41
|
+
scrollTop: container.scrollTop,
|
|
42
|
+
scrollLeft: container.scrollLeft,
|
|
43
|
+
scrollWidth: container.scrollWidth,
|
|
44
|
+
scrollHeight: container.scrollHeight
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
resizeObserver.observe(container);
|
|
48
|
+
const unsubscribeScrollRequest = viewportProvides.onScrollRequest(
|
|
49
|
+
({ x, y, behavior = "auto" }) => {
|
|
50
|
+
requestAnimationFrame(() => {
|
|
51
|
+
container.scrollTo({ left: x, top: y, behavior });
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
return () => {
|
|
56
|
+
viewportProvides.registerBoundingRectProvider(null);
|
|
57
|
+
container.removeEventListener("scroll", onScroll);
|
|
58
|
+
resizeObserver.disconnect();
|
|
59
|
+
unsubscribeScrollRequest();
|
|
60
|
+
};
|
|
61
|
+
}, [viewportProvides]);
|
|
62
|
+
return containerRef;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/preact/components/viewport.tsx
|
|
66
|
+
import { jsx } from "preact/jsx-runtime";
|
|
67
|
+
function Viewport({ children, ...props }) {
|
|
68
|
+
const [viewportGap, setViewportGap] = useState(0);
|
|
69
|
+
const viewportRef = useViewportRef();
|
|
70
|
+
const { provides: viewportProvides } = useViewportCapability();
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
if (viewportProvides) {
|
|
73
|
+
setViewportGap(viewportProvides.getViewportGap());
|
|
74
|
+
}
|
|
75
|
+
}, [viewportProvides]);
|
|
76
|
+
const { style, ...restProps } = props;
|
|
77
|
+
return /* @__PURE__ */ jsx(
|
|
78
|
+
"div",
|
|
79
|
+
{
|
|
80
|
+
...restProps,
|
|
81
|
+
ref: viewportRef,
|
|
82
|
+
style: {
|
|
83
|
+
...typeof style === "object" ? style : {},
|
|
84
|
+
padding: `${viewportGap}px`
|
|
85
|
+
},
|
|
86
|
+
children
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
export {
|
|
91
|
+
Viewport,
|
|
92
|
+
useViewport,
|
|
93
|
+
useViewportCapability
|
|
94
|
+
};
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/preact/hooks/use-viewport.ts","../../src/preact/components/viewport.tsx","../../src/preact/hooks/use-viewport-ref.ts"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/preact';\nimport { ViewportPlugin } from '@embedpdf/plugin-viewport';\n\nexport const useViewport = () => usePlugin<ViewportPlugin>(ViewportPlugin.id);\nexport const useViewportCapability = () => useCapability<ViewportPlugin>(ViewportPlugin.id);\n","/** @jsxImportSource preact */\nimport { ComponentChildren, JSX } from 'preact';\nimport { useEffect, useState } from 'preact/hooks';\n\nimport { useViewportCapability } from '../hooks';\nimport { useViewportRef } from '../hooks/use-viewport-ref';\n\ntype ViewportProps = JSX.HTMLAttributes<HTMLDivElement> & {\n children: ComponentChildren;\n};\n\nexport function Viewport({ children, ...props }: ViewportProps) {\n const [viewportGap, setViewportGap] = useState(0);\n const viewportRef = useViewportRef();\n const { provides: viewportProvides } = useViewportCapability();\n\n useEffect(() => {\n if (viewportProvides) {\n setViewportGap(viewportProvides.getViewportGap());\n }\n }, [viewportProvides]);\n\n const { style, ...restProps } = props;\n return (\n <div\n {...restProps}\n ref={viewportRef}\n style={{\n ...(typeof style === 'object' ? style : {}),\n padding: `${viewportGap}px`,\n }}\n >\n {children}\n </div>\n );\n}\n","import { useLayoutEffect, useRef } from 'preact/hooks';\n\nimport { useViewportCapability } from './use-viewport';\nimport { Rect } from '@embedpdf/models';\n\nexport function useViewportRef() {\n const { provides: viewportProvides } = useViewportCapability();\n const containerRef = useRef<HTMLDivElement>(null);\n\n useLayoutEffect(() => {\n if (!viewportProvides) return;\n\n const container = containerRef.current;\n if (!container) return;\n\n /* ---------- live rect provider --------------------------------- */\n const provideRect = (): Rect => {\n const r = container.getBoundingClientRect();\n return {\n origin: { x: r.left, y: r.top },\n size: { width: r.width, height: r.height },\n };\n };\n viewportProvides.registerBoundingRectProvider(provideRect);\n\n // Example: On scroll, call setMetrics\n const onScroll = () => {\n viewportProvides.setViewportScrollMetrics({\n scrollTop: container.scrollTop,\n scrollLeft: container.scrollLeft,\n });\n };\n container.addEventListener('scroll', onScroll);\n\n // Example: On resize, call setMetrics\n const resizeObserver = new ResizeObserver(() => {\n const rect = container.getBoundingClientRect();\n\n viewportProvides.setViewportMetrics({\n width: container.offsetWidth,\n height: container.offsetHeight,\n clientWidth: container.clientWidth,\n clientHeight: container.clientHeight,\n scrollTop: container.scrollTop,\n scrollLeft: container.scrollLeft,\n scrollWidth: container.scrollWidth,\n scrollHeight: container.scrollHeight,\n });\n });\n resizeObserver.observe(container);\n\n const unsubscribeScrollRequest = viewportProvides.onScrollRequest(\n ({ x, y, behavior = 'auto' }) => {\n requestAnimationFrame(() => {\n container.scrollTo({ left: x, top: y, behavior });\n });\n },\n );\n\n // Cleanup\n return () => {\n viewportProvides.registerBoundingRectProvider(null);\n container.removeEventListener('scroll', onScroll);\n resizeObserver.disconnect();\n unsubscribeScrollRequest();\n };\n }, [viewportProvides]);\n\n // Return the ref so your React code can attach it to a div\n return containerRef;\n}\n"],"mappings":";AAAA,SAAS,eAAe,iBAAiB;AACzC,SAAS,sBAAsB;AAExB,IAAM,cAAc,MAAM,UAA0B,eAAe,EAAE;AACrE,IAAM,wBAAwB,MAAM,cAA8B,eAAe,EAAE;;;ACF1F,SAAS,WAAW,gBAAgB;;;ACFpC,SAAS,iBAAiB,cAAc;AAKjC,SAAS,iBAAiB;AAC/B,QAAM,EAAE,UAAU,iBAAiB,IAAI,sBAAsB;AAC7D,QAAM,eAAe,OAAuB,IAAI;AAEhD,kBAAgB,MAAM;AACpB,QAAI,CAAC,iBAAkB;AAEvB,UAAM,YAAY,aAAa;AAC/B,QAAI,CAAC,UAAW;AAGhB,UAAM,cAAc,MAAY;AAC9B,YAAM,IAAI,UAAU,sBAAsB;AAC1C,aAAO;AAAA,QACL,QAAQ,EAAE,GAAG,EAA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AU,MAAM;AACd,QAAI,kBAAkB;AACpB,qBAAe,iBAAiB,eAAe,CAAC;AAAA,IAClD;AAAA,EACF,GAAG,CAAC,gBAAgB,CAAC;AAErB,QAAM,EAAE,OAAO,GAAG,UAAU,IAAI;AAChC,SACE;AAAA,IAAC;AAAA;AAAA,MACE,GAAG;AAAA,MACJ,KAAK;AAAA,MACL,OAAO;AAAA,QACL,GAAI,OAAO,UAAU,WAAW,QAAQ,CAAC;AAAA,QACzC,SAAS,GAAG,WAAW;AAAA,MACzB;AAAA,MAEC;AAAA;AAAA,EACH;AAEJ;","names":[]}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/react/index.ts
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
Viewport: () => Viewport,
|
|
24
|
+
useViewport: () => useViewport,
|
|
25
|
+
useViewportCapability: () => useViewportCapability,
|
|
26
|
+
useViewportRef: () => useViewportRef
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(react_exports);
|
|
29
|
+
|
|
30
|
+
// src/react/components/viewport.tsx
|
|
31
|
+
var import_react3 = require("react");
|
|
32
|
+
|
|
33
|
+
// src/react/hooks/use-viewport.ts
|
|
34
|
+
var import_react = require("@embedpdf/core/react");
|
|
35
|
+
var import_plugin_viewport = require("@embedpdf/plugin-viewport");
|
|
36
|
+
var useViewport = () => (0, import_react.usePlugin)(import_plugin_viewport.ViewportPlugin.id);
|
|
37
|
+
var useViewportCapability = () => (0, import_react.useCapability)(import_plugin_viewport.ViewportPlugin.id);
|
|
38
|
+
|
|
39
|
+
// src/react/hooks/use-viewport-ref.ts
|
|
40
|
+
var import_react2 = require("react");
|
|
41
|
+
function useViewportRef() {
|
|
42
|
+
const { provides: viewportProvides } = useViewportCapability();
|
|
43
|
+
const containerRef = (0, import_react2.useRef)(null);
|
|
44
|
+
(0, import_react2.useLayoutEffect)(() => {
|
|
45
|
+
if (!viewportProvides) return;
|
|
46
|
+
const container = containerRef.current;
|
|
47
|
+
if (!container) return;
|
|
48
|
+
const provideRect = () => {
|
|
49
|
+
const r = container.getBoundingClientRect();
|
|
50
|
+
return {
|
|
51
|
+
origin: { x: r.left, y: r.top },
|
|
52
|
+
size: { width: r.width, height: r.height }
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
viewportProvides.registerBoundingRectProvider(provideRect);
|
|
56
|
+
const onScroll = () => {
|
|
57
|
+
viewportProvides.setViewportScrollMetrics({
|
|
58
|
+
scrollTop: container.scrollTop,
|
|
59
|
+
scrollLeft: container.scrollLeft
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
container.addEventListener("scroll", onScroll);
|
|
63
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
64
|
+
const rect = container.getBoundingClientRect();
|
|
65
|
+
viewportProvides.setViewportMetrics({
|
|
66
|
+
width: container.offsetWidth,
|
|
67
|
+
height: container.offsetHeight,
|
|
68
|
+
clientWidth: container.clientWidth,
|
|
69
|
+
clientHeight: container.clientHeight,
|
|
70
|
+
scrollTop: container.scrollTop,
|
|
71
|
+
scrollLeft: container.scrollLeft,
|
|
72
|
+
scrollWidth: container.scrollWidth,
|
|
73
|
+
scrollHeight: container.scrollHeight
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
resizeObserver.observe(container);
|
|
77
|
+
const unsubscribeScrollRequest = viewportProvides.onScrollRequest(
|
|
78
|
+
({ x, y, behavior = "auto" }) => {
|
|
79
|
+
requestAnimationFrame(() => {
|
|
80
|
+
container.scrollTo({ left: x, top: y, behavior });
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
return () => {
|
|
85
|
+
viewportProvides.registerBoundingRectProvider(null);
|
|
86
|
+
container.removeEventListener("scroll", onScroll);
|
|
87
|
+
resizeObserver.disconnect();
|
|
88
|
+
unsubscribeScrollRequest();
|
|
89
|
+
};
|
|
90
|
+
}, [viewportProvides]);
|
|
91
|
+
return containerRef;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/react/components/viewport.tsx
|
|
95
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
96
|
+
function Viewport({ children, ...props }) {
|
|
97
|
+
const [viewportGap, setViewportGap] = (0, import_react3.useState)(0);
|
|
98
|
+
const viewportRef = useViewportRef();
|
|
99
|
+
const { provides: viewportProvides } = useViewportCapability();
|
|
100
|
+
(0, import_react3.useEffect)(() => {
|
|
101
|
+
if (viewportProvides) {
|
|
102
|
+
setViewportGap(viewportProvides.getViewportGap());
|
|
103
|
+
}
|
|
104
|
+
}, [viewportProvides]);
|
|
105
|
+
const { style, ...restProps } = props;
|
|
106
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
107
|
+
"div",
|
|
108
|
+
{
|
|
109
|
+
...restProps,
|
|
110
|
+
ref: viewportRef,
|
|
111
|
+
style: {
|
|
112
|
+
...typeof style === "object" ? style : {},
|
|
113
|
+
padding: `${viewportGap}px`
|
|
114
|
+
},
|
|
115
|
+
children
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
120
|
+
0 && (module.exports = {
|
|
121
|
+
Viewport,
|
|
122
|
+
useViewport,
|
|
123
|
+
useViewportCapability,
|
|
124
|
+
useViewportRef
|
|
125
|
+
});
|
|
126
|
+
//# sourceMappingURL=index.cjs.map
|