react-zoom-pan-pinch 3.7.0 → 4.0.1
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.cjs.js +785 -310
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +117 -23
- package/dist/index.esm.js +781 -302
- package/dist/index.esm.js.map +1 -1
- package/package.json +74 -62
- package/src/components/index.ts +5 -0
- package/src/components/keep-scale/keep-scale.tsx +28 -0
- package/src/components/mini-map/mini-map.tsx +292 -0
- package/src/components/mini-map/use-resize.hook.ts +74 -0
- package/src/components/transform-component/transform-component.module.css +43 -0
- package/src/components/transform-component/transform-component.tsx +96 -0
- package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
- package/src/components/virtualize/virtualize.tsx +133 -0
- package/src/components/virtualize/virtualize.utils.ts +104 -0
- package/src/constants/state.constants.ts +99 -0
- package/src/core/animations/animations.constants.ts +78 -0
- package/src/core/animations/animations.utils.ts +115 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +226 -0
- package/src/core/double-click/double-click.logic.ts +127 -0
- package/src/core/handlers/handlers.logic.ts +122 -0
- package/src/core/handlers/handlers.utils.ts +243 -0
- package/src/core/instance.core.ts +704 -0
- package/src/core/pan/panning.logic.ts +122 -0
- package/src/core/pan/panning.utils.ts +216 -0
- package/src/core/pan/velocity.logic.ts +187 -0
- package/src/core/pan/velocity.utils.ts +83 -0
- package/src/core/pinch/pinch.logic.ts +139 -0
- package/src/core/pinch/pinch.utils.ts +95 -0
- package/src/core/wheel/wheel.logic.ts +161 -0
- package/src/core/wheel/wheel.utils.ts +157 -0
- package/src/core/zoom/zoom.logic.ts +71 -0
- package/src/core/zoom/zoom.utils.ts +62 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/use-controls.tsx +9 -0
- package/src/hooks/use-transform-component.tsx +30 -0
- package/src/hooks/use-transform-context.tsx +13 -0
- package/src/hooks/use-transform-effect.tsx +22 -0
- package/src/hooks/use-transform-init.tsx +28 -0
- package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +34 -0
- package/src/models/context.model.ts +218 -0
- package/src/models/helpers.model.ts +15 -0
- package/src/models/index.ts +3 -0
- package/src/react-app-env.d.ts +1 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +21 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +49 -0
- package/src/utils/event.utils.ts +38 -0
- package/src/utils/helpers.utils.ts +22 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/ref.utils.ts +32 -0
- package/src/utils/state.utils.ts +77 -0
- package/src/utils/styles.utils.ts +49 -0
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-zoom-pan-pinch",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
3
|
+
"version": "4.0.1",
|
|
4
|
+
"packageManager": "yarn@4.9.2",
|
|
5
|
+
"description": "Zoom and pan html elements in easy way.",
|
|
5
6
|
"author": "prc5",
|
|
6
7
|
"license": "MIT",
|
|
7
8
|
"main": "dist/index.cjs.js",
|
|
@@ -9,95 +10,105 @@
|
|
|
9
10
|
"types": "dist/index.d.ts",
|
|
10
11
|
"source": "src/index.ts",
|
|
11
12
|
"files": [
|
|
12
|
-
"dist"
|
|
13
|
+
"dist",
|
|
14
|
+
"src",
|
|
15
|
+
"!src/stories"
|
|
13
16
|
],
|
|
14
17
|
"scripts": {
|
|
15
18
|
"build": "rollup -c",
|
|
16
|
-
"build:docs": "build
|
|
17
|
-
"start": "cross-env DISABLE_ESLINT_PLUGIN=true
|
|
19
|
+
"build:docs": "storybook build",
|
|
20
|
+
"start": "cross-env DISABLE_ESLINT_PLUGIN=true yarn storybook dev -p 6006",
|
|
18
21
|
"test": "jest",
|
|
22
|
+
"test:debug": "jest-preview",
|
|
19
23
|
"lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",
|
|
20
24
|
"format": "prettier --write .",
|
|
21
25
|
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
|
|
22
|
-
"prepare": "install-peers && yarn build",
|
|
23
26
|
"predeploy": "yarn run build:docs",
|
|
24
27
|
"deploy": "gh-pages -d storybook-static",
|
|
25
|
-
"release": "yarn semantic-release"
|
|
28
|
+
"release": "yarn semantic-release",
|
|
29
|
+
"storybook": "storybook dev -p 6006",
|
|
30
|
+
"storybook:build": "storybook build"
|
|
26
31
|
},
|
|
27
32
|
"peerDependencies": {
|
|
28
33
|
"react": "*",
|
|
29
34
|
"react-dom": "*"
|
|
30
35
|
},
|
|
31
36
|
"devDependencies": {
|
|
32
|
-
"@babel/core": "^7.
|
|
33
|
-
"@babel/runtime": "^7.
|
|
34
|
-
"@commitlint/cli": "^
|
|
35
|
-
"@commitlint/config-conventional": "^
|
|
36
|
-
"@rollup/plugin-babel": "^
|
|
37
|
-
"@rollup/plugin-typescript": "^
|
|
38
|
-
"@semantic-release/changelog": "^6.0.
|
|
39
|
-
"@semantic-release/commit-analyzer": "^
|
|
37
|
+
"@babel/core": "^7.23.7",
|
|
38
|
+
"@babel/runtime": "^7.23.8",
|
|
39
|
+
"@commitlint/cli": "^18.4.4",
|
|
40
|
+
"@commitlint/config-conventional": "^18.4.4",
|
|
41
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
42
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
43
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
44
|
+
"@semantic-release/commit-analyzer": "^11.1.0",
|
|
40
45
|
"@semantic-release/git": "^10.0.1",
|
|
41
|
-
"@semantic-release/github": "^
|
|
42
|
-
"@semantic-release/npm": "^
|
|
43
|
-
"@semantic-release/release-notes-generator": "^
|
|
44
|
-
"@storybook/addon-a11y": "
|
|
45
|
-
"@storybook/addon-actions": "
|
|
46
|
-
"@storybook/addon-docs": "
|
|
47
|
-
"@storybook/addon-essentials": "
|
|
48
|
-
"@storybook/addon-
|
|
49
|
-
"@storybook/addon-
|
|
50
|
-
"@storybook/
|
|
51
|
-
"@storybook/
|
|
52
|
-
"@storybook/
|
|
53
|
-
"@storybook/
|
|
54
|
-
"@
|
|
55
|
-
"@
|
|
46
|
+
"@semantic-release/github": "^9.2.6",
|
|
47
|
+
"@semantic-release/npm": "^11.0.2",
|
|
48
|
+
"@semantic-release/release-notes-generator": "^12.1.0",
|
|
49
|
+
"@storybook/addon-a11y": "7.6.9",
|
|
50
|
+
"@storybook/addon-actions": "7.6.9",
|
|
51
|
+
"@storybook/addon-docs": "7.6.9",
|
|
52
|
+
"@storybook/addon-essentials": "7.6.9",
|
|
53
|
+
"@storybook/addon-interactions": "^7.6.9",
|
|
54
|
+
"@storybook/addon-links": "7.6.9",
|
|
55
|
+
"@storybook/addon-mdx-gfm": "7.6.9",
|
|
56
|
+
"@storybook/addon-storysource": "7.6.9",
|
|
57
|
+
"@storybook/blocks": "^7.6.9",
|
|
58
|
+
"@storybook/cli": "^7.6.9",
|
|
59
|
+
"@storybook/nextjs": "7.6.9",
|
|
60
|
+
"@storybook/react": "7.6.9",
|
|
61
|
+
"@storybook/react-vite": "^7.6.9",
|
|
62
|
+
"@storybook/test": "^7.6.9",
|
|
63
|
+
"@testing-library/dom": "^10.1.0",
|
|
64
|
+
"@testing-library/react": "^16.0.0",
|
|
65
|
+
"@testing-library/user-event": "^14.5.2",
|
|
56
66
|
"@types/jest": "^26.0.22",
|
|
57
67
|
"@types/node": "^15.6.1",
|
|
58
|
-
"@types/react": "^
|
|
59
|
-
"@types/react-dom": "^
|
|
60
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
61
|
-
"@typescript-eslint/parser": "^
|
|
62
|
-
"
|
|
63
|
-
"babel-loader": "^8.2.2",
|
|
64
|
-
"commitlint": "^17.0.2",
|
|
68
|
+
"@types/react": "^18.2.48",
|
|
69
|
+
"@types/react-dom": "^18.2.18",
|
|
70
|
+
"@typescript-eslint/eslint-plugin": "^6.19.0",
|
|
71
|
+
"@typescript-eslint/parser": "^6.19.0",
|
|
72
|
+
"commitlint": "^18.4.4",
|
|
65
73
|
"cross-env": "^7.0.3",
|
|
66
|
-
"eslint": "^8.
|
|
74
|
+
"eslint": "^8.56.0",
|
|
67
75
|
"eslint-config-airbnb": "^19.0.4",
|
|
68
|
-
"eslint-config-airbnb-typescript": "^17.
|
|
69
|
-
"eslint-config-next": "
|
|
70
|
-
"eslint-config-prettier": "^
|
|
76
|
+
"eslint-config-airbnb-typescript": "^17.1.0",
|
|
77
|
+
"eslint-config-next": "14.0.4",
|
|
78
|
+
"eslint-config-prettier": "^9.1.0",
|
|
71
79
|
"eslint-config-react-app": "^7.0.1",
|
|
72
|
-
"eslint-plugin-import": "^2.
|
|
73
|
-
"eslint-plugin-jsx-a11y": "^6.
|
|
74
|
-
"eslint-plugin-
|
|
75
|
-
"eslint-plugin-
|
|
80
|
+
"eslint-plugin-import": "^2.29.1",
|
|
81
|
+
"eslint-plugin-jsx-a11y": "^6.8.0",
|
|
82
|
+
"eslint-plugin-mdx": "^3.1.5",
|
|
83
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
84
|
+
"eslint-plugin-react": "^7.33.2",
|
|
76
85
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
86
|
+
"eslint-plugin-storybook": "^0.6.15",
|
|
77
87
|
"gh-pages": "^3.1.0",
|
|
78
|
-
"
|
|
88
|
+
"jest": "^29.4.1",
|
|
79
89
|
"jest-css-modules": "^2.1.0",
|
|
80
|
-
"jest-
|
|
81
|
-
"
|
|
82
|
-
"
|
|
90
|
+
"jest-environment-jsdom": "^29.4.1",
|
|
91
|
+
"jest-extended": "^4.0.2",
|
|
92
|
+
"jest-preview": "^0.3.1",
|
|
93
|
+
"jest-watch-typeahead": "^2.2.2",
|
|
94
|
+
"prettier": "^3.3.2",
|
|
95
|
+
"prism-react-renderer": "^2.3.1",
|
|
83
96
|
"react": "*",
|
|
84
97
|
"react-dom": "*",
|
|
85
|
-
"
|
|
86
|
-
"resize-observer-polyfill": "^1.5.1",
|
|
87
|
-
"rollup": "^2.46.0",
|
|
98
|
+
"rollup": "^4.9.5",
|
|
88
99
|
"rollup-plugin-delete": "^2.0.0",
|
|
89
|
-
"rollup-plugin-dts": "^
|
|
100
|
+
"rollup-plugin-dts": "^6.1.0",
|
|
90
101
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
91
|
-
"rollup-plugin-postcss": "^4.0.
|
|
102
|
+
"rollup-plugin-postcss": "^4.0.2",
|
|
92
103
|
"semantic-release": "^19.0.5",
|
|
93
|
-
"
|
|
94
|
-
"ts-jest": "^29.
|
|
95
|
-
"ts-loader": "^9.
|
|
96
|
-
"ts-node": "~10.9.
|
|
97
|
-
"tslib": "^2.6.3",
|
|
104
|
+
"storybook": "^7.6.9",
|
|
105
|
+
"ts-jest": "^29.1.1",
|
|
106
|
+
"ts-loader": "^9.5.1",
|
|
107
|
+
"ts-node": "~10.9.2",
|
|
98
108
|
"typescript": "^4.2.3",
|
|
99
|
-
"
|
|
100
|
-
"
|
|
109
|
+
"vite": "^5.0.11",
|
|
110
|
+
"vite-plugin-svgr": "^4.2.0",
|
|
111
|
+
"vite-tsconfig-paths": "^4.3.1"
|
|
101
112
|
},
|
|
102
113
|
"engines": {
|
|
103
114
|
"node": ">=8",
|
|
@@ -115,7 +126,8 @@
|
|
|
115
126
|
"eslintConfig": {
|
|
116
127
|
"extends": [
|
|
117
128
|
"react-app",
|
|
118
|
-
"react-app/jest"
|
|
129
|
+
"react-app/jest",
|
|
130
|
+
"plugin:storybook/recommended"
|
|
119
131
|
]
|
|
120
132
|
},
|
|
121
133
|
"keywords": [
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React, { useContext, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
import { mergeRefs } from "utils/ref.utils";
|
|
4
|
+
import { Context } from "../transform-wrapper/transform-wrapper";
|
|
5
|
+
|
|
6
|
+
export const KeepScale = React.forwardRef<
|
|
7
|
+
HTMLDivElement,
|
|
8
|
+
React.HTMLAttributes<HTMLDivElement>
|
|
9
|
+
>((props, ref) => {
|
|
10
|
+
const localRef = useRef<HTMLDivElement>(null);
|
|
11
|
+
const instance = useContext(Context);
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
return instance.onChange((ctx) => {
|
|
15
|
+
if (localRef.current) {
|
|
16
|
+
const positionX = 0;
|
|
17
|
+
const positionY = 0;
|
|
18
|
+
localRef.current.style.transform = instance.handleTransformStyles(
|
|
19
|
+
positionX,
|
|
20
|
+
positionY,
|
|
21
|
+
1 / ctx.instance.state.scale,
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}, [instance]);
|
|
26
|
+
|
|
27
|
+
return <div {...props} ref={mergeRefs([localRef, ref])} />;
|
|
28
|
+
});
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/* eslint-disable react/require-default-props */
|
|
2
|
+
import React, {
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from "react";
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
useTransformContext,
|
|
12
|
+
useTransformEffect,
|
|
13
|
+
useTransformInit,
|
|
14
|
+
} from "hooks";
|
|
15
|
+
import { useResize } from "./use-resize.hook";
|
|
16
|
+
import { ReactZoomPanPinchRef } from "models";
|
|
17
|
+
import { boundLimiter } from "core/bounds/bounds.utils";
|
|
18
|
+
|
|
19
|
+
export type MiniMapProps = {
|
|
20
|
+
children: React.ReactNode;
|
|
21
|
+
width?: number;
|
|
22
|
+
height?: number;
|
|
23
|
+
borderColor?: string;
|
|
24
|
+
/** Merged onto the viewport indicator (`.rzpp-minimap-preview`). */
|
|
25
|
+
previewStyle?: React.CSSProperties;
|
|
26
|
+
/** Extra class name(s) appended to `.rzpp-minimap-wrapper`. */
|
|
27
|
+
wrapperClassName?: string;
|
|
28
|
+
/** Extra class name(s) appended to `.rzpp-minimap-preview`. */
|
|
29
|
+
previewClassName?: string;
|
|
30
|
+
panning?: boolean;
|
|
31
|
+
} & React.DetailedHTMLProps<
|
|
32
|
+
React.HTMLAttributes<HTMLDivElement>,
|
|
33
|
+
HTMLDivElement
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
const previewStyles = {
|
|
37
|
+
position: "absolute",
|
|
38
|
+
zIndex: 2,
|
|
39
|
+
top: "0px",
|
|
40
|
+
left: "0px",
|
|
41
|
+
boxSizing: "border-box",
|
|
42
|
+
border: "3px solid red",
|
|
43
|
+
transformOrigin: "0% 0%",
|
|
44
|
+
boxShadow: "rgba(0,0,0,0.2) 0 0 0 10000000px",
|
|
45
|
+
pointerEvents: "none",
|
|
46
|
+
} as const;
|
|
47
|
+
|
|
48
|
+
export const MiniMap: React.FC<MiniMapProps> = ({
|
|
49
|
+
width = 200,
|
|
50
|
+
height = 200,
|
|
51
|
+
borderColor = "red",
|
|
52
|
+
previewStyle,
|
|
53
|
+
wrapperClassName,
|
|
54
|
+
previewClassName,
|
|
55
|
+
children,
|
|
56
|
+
panning = true,
|
|
57
|
+
...rest
|
|
58
|
+
}) => {
|
|
59
|
+
const [initialized, setInitialized] = useState(false);
|
|
60
|
+
const instance = useTransformContext();
|
|
61
|
+
const [isDown, setIsDown] = useState(false);
|
|
62
|
+
const miniMapInstance = useRef<ReactZoomPanPinchRef>(null);
|
|
63
|
+
|
|
64
|
+
const mainRef = useRef<HTMLDivElement | null>(null);
|
|
65
|
+
const wrapperRef = useRef<HTMLDivElement | null>(null);
|
|
66
|
+
const previewRef = useRef<HTMLDivElement | null>(null);
|
|
67
|
+
|
|
68
|
+
const getViewportSize = useCallback(() => {
|
|
69
|
+
if (instance.wrapperComponent) {
|
|
70
|
+
const rect = instance.wrapperComponent.getBoundingClientRect();
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
width: rect.width,
|
|
74
|
+
height: rect.height,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
width: 0,
|
|
79
|
+
height: 0,
|
|
80
|
+
};
|
|
81
|
+
}, [instance.wrapperComponent]);
|
|
82
|
+
|
|
83
|
+
const getContentSize = useCallback(() => {
|
|
84
|
+
if (instance.contentComponent) {
|
|
85
|
+
const rect = instance.contentComponent.getBoundingClientRect();
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
width: rect.width / instance.state.scale,
|
|
89
|
+
height: rect.height / instance.state.scale,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
width: 0,
|
|
94
|
+
height: 0,
|
|
95
|
+
};
|
|
96
|
+
}, [instance.contentComponent, instance.state.scale]);
|
|
97
|
+
|
|
98
|
+
const computeMiniMapScale = useCallback(() => {
|
|
99
|
+
const contentSize = getContentSize();
|
|
100
|
+
const scaleX = width / contentSize.width;
|
|
101
|
+
const scaleY = height / contentSize.height;
|
|
102
|
+
const scale = scaleY > scaleX ? scaleX : scaleY;
|
|
103
|
+
|
|
104
|
+
return scale;
|
|
105
|
+
}, [getContentSize, height, width]);
|
|
106
|
+
|
|
107
|
+
const computeMiniMapSize = () => {
|
|
108
|
+
const contentSize = getContentSize();
|
|
109
|
+
const scaleX = width / contentSize.width;
|
|
110
|
+
const scaleY = height / contentSize.height;
|
|
111
|
+
if (scaleY > scaleX) {
|
|
112
|
+
return { width, height: contentSize.height * scaleX };
|
|
113
|
+
}
|
|
114
|
+
return { width: contentSize.width * scaleY, height };
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const computeMiniMapStyle = () => {
|
|
118
|
+
const scale = computeMiniMapScale();
|
|
119
|
+
const style = {
|
|
120
|
+
transform: `scale(${scale || 1})`,
|
|
121
|
+
transformOrigin: "0% 0%",
|
|
122
|
+
position: "absolute",
|
|
123
|
+
boxSizing: "border-box",
|
|
124
|
+
zIndex: 1,
|
|
125
|
+
// overflow: "hidden",
|
|
126
|
+
} as const;
|
|
127
|
+
|
|
128
|
+
if (wrapperRef.current) {
|
|
129
|
+
const el = wrapperRef.current.style;
|
|
130
|
+
el.transform = style.transform;
|
|
131
|
+
el.transformOrigin = style.transformOrigin;
|
|
132
|
+
el.position = style.position;
|
|
133
|
+
el.boxSizing = style.boxSizing;
|
|
134
|
+
el.zIndex = String(style.zIndex);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const transformMiniMap = () => {
|
|
139
|
+
computeMiniMapStyle();
|
|
140
|
+
const miniSize = computeMiniMapSize();
|
|
141
|
+
const wrapSize = getContentSize();
|
|
142
|
+
if (wrapperRef.current) {
|
|
143
|
+
wrapperRef.current.style.width = `${wrapSize.width}px`;
|
|
144
|
+
wrapperRef.current.style.height = `${wrapSize.height}px`;
|
|
145
|
+
}
|
|
146
|
+
if (mainRef.current) {
|
|
147
|
+
mainRef.current.style.width = `${miniSize.width}px`;
|
|
148
|
+
mainRef.current.style.height = `${miniSize.height}px`;
|
|
149
|
+
}
|
|
150
|
+
if (previewRef.current) {
|
|
151
|
+
const size = getViewportSize();
|
|
152
|
+
const scale = computeMiniMapScale();
|
|
153
|
+
const previewScale = scale * (1 / instance.state.scale);
|
|
154
|
+
const transform = instance.handleTransformStyles(
|
|
155
|
+
-instance.state.positionX * previewScale,
|
|
156
|
+
-instance.state.positionY * previewScale,
|
|
157
|
+
1,
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
previewRef.current.style.transform = transform;
|
|
161
|
+
previewRef.current.style.width = `${size.width * previewScale}px`;
|
|
162
|
+
previewRef.current.style.height = `${size.height * previewScale}px`;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const initialize = () => {
|
|
167
|
+
transformMiniMap();
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
useTransformEffect(() => {
|
|
171
|
+
transformMiniMap();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
useTransformInit(() => {
|
|
175
|
+
initialize();
|
|
176
|
+
setInitialized(true);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
useResize(instance.contentComponent, initialize, [initialized]);
|
|
180
|
+
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
return instance.onChange((zpp) => {
|
|
183
|
+
const scale = computeMiniMapScale();
|
|
184
|
+
if (miniMapInstance.current) {
|
|
185
|
+
miniMapInstance.current.instance.state.scale = zpp.instance.state.scale;
|
|
186
|
+
miniMapInstance.current.instance.state.positionX =
|
|
187
|
+
zpp.instance.state.positionX * scale;
|
|
188
|
+
miniMapInstance.current.instance.state.positionY =
|
|
189
|
+
zpp.instance.state.positionY * scale;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}, [computeMiniMapScale, instance, miniMapInstance]);
|
|
193
|
+
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
const move = (e: MouseEvent) => {
|
|
196
|
+
if (
|
|
197
|
+
panning &&
|
|
198
|
+
isDown &&
|
|
199
|
+
instance.contentComponent &&
|
|
200
|
+
previewRef.current &&
|
|
201
|
+
mainRef.current
|
|
202
|
+
) {
|
|
203
|
+
const scale = computeMiniMapScale();
|
|
204
|
+
const previewRect = previewRef.current.getBoundingClientRect();
|
|
205
|
+
const mainRect = mainRef.current.getBoundingClientRect();
|
|
206
|
+
|
|
207
|
+
const relativeX = (e.clientX - mainRect.left) / scale;
|
|
208
|
+
const relativeY = (e.clientY - mainRect.top) / scale;
|
|
209
|
+
|
|
210
|
+
const x = relativeX - previewRect.width / 2;
|
|
211
|
+
const y = relativeY - previewRect.height / 2;
|
|
212
|
+
|
|
213
|
+
const instanceWidth =
|
|
214
|
+
(instance.wrapperComponent?.offsetWidth || 0) * instance.state.scale;
|
|
215
|
+
const instanceHeight =
|
|
216
|
+
(instance.wrapperComponent?.offsetHeight || 0) * instance.state.scale;
|
|
217
|
+
|
|
218
|
+
const limitWidth =
|
|
219
|
+
instanceWidth - previewRect.width * 2 * instance.state.scale;
|
|
220
|
+
const limitHeight =
|
|
221
|
+
instanceHeight - previewRect.height * 2 * instance.state.scale;
|
|
222
|
+
|
|
223
|
+
const boundedX = boundLimiter(
|
|
224
|
+
x * instance.state.scale,
|
|
225
|
+
0,
|
|
226
|
+
limitWidth,
|
|
227
|
+
true,
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
const boundedY = boundLimiter(
|
|
231
|
+
y * instance.state.scale,
|
|
232
|
+
0,
|
|
233
|
+
limitHeight,
|
|
234
|
+
true,
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
instance.setState(instance.state.scale, -boundedX, -boundedY);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const setDown = (e: MouseEvent) => {
|
|
242
|
+
if (
|
|
243
|
+
mainRef.current?.contains(e.target as Node) ||
|
|
244
|
+
e.target === mainRef.current
|
|
245
|
+
) {
|
|
246
|
+
move(e);
|
|
247
|
+
setIsDown(true);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
const setUp = () => {
|
|
251
|
+
setIsDown(false);
|
|
252
|
+
};
|
|
253
|
+
document.addEventListener("mousedown", setDown);
|
|
254
|
+
document.addEventListener("mouseup", setUp);
|
|
255
|
+
document.addEventListener("mousemove", move);
|
|
256
|
+
return () => {
|
|
257
|
+
document.removeEventListener("mousemove", move);
|
|
258
|
+
document.removeEventListener("mouseup", setUp);
|
|
259
|
+
};
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
const wrapperStyle = useMemo(() => {
|
|
263
|
+
return {
|
|
264
|
+
position: "relative",
|
|
265
|
+
zIndex: 2,
|
|
266
|
+
// overflow: "hidden",
|
|
267
|
+
userSelect: "none",
|
|
268
|
+
} as const;
|
|
269
|
+
}, []);
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<div
|
|
273
|
+
{...rest}
|
|
274
|
+
ref={mainRef}
|
|
275
|
+
style={wrapperStyle}
|
|
276
|
+
className={`rzpp-mini-map ${rest.className || ""}`.trim()}
|
|
277
|
+
>
|
|
278
|
+
<div
|
|
279
|
+
style={{ pointerEvents: "none" }}
|
|
280
|
+
ref={wrapperRef}
|
|
281
|
+
className={`rzpp-minimap-wrapper ${wrapperClassName || ""}`.trim()}
|
|
282
|
+
>
|
|
283
|
+
{children}
|
|
284
|
+
</div>
|
|
285
|
+
<div
|
|
286
|
+
className={`rzpp-minimap-preview ${previewClassName || ""}`.trim()}
|
|
287
|
+
ref={previewRef}
|
|
288
|
+
style={{ ...previewStyles, borderColor, ...previewStyle }}
|
|
289
|
+
/>
|
|
290
|
+
</div>
|
|
291
|
+
);
|
|
292
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
|
|
41
|
+
if (!("ResizeObserver" in window)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (ref) {
|
|
46
|
+
resizeObserverRef.current = new ResizeObserver(
|
|
47
|
+
(entries: ResizeObserverEntry[]) => {
|
|
48
|
+
const newSize = ref.getBoundingClientRect();
|
|
49
|
+
if (
|
|
50
|
+
!Array.isArray(entries) ||
|
|
51
|
+
!entries.length ||
|
|
52
|
+
didUnmount.current ||
|
|
53
|
+
(newSize.width === rectRef.current.width &&
|
|
54
|
+
newSize.height === rectRef.current.height)
|
|
55
|
+
)
|
|
56
|
+
return;
|
|
57
|
+
|
|
58
|
+
onResize(newSize, ref);
|
|
59
|
+
rectRef.current = newSize;
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
resizeObserverRef.current?.observe(ref);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return () => {
|
|
67
|
+
didUnmount.current = true;
|
|
68
|
+
if (ref) {
|
|
69
|
+
resizeObserverRef.current?.unobserve(ref);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
73
|
+
}, [onResize, ref, ...dependencies]);
|
|
74
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
.wrapper {
|
|
2
|
+
position: relative;
|
|
3
|
+
width: -moz-fit-content;
|
|
4
|
+
width: fit-content;
|
|
5
|
+
height: -moz-fit-content;
|
|
6
|
+
height: fit-content;
|
|
7
|
+
overflow: hidden;
|
|
8
|
+
-webkit-touch-callout: none; /* iOS Safari */
|
|
9
|
+
-webkit-user-select: none; /* Safari */
|
|
10
|
+
-khtml-user-select: none; /* Konqueror HTML */
|
|
11
|
+
-moz-user-select: none; /* Firefox */
|
|
12
|
+
-ms-user-select: none; /* Internet Explorer/Edge */
|
|
13
|
+
user-select: none;
|
|
14
|
+
margin: 0;
|
|
15
|
+
padding: 0;
|
|
16
|
+
transform: translate3d(0, 0, 0);
|
|
17
|
+
}
|
|
18
|
+
.content {
|
|
19
|
+
display: flex;
|
|
20
|
+
flex-wrap: wrap;
|
|
21
|
+
width: -moz-fit-content;
|
|
22
|
+
width: fit-content;
|
|
23
|
+
height: -moz-fit-content;
|
|
24
|
+
height: fit-content;
|
|
25
|
+
margin: 0;
|
|
26
|
+
padding: 0;
|
|
27
|
+
transform-origin: 0% 0%;
|
|
28
|
+
}
|
|
29
|
+
.content img {
|
|
30
|
+
pointer-events: none;
|
|
31
|
+
}
|
|
32
|
+
.infiniteGrid {
|
|
33
|
+
position: absolute;
|
|
34
|
+
inset: 0;
|
|
35
|
+
pointer-events: none;
|
|
36
|
+
background-image: radial-gradient(
|
|
37
|
+
circle,
|
|
38
|
+
rgba(0, 0, 0, 0.12) 1px,
|
|
39
|
+
transparent 1px
|
|
40
|
+
);
|
|
41
|
+
background-size: 20px 20px;
|
|
42
|
+
background-position: 0 0;
|
|
43
|
+
}
|