1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-desktop/gui/VerticalResizer.jsx

101 lines
2.5 KiB
React
Raw Normal View History

const React = require('react');
const bridge = require('@electron/remote').require('./bridge').default;
2019-02-16 03:12:43 +02:00
class VerticalResizer extends React.PureComponent {
constructor() {
super();
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) {
document.addEventListener('dragover', this.document_onDragOver);
event.dataTransfer.dropEffect = 'none';
2019-02-16 03:12:43 +02:00
2019-12-30 16:10:43 +02:00
const cursor = bridge().screen().getCursorScreenPoint();
2019-02-16 03:12:43 +02:00
this.setState({
drag: {
startX: cursor.x,
lastX: cursor.x,
},
2019-02-16 03:12:43 +02:00
});
if (this.props.onDragStart) this.props.onDragStart({});
}
onDrag() {
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.
// Also if clientX, screenX, etc. are 0, it's also the last event and we want to ignore these buggy values.
// 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-12-30 16:10:43 +02:00
const cursor = bridge().screen().getCursorScreenPoint();
const newX = cursor.x;
2019-02-16 03:12:43 +02:00
const delta = newX - this.state.drag.lastX;
if (!delta) return;
this.setState(
{
drag: Object.assign({}, this.state.drag, { lastX: newX }),
},
() => {
this.props.onDrag({ deltaX: delta });
}
);
2019-02-16 03:12:43 +02:00
}
onDragEnd() {
2019-02-16 03:12:43 +02:00
document.removeEventListener('dragover', this.document_onDragOver);
}
componentWillUnmount() {
document.removeEventListener('dragover', this.document_onDragOver);
}
render() {
const debug = false;
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
);
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;