2020-05-13 04:16:43 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2021-06-29 15:11:58 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2020-05-13 04:16:43 +03:00
|
|
|
|
|
|
|
interface IProps {
|
2020-06-18 16:32:43 +03:00
|
|
|
className: string;
|
|
|
|
dragFunc: (currentLocation: ILocationState, event: MouseEvent) => ILocationState;
|
|
|
|
onMouseUp: (event: MouseEvent) => void;
|
2020-05-13 04:16:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
2020-06-18 16:32:43 +03:00
|
|
|
onMouseMove: (event: MouseEvent) => void;
|
|
|
|
onMouseUp: (event: MouseEvent) => void;
|
|
|
|
location: ILocationState;
|
2020-05-13 04:16:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ILocationState {
|
2020-06-18 16:32:43 +03:00
|
|
|
currentX: number;
|
|
|
|
currentY: number;
|
2020-05-13 04:16:43 +03:00
|
|
|
}
|
|
|
|
|
2021-03-09 05:59:41 +03:00
|
|
|
@replaceableComponent("views.elements.Draggable")
|
2020-05-13 04:16:43 +03:00
|
|
|
export default class Draggable extends React.Component<IProps, IState> {
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
onMouseMove: this.onMouseMove.bind(this),
|
|
|
|
onMouseUp: this.onMouseUp.bind(this),
|
|
|
|
location: {
|
|
|
|
currentX: 0,
|
|
|
|
currentY: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private onMouseDown = (event: MouseEvent): void => {
|
|
|
|
this.setState({
|
|
|
|
location: {
|
|
|
|
currentX: event.clientX,
|
|
|
|
currentY: event.clientY,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
document.addEventListener("mousemove", this.state.onMouseMove);
|
|
|
|
document.addEventListener("mouseup", this.state.onMouseUp);
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-05-13 04:16:43 +03:00
|
|
|
|
|
|
|
private onMouseUp = (event: MouseEvent): void => {
|
|
|
|
document.removeEventListener("mousemove", this.state.onMouseMove);
|
|
|
|
document.removeEventListener("mouseup", this.state.onMouseUp);
|
|
|
|
this.props.onMouseUp(event);
|
2020-06-18 16:32:43 +03:00
|
|
|
};
|
2020-05-13 04:16:43 +03:00
|
|
|
|
|
|
|
private onMouseMove(event: MouseEvent): void {
|
|
|
|
const newLocation = this.props.dragFunc(this.state.location, event);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
location: newLocation,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-06-18 16:32:43 +03:00
|
|
|
return <div className={this.props.className} onMouseDown={this.onMouseDown.bind(this)} />;
|
2020-05-13 04:16:43 +03:00
|
|
|
}
|
2020-08-29 03:11:08 +03:00
|
|
|
}
|