-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
workpad.js
158 lines (148 loc) · 5.35 KB
/
workpad.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import { Shortcuts } from 'react-shortcuts';
import Style from 'style-it';
import { WorkpadPage } from '../workpad_page';
import { Fullscreen } from '../fullscreen';
import { isTextInput } from '../../lib/is_text_input';
import { WORKPAD_CANVAS_BUFFER } from '../../../common/lib/constants';
export class Workpad extends React.PureComponent {
static propTypes = {
selectedPageNumber: PropTypes.number.isRequired,
getAnimation: PropTypes.func.isRequired,
onTransitionEnd: PropTypes.func.isRequired,
grid: PropTypes.bool.isRequired,
setGrid: PropTypes.func.isRequired,
pages: PropTypes.array.isRequired,
totalElementCount: PropTypes.number.isRequired,
isFullscreen: PropTypes.bool.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
workpadCss: PropTypes.string.isRequired,
undoHistory: PropTypes.func.isRequired,
redoHistory: PropTypes.func.isRequired,
nextPage: PropTypes.func.isRequired,
previousPage: PropTypes.func.isRequired,
fetchAllRenderables: PropTypes.func.isRequired,
registerLayout: PropTypes.func.isRequired,
unregisterLayout: PropTypes.func.isRequired,
zoomIn: PropTypes.func.isRequired,
zoomOut: PropTypes.func.isRequired,
resetZoom: PropTypes.func.isRequired,
};
_toggleFullscreen = () => {
const { setFullscreen, isFullscreen } = this.props;
setFullscreen(!isFullscreen);
};
// handle keypress events for editor events
_keyMap = {
REFRESH: this.props.fetchAllRenderables,
UNDO: this.props.undoHistory,
REDO: this.props.redoHistory,
GRID: () => this.props.setGrid(!this.props.grid),
ZOOM_IN: this.props.zoomIn,
ZOOM_OUT: this.props.zoomOut,
ZOOM_RESET: this.props.resetZoom,
PREV: this.props.previousPage,
NEXT: this.props.nextPage,
FULLSCREEN: this._toggleFullscreen,
};
_keyHandler = (action, event) => {
if (!isTextInput(event.target) && typeof this._keyMap[action] === 'function') {
event.preventDefault();
this._keyMap[action]();
}
};
render() {
const {
selectedPageNumber,
getAnimation,
onTransitionEnd,
pages,
totalElementCount,
width,
height,
workpadCss,
grid,
isFullscreen,
registerLayout,
unregisterLayout,
zoomScale,
} = this.props;
const bufferStyle = {
height: isFullscreen ? height : (height + 2 * WORKPAD_CANVAS_BUFFER) * zoomScale,
width: isFullscreen ? width : (width + 2 * WORKPAD_CANVAS_BUFFER) * zoomScale,
};
return (
<div className="canvasWorkpad__buffer" style={bufferStyle}>
<div
className="canvasCheckered"
style={{
height,
width,
transformOrigin: '0 0',
transform: isFullscreen ? undefined : `scale3d(${zoomScale}, ${zoomScale}, 1)`, // don't scale in fullscreen mode
}}
>
{!isFullscreen && (
<Shortcuts name="EDITOR" handler={this._keyHandler} targetNodeSelector="body" global />
)}
<Fullscreen>
{({ isFullscreen, windowSize }) => {
const scale = Math.min(windowSize.height / height, windowSize.width / width);
const fsStyle = isFullscreen
? {
transform: `scale3d(${scale}, ${scale}, 1)`,
WebkitTransform: `scale3d(${scale}, ${scale}, 1)`,
msTransform: `scale3d(${scale}, ${scale}, 1)`,
height: windowSize.height < height ? 'auto' : height,
width: windowSize.width < width ? 'auto' : width,
}
: {};
// NOTE: the data-shared-* attributes here are used for reporting
return Style.it(
workpadCss,
<div
className={`canvasWorkpad ${isFullscreen ? 'fullscreen' : ''}`}
style={fsStyle}
data-shared-items-count={totalElementCount}
>
{isFullscreen && (
<Shortcuts
name="PRESENTATION"
handler={this.keyHandler}
targetNodeSelector="body"
global
/>
)}
{pages.map((page, i) => (
<WorkpadPage
key={page.id}
pageId={page.id}
height={height}
width={width}
isSelected={i + 1 === selectedPageNumber}
animation={getAnimation(i + 1)}
onAnimationEnd={onTransitionEnd}
registerLayout={registerLayout}
unregisterLayout={unregisterLayout}
/>
))}
<div
className="canvasGrid"
style={{ height, width, display: grid ? 'block' : 'none' }}
/>
</div>
);
}}
</Fullscreen>
</div>
</div>
);
}
}