@modusoperandi/licit 0.14.0 → 0.14.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/EditorCommands.js +1 -7
- package/dist/EditorCommands.js.flow +0 -4
- package/dist/EditorNodes.js +0 -3
- package/dist/EditorNodes.js.flow +0 -3
- package/dist/bom.xml +168 -169
- package/dist/buildEditorPlugins.js +1 -2
- package/dist/buildEditorPlugins.js.flow +0 -2
- package/dist/ui/Editor.js +0 -2
- package/dist/ui/Editor.js.flow +1 -3
- package/dist/ui/EditorToolbarConfig.js +0 -6
- package/dist/ui/EditorToolbarConfig.js.flow +0 -8
- package/package.json +3 -3
- package/src/EditorCommands.js +0 -4
- package/src/EditorNodes.js +0 -3
- package/src/buildEditorPlugins.js +0 -2
- package/src/ui/Editor.js +1 -3
- package/src/ui/EditorToolbarConfig.js +0 -8
- package/dist/ImageFromURLCommand.js +0 -19
- package/dist/ImageFromURLCommand.js.flow +0 -14
- package/dist/ImageNodeSpec.js +0 -109
- package/dist/ImageNodeSpec.js.flow +0 -90
- package/dist/ImageSourceCommand.js +0 -117
- package/dist/ImageSourceCommand.js.flow +0 -117
- package/dist/ImageUploadCommand.js +0 -49
- package/dist/ImageUploadCommand.js.flow +0 -36
- package/dist/ui/ImageAlignEditor.js +0 -71
- package/dist/ui/ImageAlignEditor.js.flow +0 -60
- package/dist/ui/ImageNodeView.js +0 -403
- package/dist/ui/ImageNodeView.js.flow +0 -404
- package/dist/ui/ImageResizeBox.js +0 -206
- package/dist/ui/ImageResizeBox.js.flow +0 -219
- package/dist/ui/ImageURLEditor.js +0 -108
- package/dist/ui/ImageURLEditor.js.flow +0 -119
- package/dist/ui/ImageUploadEditor.js +0 -123
- package/dist/ui/ImageUploadEditor.js.flow +0 -117
- package/package-lock.json.old +0 -32889
- package/src/ImageFromURLCommand.js +0 -14
- package/src/ImageNodeSpec.js +0 -90
- package/src/ImageSourceCommand.js +0 -117
- package/src/ImageUploadCommand.js +0 -36
- package/src/ui/ImageAlignEditor.js +0 -60
- package/src/ui/ImageNodeView.js +0 -404
- package/src/ui/ImageResizeBox.js +0 -219
- package/src/ui/ImageURLEditor.js +0 -119
- package/src/ui/ImageUploadEditor.js +0 -117
package/src/ui/ImageNodeView.js
DELETED
|
@@ -1,404 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import cx from 'classnames';
|
|
4
|
-
import { Node } from 'prosemirror-model';
|
|
5
|
-
import { Decoration } from 'prosemirror-view';
|
|
6
|
-
import { NodeSelection } from 'prosemirror-state';
|
|
7
|
-
import * as React from 'react';
|
|
8
|
-
import ReactDOM from 'react-dom';
|
|
9
|
-
|
|
10
|
-
import CustomNodeView from './CustomNodeView';
|
|
11
|
-
import { FRAMESET_BODY_CLASSNAME } from './EditorFrameset';
|
|
12
|
-
import Icon from './Icon';
|
|
13
|
-
import ImageInlineEditor from './ImageInlineEditor';
|
|
14
|
-
import ImageResizeBox from './ImageResizeBox';
|
|
15
|
-
import { MIN_SIZE } from './ImageResizeBox';
|
|
16
|
-
import { atAnchorBottomCenter } from '@modusoperandi/licit-ui-commands';
|
|
17
|
-
import ResizeObserver from './ResizeObserver';
|
|
18
|
-
import { createPopUp } from '@modusoperandi/licit-ui-commands';
|
|
19
|
-
import resolveImage from './resolveImage';
|
|
20
|
-
import uuid from './uuid';
|
|
21
|
-
|
|
22
|
-
import './czi-image-view.css';
|
|
23
|
-
|
|
24
|
-
import type { EditorRuntime } from '../Types';
|
|
25
|
-
import type { NodeViewProps } from './CustomNodeView';
|
|
26
|
-
import type { ResizeObserverEntry } from './ResizeObserver';
|
|
27
|
-
|
|
28
|
-
const EMPTY_SRC =
|
|
29
|
-
'data:image/gif;base64,' +
|
|
30
|
-
'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
|
|
31
|
-
|
|
32
|
-
/* This value must be synced with the margin defined at .czi-image-view */
|
|
33
|
-
const IMAGE_MARGIN = 2;
|
|
34
|
-
|
|
35
|
-
const MAX_SIZE = 100000;
|
|
36
|
-
const IMAGE_PLACEHOLDER_SIZE = 24;
|
|
37
|
-
|
|
38
|
-
const DEFAULT_ORIGINAL_SIZE = {
|
|
39
|
-
src: '',
|
|
40
|
-
complete: false,
|
|
41
|
-
height: 0,
|
|
42
|
-
width: 0,
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// Get the maxWidth that the image could be resized to.
|
|
46
|
-
function getMaxResizeWidth(el: any): number {
|
|
47
|
-
// Ideally, the image should bot be wider then its containing element.
|
|
48
|
-
let node: any = el.parentElement;
|
|
49
|
-
while (node && !node.offsetParent) {
|
|
50
|
-
node = node.parentElement;
|
|
51
|
-
}
|
|
52
|
-
if (
|
|
53
|
-
node &&
|
|
54
|
-
node.offsetParent &&
|
|
55
|
-
node.offsetParent.offsetWidth &&
|
|
56
|
-
node.offsetParent.offsetWidth > 0
|
|
57
|
-
) {
|
|
58
|
-
const { offsetParent } = node;
|
|
59
|
-
const style = el.ownerDocument.defaultView.getComputedStyle(offsetParent);
|
|
60
|
-
let width = offsetParent.clientWidth - IMAGE_MARGIN * 2;
|
|
61
|
-
if (style.boxSizing === 'border-box') {
|
|
62
|
-
const pl = parseInt(style.paddingLeft, 10);
|
|
63
|
-
const pr = parseInt(style.paddingRight, 10);
|
|
64
|
-
width -= pl + pr;
|
|
65
|
-
}
|
|
66
|
-
return Math.max(width, MIN_SIZE);
|
|
67
|
-
}
|
|
68
|
-
// Let the image resize freely.
|
|
69
|
-
return MAX_SIZE;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function resolveURL(runtime: ?EditorRuntime, src: ?string): ?string {
|
|
73
|
-
if (!runtime) {
|
|
74
|
-
return src;
|
|
75
|
-
}
|
|
76
|
-
const { canProxyImageSrc, getProxyImageSrc } = runtime;
|
|
77
|
-
if (src && canProxyImageSrc && getProxyImageSrc && canProxyImageSrc(src)) {
|
|
78
|
-
return await getProxyImageSrc(src);
|
|
79
|
-
}
|
|
80
|
-
return src;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
class ImageViewBody extends React.PureComponent<any, any> {
|
|
84
|
-
props: NodeViewProps;
|
|
85
|
-
|
|
86
|
-
_body = null;
|
|
87
|
-
_id = uuid();
|
|
88
|
-
_inlineEditor = null;
|
|
89
|
-
_mounted = false;
|
|
90
|
-
|
|
91
|
-
state = {
|
|
92
|
-
maxSize: {
|
|
93
|
-
width: MAX_SIZE,
|
|
94
|
-
height: MAX_SIZE,
|
|
95
|
-
complete: false,
|
|
96
|
-
},
|
|
97
|
-
originalSize: DEFAULT_ORIGINAL_SIZE,
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
componentDidMount(): void {
|
|
101
|
-
this._mounted = true;
|
|
102
|
-
this._resolveOriginalSize();
|
|
103
|
-
this._renderInlineEditor();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
componentWillUnmount(): void {
|
|
107
|
-
this._mounted = false;
|
|
108
|
-
this._inlineEditor && this._inlineEditor.close();
|
|
109
|
-
this._inlineEditor = null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
componentDidUpdate(prevProps: NodeViewProps): void {
|
|
113
|
-
const prevSrc = prevProps.node.attrs.src;
|
|
114
|
-
const { node } = this.props;
|
|
115
|
-
const { src } = node.attrs;
|
|
116
|
-
if (prevSrc !== src) {
|
|
117
|
-
// A new image is provided, resolve it.
|
|
118
|
-
this._resolveOriginalSize();
|
|
119
|
-
}
|
|
120
|
-
this._renderInlineEditor();
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
render(): React.Element<any> {
|
|
124
|
-
const { originalSize, maxSize } = this.state;
|
|
125
|
-
const { editorView, node, selected, focused } = this.props;
|
|
126
|
-
const { readOnly } = editorView;
|
|
127
|
-
const { attrs } = node;
|
|
128
|
-
const { align, crop, rotate } = attrs;
|
|
129
|
-
|
|
130
|
-
// It's only active when the image's fully loaded.
|
|
131
|
-
const loading = originalSize === DEFAULT_ORIGINAL_SIZE;
|
|
132
|
-
const active = !loading && focused && !readOnly && originalSize.complete;
|
|
133
|
-
const src = originalSize.complete ? originalSize.src : EMPTY_SRC;
|
|
134
|
-
const aspectRatio = loading ? 1 : originalSize.width / originalSize.height;
|
|
135
|
-
const error = !loading && !originalSize.complete;
|
|
136
|
-
|
|
137
|
-
let { width, height } = attrs;
|
|
138
|
-
|
|
139
|
-
if (loading) {
|
|
140
|
-
width = width || IMAGE_PLACEHOLDER_SIZE;
|
|
141
|
-
height = height || IMAGE_PLACEHOLDER_SIZE;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (width && !height) {
|
|
145
|
-
height = width / aspectRatio;
|
|
146
|
-
} else if (height && !width) {
|
|
147
|
-
width = height * aspectRatio;
|
|
148
|
-
} else if (!width && !height) {
|
|
149
|
-
width = originalSize.width;
|
|
150
|
-
height = originalSize.height;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
let scale = 1;
|
|
154
|
-
if (width > maxSize.width && (!crop || crop.width > maxSize.width)) {
|
|
155
|
-
// Scale image to fit its containing space.
|
|
156
|
-
// If the image is not cropped.
|
|
157
|
-
width = maxSize.width;
|
|
158
|
-
height = width / aspectRatio;
|
|
159
|
-
scale = maxSize.width / width;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const className = cx('czi-image-view-body', {
|
|
163
|
-
active,
|
|
164
|
-
error,
|
|
165
|
-
focused,
|
|
166
|
-
loading,
|
|
167
|
-
selected,
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
const resizeBox =
|
|
171
|
-
active && !crop && !rotate ? (
|
|
172
|
-
<ImageResizeBox
|
|
173
|
-
height={height}
|
|
174
|
-
onResizeEnd={this._onResizeEnd}
|
|
175
|
-
src={src}
|
|
176
|
-
width={width}
|
|
177
|
-
/>
|
|
178
|
-
) : null;
|
|
179
|
-
|
|
180
|
-
const imageStyle: Object = {
|
|
181
|
-
display: 'inline-block',
|
|
182
|
-
height: height + 'px',
|
|
183
|
-
left: '0',
|
|
184
|
-
top: '0',
|
|
185
|
-
width: width + 'px',
|
|
186
|
-
position: 'relative',
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
const clipStyle: Object = {};
|
|
190
|
-
if (crop) {
|
|
191
|
-
const cropped = { ...crop };
|
|
192
|
-
if (scale !== 1) {
|
|
193
|
-
scale = maxSize.width / cropped.width;
|
|
194
|
-
cropped.width *= scale;
|
|
195
|
-
cropped.height *= scale;
|
|
196
|
-
cropped.left *= scale;
|
|
197
|
-
cropped.top *= scale;
|
|
198
|
-
}
|
|
199
|
-
clipStyle.width = cropped.width + 'px';
|
|
200
|
-
clipStyle.height = cropped.height + 'px';
|
|
201
|
-
imageStyle.left = cropped.left + 'px';
|
|
202
|
-
imageStyle.top = cropped.top + 'px';
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (rotate) {
|
|
206
|
-
clipStyle.transform = `rotate(${rotate}rad)`;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const errorView = error ? Icon.get('error') : null;
|
|
210
|
-
const errorTitle = error
|
|
211
|
-
? `Unable to load image from ${attrs.src || ''}`
|
|
212
|
-
: undefined;
|
|
213
|
-
|
|
214
|
-
return (
|
|
215
|
-
<span
|
|
216
|
-
className={className}
|
|
217
|
-
data-active={active ? 'true' : undefined}
|
|
218
|
-
data-original-src={String(attrs.src)}
|
|
219
|
-
id={this._id}
|
|
220
|
-
ref={this._onBodyRef}
|
|
221
|
-
title={errorTitle}
|
|
222
|
-
>
|
|
223
|
-
<span className="czi-image-view-body-img-clip" style={clipStyle}>
|
|
224
|
-
<span style={imageStyle}>
|
|
225
|
-
<img
|
|
226
|
-
alt=""
|
|
227
|
-
className="czi-image-view-body-img"
|
|
228
|
-
data-align={align}
|
|
229
|
-
height={height}
|
|
230
|
-
id={`${this._id}-img`}
|
|
231
|
-
src={src}
|
|
232
|
-
width={width}
|
|
233
|
-
/>
|
|
234
|
-
{errorView}
|
|
235
|
-
</span>
|
|
236
|
-
</span>
|
|
237
|
-
{resizeBox}
|
|
238
|
-
</span>
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
_renderInlineEditor(): void {
|
|
243
|
-
const el = document.getElementById(this._id);
|
|
244
|
-
if (!el || el.getAttribute('data-active') !== 'true') {
|
|
245
|
-
this._inlineEditor && this._inlineEditor.close();
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const { node } = this.props;
|
|
250
|
-
const editorProps = {
|
|
251
|
-
value: node.attrs,
|
|
252
|
-
onSelect: this._onChange,
|
|
253
|
-
};
|
|
254
|
-
if (this._inlineEditor) {
|
|
255
|
-
this._inlineEditor.update(editorProps);
|
|
256
|
-
} else {
|
|
257
|
-
this._inlineEditor = createPopUp(ImageInlineEditor, editorProps, {
|
|
258
|
-
anchor: el,
|
|
259
|
-
autoDismiss: false,
|
|
260
|
-
container: el.closest(`.${FRAMESET_BODY_CLASSNAME}`),
|
|
261
|
-
position: atAnchorBottomCenter,
|
|
262
|
-
onClose: () => {
|
|
263
|
-
this._inlineEditor = null;
|
|
264
|
-
},
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
_resolveOriginalSize = async (): Promise<void> => {
|
|
270
|
-
if (!this._mounted) {
|
|
271
|
-
// unmounted;
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
this.setState({ originalSize: DEFAULT_ORIGINAL_SIZE });
|
|
276
|
-
const src = this.props.node.attrs.src;
|
|
277
|
-
const url = await resolveURL(this.props.editorView.runtime, src);
|
|
278
|
-
const originalSize = await resolveImage(url);
|
|
279
|
-
if (!this._mounted) {
|
|
280
|
-
// unmounted;
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
if (this.props.node.attrs.src !== src) {
|
|
284
|
-
// src had changed.
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
if (!originalSize.complete) {
|
|
288
|
-
originalSize.width = MIN_SIZE;
|
|
289
|
-
originalSize.height = MIN_SIZE;
|
|
290
|
-
}
|
|
291
|
-
this.setState({ originalSize });
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
_onResizeEnd = (width: number, height: number): void => {
|
|
295
|
-
const { getPos, node, editorView } = this.props;
|
|
296
|
-
const pos = getPos();
|
|
297
|
-
const attrs = {
|
|
298
|
-
...node.attrs,
|
|
299
|
-
// TODO: Support UI for cropping later.
|
|
300
|
-
crop: null,
|
|
301
|
-
width,
|
|
302
|
-
height,
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
let tr = editorView.state.tr;
|
|
306
|
-
const { selection } = editorView.state;
|
|
307
|
-
tr = tr.setNodeMarkup(pos, null, attrs);
|
|
308
|
-
// [FS] IRAD-1005 2020-07-09
|
|
309
|
-
// Upgrade outdated packages.
|
|
310
|
-
// reset selection to original using the latest doc.
|
|
311
|
-
const origSelection = NodeSelection.create(tr.doc, selection.from);
|
|
312
|
-
tr = tr.setSelection(origSelection);
|
|
313
|
-
editorView.dispatch(tr);
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
_onChange = (value: ?{ align: ?string }): void => {
|
|
317
|
-
if (!this._mounted) {
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
const align = value ? value.align : null;
|
|
322
|
-
const { getPos, node, editorView } = this.props;
|
|
323
|
-
const pos = getPos();
|
|
324
|
-
const attrs = {
|
|
325
|
-
...node.attrs,
|
|
326
|
-
align,
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
let tr = editorView.state.tr;
|
|
330
|
-
const { selection } = editorView.state;
|
|
331
|
-
tr = tr.setNodeMarkup(pos, null, attrs);
|
|
332
|
-
// [FS] IRAD-1005 2020-07-09
|
|
333
|
-
// Upgrade outdated packages.
|
|
334
|
-
// reset selection to original using the latest doc.
|
|
335
|
-
const origSelection = NodeSelection.create(tr.doc, selection.from);
|
|
336
|
-
tr = tr.setSelection(origSelection);
|
|
337
|
-
editorView.dispatch(tr);
|
|
338
|
-
};
|
|
339
|
-
|
|
340
|
-
_onBodyRef = (ref: any): void => {
|
|
341
|
-
if (ref) {
|
|
342
|
-
this._body = ref;
|
|
343
|
-
// Mounting
|
|
344
|
-
const el = ReactDOM.findDOMNode(ref);
|
|
345
|
-
if (el instanceof HTMLElement) {
|
|
346
|
-
ResizeObserver.observe(el, this._onBodyResize);
|
|
347
|
-
}
|
|
348
|
-
} else {
|
|
349
|
-
// Unmounting.
|
|
350
|
-
const el = this._body && ReactDOM.findDOMNode(this._body);
|
|
351
|
-
if (el instanceof HTMLElement) {
|
|
352
|
-
ResizeObserver.unobserve(el);
|
|
353
|
-
}
|
|
354
|
-
this._body = null;
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
_onBodyResize = (info: ResizeObserverEntry): void => {
|
|
359
|
-
const width = this._body
|
|
360
|
-
? getMaxResizeWidth(ReactDOM.findDOMNode(this._body))
|
|
361
|
-
: MAX_SIZE;
|
|
362
|
-
|
|
363
|
-
this.setState({
|
|
364
|
-
maxSize: {
|
|
365
|
-
width,
|
|
366
|
-
height: MAX_SIZE,
|
|
367
|
-
complete: !!this._body,
|
|
368
|
-
},
|
|
369
|
-
});
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
class ImageNodeView extends CustomNodeView {
|
|
374
|
-
// @override
|
|
375
|
-
createDOMElement(): HTMLElement {
|
|
376
|
-
const el = document.createElement('span');
|
|
377
|
-
el.className = 'czi-image-view';
|
|
378
|
-
this._updateDOM(el);
|
|
379
|
-
return el;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
// @override
|
|
383
|
-
update(node: Node, decorations: Array<Decoration>): boolean {
|
|
384
|
-
super.update(node, decorations);
|
|
385
|
-
this._updateDOM(this.dom);
|
|
386
|
-
return true;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// @override
|
|
390
|
-
renderReactComponent(): React.Element<any> {
|
|
391
|
-
return <ImageViewBody {...this.props} />;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
_updateDOM(el: HTMLElement): void {
|
|
395
|
-
const { align } = this.props.node.attrs;
|
|
396
|
-
let className = 'czi-image-view';
|
|
397
|
-
if (align) {
|
|
398
|
-
className += ' align-' + align;
|
|
399
|
-
}
|
|
400
|
-
el.className = className;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
export default ImageNodeView;
|
package/src/ui/ImageResizeBox.js
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
|
-
import cx from 'classnames';
|
|
4
|
-
import nullthrows from 'nullthrows';
|
|
5
|
-
import * as React from 'react';
|
|
6
|
-
|
|
7
|
-
import { clamp } from '@modusoperandi/licit-ui-commands';
|
|
8
|
-
import uuid from './uuid';
|
|
9
|
-
|
|
10
|
-
import './czi-image-resize-box.css';
|
|
11
|
-
|
|
12
|
-
type Props = {
|
|
13
|
-
height: number,
|
|
14
|
-
onResizeEnd: (w: number, height: number) => void,
|
|
15
|
-
src: string,
|
|
16
|
-
width: number,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export const MIN_SIZE = 20;
|
|
20
|
-
export const MAX_SIZE = 10000;
|
|
21
|
-
|
|
22
|
-
function setWidth(el: HTMLElement, width: number, height: number): void {
|
|
23
|
-
el.style.width = width + 'px';
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function setHeight(el: HTMLElement, width: number, height: number): void {
|
|
27
|
-
el.style.height = height + 'px';
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function setSize(el: HTMLElement, width: number, height: number): void {
|
|
31
|
-
el.style.width = Math.round(width) + 'px';
|
|
32
|
-
el.style.height = Math.round(height) + 'px';
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const ResizeDirection = {
|
|
36
|
-
top: setHeight,
|
|
37
|
-
top_right: setSize,
|
|
38
|
-
right: setWidth,
|
|
39
|
-
bottom_right: setSize,
|
|
40
|
-
bottom: setHeight,
|
|
41
|
-
bottom_left: setSize,
|
|
42
|
-
left: setWidth,
|
|
43
|
-
top_left: setSize,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
class ImageResizeBoxControl extends React.PureComponent<any, any> {
|
|
47
|
-
props: {
|
|
48
|
-
boxID: string,
|
|
49
|
-
direction: string,
|
|
50
|
-
height: number,
|
|
51
|
-
onResizeEnd: (w: number, height: number) => void,
|
|
52
|
-
width: number,
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
_active = false;
|
|
56
|
-
_el = null;
|
|
57
|
-
_h = '';
|
|
58
|
-
_rafID = 0;
|
|
59
|
-
_w = '';
|
|
60
|
-
_x1 = 0;
|
|
61
|
-
_x2 = 0;
|
|
62
|
-
_y1 = 0;
|
|
63
|
-
_y2 = 0;
|
|
64
|
-
_ww = 0;
|
|
65
|
-
_hh = 0;
|
|
66
|
-
|
|
67
|
-
componentWillUnmount(): void {
|
|
68
|
-
this._end();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
render(): React.Element<any> {
|
|
72
|
-
const { direction } = this.props;
|
|
73
|
-
|
|
74
|
-
const className = cx({
|
|
75
|
-
'czi-image-resize-box-control': true,
|
|
76
|
-
[direction]: true,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
return <span className={className} onMouseDown={this._onMouseDown} />;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
_syncSize = (): void => {
|
|
83
|
-
if (!this._active) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const { direction, width, height } = this.props;
|
|
87
|
-
|
|
88
|
-
const dx = (this._x2 - this._x1) * (/left/.test(direction) ? -1 : 1);
|
|
89
|
-
const dy = (this._y2 - this._y1) * (/top/.test(direction) ? -1 : 1);
|
|
90
|
-
|
|
91
|
-
const el = nullthrows(this._el);
|
|
92
|
-
const fn = nullthrows(ResizeDirection[direction]);
|
|
93
|
-
const aspect = width / height;
|
|
94
|
-
let ww = clamp(MIN_SIZE, width + Math.round(dx), MAX_SIZE);
|
|
95
|
-
let hh = clamp(MIN_SIZE, height + Math.round(dy), MAX_SIZE);
|
|
96
|
-
|
|
97
|
-
if (fn === setSize) {
|
|
98
|
-
hh = Math.max(ww / aspect, MIN_SIZE);
|
|
99
|
-
ww = hh * aspect;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
fn(el, Math.round(ww), Math.round(hh));
|
|
103
|
-
this._ww = ww;
|
|
104
|
-
this._hh = hh;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
_start(e: SyntheticMouseEvent<>): void {
|
|
108
|
-
if (this._active) {
|
|
109
|
-
this._end();
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
this._active = true;
|
|
113
|
-
|
|
114
|
-
const { boxID, direction, width, height } = this.props;
|
|
115
|
-
const el = nullthrows(document.getElementById(boxID));
|
|
116
|
-
el.className += ' ' + direction;
|
|
117
|
-
|
|
118
|
-
this._el = el;
|
|
119
|
-
this._x1 = e.clientX;
|
|
120
|
-
this._y1 = e.clientY;
|
|
121
|
-
this._x2 = this._x1;
|
|
122
|
-
this._y2 = this._y1;
|
|
123
|
-
this._w = this._el.style.width;
|
|
124
|
-
this._h = this._el.style.height;
|
|
125
|
-
this._ww = width;
|
|
126
|
-
this._hh = height;
|
|
127
|
-
|
|
128
|
-
document.addEventListener('mousemove', this._onMouseMove, true);
|
|
129
|
-
document.addEventListener('mouseup', this._onMouseUp, true);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
_end(): void {
|
|
133
|
-
if (!this._active) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
this._active = false;
|
|
138
|
-
document.removeEventListener('mousemove', this._onMouseMove, true);
|
|
139
|
-
document.removeEventListener('mouseup', this._onMouseUp, true);
|
|
140
|
-
|
|
141
|
-
const el = nullthrows(this._el);
|
|
142
|
-
el.style.width = this._w;
|
|
143
|
-
el.style.height = this._h;
|
|
144
|
-
el.className = 'czi-image-resize-box';
|
|
145
|
-
this._el = null;
|
|
146
|
-
|
|
147
|
-
this._rafID && cancelAnimationFrame(this._rafID);
|
|
148
|
-
this._rafID = null;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
_onMouseDown = (e: SyntheticMouseEvent<>): void => {
|
|
152
|
-
e.preventDefault();
|
|
153
|
-
e.stopPropagation();
|
|
154
|
-
this._end();
|
|
155
|
-
this._start(e);
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
_onMouseMove = (e: MouseEvent): void => {
|
|
159
|
-
e.preventDefault();
|
|
160
|
-
e.stopPropagation();
|
|
161
|
-
this._x2 = e.clientX;
|
|
162
|
-
this._y2 = e.clientY;
|
|
163
|
-
this._rafID = requestAnimationFrame(this._syncSize);
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
_onMouseUp = (e: MouseEvent): void => {
|
|
167
|
-
e.preventDefault();
|
|
168
|
-
e.stopPropagation();
|
|
169
|
-
this._x2 = e.clientX;
|
|
170
|
-
this._y2 = e.clientY;
|
|
171
|
-
|
|
172
|
-
const { direction } = this.props;
|
|
173
|
-
const el = nullthrows(this._el);
|
|
174
|
-
el.classList.remove(direction);
|
|
175
|
-
|
|
176
|
-
this._end();
|
|
177
|
-
this.props.onResizeEnd(this._ww, this._hh);
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
class ImageResizeBox extends React.PureComponent<any, any> {
|
|
182
|
-
props: Props;
|
|
183
|
-
|
|
184
|
-
_id = uuid();
|
|
185
|
-
|
|
186
|
-
render(): React.Element<any> {
|
|
187
|
-
const { onResizeEnd, width, height, src } = this.props;
|
|
188
|
-
|
|
189
|
-
const style = {
|
|
190
|
-
height: height + 'px',
|
|
191
|
-
width: width + 'px',
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
const boxID = this._id;
|
|
195
|
-
|
|
196
|
-
const controls = Object.keys(ResizeDirection).map((key) => {
|
|
197
|
-
return (
|
|
198
|
-
<ImageResizeBoxControl
|
|
199
|
-
boxID={boxID}
|
|
200
|
-
config={ResizeDirection[key]}
|
|
201
|
-
direction={key}
|
|
202
|
-
height={height}
|
|
203
|
-
key={key}
|
|
204
|
-
onResizeEnd={onResizeEnd}
|
|
205
|
-
width={width}
|
|
206
|
-
/>
|
|
207
|
-
);
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
return (
|
|
211
|
-
<span className="czi-image-resize-box" id={boxID} style={style}>
|
|
212
|
-
{controls}
|
|
213
|
-
<img className="czi-image-resize-box-image" src={src} />
|
|
214
|
-
</span>
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export default ImageResizeBox;
|