Skip to content

Commit

Permalink
fix: fix guide zoom pos daybrush#25
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Sep 19, 2020
1 parent 2e04256 commit 95d9757
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/react-guides/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/react-guides/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scena/react-guides",
"version": "0.10.4",
"version": "0.10.5",
"description": "A React Guides component that can draw ruler and manage guidelines.",
"main": "./dist/guides.cjs.js",
"module": "./dist/guides.esm.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-guides/src/demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class App extends Component<{}> {
private scrollX: number = 0;
private scrollY: number = 0;
public render() {
return (<div className="page" style={{ transform: "rotate(20deg)"}}>
return (<div className="page">
<div className="box" onClick={this.restore}></div>
<div className="ruler horizontal" style={{ }}>
<Guides ref={ref(this, "guides1")}
Expand Down
23 changes: 12 additions & 11 deletions packages/react-guides/src/react-guides/Guides.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
displayDragPos,
cspNonce,
} = this.props as Required<GuidesProps>;
const translateName = this.getTranslateName();

return <GuidesElement
ref={ref(this, "manager")}
cspNonce={cspNonce}
Expand All @@ -80,7 +82,9 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
direction={direction}
textFormat={textFormat}
/>
<div className={GUIDES} ref={ref(this, "guidesElement")}>
<div className={GUIDES} ref={ref(this, "guidesElement")} style={{
transform: `${translateName}(${-this.scrollPos * zoom}px)`,
}}>
{displayDragPos && <div className={DISPLAY_DRAG} ref={ref(this, "displayElement")} />}
<div className={ADDER} ref={ref(this, "adderElement")} />
{this.renderGuides()}
Expand All @@ -89,7 +93,7 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
}
public renderGuides() {
const { type, zoom, showGuides } = this.props as Required<GuidesProps>;
const translateName = type === "horizontal" ? "translateY" : "translateX";
const translateName = this.getTranslateName();
const guides = this.state.guides;

this.guideElements = [];
Expand All @@ -110,8 +114,7 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
public componentDidMount() {
this.gesto = new Gesto(this.manager.getElement(), {
container: document.body,
},
).on("dragStart", e => {
}).on("dragStart", e => {
const inputEvent = e.inputEvent;
const target = inputEvent.target;
const datas = e.datas;
Expand All @@ -126,7 +129,7 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
]);
offsetPos[0] -= guidesElement.offsetLeft;
offsetPos[1] -= guidesElement.offsetTop;
offsetPos[isHorizontal ? 1 : 0] += this.scrollPos;
offsetPos[isHorizontal ? 1 : 0] += this.scrollPos * this.props.zoom!;

datas.offsetPos = offsetPos;
datas.matrix = matrix;
Expand All @@ -150,9 +153,7 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
public componentDidUpdate(prevProps: any) {
if (prevProps.defaultGuides !== this.props.defaultGuides) {
// to dynamically update guides from code rather than dragging guidelines
this.setState({ guides: this.props.defaultGuides || [] }, () => {
this.renderGuides();
});
this.setState({ guides: this.props.defaultGuides || [] });
}
}
/**
Expand Down Expand Up @@ -284,7 +285,7 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
} else {
const index = datas.target.getAttribute("data-index");

if (isDouble || pos < this.scrollPos) {
if (isDouble || guidePos < this.scrollPos) {
guides.splice(index, 1);
} else if (guides.indexOf(guidePos) > -1) {
return;
Expand Down Expand Up @@ -327,8 +328,8 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
}
if (displayDragPos) {
const displayPos = type === "horizontal"
? [offsetX, guidePos]
: [guidePos, offsetY];
? [offsetX, nextPos]
: [nextPos, offsetY];
this.displayElement.style.cssText += `display: block;transform: translate(-50%, -50%) translate(${
displayPos.map(v => `${v}px`).join(", ")
})`;
Expand Down

0 comments on commit 95d9757

Please sign in to comment.