-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathSplit.tsx
143 lines (119 loc) · 4.82 KB
/
Split.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import classNames from 'classnames';
import { clamp } from 'lodash-es';
import { throttle } from 'lodash-es';
import React from 'react';
import { EnabledResizeOptions, MosaicDirection } from './types';
import { BoundingBox } from './util/BoundingBox';
const RESIZE_THROTTLE_MS = 1000 / 30; // 30 fps
const TOUCH_EVENT_OPTIONS = {
capture: true,
passive: false,
};
export interface SplitProps extends EnabledResizeOptions {
direction: MosaicDirection;
boundingBox: BoundingBox;
splitPercentage: number;
onChange?: (percentOfParent: number) => void;
onRelease?: (percentOfParent: number) => void;
}
export class Split extends React.PureComponent<SplitProps> {
private rootElement = React.createRef<HTMLDivElement>();
private listenersBound = false;
static defaultProps = {
onChange: () => void 0,
onRelease: () => void 0,
minimumPaneSizePercentage: 20,
};
render() {
const { direction } = this.props;
return (
<div
className={classNames('mosaic-split', {
'-row': direction === 'row',
'-column': direction === 'column',
})}
ref={this.rootElement}
onMouseDown={this.onMouseDown}
style={this.computeStyle()}
>
<div className="mosaic-split-line" />
</div>
);
}
componentDidMount() {
this.rootElement.current!.addEventListener('touchstart', this.onMouseDown, TOUCH_EVENT_OPTIONS);
}
componentWillUnmount() {
this.unbindListeners();
if (this.rootElement.current) {
this.rootElement.current.ownerDocument!.removeEventListener('touchstart', this.onMouseDown, TOUCH_EVENT_OPTIONS);
}
}
private bindListeners() {
if (!this.listenersBound) {
this.rootElement.current!.ownerDocument!.addEventListener('mousemove', this.onMouseMove, true);
this.rootElement.current!.ownerDocument!.addEventListener('touchmove', this.onMouseMove, TOUCH_EVENT_OPTIONS);
this.rootElement.current!.ownerDocument!.addEventListener('mouseup', this.onMouseUp, true);
this.rootElement.current!.ownerDocument!.addEventListener('touchend', this.onMouseUp, true);
this.listenersBound = true;
}
}
private unbindListeners() {
if (this.rootElement.current) {
this.rootElement.current.ownerDocument!.removeEventListener('mousemove', this.onMouseMove, true);
this.rootElement.current.ownerDocument!.removeEventListener('touchmove', this.onMouseMove, TOUCH_EVENT_OPTIONS);
this.rootElement.current.ownerDocument!.removeEventListener('mouseup', this.onMouseUp, true);
this.rootElement.current.ownerDocument!.removeEventListener('touchend', this.onMouseUp, true);
this.listenersBound = false;
}
}
private computeStyle() {
const { boundingBox, direction, splitPercentage } = this.props;
const positionStyle = direction === 'column' ? 'top' : 'left';
const absolutePercentage = BoundingBox.getAbsoluteSplitPercentage(boundingBox, splitPercentage, direction);
return {
...BoundingBox.asStyles(boundingBox),
[positionStyle]: `${absolutePercentage}%`,
};
}
private onMouseDown = (event: React.MouseEvent<HTMLDivElement> | TouchEvent) => {
if (!isTouchEvent(event)) {
if (event.button !== 0) {
return;
}
}
event.preventDefault();
this.bindListeners();
};
private onMouseUp = (event: MouseEvent | TouchEvent) => {
this.unbindListeners();
const percentage = this.calculateRelativePercentage(event);
this.props.onRelease!(percentage);
};
private onMouseMove = (event: MouseEvent | TouchEvent) => {
event.preventDefault();
this.throttledUpdatePercentage(event);
};
private throttledUpdatePercentage = throttle((event: MouseEvent | TouchEvent) => {
const percentage = this.calculateRelativePercentage(event);
if (percentage !== this.props.splitPercentage) {
this.props.onChange!(percentage);
}
}, RESIZE_THROTTLE_MS);
private calculateRelativePercentage(event: MouseEvent | TouchEvent): number {
const { minimumPaneSizePercentage, direction, boundingBox } = this.props;
const parentBBox = this.rootElement.current!.parentElement!.getBoundingClientRect();
const location = isTouchEvent(event) ? event.changedTouches[0] : event;
let absolutePercentage: number;
if (direction === 'column') {
absolutePercentage = ((location.clientY - parentBBox.top) / parentBBox.height) * 100.0;
} else {
absolutePercentage = ((location.clientX - parentBBox.left) / parentBBox.width) * 100.0;
}
const relativePercentage = BoundingBox.getRelativeSplitPercentage(boundingBox, absolutePercentage, direction);
return clamp(relativePercentage, minimumPaneSizePercentage!, 100 - minimumPaneSizePercentage!);
}
}
function isTouchEvent(event: MouseEvent | TouchEvent | React.MouseEvent<any>): event is TouchEvent {
return (event as TouchEvent).changedTouches != null;
}