Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InfinitePlane Typescript #71

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component } from 'react';
import { Component, type PropsWithChildren } from 'react';
import { computeBoxProps } from '../common/ui';
import type { BoxProps } from './Box';
import { Button } from './Button';
import { ProgressBar } from './ProgressBar';
import { Stack } from './Stack';
Expand All @@ -9,8 +10,39 @@ const ZOOM_MAX_VAL = 1.5;

const ZOOM_INCREMENT = 0.1;

export class InfinitePlane extends Component {
constructor(props) {
export type InfinitePlaneProps = PropsWithChildren<
{
onZoomChange?: (newZoomValue: number) => void;
onBackgroundMoved?: (newX: number, newY: number) => void;
initialLeft?: number;
initialTop?: number;
backgroundImage?: string;
imageWidth: number;
} & BoxProps
>;

type InfinitePlaneState = {
mouseDown: boolean;

left: number;
top: number;

lastLeft: number;
lastTop: number;

zoom: number;
};

export type MouseEventExtension = {
screenZoomX: number;
screenZoomY: number;
};

export class InfinitePlane extends Component<
InfinitePlaneProps,
InfinitePlaneState
> {
constructor(props: InfinitePlaneProps) {
super(props);

this.state = {
Expand All @@ -24,14 +56,6 @@ export class InfinitePlane extends Component {

zoom: 1,
};

this.handleMouseDown = this.handleMouseDown.bind(this);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleZoomIncrease = this.handleZoomIncrease.bind(this);
this.handleZoomDecrease = this.handleZoomDecrease.bind(this);
this.onMouseUp = this.onMouseUp.bind(this);

this.doOffsetMouse = this.doOffsetMouse.bind(this);
}

componentDidMount() {
Expand All @@ -50,29 +74,30 @@ export class InfinitePlane extends Component {
window.removeEventListener('mouseup', this.doOffsetMouse);
}

doOffsetMouse(event) {
// This is really, REALLY cursed and basically overrides a built-in browser event via propagation rules
doOffsetMouse = (event: MouseEvent & MouseEventExtension) => {
const { zoom } = this.state;
event.screenZoomX = event.screenX * zoom ** -1;
event.screenZoomY = event.screenY * zoom ** -1;
}
};

handleMouseDown(event) {
handleMouseDown = (event: React.MouseEvent<HTMLDivElement>) => {
this.setState((state) => {
return {
mouseDown: true,
lastLeft: event.clientX - state.left,
lastTop: event.clientY - state.top,
};
});
}
};

onMouseUp() {
onMouseUp = () => {
this.setState({
mouseDown: false,
});
}
};

handleZoomIncrease(_event) {
handleZoomIncrease = (_event: any) => {
const { onZoomChange } = this.props;
const { zoom } = this.state;
const newZoomValue = Math.min(zoom + ZOOM_INCREMENT, ZOOM_MAX_VAL);
Expand All @@ -82,9 +107,9 @@ export class InfinitePlane extends Component {
if (onZoomChange) {
onZoomChange(newZoomValue);
}
}
};

handleZoomDecrease(_event) {
handleZoomDecrease = (_event: any) => {
const { onZoomChange } = this.props;
const { zoom } = this.state;
const newZoomValue = Math.max(zoom - ZOOM_INCREMENT, ZOOM_MIN_VAL);
Expand All @@ -95,26 +120,26 @@ export class InfinitePlane extends Component {
if (onZoomChange) {
onZoomChange(newZoomValue);
}
}
};

handleMouseMove(event) {
handleMouseMove = (event: React.MouseEvent<HTMLDivElement>) => {
const { onBackgroundMoved, initialLeft = 0, initialTop = 0 } = this.props;
if (this.state.mouseDown) {
let newX;
let newY;
let newX: number;
let newY: number;
this.setState((state) => {
newX = event.clientX - state.lastLeft;
newY = event.clientY - state.lastTop;
if (onBackgroundMoved) {
onBackgroundMoved(newX + initialLeft, newY + initialTop);
}
return {
left: newX,
top: newY,
};
});
if (onBackgroundMoved) {
onBackgroundMoved(newX + initialLeft, newY + initialTop);
}
}
}
};

render() {
const {
Expand All @@ -132,7 +157,6 @@ export class InfinitePlane extends Component {

return (
<div
ref={this.ref}
{...computeBoxProps({
...rest,
style: {
Expand Down
Loading