-
Notifications
You must be signed in to change notification settings - Fork 82
/
index.jsx
153 lines (133 loc) · 3.79 KB
/
index.jsx
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
import React from "react";
import classnames from "classnames";
import { connect } from "react-redux";
import {
SortableContainer,
SortableElement,
SortableHandle
} from "react-sortable-hoc";
import { removeAt, insert } from "timm";
import EditorIcon from "visual/component/EditorIcon";
import { pageAssembledSelector } from "visual/redux/selectors";
import { removeBlock, reorderBlocks } from "visual/redux/actions";
import { t } from "visual/utils/i18n";
import Config from "visual/global/Config";
import BlockThumbnail from "./BlockThumbnail";
const { isGlobalPopup: IS_GLOBAL_POPUP } = Config.get("wp") || {};
const DragHandle = SortableHandle(({ item }) => (
<BlockThumbnail blockData={item} />
));
const SortableItem = SortableElement(({ item, onRemove }) => {
return (
<div className="brz-ed-sidebar-block-item">
<DragHandle item={item} />
<div className="brz-ed-sidebar-block-remove" onClick={onRemove}>
<EditorIcon icon="nc-circle-remove-2" className="brz-ed-bar-icon" />
</div>
</div>
);
});
const SortableList = SortableContainer(({ isSorting, items, onItemRemove }) => {
const filteredItems = [];
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item.value._blockVisibility === "unlisted") {
continue;
}
filteredItems.push(
<SortableItem
key={item.value._id}
item={item}
index={i}
onRemove={() => onItemRemove(i)}
/>
);
}
const className = classnames("brz-ed-sidebar-sortable", {
"brz-ed-sidebar-sortable--sorting": isSorting
});
return <div className={className}>{filteredItems}</div>;
});
class DrawerComponent extends React.Component {
static getDerivedStateFromProps(props, state) {
// the 'optimistic' flag tells us to keep the state
// to perform an optimistic update until we get new props.
// This is why we discard the flag after accepting the state
if (state.optimistic) {
return {
blocks: state.blocks,
optimistic: false
};
}
if (props.page.data.items !== state.blocks) {
return {
blocks: props.page.data.items || []
};
}
return null;
}
state = {
blocks: [],
isSorting: false
};
handleBeforeSortStart = () => {
this.setState({
isSorting: true
});
};
handleSortEnd = ({ oldIndex, newIndex }) => {
if (oldIndex !== newIndex) {
// make an optimistic update,
// after which update the store fo real
this.setState(
state => {
const { blocks } = state;
const movedBlock = blocks[oldIndex];
return {
blocks: insert(removeAt(blocks, oldIndex), newIndex, movedBlock),
optimistic: true,
isSorting: false
};
},
() => {
setTimeout(() => {
this.props.dispatch(reorderBlocks({ oldIndex, newIndex }));
}, 100);
}
);
} else {
this.setState({
isSorting: false
});
}
};
handleItemRemove = index => {
this.props.dispatch(removeBlock({ index }));
};
render() {
const { blocks, isSorting } = this.state;
return (
<SortableList
helperClass="brz-ed-sidebar-block-item-helper"
isSorting={isSorting}
items={blocks}
distance={5}
useDragHandle={true}
contentWindow={() => window.parent}
onSortStart={this.handleBeforeSortStart}
onSortEnd={this.handleSortEnd}
onItemRemove={this.handleItemRemove}
/>
);
}
}
const mapStateToProps = state => ({
page: pageAssembledSelector(state)
});
export const BlocksSortable = {
id: "blocksSortable",
icon: "nc-reorder",
disabled: IS_GLOBAL_POPUP,
drawerTitle: t("Reorder Blocks"),
drawerComponent: connect(mapStateToProps)(DrawerComponent)
};