-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateDecorator.js
45 lines (40 loc) · 1.79 KB
/
createDecorator.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
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
const getDisplayName = (WrappedComponent) => {
const component = WrappedComponent.WrappedComponent || WrappedComponent;
return component.displayName || component.name || 'Component';
};
export default ({ store }) => (WrappedComponent) => class BlockDeleteImg extends Component {
static displayName = `BlockDeleteImg(${getDisplayName(WrappedComponent)})`;
static WrappedComponent = WrappedComponent.WrappedComponent || WrappedComponent;
componentDidUpdate = () => {
if (this.props.blockProps.isFocused && this.props.blockProps.isCollapsedSelection) {
// TODO figure out if and how to achieve this without fetching the DOM node
// eslint-disable-next-line react/no-find-dom-node
const blockNode = ReactDOM.findDOMNode(this);
const boundingRectReletiveToParent = {
top: blockNode.offsetTop,
left: blockNode.offsetLeft,
width: blockNode.offsetWidth,
height: blockNode.offsetHeight,
};
store.updateItem('boundingRect', boundingRectReletiveToParent);
store.updateItem('visibleBlock', this.props.block.getKey());
// Only set visibleBlock to null in case it's the current one. This is important
// in case the focus directly switches from one block to the other. Then the
// Alignment tool should not be hidden, but just moved.
} else if (store.getItem('visibleBlock') === this.props.block.getKey()) {
store.updateItem('visibleBlock', null);
}
}
componentWillUnmount() {
store.updateItem('visibleBlock', null);
}
render() {
const {
// using destructuring to make sure unused props are not passed down to the block
...elementProps
} = this.props;
return (<WrappedComponent {...elementProps} />);
}
};