@omer-x/svg-viewport 0.2.0 → 0.3.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.d.ts +4 -13
- package/dist/index.js +12 -3
- package/package.json +3 -2
- package/dist/build/components/SvgViewport.d.ts +0 -21
- package/dist/build/core/matrix.d.ts +0 -4
- package/dist/build/hooks/polyfill-state.d.ts +0 -2
- package/dist/build/types/point.d.ts +0 -4
- package/dist/build/types/viewport.d.ts +0 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
1
|
import React, { Dispatch, SetStateAction, ReactNode } from 'react';
|
|
2
2
|
|
|
3
|
-
type
|
|
4
|
-
x: number,
|
|
5
|
-
y: number,
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
declare function focusTo(matrix: DOMMatrix, { x, y }: Point, width: number, height: number, zoom: number) {
|
|
9
|
-
return matrix.translate(
|
|
10
|
-
((width / 2) / zoom) - x - (matrix.m41 / zoom),
|
|
11
|
-
((height / 2) / zoom) - y - (matrix.m42 / zoom),
|
|
12
|
-
);
|
|
13
|
-
}
|
|
3
|
+
type FocusPoint = "center" | "top-left";
|
|
14
4
|
|
|
15
5
|
type ViewportTransform = {
|
|
16
6
|
zoom: number,
|
|
@@ -28,9 +18,10 @@ type SvgViewportProps = {
|
|
|
28
18
|
setPanning?: Dispatch<SetStateAction<boolean>>;
|
|
29
19
|
transformation?: ViewportTransform | null;
|
|
30
20
|
setTransformation?: Dispatch<SetStateAction<ViewportTransform | null>>;
|
|
21
|
+
initialFocusPoint?: FocusPoint;
|
|
31
22
|
className?: string;
|
|
32
23
|
children: ReactNode;
|
|
33
24
|
};
|
|
34
|
-
declare const SvgViewport: ({ width, height, pannable, zoomable, minZoom, maxZoom, panning, setPanning, transformation, setTransformation, className, children, }: SvgViewportProps) => React.JSX.Element;
|
|
25
|
+
declare const SvgViewport: ({ width, height, pannable, zoomable, minZoom, maxZoom, panning, setPanning, transformation, setTransformation, initialFocusPoint, className, children, }: SvgViewportProps) => React.JSX.Element;
|
|
35
26
|
|
|
36
|
-
export {
|
|
27
|
+
export { SvgViewport as default };
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,15 @@ function adjustWithZoom(matrix, scale, svgElement, eX, eY) {
|
|
|
21
21
|
return matrix.multiply(modifier);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
function getFocusedMatrix(focusPoint, width, height) {
|
|
25
|
+
switch (focusPoint) {
|
|
26
|
+
case "center":
|
|
27
|
+
return focusTo(new DOMMatrix(), { x: 0, y: 0 }, width, height, 1);
|
|
28
|
+
case "top-left":
|
|
29
|
+
return focusTo(new DOMMatrix(), { x: 0, y: 0 }, 0, 0, 1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
24
33
|
function usePolyfillState(state, dispatch) {
|
|
25
34
|
const [polyfill, setPolyfill] = useState(state);
|
|
26
35
|
if (dispatch) {
|
|
@@ -29,7 +38,7 @@ function usePolyfillState(state, dispatch) {
|
|
|
29
38
|
return [polyfill, setPolyfill];
|
|
30
39
|
}
|
|
31
40
|
|
|
32
|
-
const SvgViewport = ({ width, height, pannable = false, zoomable = false, minZoom = 0.5, maxZoom = 2, panning = false, setPanning, transformation = null, setTransformation, className, children, }) => {
|
|
41
|
+
const SvgViewport = ({ width, height, pannable = false, zoomable = false, minZoom = 0.5, maxZoom = 2, panning = false, setPanning, transformation = null, setTransformation, initialFocusPoint = "center", className, children, }) => {
|
|
33
42
|
const pointer = useRef({ x: 0, y: 0 });
|
|
34
43
|
const [grabbing, setGrabbing] = useState(false);
|
|
35
44
|
const [activeTransformation, activeSetTransformation] = usePolyfillState(transformation, setTransformation);
|
|
@@ -42,7 +51,7 @@ const SvgViewport = ({ width, height, pannable = false, zoomable = false, minZoo
|
|
|
42
51
|
return;
|
|
43
52
|
activeSetTransformation({
|
|
44
53
|
zoom: 1,
|
|
45
|
-
matrix:
|
|
54
|
+
matrix: getFocusedMatrix(initialFocusPoint, width, height),
|
|
46
55
|
});
|
|
47
56
|
}, [setTransformation]);
|
|
48
57
|
// panning
|
|
@@ -98,4 +107,4 @@ const SvgViewport = ({ width, height, pannable = false, zoomable = false, minZoo
|
|
|
98
107
|
React.createElement("g", { transform: transform(activeTransformation === null || activeTransformation === void 0 ? void 0 : activeTransformation.matrix) }, activeTransformation && children)));
|
|
99
108
|
};
|
|
100
109
|
|
|
101
|
-
export { SvgViewport as default
|
|
110
|
+
export { SvgViewport as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@omer-x/svg-viewport",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Provides a simple React component for displaying SVG content with zooming and panning capabilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -36,8 +36,9 @@
|
|
|
36
36
|
"main": "./dist/index.js",
|
|
37
37
|
"types": "./dist/index.d.ts",
|
|
38
38
|
"scripts": {
|
|
39
|
-
"check-unused-exports": "ts-unused-exports tsconfig.json --excludePathsFromReport='
|
|
39
|
+
"check-unused-exports": "ts-unused-exports tsconfig.json --excludePathsFromReport='src/index'",
|
|
40
40
|
"prebuild": "npm run check-unused-exports && tsc",
|
|
41
|
+
"postbuild": "rimraf dist/build",
|
|
41
42
|
"build": "rollup --config"
|
|
42
43
|
},
|
|
43
44
|
"dependencies": {
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import React, { type Dispatch, type ReactNode, type SetStateAction } from "react";
|
|
2
|
-
import { focusTo } from "~/core/matrix";
|
|
3
|
-
import type { ViewportTransform } from "~/types/viewport";
|
|
4
|
-
export type { ViewportTransform };
|
|
5
|
-
export { focusTo };
|
|
6
|
-
export type SvgViewportProps = {
|
|
7
|
-
width: number;
|
|
8
|
-
height: number;
|
|
9
|
-
pannable?: boolean;
|
|
10
|
-
zoomable?: boolean;
|
|
11
|
-
minZoom?: number;
|
|
12
|
-
maxZoom?: number;
|
|
13
|
-
panning?: boolean;
|
|
14
|
-
setPanning?: Dispatch<SetStateAction<boolean>>;
|
|
15
|
-
transformation?: ViewportTransform | null;
|
|
16
|
-
setTransformation?: Dispatch<SetStateAction<ViewportTransform | null>>;
|
|
17
|
-
className?: string;
|
|
18
|
-
children: ReactNode;
|
|
19
|
-
};
|
|
20
|
-
declare const SvgViewport: ({ width, height, pannable, zoomable, minZoom, maxZoom, panning, setPanning, transformation, setTransformation, className, children, }: SvgViewportProps) => React.JSX.Element;
|
|
21
|
-
export default SvgViewport;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { Point } from "~/types/point";
|
|
2
|
-
export declare function transform(matrix?: DOMMatrix): string | undefined;
|
|
3
|
-
export declare function focusTo(matrix: DOMMatrix, { x, y }: Point, width: number, height: number, zoom: number): DOMMatrix;
|
|
4
|
-
export declare function adjustWithZoom(matrix: DOMMatrix, scale: number, svgElement: SVGSVGElement, eX: number, eY: number): DOMMatrix;
|