@netless/app-slide 0.2.85 → 0.2.87-beta.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/src/index.ts CHANGED
@@ -76,6 +76,7 @@ export interface AppOptions
76
76
  invisibleBehavior?: "frozen" | "pause";
77
77
  /** just readonly, no operate silder */
78
78
  justSildeReadonly?: true;
79
+ enableScale?: boolean;
79
80
  }
80
81
 
81
82
  export interface ILogger {
@@ -95,6 +96,10 @@ export interface AppResult {
95
96
  prevPage: () => boolean;
96
97
  jumpToPage: (page: number) => boolean;
97
98
  setSildeReadonly: (bol: boolean) => void;
99
+ onScaleChanged: (cb: (scale: number) => void) => void;
100
+ scaleView: (to: number) => void;
101
+ getViewScale: () => number | undefined;
102
+ translateView: (offsetX: number, offsetY: number) => void;
98
103
  }
99
104
 
100
105
  const SlideApp: NetlessApp<Attributes, MagixEvents, AppOptions, AppResult> = {
@@ -189,6 +194,7 @@ const SlideApp: NetlessApp<Attributes, MagixEvents, AppOptions, AppResult> = {
189
194
  baseScenePath,
190
195
  appId: context.appId,
191
196
  urlInterrupter: context.getAppOptions()?.urlInterrupter,
197
+ enableScale: context.getAppOptions()?.enableScale ?? false,
192
198
  onPagesReady: ({ length }) => {
193
199
  const index = docsViewer?.viewer.pageIndex || 0;
194
200
  context.dispatchAppEvent("pageStateChange", { index, length });
@@ -243,6 +249,63 @@ const SlideApp: NetlessApp<Attributes, MagixEvents, AppOptions, AppResult> = {
243
249
  docsViewer.mount();
244
250
 
245
251
  return {
252
+ onScaleChanged: (cb: (scale: number) => void) => {
253
+ if (!docsViewer) {
254
+ return;
255
+ }
256
+ docsViewer.resizableContainer.onScaleChanged = cb;
257
+ },
258
+ scaleView: (to: number) => {
259
+ let applyScale = Number(to);
260
+ if (Number.isNaN(applyScale)) {
261
+ applyScale = 1;
262
+ }
263
+ if (applyScale < 1.0) {
264
+ applyScale = 1.0;
265
+ }
266
+ if (applyScale > 4.0) {
267
+ applyScale = 4.0;
268
+ }
269
+
270
+ context.storage.setState({ slideScale: applyScale });
271
+ context.storage.setState({ translateX: 0.5, translateY: 0.5 });
272
+ docsViewer?.resizableContainer.scaleContainer(applyScale);
273
+ },
274
+ getViewScale: () => {
275
+ return docsViewer?.resizableContainer.getScale();
276
+ },
277
+ translateView: (offsetX: number, offsetY: number) => {
278
+ if (docsViewer?.resizableContainer) {
279
+ const container = docsViewer.resizableContainer;
280
+ const parentBounds = context.getBox().$content.getBoundingClientRect();
281
+ const containerBounds = container.container.getBoundingClientRect();
282
+
283
+ // 计算当前的最大可滚动像素范围
284
+ const maxScrollX = containerBounds.width - parentBounds.width;
285
+ const maxScrollY = containerBounds.height - parentBounds.height;
286
+
287
+ if (maxScrollX > 0 || maxScrollY > 0) {
288
+ // 获取当前标准化位置 (0 ~ 1)
289
+ const currentX = container['translateX'] ?? 0.5;
290
+ const currentY = container['translateY'] ?? 0.5;
291
+
292
+ // 将像素偏移转换为标准化偏移
293
+ const normalizedDeltaX = maxScrollX > 0 ? offsetX / maxScrollX : 0;
294
+ const normalizedDeltaY = maxScrollY > 0 ? offsetY / maxScrollY : 0;
295
+
296
+ // 计算新的标准化位置
297
+ const newX = Math.max(0, Math.min(1, currentX + normalizedDeltaX));
298
+ const newY = Math.max(0, Math.min(1, currentY + normalizedDeltaY));
299
+
300
+ // 存储到 context.storage 实现跨客户端同步
301
+ context.storage.setState({ translateX: newX, translateY: newY });
302
+ container.handleNormalizeTranslate(newX, newY, {
303
+ triggerScrollBar: true,
304
+ triggerSync: false,
305
+ });
306
+ }
307
+ }
308
+ },
246
309
  setSildeReadonly: (bol: boolean) => {
247
310
  docsViewer?.setJustSildeReadonly(bol);
248
311
  },
package/src/typings.ts CHANGED
@@ -12,6 +12,9 @@ export interface Attributes {
12
12
  resourceList: string[];
13
13
  previewList: string[];
14
14
  customLinks: CustomLink[];
15
+ slideScale: number;
16
+ translateX: number;
17
+ translateY: number;
15
18
  }
16
19
 
17
20
  export type MagixPayload = {