Skip to content

Commit

Permalink
feat: add zoomTo prop #62
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Jan 29, 2023
1 parent 44f0e91 commit ac19fee
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 10 deletions.
35 changes: 35 additions & 0 deletions packages/react-guides/src/demo/InfiniteViewer.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
html,
body,
.App,
#root {
padding: 0;
margin: 0;
position: relative;
height: 100%;
}
.guides.horizontal {
position: absolute;
height: 30px;
left: 30px;
width: calc(100% - 30px);
}
.guides.vertical {
position: absolute;
width: 30px;
top: 30px;
height: calc(100% - 30px);
}
.viewer {
position: absolute;
top: 30px;
left: 30px;
width: calc(100% - 30px);
height: calc(100% - 30px);
}

.viewport {
position: relative;
border: 2px solid #ccc;
width: 400px;
height: 600px;
}
65 changes: 65 additions & 0 deletions packages/react-guides/src/demo/InfiniteViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from "react";
import Guides from "../react-guides";
import InfiniteViewer from "react-infinite-viewer";

import "./InfiniteViewer.css";

export default function App() {
const [zoom, setZoom] = React.useState(1);
const unit = Math.round(Math.floor(1 / zoom) * 50) || 50;
const viewerRef = React.useRef<InfiniteViewer>(null);
const horizontalGuidesRef = React.useRef<Guides>(null);
const verticalGuidesRef = React.useRef<Guides>(null);

React.useEffect(() => {
viewerRef.current!.scrollCenter();
}, []);

return (
<div className="App">
<div className="guides horizontal">
<Guides
ref={horizontalGuidesRef}
type="horizontal"
useResizeObserver={true}
displayDragPos={true}
displayGuidePos={true}
zoom={zoom}
unit={unit}
/>
</div>
<div className="guides vertical">
<Guides
ref={verticalGuidesRef}
type="vertical"
useResizeObserver={true}
displayDragPos={true}
displayGuidePos={true}
zoom={zoom}
unit={unit}
/>
</div>
<InfiniteViewer
ref={viewerRef}
className="viewer"
useAutoZoom={true}
useWheelScroll={true}
onScroll={(e) => {
horizontalGuidesRef.current!.scroll(e.scrollLeft);
horizontalGuidesRef.current!.scrollGuides(e.scrollTop);

verticalGuidesRef.current!.scroll(e.scrollTop);
verticalGuidesRef.current!.scrollGuides(e.scrollLeft);
}}
onPinch={(e) => {
const zoom = e.zoom;
horizontalGuidesRef.current!.zoomTo(zoom);
verticalGuidesRef.current!.zoomTo(zoom);
setZoom(e.zoom);
}}
>
<div className="viewport">Viewport</div>
</InfiniteViewer>
</div>
);
}
3 changes: 2 additions & 1 deletion packages/react-guides/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import ReactDOM from "react-dom";
import App from "./demo/App";
// import App from "./demo/App";
import App from "./demo/InfiniteViewer";
import * as serviceWorker from "./demo/serviceWorker";

ReactDOM.render(<App />, document.getElementById("root"));
Expand Down
25 changes: 20 additions & 5 deletions packages/react-guides/src/react-guides/Guides.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
private gesto!: Gesto;
private guideElements: HTMLElement[] = [];
private _isFirstMove = false;
private _zoom = 0;
private _zoom = 1;
private _observer: ResizeObserver | null = null;

constructor(props: GuidesProps) {
Expand Down Expand Up @@ -244,20 +244,34 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
*/
public scrollGuides(pos: number, nextZoom = this._zoom) {
this._zoom = nextZoom;
const translateName = this.getTranslateName();
const guidesElement = this.guidesElement;

this.scrollPos = pos;
guidesElement.style.transform = `${this.getTranslateName()}(${-pos * nextZoom}px)`;
guidesElement.style.transform = `${translateName}(${-pos * nextZoom}px)`;

const guides = this.state.guides;
const guidesOffset = this.props.guidesOffset || 0;
this.guideElements.forEach((el, i) => {
if (!el) {
return;
}
el.style.display = -pos + guides[i] + guidesOffset < 0 ? "none" : "block";
const guidePos = guides[i] + (guidesOffset || 0);

el.style.transform = `${translateName}(${guidePos * nextZoom}px) translateZ(0px)`;
el.style.display = -pos +guidePos < 0 ? "none" : "block";
});
}
/**
* Set to the next zoom.
* @memberof Guides
* @since 0.22.0
* @param nextZoom - next zoom
*/
public zoomTo(nextZoom: number) {
this.scroll(this.getRulerScrollPos(), nextZoom);
this.scrollGuides(this.getGuideScrollPos(), nextZoom);
}
/**
* Get Guides DOM Element
* @memberof Guides
Expand Down Expand Up @@ -292,6 +306,9 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
* @instance
*/
public scroll(pos: number, nextZoom = this._zoom) {
if (this.props.type === "horizontal") {
console.log(pos, nextZoom);
}
this._zoom = nextZoom;
this.ruler.scroll(pos, nextZoom);
}
Expand Down Expand Up @@ -502,9 +519,7 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
target.style.transform = `${this.getTranslateName()}(${nextPos + guidesOffset * zoom}px)`;
}

console.log(nextPos);
return nextPos;

}
private getTranslateName() {
return this.props.type === "horizontal" ? "translateY" : "translateX";
Expand Down
1 change: 1 addition & 0 deletions packages/react-guides/src/react-guides/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const METHODS = [
"forceUpdate",
"getRulerScrollPos",
"getGuideScrollPos",
"zoomTo",
] as const;

export const EVENTS = [
Expand Down
40 changes: 36 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@
dependencies:
prototype-minify "^1.0.0"

"@daybrush/utils@1.6.0", "@daybrush/utils@^0.10.0", "@daybrush/utils@^0.11.0", "@daybrush/utils@^0.5.2", "@daybrush/utils@^0.7.1", "@daybrush/utils@^1.0.0", "@daybrush/utils@^1.1.1", "@daybrush/utils@^1.3.1", "@daybrush/utils@^1.4.0", "@daybrush/utils@^1.6.0", "@daybrush/utils@^1.7.1":
"@daybrush/utils@1.6.0", "@daybrush/utils@^0.10.0", "@daybrush/utils@^0.11.0", "@daybrush/utils@^0.5.2", "@daybrush/utils@^0.7.1", "@daybrush/utils@^1.0.0", "@daybrush/utils@^1.1.1", "@daybrush/utils@^1.3.1", "@daybrush/utils@^1.4.0", "@daybrush/utils@^1.6.0", "@daybrush/utils@^1.7.1", "@daybrush/utils@^1.9.1":
version "1.10.0"
resolved "https://registry.npmjs.org/@daybrush/utils/-/utils-1.10.0.tgz#2a2235269b960b7ffaf05ca2e75379ea1fb58204"
integrity sha512-IDT0DWAGcjxb2+WMr8pHrVTkHVKdilBumX1SubXJUlowN4rMSLphwDxvMNfv+vQOrdCPXSoPVZ3mhfrHsQ9Xow==
Expand Down Expand Up @@ -3028,7 +3028,7 @@
"@daybrush/utils" "1.6.0"
"@scena/event-emitter" "^1.0.2"

"@scena/event-emitter@^1.0.2", "@scena/event-emitter@^1.0.3", "@scena/event-emitter@^1.0.4":
"@scena/event-emitter@^1.0.2", "@scena/event-emitter@^1.0.3", "@scena/event-emitter@^1.0.4", "@scena/event-emitter@^1.0.5":
version "1.0.5"
resolved "https://registry.npmjs.org/@scena/event-emitter/-/event-emitter-1.0.5.tgz#047e3acef93cf238d7ce3a8cc5a12ec6bd9c3bb1"
integrity sha512-AzY4OTb0+7ynefmWFQ6hxDdk0CySAq/D4efljfhtRHCOP7MBF9zUfhKG3TJiroVjASqVgkRJFdenS8ArZo6Olg==
Expand All @@ -3042,6 +3042,19 @@
dependencies:
"@daybrush/utils" "^1.4.0"

"@scena/react-guides@~0.21.0":
version "0.21.0"
resolved "https://registry.npmjs.org/@scena/react-guides/-/react-guides-0.21.0.tgz#a9659ad1ee1af7fced844b0d80ea00ed8a0ee742"
integrity sha512-WZiuNllfmArWIaXKCoz+ZxCi6XjWTZZvWFRST6EPvvFX4op47zteIVqZBSgyWw/DgafE4THPOjrC5l7fNsdrRQ==
dependencies:
"@daybrush/utils" "^1.4.0"
"@scena/dragscroll" "^1.3.0"
"@scena/react-ruler" "^0.14.2"
css-to-mat "^1.0.3"
framework-utils "^1.1.0"
gesto "^1.13.4"
react-css-styled "^1.0.2"

"@scena/react-ruler@^0.14.2", "@scena/react-ruler@~0.14.2":
version "0.14.2"
resolved "https://registry.npmjs.org/@scena/react-ruler/-/react-ruler-0.14.2.tgz#dccfd1cb68c820aef75f839259d63303803aee8f"
Expand Down Expand Up @@ -6762,7 +6775,7 @@ css-selector-tokenizer@^0.7.0:
cssesc "^3.0.0"
fastparse "^1.1.2"

css-styled@^1.0.0, css-styled@~1.0.1:
css-styled@^1.0.0, css-styled@^1.0.1, css-styled@~1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/css-styled/-/css-styled-1.0.1.tgz#803dc2bf615954805a56679cc500065c756ad525"
integrity sha512-psJCbNDPPusDBWH/gszP6BetPh577QaqpvaysTNPitxX0nxdGiTgELiOCus0gZ0yXk3gvjShBrFP07nvn58/TQ==
Expand Down Expand Up @@ -9052,7 +9065,7 @@ gensync@^1.0.0-beta.1, gensync@^1.0.0-beta.2:
resolved "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz"
integrity sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==

gesto@^1.13.4:
gesto@^1.13.0, gesto@^1.13.4:
version "1.13.4"
resolved "https://registry.npmjs.org/gesto/-/gesto-1.13.4.tgz#5f53608dfac589e21acd2b905f51fb259f15a77f"
integrity sha512-TkXGgv8nC/IIjOO2dMrgCNovSnuRfPTJybmtwponzGihCje8QgLEdMf+h/RERu6O+wq8FZTSJ6ihIO9ytt4/Qg==
Expand Down Expand Up @@ -10046,6 +10059,18 @@ infer-owner@^1.0.3, infer-owner@^1.0.4:
resolved "https://registry.npmjs.org/infer-owner/-/infer-owner-1.0.4.tgz"
integrity sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A==

infinite-viewer@~0.20.0:
version "0.20.0"
resolved "https://registry.npmjs.org/infinite-viewer/-/infinite-viewer-0.20.0.tgz#74cf73a4ccbbbdb2b4cc111884c9bfd2d1671861"
integrity sha512-5DGwVkB20rWNQon1CyVikbXeXbZGP8YyzhhWhztfDVLZp2HH1rgTub7ym6U66V3HYiyaniCwLS1g9R6T6h3AdQ==
dependencies:
"@daybrush/utils" "^1.9.1"
"@egjs/agent" "^2.2.1"
"@scena/event-emitter" "^1.0.5"
css-styled "^1.0.1"
framework-utils "^1.1.0"
gesto "^1.13.0"

inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz"
Expand Down Expand Up @@ -15779,6 +15804,13 @@ react-error-overlay@^6.0.7:
resolved "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.11.tgz#92835de5841c5cf08ba00ddd2d677b6d17ff9adb"
integrity sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==

react-infinite-viewer@^0.20.0:
version "0.20.0"
resolved "https://registry.npmjs.org/react-infinite-viewer/-/react-infinite-viewer-0.20.0.tgz#de9486eae8af950cfd7c74561931345c34a4961f"
integrity sha512-Px1m7dLkvg9pxhvq8K0KrrYnex2OYSCEOIpykwd9bSlE5Dh+bgCw2zdHqBU/S8BDk1+z7c/iB2X9+3Y29pPHLA==
dependencies:
infinite-viewer "~0.20.0"

react-is@^16.13.1, react-is@^16.8.4:
version "16.13.1"
resolved "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
Expand Down

0 comments on commit ac19fee

Please sign in to comment.