-
-
Notifications
You must be signed in to change notification settings - Fork 871
Expand file tree
/
Copy pathEdit.jsx
More file actions
213 lines (199 loc) · 6.11 KB
/
Copy pathEdit.jsx
File metadata and controls
213 lines (199 loc) · 6.11 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Edit block.
* @module components/manage/Blocks/Block/Edit
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { compose } from 'redux';
import { connect } from 'react-redux';
import { defineMessages, injectIntl } from 'react-intl';
import cx from 'classnames';
import { setSidebarTab } from '@plone/volto/actions';
import config from '@plone/volto/registry';
import withObjectBrowser from '@plone/volto/components/manage/Sidebar/ObjectBrowser';
import { applyBlockDefaults } from '@plone/volto/helpers';
import {
SidebarPortal,
BlockSettingsSidebar,
BlockSettingsSchema,
} from '@plone/volto/components';
const messages = defineMessages({
unknownBlock: {
id: 'Unknown Block',
defaultMessage: 'Unknown Block {block}',
},
});
/**
* Edit block class.
* @class Edit
* @extends Component
*/
export class Edit extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
type: PropTypes.string.isRequired,
data: PropTypes.objectOf(PropTypes.any).isRequired,
// properties is mapped to formData, so it's not connected to changes of the object
properties: PropTypes.objectOf(PropTypes.any).isRequired,
selected: PropTypes.bool.isRequired,
multiSelected: PropTypes.bool,
index: PropTypes.number.isRequired,
id: PropTypes.string.isRequired,
manage: PropTypes.bool,
onMoveBlock: PropTypes.func.isRequired,
onDeleteBlock: PropTypes.func.isRequired,
editable: PropTypes.bool,
};
/**
* Default properties.
* @property {Object} defaultProps Default properties.
* @static
*/
static defaultProps = {
manage: false,
editable: true,
};
componentDidMount() {
const { type } = this.props;
const { blocksConfig = config.blocks.blocksConfig } = this.props;
const blockHasOwnFocusManagement =
blocksConfig?.[type]?.['blockHasOwnFocusManagement'] || null;
if (
!blockHasOwnFocusManagement &&
this.props.selected &&
this.blockNode.current
) {
this.blockNode.current.focus();
}
const tab = this.props.manage ? 1 : blocksConfig?.[type]?.sidebarTab || 0;
if (this.props.selected && this.props.editable) {
this.props.setSidebarTab(tab);
}
}
UNSAFE_componentWillReceiveProps(nextProps) {
const { blocksConfig = config.blocks.blocksConfig } = this.props;
const { selected, type } = this.props;
const blockHasOwnFocusManagement =
blocksConfig?.[type]?.['blockHasOwnFocusManagement'] || null;
if (
!blockHasOwnFocusManagement &&
nextProps.selected &&
selected !== nextProps.selected &&
this.blockNode.current
) {
this.blockNode.current.focus();
}
if (
((!this.props.selected && nextProps.selected) ||
type !== nextProps.type) &&
this.props.editable
) {
const tab = this.props.manage
? 1
: blocksConfig?.[nextProps.type]?.sidebarTab || 0;
this.props.setSidebarTab(tab);
}
}
blockNode = React.createRef();
/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
const { blocksConfig = config.blocks.blocksConfig } = this.props;
const { editable, type } = this.props;
const disableNewBlocks = this.props.data?.disableNewBlocks;
let Block = blocksConfig?.[type]?.['edit'] || null;
if (
this.props.data?.readOnly ||
(!editable && !config.blocks.showEditBlocksInBabelView)
) {
Block = blocksConfig?.[type]?.['view'] || null;
}
const schema = blocksConfig?.[type]?.['schema'] || BlockSettingsSchema;
const blockHasOwnFocusManagement =
blocksConfig?.[type]?.['blockHasOwnFocusManagement'] || null;
return Block !== null ? (
<div
role="presentation"
onClick={(e) => {
const isMultipleSelection = e.shiftKey || e.ctrlKey || e.metaKey;
!this.props.selected &&
this.props.onSelectBlock(
this.props.id,
this.props.selected ? false : isMultipleSelection,
e,
);
}}
onKeyDown={
!(blockHasOwnFocusManagement || disableNewBlocks)
? (e) =>
this.props.handleKeyDown(
e,
this.props.index,
this.props.id,
this.blockNode.current,
)
: null
}
className={cx(`block ${type}`, {
selected: this.props.selected || this.props.multiSelected,
multiSelected: this.props.multiSelected,
})}
style={{ outline: 'none' }}
ref={this.blockNode}
// The tabIndex is required for the keyboard navigation
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
tabIndex={!blockHasOwnFocusManagement ? -1 : null}
>
<Block
{...this.props}
blockNode={this.blockNode}
data={applyBlockDefaults(this.props)}
/>
{this.props.manage && (
<SidebarPortal selected={this.props.selected} tab="sidebar-settings">
<BlockSettingsSidebar {...this.props} schema={schema} />
</SidebarPortal>
)}
</div>
) : (
<div
role="presentation"
onClick={() =>
!this.props.selected && this.props.onSelectBlock(this.props.id)
}
onKeyDown={
!(blockHasOwnFocusManagement || disableNewBlocks)
? (e) =>
this.props.handleKeyDown(
e,
this.props.index,
this.props.id,
this.blockNode.current,
)
: null
}
className={cx(`block ${type}`, { selected: this.props.selected })}
style={{ outline: 'none' }}
ref={this.blockNode}
// The tabIndex is required for the keyboard navigation
tabIndex={-1}
>
{this.props.intl.formatMessage(messages.unknownBlock, {
block: type,
})}
</div>
);
}
}
export default compose(
injectIntl,
withObjectBrowser,
connect(null, { setSidebarTab }),
)(Edit);