react-zoom-pan-pinch 3.0.1 → 3.0.3
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/.storybook/main.js +23 -0
- package/.storybook/preview-head.html +5 -0
- package/.storybook/preview.js +11 -0
- package/.storybook/theme-overrides.css +49 -0
- package/README.md +7 -2
- package/__tests__/features/props.spec.tsx +11 -0
- package/__tests__/features/rendering.spec.tsx +36 -0
- package/__tests__/shared/render.shared.tsx +41 -0
- package/__tests__/utils/controls.utils.tsx +25 -0
- package/__tests__/utils/example.tsx +81 -0
- package/dist/__tests__/features/props.spec.d.ts +1 -0
- package/dist/__tests__/features/rendering.spec.d.ts +1 -0
- package/dist/__tests__/shared/render.shared.d.ts +11 -0
- package/dist/__tests__/utils/controls.utils.d.ts +3 -0
- package/dist/__tests__/utils/example.d.ts +6 -0
- package/dist/index.cjs.js +92 -34
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +93 -35
- package/dist/index.esm.js.map +1 -1
- package/dist/src/components/mini-map/mini-map.d.ts +1 -1
- package/dist/src/components/mini-map/use-resize.hook.d.ts +8 -0
- package/dist/src/stories/docs/handlers.d.ts +12 -0
- package/dist/src/stories/docs/props.d.ts +14 -0
- package/dist/src/stories/examples/content-rerendering/example.d.ts +2 -0
- package/dist/src/stories/examples/image-responsive/example.d.ts +2 -0
- package/dist/src/stories/examples/keep-scale/example.d.ts +2 -0
- package/dist/src/stories/examples/mini-map/example.d.ts +1 -0
- package/dist/src/stories/examples/stress/example.d.ts +2 -0
- package/dist/src/stories/examples/svg-zoom-to-element/example.d.ts +2 -0
- package/dist/src/stories/examples/zoom-to-element/example.d.ts +2 -0
- package/dist/src/stories/types/args.types.d.ts +268 -0
- package/dist/src/stories/utils/args.utils.d.ts +3 -0
- package/dist/src/stories/utils/code.utils.d.ts +6 -0
- package/dist/src/stories/utils/controls.utils.d.ts +3 -0
- package/dist/src/stories/utils/index.d.ts +3 -0
- package/jest.config.ts +45 -0
- package/package.json +1 -1
- package/src/components/mini-map/mini-map.tsx +60 -33
- package/src/components/mini-map/use-resize.hook.ts +69 -0
- package/src/hooks/use-transform-init.tsx +0 -2
- package/src/stories/assets/big-image.jpeg +0 -0
- package/src/stories/assets/map.jpg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/pin.svg +1 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/Introduction.stories.mdx +174 -0
- package/src/stories/docs/handlers.stories.mdx +48 -0
- package/src/stories/docs/handlers.tsx +107 -0
- package/src/stories/docs/props.stories.mdx +92 -0
- package/src/stories/docs/props.tsx +496 -0
- package/src/stories/examples/content/content.stories.mdx +121 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +45 -0
- package/src/stories/examples/content-rerendering/example.tsx +73 -0
- package/src/stories/examples/controls/controls.stories.mdx +71 -0
- package/src/stories/examples/image-big/image-big.stories.mdx +43 -0
- package/src/stories/examples/image-medium/image-medium.stories.mdx +41 -0
- package/src/stories/examples/image-responsive/example.tsx +103 -0
- package/src/stories/examples/image-responsive/image-responsive.stories.mdx +32 -0
- package/src/stories/examples/image-small/image-small.stories.mdx +41 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +46 -0
- package/src/stories/examples/keep-scale/example.tsx +92 -0
- package/src/stories/examples/keep-scale/keep-scale.stories.mdx +45 -0
- package/src/stories/examples/mini-map/example.tsx +54 -0
- package/src/stories/examples/mini-map/mini-map.stories.mdx +33 -0
- package/src/stories/examples/stress/example.tsx +23 -0
- package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
- package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
- package/src/stories/examples/svg-zoom-to-element/example.tsx +57 -0
- package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
- package/src/stories/examples/zoom-to-element/example.tsx +94 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +28 -0
- package/src/stories/hooks/use-controls.stories.mdx +24 -0
- package/src/stories/hooks/use-transform-context.stories.mdx +15 -0
- package/src/stories/hooks/use-transform-effect.stories.mdx +29 -0
- package/src/stories/hooks/use-transform-init.stories.mdx +25 -0
- package/src/stories/types/args.types.ts +201 -0
- package/src/stories/utils/args.utils.ts +34 -0
- package/src/stories/utils/code.utils.tsx +24 -0
- package/src/stories/utils/controls.utils.tsx +43 -0
- package/src/stories/utils/index.ts +3 -0
- package/src/stories/utils/styles.module.css +34 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
export declare const argsTypes: {
|
|
2
|
+
wheel: {
|
|
3
|
+
table: {
|
|
4
|
+
disable: boolean;
|
|
5
|
+
};
|
|
6
|
+
};
|
|
7
|
+
"wheel.step": {
|
|
8
|
+
defaultValue: number;
|
|
9
|
+
control: {
|
|
10
|
+
type: string;
|
|
11
|
+
min: number;
|
|
12
|
+
};
|
|
13
|
+
table: {
|
|
14
|
+
defaultValue: {
|
|
15
|
+
summary: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
"wheel.disabled": {
|
|
20
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
21
|
+
control: {
|
|
22
|
+
type: string;
|
|
23
|
+
};
|
|
24
|
+
table: {
|
|
25
|
+
defaultValue: {
|
|
26
|
+
summary: string;
|
|
27
|
+
};
|
|
28
|
+
type: {
|
|
29
|
+
summary: string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
"wheel.wheelDisabled": {
|
|
34
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
35
|
+
control: {
|
|
36
|
+
type: string;
|
|
37
|
+
};
|
|
38
|
+
table: {
|
|
39
|
+
defaultValue: {
|
|
40
|
+
summary: string;
|
|
41
|
+
};
|
|
42
|
+
type: {
|
|
43
|
+
summary: string;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
"wheel.touchPadDisabled": {
|
|
48
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
49
|
+
control: {
|
|
50
|
+
type: string;
|
|
51
|
+
};
|
|
52
|
+
table: {
|
|
53
|
+
defaultValue: {
|
|
54
|
+
summary: string;
|
|
55
|
+
};
|
|
56
|
+
type: {
|
|
57
|
+
summary: string;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
"wheel.activationKeys": {
|
|
62
|
+
defaultValue: import("../../models").DeepNonNullableArray<string>;
|
|
63
|
+
control: {
|
|
64
|
+
type: string;
|
|
65
|
+
};
|
|
66
|
+
table: {
|
|
67
|
+
defaultValue: {
|
|
68
|
+
summary: string;
|
|
69
|
+
};
|
|
70
|
+
type: {
|
|
71
|
+
summary: string;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
"wheel.excluded": {
|
|
76
|
+
defaultValue: import("../../models").DeepNonNullableArray<string>;
|
|
77
|
+
control: {
|
|
78
|
+
type: string;
|
|
79
|
+
};
|
|
80
|
+
table: {
|
|
81
|
+
defaultValue: {
|
|
82
|
+
summary: string;
|
|
83
|
+
};
|
|
84
|
+
type: {
|
|
85
|
+
summary: string;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
panning: {
|
|
90
|
+
table: {
|
|
91
|
+
disable: boolean;
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
"panning.disabled": {
|
|
95
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
96
|
+
control: {
|
|
97
|
+
type: string;
|
|
98
|
+
};
|
|
99
|
+
table: {
|
|
100
|
+
defaultValue: {
|
|
101
|
+
summary: string;
|
|
102
|
+
};
|
|
103
|
+
type: {
|
|
104
|
+
summary: string;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
"panning.velocityDisabled": {
|
|
109
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
110
|
+
control: {
|
|
111
|
+
type: string;
|
|
112
|
+
};
|
|
113
|
+
table: {
|
|
114
|
+
defaultValue: {
|
|
115
|
+
summary: string;
|
|
116
|
+
};
|
|
117
|
+
type: {
|
|
118
|
+
summary: string;
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
"panning.lockAxisX": {
|
|
123
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
124
|
+
control: {
|
|
125
|
+
type: string;
|
|
126
|
+
};
|
|
127
|
+
table: {
|
|
128
|
+
defaultValue: {
|
|
129
|
+
summary: string;
|
|
130
|
+
};
|
|
131
|
+
type: {
|
|
132
|
+
summary: string;
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
"panning.lockAxisY": {
|
|
137
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
138
|
+
control: {
|
|
139
|
+
type: string;
|
|
140
|
+
};
|
|
141
|
+
table: {
|
|
142
|
+
defaultValue: {
|
|
143
|
+
summary: string;
|
|
144
|
+
};
|
|
145
|
+
type: {
|
|
146
|
+
summary: string;
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
"panning.activationKeys": {
|
|
151
|
+
defaultValue: import("../../models").DeepNonNullableArray<string>;
|
|
152
|
+
control: {
|
|
153
|
+
type: string;
|
|
154
|
+
};
|
|
155
|
+
table: {
|
|
156
|
+
defaultValue: {
|
|
157
|
+
summary: string;
|
|
158
|
+
};
|
|
159
|
+
type: {
|
|
160
|
+
summary: string;
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
"panning.excluded": {
|
|
165
|
+
defaultValue: import("../../models").DeepNonNullableArray<string>;
|
|
166
|
+
control: {
|
|
167
|
+
type: string;
|
|
168
|
+
};
|
|
169
|
+
table: {
|
|
170
|
+
defaultValue: {
|
|
171
|
+
summary: string;
|
|
172
|
+
};
|
|
173
|
+
type: {
|
|
174
|
+
summary: string;
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
pinch: {
|
|
179
|
+
table: {
|
|
180
|
+
disable: boolean;
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
"pinch.step": {
|
|
184
|
+
defaultValue: number;
|
|
185
|
+
control: {
|
|
186
|
+
type: string;
|
|
187
|
+
min: number;
|
|
188
|
+
};
|
|
189
|
+
table: {
|
|
190
|
+
defaultValue: {
|
|
191
|
+
summary: string;
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
"pinch.disabled": {
|
|
196
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
197
|
+
control: {
|
|
198
|
+
type: string;
|
|
199
|
+
};
|
|
200
|
+
table: {
|
|
201
|
+
defaultValue: {
|
|
202
|
+
summary: string;
|
|
203
|
+
};
|
|
204
|
+
type: {
|
|
205
|
+
summary: string;
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
"pinch.excluded": {
|
|
210
|
+
defaultValue: import("../../models").DeepNonNullableArray<string>;
|
|
211
|
+
control: {
|
|
212
|
+
type: string;
|
|
213
|
+
};
|
|
214
|
+
table: {
|
|
215
|
+
defaultValue: {
|
|
216
|
+
summary: string;
|
|
217
|
+
};
|
|
218
|
+
type: {
|
|
219
|
+
summary: string;
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
doubleClick: {
|
|
224
|
+
table: {
|
|
225
|
+
disable: boolean;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
"doubleClick.step": {
|
|
229
|
+
defaultValue: number;
|
|
230
|
+
control: {
|
|
231
|
+
type: string;
|
|
232
|
+
min: number;
|
|
233
|
+
};
|
|
234
|
+
table: {
|
|
235
|
+
defaultValue: {
|
|
236
|
+
summary: string;
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
"doubleClick.disabled": {
|
|
241
|
+
defaultValue: NonNullable<boolean | undefined>;
|
|
242
|
+
control: {
|
|
243
|
+
type: string;
|
|
244
|
+
};
|
|
245
|
+
table: {
|
|
246
|
+
defaultValue: {
|
|
247
|
+
summary: string;
|
|
248
|
+
};
|
|
249
|
+
type: {
|
|
250
|
+
summary: string;
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
"doubleClick.excluded": {
|
|
255
|
+
defaultValue: import("../../models").DeepNonNullableArray<string>;
|
|
256
|
+
control: {
|
|
257
|
+
type: string;
|
|
258
|
+
};
|
|
259
|
+
table: {
|
|
260
|
+
defaultValue: {
|
|
261
|
+
summary: string;
|
|
262
|
+
};
|
|
263
|
+
type: {
|
|
264
|
+
summary: string;
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
};
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Config } from "@jest/types";
|
|
2
|
+
|
|
3
|
+
const config: Config.InitialOptions = {
|
|
4
|
+
cacheDirectory: "../../node_modules/.cache/rhf/jest",
|
|
5
|
+
testEnvironment: "jsdom",
|
|
6
|
+
preset: "ts-jest",
|
|
7
|
+
testRegex: [".spec.ts"],
|
|
8
|
+
roots: ["<rootDir>/node_modules", "<rootDir>/src", "<rootDir>/__tests__"],
|
|
9
|
+
coverageProvider: "v8",
|
|
10
|
+
coverageReporters: [
|
|
11
|
+
["lcov", { projectRoot: "../.." }],
|
|
12
|
+
"clover",
|
|
13
|
+
"json",
|
|
14
|
+
"text",
|
|
15
|
+
],
|
|
16
|
+
collectCoverageFrom: ["<rootDir>/src/**/*.ts", "<rootDir>/src/**/*.tsx"],
|
|
17
|
+
coveragePathIgnorePatterns: [
|
|
18
|
+
".spec",
|
|
19
|
+
"test",
|
|
20
|
+
"tests",
|
|
21
|
+
"types",
|
|
22
|
+
"constants",
|
|
23
|
+
"index.ts",
|
|
24
|
+
".d.ts",
|
|
25
|
+
],
|
|
26
|
+
moduleDirectories: ["node_modules", "src"],
|
|
27
|
+
transform: {
|
|
28
|
+
"^.+\\.tsx?$": [
|
|
29
|
+
"ts-jest",
|
|
30
|
+
{
|
|
31
|
+
tsconfig: "./tsconfig.json",
|
|
32
|
+
isolatedModules: true,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
moduleNameMapper: {
|
|
37
|
+
"\\.(css|less|scss|sss|styl)$": "<rootDir>/node_modules/jest-css-modules",
|
|
38
|
+
},
|
|
39
|
+
watchPlugins: [
|
|
40
|
+
"jest-watch-typeahead/filename",
|
|
41
|
+
"jest-watch-typeahead/testname",
|
|
42
|
+
],
|
|
43
|
+
setupFilesAfterEnv: ["jest-extended/all"],
|
|
44
|
+
};
|
|
45
|
+
export default config;
|
package/package.json
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
/* eslint-disable react/require-default-props */
|
|
2
|
-
import 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 [
|
|
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.
|
|
46
|
-
height: instance.
|
|
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
|
-
|
|
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
|
-
|
|
105
|
+
computeMiniMapStyle();
|
|
106
|
+
const miniSize = computeMiniMapSize();
|
|
107
|
+
const wrapSize = getContentSize();
|
|
95
108
|
if (wrapperRef.current) {
|
|
96
|
-
wrapperRef.current.style.width = `${
|
|
97
|
-
wrapperRef.current.style.
|
|
98
|
-
|
|
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 = `${
|
|
110
|
-
previewRef.current.style.height = `${
|
|
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
|
-
}, [
|
|
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
|
});
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg enable-background="new 0 0 24 24" height="512" viewBox="0 0 24 24" width="512" xmlns="http://www.w3.org/2000/svg"><path d="m12 0c-4.962 0-9 4.066-9 9.065 0 7.103 8.154 14.437 8.501 14.745.143.127.321.19.499.19s.356-.063.499-.189c.347-.309 8.501-7.643 8.501-14.746 0-4.999-4.038-9.065-9-9.065zm0 14c-2.757 0-5-2.243-5-5s2.243-5 5-5 5 2.243 5 5-2.243 5-5 5z"/></svg>
|
|
Binary file
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg version="1.1"
|
|
2
|
+
baseProfile="full"
|
|
3
|
+
width="300" height="200"
|
|
4
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
5
|
+
id="example-svg"
|
|
6
|
+
>
|
|
7
|
+
|
|
8
|
+
<rect width="100%" height="100%" fill="red" />
|
|
9
|
+
|
|
10
|
+
<circle cx="150" cy="100" r="80" fill="green" />
|
|
11
|
+
|
|
12
|
+
<text x="150" y="125" font-size="30" text-anchor="middle" fill="white" id="text-svg">Example</text>
|
|
13
|
+
|
|
14
|
+
</svg>
|