react-zoom-pan-pinch 3.0.1 → 3.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.
@@ -1,16 +1,24 @@
1
1
  /* eslint-disable react/require-default-props */
2
- import React, { useMemo, useRef, useState } from "react";
2
+ import React, {
3
+ useCallback,
4
+ useEffect,
5
+ useMemo,
6
+ useRef,
7
+ useState,
8
+ } from "react";
3
9
 
4
10
  import {
5
11
  useTransformContext,
6
12
  useTransformEffect,
7
13
  useTransformInit,
8
14
  } from "hooks";
15
+ import { useResize } from "./use-resize.hook";
16
+ import { ReactZoomPanPinchRef } from "models";
9
17
 
10
18
  export type MiniMapProps = {
19
+ children: React.ReactNode;
11
20
  width?: number;
12
21
  height?: number;
13
- children: React.ReactNode;
14
22
  } & React.DetailedHTMLProps<
15
23
  React.HTMLAttributes<HTMLDivElement>,
16
24
  HTMLDivElement
@@ -33,33 +41,37 @@ export const MiniMap: React.FC<MiniMapProps> = ({
33
41
  children,
34
42
  ...rest
35
43
  }) => {
36
- const [size, setSize] = useState({ width: 0, height: 0 });
44
+ const [initialized, setInitialized] = useState(false);
37
45
  const instance = useTransformContext();
46
+ const miniMapInstance = useRef<ReactZoomPanPinchRef>(null);
38
47
 
48
+ const mainRef = useRef<HTMLDivElement | null>(null);
39
49
  const wrapperRef = useRef<HTMLDivElement | null>(null);
40
50
  const previewRef = useRef<HTMLDivElement | null>(null);
41
51
 
42
- const getContentSize = () => {
52
+ const getContentSize = useCallback(() => {
43
53
  if (instance.contentComponent) {
54
+ const rect = instance.contentComponent.getBoundingClientRect();
55
+
44
56
  return {
45
- width: instance.contentComponent.offsetWidth,
46
- height: instance.contentComponent.offsetHeight,
57
+ width: rect.width / instance.transformState.scale,
58
+ height: rect.height / instance.transformState.scale,
47
59
  };
48
60
  }
49
61
  return {
50
62
  width: 0,
51
63
  height: 0,
52
64
  };
53
- };
65
+ }, [instance.contentComponent, instance.transformState.scale]);
54
66
 
55
- const computeMiniMapScale = () => {
67
+ const computeMiniMapScale = useCallback(() => {
56
68
  const contentSize = getContentSize();
57
69
  const scaleX = width / contentSize.width;
58
70
  const scaleY = height / contentSize.height;
59
71
  const scale = scaleY > scaleX ? scaleX : scaleY;
60
72
 
61
73
  return scale;
62
- };
74
+ }, [getContentSize, height, width]);
63
75
 
64
76
  const computeMiniMapSize = () => {
65
77
  const contentSize = getContentSize();
@@ -71,16 +83,9 @@ export const MiniMap: React.FC<MiniMapProps> = ({
71
83
  return { width: contentSize.width * scaleY, height };
72
84
  };
73
85
 
74
- const computeWrapperStyle = () => {
75
- return {
76
- width: instance.contentComponent?.offsetWidth || 0,
77
- height: instance.contentComponent?.offsetHeight || 0,
78
- };
79
- };
80
-
81
86
  const computeMiniMapStyle = () => {
82
87
  const scale = computeMiniMapScale();
83
- return {
88
+ const style = {
84
89
  transform: `scale(${scale || 1})`,
85
90
  transformOrigin: "0% 0%",
86
91
  position: "absolute",
@@ -88,16 +93,28 @@ export const MiniMap: React.FC<MiniMapProps> = ({
88
93
  zIndex: 1,
89
94
  overflow: "hidden",
90
95
  } as const;
96
+
97
+ Object.keys(style).forEach((key) => {
98
+ if (wrapperRef.current) {
99
+ wrapperRef.current.style[key] = style[key];
100
+ }
101
+ });
91
102
  };
92
103
 
93
104
  const transformMiniMap = () => {
94
- const style = computeWrapperStyle();
105
+ computeMiniMapStyle();
106
+ const miniSize = computeMiniMapSize();
107
+ const wrapSize = getContentSize();
95
108
  if (wrapperRef.current) {
96
- wrapperRef.current.style.width = `${style.width}px`;
97
- wrapperRef.current.style.width = `${style.width}px`;
98
- wrapperRef.current.style.height = `${style.height}px`;
109
+ wrapperRef.current.style.width = `${wrapSize.width}px`;
110
+ wrapperRef.current.style.height = `${wrapSize.height}px`;
111
+ }
112
+ if (mainRef.current) {
113
+ mainRef.current.style.width = `${miniSize.width}px`;
114
+ mainRef.current.style.height = `${miniSize.height}px`;
99
115
  }
100
116
  if (previewRef.current) {
117
+ const size = getContentSize();
101
118
  const scale = computeMiniMapScale();
102
119
  const previewScale = scale * (1 / instance.transformState.scale);
103
120
  const transform = instance.handleTransformStyles(
@@ -105,21 +122,14 @@ export const MiniMap: React.FC<MiniMapProps> = ({
105
122
  -instance.transformState.positionY * previewScale,
106
123
  1,
107
124
  );
125
+
108
126
  previewRef.current.style.transform = transform;
109
- previewRef.current.style.width = `${style.width * previewScale}px`;
110
- previewRef.current.style.height = `${style.height * previewScale}px`;
127
+ previewRef.current.style.width = `${size.width * previewScale}px`;
128
+ previewRef.current.style.height = `${size.height * previewScale}px`;
111
129
  }
112
130
  };
113
131
 
114
132
  const initialize = () => {
115
- const style = computeMiniMapStyle();
116
- const initSize = computeMiniMapSize();
117
- setSize(initSize);
118
- Object.keys(style).forEach((key) => {
119
- if (wrapperRef.current) {
120
- wrapperRef.current.style[key] = style[key];
121
- }
122
- });
123
133
  transformMiniMap();
124
134
  };
125
135
 
@@ -129,20 +139,37 @@ export const MiniMap: React.FC<MiniMapProps> = ({
129
139
 
130
140
  useTransformInit(() => {
131
141
  initialize();
142
+ setInitialized(true);
132
143
  });
133
144
 
145
+ useResize(instance.contentComponent, initialize, [initialized]);
146
+
147
+ useEffect(() => {
148
+ return instance.onChange((zpp) => {
149
+ const scale = computeMiniMapScale();
150
+ if (miniMapInstance.current) {
151
+ miniMapInstance.current.instance.transformState.scale =
152
+ zpp.instance.transformState.scale;
153
+ miniMapInstance.current.instance.transformState.positionX =
154
+ zpp.instance.transformState.positionX * scale;
155
+ miniMapInstance.current.instance.transformState.positionY =
156
+ zpp.instance.transformState.positionY * scale;
157
+ }
158
+ });
159
+ }, [computeMiniMapScale, instance, miniMapInstance]);
160
+
134
161
  const wrapperStyle = useMemo(() => {
135
162
  return {
136
- ...size,
137
163
  position: "relative",
138
164
  zIndex: 2,
139
165
  overflow: "hidden",
140
166
  } as const;
141
- }, [size]);
167
+ }, []);
142
168
 
143
169
  return (
144
170
  <div
145
171
  {...rest}
172
+ ref={mainRef}
146
173
  style={wrapperStyle}
147
174
  className={`rzpp-mini-map ${rest.className || ""}`}
148
175
  >
@@ -0,0 +1,69 @@
1
+ import { useLayoutEffect, useRef } from "react";
2
+
3
+ export type NonNullableKeys<T> = {
4
+ [P in keyof T]-?: NonNullable<T[P]>;
5
+ };
6
+
7
+ export type Nullable<T> = T | null;
8
+
9
+ export type ElementRect = Omit<DOMRect, "toJSON">;
10
+
11
+ const initialElementRect: ElementRect = {
12
+ width: 0,
13
+ height: 0,
14
+ y: 0,
15
+ x: 0,
16
+ top: 0,
17
+ bottom: 0,
18
+ left: 0,
19
+ right: 0,
20
+ };
21
+
22
+ type ResizeHandler<T extends HTMLElement> = (
23
+ elementRect: ElementRect,
24
+ element: T,
25
+ ) => void;
26
+
27
+ export const useResize = <T extends HTMLElement>(
28
+ ref: Nullable<T>,
29
+ onResize: ResizeHandler<T>,
30
+ dependencies: any[],
31
+ ) => {
32
+ const resizeObserverRef = useRef<ResizeObserver>();
33
+
34
+ const rectRef = useRef(initialElementRect);
35
+
36
+ const didUnmount = useRef(false);
37
+
38
+ useLayoutEffect(() => {
39
+ didUnmount.current = false;
40
+ if (ref) {
41
+ resizeObserverRef.current = new ResizeObserver(
42
+ (entries: ResizeObserverEntry[]) => {
43
+ const newSize = ref.getBoundingClientRect();
44
+ if (
45
+ !Array.isArray(entries) ||
46
+ !entries.length ||
47
+ didUnmount.current ||
48
+ (newSize.width === rectRef.current.width &&
49
+ newSize.height === rectRef.current.height)
50
+ )
51
+ return;
52
+
53
+ onResize(newSize, ref);
54
+ rectRef.current = newSize;
55
+ },
56
+ );
57
+
58
+ resizeObserverRef.current?.observe(ref);
59
+ }
60
+
61
+ return () => {
62
+ didUnmount.current = true;
63
+ if (ref) {
64
+ resizeObserverRef.current?.unobserve(ref);
65
+ }
66
+ };
67
+ // eslint-disable-next-line react-hooks/exhaustive-deps
68
+ }, [onResize, ref, ...dependencies]);
69
+ };
@@ -13,10 +13,8 @@ export const useTransformInit = (
13
13
  let unmountCallback: void | (() => void);
14
14
  let unmount: void | (() => void);
15
15
  if (libraryContext.contentComponent && libraryContext.wrapperComponent) {
16
- console.warn(333);
17
16
  unmountCallback = callback(getState(libraryContext));
18
17
  } else {
19
- console.warn(444);
20
18
  unmount = libraryContext.onInit((ref) => {
21
19
  unmountCallback = callback(getState(ref.instance));
22
20
  });