2019-07-29 14:13:23 +02:00
|
|
|
const React = require('react');
|
2019-02-28 00:44:10 +02:00
|
|
|
const electron = require('electron');
|
2019-02-16 03:12:43 +02:00
|
|
|
|
|
|
|
class VerticalResizer extends React.PureComponent {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
this.state = {
|
2019-02-16 03:12:43 +02:00
|
|
|
parentRight: 0,
|
|
|
|
parentHeight: 0,
|
|
|
|
parentWidth: 0,
|
|
|
|
drag: {
|
|
|
|
startX: 0,
|
|
|
|
lastX: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onDragStart = this.onDragStart.bind(this);
|
|
|
|
this.onDrag = this.onDrag.bind(this);
|
|
|
|
this.onDragEnd = this.onDragEnd.bind(this);
|
|
|
|
this.document_onDragOver = this.document_onDragOver.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
document_onDragOver(event) {
|
2019-02-16 03:15:30 +02:00
|
|
|
// This is just to prevent the cursor from changing to a "+" as it's dragged
|
|
|
|
// over other elements. With this it stays a normal cursor.
|
2019-02-16 03:12:43 +02:00
|
|
|
event.dataTransfer.dropEffect = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
onDragStart(event) {
|
2019-07-29 14:13:23 +02:00
|
|
|
document.addEventListener('dragover', this.document_onDragOver);
|
|
|
|
|
|
|
|
event.dataTransfer.dropEffect = 'none';
|
2019-02-16 03:12:43 +02:00
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
const cursor = electron.screen.getCursorScreenPoint();
|
2019-02-28 00:44:10 +02:00
|
|
|
|
2019-02-16 03:12:43 +02:00
|
|
|
this.setState({
|
|
|
|
drag: {
|
2019-02-28 00:44:10 +02:00
|
|
|
startX: cursor.x,
|
|
|
|
lastX: cursor.x,
|
2019-07-29 14:13:23 +02:00
|
|
|
},
|
2019-02-16 03:12:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this.props.onDragStart) this.props.onDragStart({});
|
|
|
|
}
|
|
|
|
|
|
|
|
onDrag(event) {
|
2019-02-16 03:15:30 +02:00
|
|
|
// If we got a drag event with no buttons pressed, it's the last drag event
|
|
|
|
// that we should ignore, because it's sometimes use to put the dragged element
|
|
|
|
// back to its original position (if there was no valid drop target), which we don't want.
|
2019-02-27 02:13:56 +02:00
|
|
|
// Also if clientX, screenX, etc. are 0, it's also the last event and we want to ignore these buggy values.
|
2019-02-28 00:44:10 +02:00
|
|
|
// const e = event.nativeEvent;
|
|
|
|
// if (!e.buttons || (!e.clientX && !e.clientY && !e.screenX && !e.screenY)) return;
|
2019-02-16 03:12:43 +02:00
|
|
|
|
2019-02-28 00:44:10 +02:00
|
|
|
const cursor = electron.screen.getCursorScreenPoint();
|
|
|
|
const newX = cursor.x;
|
2019-02-16 03:12:43 +02:00
|
|
|
const delta = newX - this.state.drag.lastX;
|
|
|
|
if (!delta) return;
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
drag: Object.assign({}, this.state.drag, { lastX: newX }),
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this.props.onDrag({ deltaX: delta });
|
|
|
|
}
|
|
|
|
);
|
2019-02-16 03:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onDragEnd(event) {
|
|
|
|
document.removeEventListener('dragover', this.document_onDragOver);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('dragover', this.document_onDragOver);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const debug = false;
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
const rootStyle = Object.assign(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
height: '100%',
|
|
|
|
width: 5,
|
|
|
|
borderColor: 'red',
|
|
|
|
borderWidth: debug ? 1 : 0,
|
|
|
|
borderStyle: 'solid',
|
|
|
|
cursor: 'col-resize',
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
opacity: 0,
|
|
|
|
},
|
|
|
|
this.props.style
|
2019-02-16 03:12:43 +02:00
|
|
|
);
|
2019-07-29 14:13:23 +02:00
|
|
|
|
|
|
|
return <div style={rootStyle} draggable={true} onDragStart={this.onDragStart} onDrag={this.onDrag} onDragEnd={this.onDragEnd} />;
|
2019-02-16 03:12:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = VerticalResizer;
|