@plone/volto-slate 18.0.0-alpha.4
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/.eslintrc.js +6 -0
- package/.release-it.json +25 -0
- package/CHANGELOG.md +19 -0
- package/LICENSE.md +21 -0
- package/README.md +10 -0
- package/build/messages/src/blocks/Table/TableBlockEdit.json +90 -0
- package/build/messages/src/blocks/Text/DefaultTextBlockEditor.json +6 -0
- package/build/messages/src/blocks/Text/DetachedTextBlockEditor.json +6 -0
- package/build/messages/src/blocks/Text/SlashMenu.json +6 -0
- package/build/messages/src/editor/plugins/AdvancedLink/index.json +10 -0
- package/build/messages/src/editor/plugins/Link/index.json +10 -0
- package/build/messages/src/editor/plugins/Table/index.json +30 -0
- package/build/messages/src/elementEditor/messages.json +10 -0
- package/build/messages/src/widgets/HtmlSlateWidget.json +6 -0
- package/build/messages/src/widgets/RichTextWidgetView.json +6 -0
- package/locales/de/LC_MESSAGES/volto.po +148 -0
- package/locales/en/LC_MESSAGES/volto.po +148 -0
- package/locales/volto.pot +182 -0
- package/package.json +42 -0
- package/src/actions/content.js +30 -0
- package/src/actions/index.js +3 -0
- package/src/actions/plugins.js +9 -0
- package/src/actions/selection.js +22 -0
- package/src/blocks/Table/Cell.jsx +87 -0
- package/src/blocks/Table/Cell.test.js +54 -0
- package/src/blocks/Table/TableBlockEdit.jsx +694 -0
- package/src/blocks/Table/TableBlockEdit.test.js +40 -0
- package/src/blocks/Table/TableBlockView.jsx +150 -0
- package/src/blocks/Table/TableBlockView.test.js +49 -0
- package/src/blocks/Table/__snapshots__/Cell.test.js.snap +3 -0
- package/src/blocks/Table/__snapshots__/TableBlockEdit.test.js.snap +22 -0
- package/src/blocks/Table/__snapshots__/TableBlockView.test.js.snap +27 -0
- package/src/blocks/Table/deconstruct.js +113 -0
- package/src/blocks/Table/extensions/normalizeTable.js +5 -0
- package/src/blocks/Table/index.js +60 -0
- package/src/blocks/Table/schema.js +122 -0
- package/src/blocks/Text/DefaultTextBlockEditor.jsx +304 -0
- package/src/blocks/Text/DetachedTextBlockEditor.jsx +77 -0
- package/src/blocks/Text/MarkdownIntroduction.jsx +59 -0
- package/src/blocks/Text/PluginSidebar.jsx +18 -0
- package/src/blocks/Text/ShortcutListing.jsx +28 -0
- package/src/blocks/Text/SlashMenu.jsx +203 -0
- package/src/blocks/Text/TextBlockEdit.jsx +38 -0
- package/src/blocks/Text/TextBlockEdit.test.js +107 -0
- package/src/blocks/Text/TextBlockSchema.js +54 -0
- package/src/blocks/Text/TextBlockView.jsx +31 -0
- package/src/blocks/Text/__snapshots__/TextBlockEdit.test.js.snap +62 -0
- package/src/blocks/Text/css/editor.css +18 -0
- package/src/blocks/Text/extensions/Readme.md +49 -0
- package/src/blocks/Text/extensions/breakList.js +100 -0
- package/src/blocks/Text/extensions/index.js +6 -0
- package/src/blocks/Text/extensions/insertBreak.js +57 -0
- package/src/blocks/Text/extensions/isSelected.js +7 -0
- package/src/blocks/Text/extensions/normalizeExternalData.js +7 -0
- package/src/blocks/Text/extensions/withDeserializers.js +87 -0
- package/src/blocks/Text/extensions/withLists.js +5 -0
- package/src/blocks/Text/index.js +171 -0
- package/src/blocks/Text/keyboard/backspaceInList.js +58 -0
- package/src/blocks/Text/keyboard/breakBlocks.js +3 -0
- package/src/blocks/Text/keyboard/cancelEsc.js +7 -0
- package/src/blocks/Text/keyboard/indentListItems.js +240 -0
- package/src/blocks/Text/keyboard/index.js +52 -0
- package/src/blocks/Text/keyboard/joinBlocks.js +180 -0
- package/src/blocks/Text/keyboard/moveListItems.js +124 -0
- package/src/blocks/Text/keyboard/slashMenu.js +19 -0
- package/src/blocks/Text/keyboard/softBreak.js +7 -0
- package/src/blocks/Text/keyboard/traverseBlocks.js +81 -0
- package/src/blocks/Text/keyboard/unwrapEmptyString.js +26 -0
- package/src/blocks/Text/schema.js +39 -0
- package/src/constants.js +123 -0
- package/src/editor/EditorContext.jsx +5 -0
- package/src/editor/EditorReference.jsx +22 -0
- package/src/editor/SlateEditor.jsx +375 -0
- package/src/editor/config.jsx +344 -0
- package/src/editor/decorate.js +68 -0
- package/src/editor/deserialize.js +185 -0
- package/src/editor/extensions/index.js +6 -0
- package/src/editor/extensions/insertBreak.js +15 -0
- package/src/editor/extensions/insertData.js +161 -0
- package/src/editor/extensions/isInline.js +14 -0
- package/src/editor/extensions/normalizeExternalData.js +8 -0
- package/src/editor/extensions/normalizeNode.js +48 -0
- package/src/editor/extensions/withDeserializers.js +15 -0
- package/src/editor/extensions/withTestingFeatures.jsx +84 -0
- package/src/editor/index.js +14 -0
- package/src/editor/less/editor.less +173 -0
- package/src/editor/less/globals.less +18 -0
- package/src/editor/less/slate.less +28 -0
- package/src/editor/plugins/AdvancedLink/deserialize.js +90 -0
- package/src/editor/plugins/AdvancedLink/extensions.js +32 -0
- package/src/editor/plugins/AdvancedLink/index.js +50 -0
- package/src/editor/plugins/AdvancedLink/render.jsx +37 -0
- package/src/editor/plugins/AdvancedLink/schema.js +114 -0
- package/src/editor/plugins/AdvancedLink/styles.less +8 -0
- package/src/editor/plugins/Blockquote/index.js +30 -0
- package/src/editor/plugins/Callout/index.js +34 -0
- package/src/editor/plugins/Image/deconstruct.js +30 -0
- package/src/editor/plugins/Image/extensions.js +51 -0
- package/src/editor/plugins/Image/index.js +11 -0
- package/src/editor/plugins/Image/render.jsx +22 -0
- package/src/editor/plugins/Link/extensions.js +58 -0
- package/src/editor/plugins/Link/index.js +159 -0
- package/src/editor/plugins/Link/render.jsx +54 -0
- package/src/editor/plugins/Markdown/constants.js +81 -0
- package/src/editor/plugins/Markdown/extensions.js +336 -0
- package/src/editor/plugins/Markdown/index.js +28 -0
- package/src/editor/plugins/Markdown/utils.js +198 -0
- package/src/editor/plugins/StyleMenu/StyleMenu.jsx +153 -0
- package/src/editor/plugins/StyleMenu/index.js +19 -0
- package/src/editor/plugins/StyleMenu/style.less +29 -0
- package/src/editor/plugins/StyleMenu/utils.js +168 -0
- package/src/editor/plugins/Table/TableButton.jsx +142 -0
- package/src/editor/plugins/Table/TableCell.jsx +44 -0
- package/src/editor/plugins/Table/TableContainer.jsx +37 -0
- package/src/editor/plugins/Table/TableSizePicker.jsx +83 -0
- package/src/editor/plugins/Table/extensions.js +87 -0
- package/src/editor/plugins/Table/index.js +390 -0
- package/src/editor/plugins/Table/less/public.less +29 -0
- package/src/editor/plugins/Table/less/table.less +28 -0
- package/src/editor/plugins/Table/render.jsx +30 -0
- package/src/editor/plugins/index.js +19 -0
- package/src/editor/render.jsx +224 -0
- package/src/editor/ui/BasicToolbar.jsx +11 -0
- package/src/editor/ui/BlockButton.jsx +31 -0
- package/src/editor/ui/ClearFormattingButton.jsx +21 -0
- package/src/editor/ui/ExpandedToolbar.jsx +18 -0
- package/src/editor/ui/Expando.jsx +5 -0
- package/src/editor/ui/InlineToolbar.jsx +69 -0
- package/src/editor/ui/MarkButton.jsx +23 -0
- package/src/editor/ui/MarkElementButton.jsx +30 -0
- package/src/editor/ui/Menu.jsx +13 -0
- package/src/editor/ui/PositionedToolbar.jsx +32 -0
- package/src/editor/ui/Separator.jsx +7 -0
- package/src/editor/ui/SlateContextToolbar.jsx +13 -0
- package/src/editor/ui/SlateToolbar.jsx +96 -0
- package/src/editor/ui/Toolbar.jsx +103 -0
- package/src/editor/ui/ToolbarButton.jsx +33 -0
- package/src/editor/ui/ToolbarButton.test.js +25 -0
- package/src/editor/ui/__snapshots__/ToolbarButton.test.js.snap +16 -0
- package/src/editor/ui/index.js +15 -0
- package/src/editor/utils.js +248 -0
- package/src/elementEditor/ContextButtons.jsx +57 -0
- package/src/elementEditor/PluginEditor.jsx +124 -0
- package/src/elementEditor/Readme.md +6 -0
- package/src/elementEditor/SchemaProvider.jsx +4 -0
- package/src/elementEditor/SidebarEditor.jsx +46 -0
- package/src/elementEditor/ToolbarButton.jsx +44 -0
- package/src/elementEditor/index.js +5 -0
- package/src/elementEditor/makeInlineElementPlugin.js +100 -0
- package/src/elementEditor/messages.js +14 -0
- package/src/elementEditor/utils.js +227 -0
- package/src/hooks/index.js +3 -0
- package/src/hooks/useEditorContext.js +6 -0
- package/src/hooks/useIsomorphicLayoutEffect.js +7 -0
- package/src/hooks/useSelectionPosition.js +25 -0
- package/src/i18n.js +180 -0
- package/src/icons/hashlink.svg +57 -0
- package/src/index.js +61 -0
- package/src/reducers/content.js +74 -0
- package/src/reducers/index.js +3 -0
- package/src/reducers/plugins.js +17 -0
- package/src/reducers/selection.js +16 -0
- package/src/utils/blocks.js +379 -0
- package/src/utils/blocks.test.js +138 -0
- package/src/utils/editor.js +31 -0
- package/src/utils/image.js +25 -0
- package/src/utils/index.js +11 -0
- package/src/utils/internals.js +46 -0
- package/src/utils/lists.js +92 -0
- package/src/utils/marks.js +104 -0
- package/src/utils/mime-types.js +24 -0
- package/src/utils/nodes.js +4 -0
- package/src/utils/ops.js +20 -0
- package/src/utils/random.js +17 -0
- package/src/utils/selection.js +236 -0
- package/src/utils/slate-string-utils.js +409 -0
- package/src/utils/volto-blocks.js +314 -0
- package/src/widgets/ErrorBoundary.jsx +27 -0
- package/src/widgets/HtmlSlateWidget.jsx +138 -0
- package/src/widgets/ObjectByTypeWidget.jsx +49 -0
- package/src/widgets/RichTextWidget.jsx +72 -0
- package/src/widgets/RichTextWidgetView.jsx +36 -0
- package/src/widgets/style.css +21 -0
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slate Table block editor.
|
|
3
|
+
* @module volto-slate/blocks/Table/Edit
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React, { Component } from 'react';
|
|
7
|
+
import PropTypes from 'prop-types';
|
|
8
|
+
import { map, remove } from 'lodash';
|
|
9
|
+
import { Button, Table } from 'semantic-ui-react';
|
|
10
|
+
import cx from 'classnames';
|
|
11
|
+
import { defineMessages, injectIntl } from 'react-intl';
|
|
12
|
+
|
|
13
|
+
import Cell from './Cell';
|
|
14
|
+
import { BlockDataForm, Icon, SidebarPortal } from '@plone/volto/components';
|
|
15
|
+
import TableSchema from './schema';
|
|
16
|
+
|
|
17
|
+
import rowBeforeSVG from '@plone/volto/icons/row-before.svg';
|
|
18
|
+
import rowAfterSVG from '@plone/volto/icons/row-after.svg';
|
|
19
|
+
import colBeforeSVG from '@plone/volto/icons/column-before.svg';
|
|
20
|
+
import colAfterSVG from '@plone/volto/icons/column-after.svg';
|
|
21
|
+
import rowDeleteSVG from '@plone/volto/icons/row-delete.svg';
|
|
22
|
+
import colDeleteSVG from '@plone/volto/icons/column-delete.svg';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns a random string of 32 digits.
|
|
26
|
+
*/
|
|
27
|
+
const getId = () => Math.floor(Math.random() * Math.pow(2, 24)).toString(32);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @returns {object} An empty Slate paragraph (a simple Slate block node with
|
|
31
|
+
* type set to "p" and a `Text` child with an empty string).
|
|
32
|
+
*/
|
|
33
|
+
function getEmptyParagraph() {
|
|
34
|
+
return [{ type: 'p', children: [{ text: '' }] }];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {string} type The type of the newly created cell: either 'header' or
|
|
39
|
+
* 'data', by default it is 'data'.
|
|
40
|
+
* @returns {object} A new cell object containing three properties: `key`,
|
|
41
|
+
* `type` and `value`.
|
|
42
|
+
*/
|
|
43
|
+
const emptyCell = (type = 'data') => ({
|
|
44
|
+
key: getId(),
|
|
45
|
+
type: type,
|
|
46
|
+
value: getEmptyParagraph(),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {Array[object]} cells Array of placeholders, each of them will be
|
|
51
|
+
* replaced in the newly created row with an empty cell. (Practically, just the
|
|
52
|
+
* length of the array matters.)
|
|
53
|
+
* @returns {object} A new row object containing the keys `key` and `cells`.
|
|
54
|
+
*/
|
|
55
|
+
const emptyRow = (cells) => ({
|
|
56
|
+
key: getId(),
|
|
57
|
+
cells: map(cells, () => emptyCell()),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The initial value for the displayed table's data. The IDs of the rows and
|
|
62
|
+
* cells are computed here only once, so each new table has the same IDs
|
|
63
|
+
* initially, but this does not have bad consequences since the key has
|
|
64
|
+
* relevance only in the context in which it is used.
|
|
65
|
+
*/
|
|
66
|
+
const initialTable = {
|
|
67
|
+
hideHeaders: false,
|
|
68
|
+
fixed: true,
|
|
69
|
+
compact: false,
|
|
70
|
+
basic: false,
|
|
71
|
+
celled: true,
|
|
72
|
+
inverted: false,
|
|
73
|
+
striped: false,
|
|
74
|
+
rows: [
|
|
75
|
+
{
|
|
76
|
+
key: getId(),
|
|
77
|
+
cells: [
|
|
78
|
+
{
|
|
79
|
+
key: getId(),
|
|
80
|
+
type: 'header',
|
|
81
|
+
value: getEmptyParagraph(),
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
key: getId(),
|
|
85
|
+
type: 'header',
|
|
86
|
+
value: getEmptyParagraph(),
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
key: getId(),
|
|
92
|
+
cells: [
|
|
93
|
+
{
|
|
94
|
+
key: getId(),
|
|
95
|
+
type: 'data',
|
|
96
|
+
value: getEmptyParagraph(),
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
key: getId(),
|
|
100
|
+
type: 'data',
|
|
101
|
+
value: getEmptyParagraph(),
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const messages = defineMessages({
|
|
109
|
+
insertRowBefore: {
|
|
110
|
+
id: 'Insert row before',
|
|
111
|
+
defaultMessage: 'Insert row before',
|
|
112
|
+
},
|
|
113
|
+
insertRowAfter: {
|
|
114
|
+
id: 'Insert row after',
|
|
115
|
+
defaultMessage: 'Insert row after',
|
|
116
|
+
},
|
|
117
|
+
deleteRow: {
|
|
118
|
+
id: 'Delete row',
|
|
119
|
+
defaultMessage: 'Delete row',
|
|
120
|
+
},
|
|
121
|
+
insertColBefore: {
|
|
122
|
+
id: 'Insert col before',
|
|
123
|
+
defaultMessage: 'Insert col before',
|
|
124
|
+
},
|
|
125
|
+
insertColAfter: {
|
|
126
|
+
id: 'Insert col after',
|
|
127
|
+
defaultMessage: 'Insert col after',
|
|
128
|
+
},
|
|
129
|
+
deleteCol: {
|
|
130
|
+
id: 'Delete col',
|
|
131
|
+
defaultMessage: 'Delete col',
|
|
132
|
+
},
|
|
133
|
+
left: {
|
|
134
|
+
id: 'Left',
|
|
135
|
+
defaultMessage: 'Left',
|
|
136
|
+
},
|
|
137
|
+
center: {
|
|
138
|
+
id: 'Center',
|
|
139
|
+
defaultMessage: 'Center',
|
|
140
|
+
},
|
|
141
|
+
right: {
|
|
142
|
+
id: 'Right',
|
|
143
|
+
defaultMessage: 'Right',
|
|
144
|
+
},
|
|
145
|
+
bottom: {
|
|
146
|
+
id: 'Bottom',
|
|
147
|
+
defaultMessage: 'Bottom',
|
|
148
|
+
},
|
|
149
|
+
middle: {
|
|
150
|
+
id: 'Middle',
|
|
151
|
+
defaultMessage: 'Middle',
|
|
152
|
+
},
|
|
153
|
+
top: {
|
|
154
|
+
id: 'Top',
|
|
155
|
+
defaultMessage: 'Top',
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Edit component for the Slate Table block type in Volto.
|
|
161
|
+
* @class Edit
|
|
162
|
+
* @extends Component
|
|
163
|
+
*/
|
|
164
|
+
class Edit extends Component {
|
|
165
|
+
/**
|
|
166
|
+
* Property types.
|
|
167
|
+
* @property {Object} propTypes Property types.
|
|
168
|
+
* @static
|
|
169
|
+
*/
|
|
170
|
+
static propTypes = {
|
|
171
|
+
data: PropTypes.objectOf(PropTypes.any).isRequired,
|
|
172
|
+
detached: PropTypes.bool,
|
|
173
|
+
index: PropTypes.number.isRequired,
|
|
174
|
+
selected: PropTypes.bool.isRequired,
|
|
175
|
+
block: PropTypes.string.isRequired,
|
|
176
|
+
onAddBlock: PropTypes.func.isRequired,
|
|
177
|
+
onChangeBlock: PropTypes.func.isRequired,
|
|
178
|
+
onDeleteBlock: PropTypes.func.isRequired,
|
|
179
|
+
onInsertBlock: PropTypes.func.isRequired,
|
|
180
|
+
onMutateBlock: PropTypes.func.isRequired,
|
|
181
|
+
onFocusPreviousBlock: PropTypes.func.isRequired,
|
|
182
|
+
onFocusNextBlock: PropTypes.func.isRequired,
|
|
183
|
+
onSelectBlock: PropTypes.func.isRequired,
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Default properties
|
|
188
|
+
* @property {Object} defaultProps Default properties.
|
|
189
|
+
* @static
|
|
190
|
+
*/
|
|
191
|
+
static defaultProps = {
|
|
192
|
+
detached: false,
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Constructor
|
|
197
|
+
* @method constructor
|
|
198
|
+
* @param {Object} props Component properties
|
|
199
|
+
* @constructs WysiwygEditor
|
|
200
|
+
*/
|
|
201
|
+
constructor(props) {
|
|
202
|
+
super(props);
|
|
203
|
+
this.state = {
|
|
204
|
+
headers: [],
|
|
205
|
+
rows: {},
|
|
206
|
+
selected: {
|
|
207
|
+
row: 0,
|
|
208
|
+
cell: 0,
|
|
209
|
+
},
|
|
210
|
+
isClient: false,
|
|
211
|
+
};
|
|
212
|
+
this.onChange = this.onChange.bind(this);
|
|
213
|
+
this.onSelectCell = this.onSelectCell.bind(this);
|
|
214
|
+
this.onInsertRowBefore = this.onInsertRowBefore.bind(this);
|
|
215
|
+
this.onInsertRowAfter = this.onInsertRowAfter.bind(this);
|
|
216
|
+
this.onInsertColBefore = this.onInsertColBefore.bind(this);
|
|
217
|
+
this.onInsertColAfter = this.onInsertColAfter.bind(this);
|
|
218
|
+
this.onDeleteRow = this.onDeleteRow.bind(this);
|
|
219
|
+
this.onDeleteCol = this.onDeleteCol.bind(this);
|
|
220
|
+
this.onChangeCell = this.onChangeCell.bind(this);
|
|
221
|
+
this.toggleCellType = this.toggleCellType.bind(this);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Component did mount lifecycle method
|
|
226
|
+
* @method componentDidMount
|
|
227
|
+
* @returns {undefined}
|
|
228
|
+
*/
|
|
229
|
+
componentDidMount() {
|
|
230
|
+
if (!this.props.data.table) {
|
|
231
|
+
this.props.onChangeBlock(this.props.block, {
|
|
232
|
+
...this.props.data,
|
|
233
|
+
table: initialTable,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
this.setState({ isClient: true });
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Component will receive props lifecycle method
|
|
241
|
+
* @method componentWillReceiveProps
|
|
242
|
+
* @param {Object} nextProps Next properties
|
|
243
|
+
* @returns {undefined}
|
|
244
|
+
*/
|
|
245
|
+
UNSAFE_componentWillReceiveProps(nextProps) {
|
|
246
|
+
if (!nextProps.data.table) {
|
|
247
|
+
this.props.onChangeBlock(nextProps.block, {
|
|
248
|
+
...nextProps.data,
|
|
249
|
+
table: initialTable,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* On change
|
|
256
|
+
* @method onChange
|
|
257
|
+
* @param {string} id Id of modified property.
|
|
258
|
+
* @param {any} value New value of modified property.
|
|
259
|
+
* @returns {undefined}
|
|
260
|
+
*/
|
|
261
|
+
onChange(id, value) {
|
|
262
|
+
const table = this.props.data.table;
|
|
263
|
+
this.props.onChangeBlock(this.props.block, {
|
|
264
|
+
...this.props.data,
|
|
265
|
+
table: {
|
|
266
|
+
...table,
|
|
267
|
+
[id]: value,
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Select cell handler
|
|
274
|
+
* @method onSelectCell
|
|
275
|
+
* @param {Number} row Row index.
|
|
276
|
+
* @param {Number} cell Cell index.
|
|
277
|
+
* @returns {undefined}
|
|
278
|
+
*/
|
|
279
|
+
onSelectCell(row, cell) {
|
|
280
|
+
this.setState({ selected: { row, cell } });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Change cell handler
|
|
285
|
+
* @param {Number} row Row index.
|
|
286
|
+
* @param {Number} cell Cell index.
|
|
287
|
+
* @param {Array} slateValue Value of the `SlateEditor` in the cell.
|
|
288
|
+
* @returns {undefined}
|
|
289
|
+
*/
|
|
290
|
+
onChangeCell(row, cell, slateValue) {
|
|
291
|
+
const table = JSON.parse(JSON.stringify(this.props.data.table));
|
|
292
|
+
table.rows[row].cells[cell] = {
|
|
293
|
+
...table.rows[row].cells[cell],
|
|
294
|
+
value: JSON.parse(JSON.stringify(slateValue)),
|
|
295
|
+
};
|
|
296
|
+
this.props.onChangeBlock(this.props.block, {
|
|
297
|
+
...this.props.data,
|
|
298
|
+
table,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Toggle cell type (from header to data or reverse)
|
|
304
|
+
* @method toggleCellType
|
|
305
|
+
* @returns {undefined}
|
|
306
|
+
*/
|
|
307
|
+
toggleCellType() {
|
|
308
|
+
const table = { ...this.props.data.table };
|
|
309
|
+
let type =
|
|
310
|
+
table.rows[this.state.selected.row].cells[this.state.selected.cell].type;
|
|
311
|
+
table.rows[this.state.selected.row].cells[this.state.selected.cell].type =
|
|
312
|
+
type === 'header' ? 'data' : 'header';
|
|
313
|
+
this.props.onChangeBlock(this.props.block, {
|
|
314
|
+
...this.props.data,
|
|
315
|
+
table,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Insert row before handler. Keeps the selected cell as selected after the
|
|
321
|
+
* operation is done.
|
|
322
|
+
* @returns {undefined}
|
|
323
|
+
*/
|
|
324
|
+
onInsertRowBefore() {
|
|
325
|
+
const table = this.props.data.table;
|
|
326
|
+
this.props.onChangeBlock(this.props.block, {
|
|
327
|
+
...this.props.data,
|
|
328
|
+
table: {
|
|
329
|
+
...table,
|
|
330
|
+
rows: [
|
|
331
|
+
...table.rows.slice(0, this.state.selected.row),
|
|
332
|
+
emptyRow(table.rows[0].cells),
|
|
333
|
+
...table.rows.slice(this.state.selected.row),
|
|
334
|
+
],
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
this.setState({
|
|
338
|
+
selected: {
|
|
339
|
+
row: this.state.selected.row + 1,
|
|
340
|
+
cell: this.state.selected.cell,
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Insert row after handler
|
|
347
|
+
* @returns {undefined}
|
|
348
|
+
*/
|
|
349
|
+
onInsertRowAfter() {
|
|
350
|
+
const table = this.props.data.table;
|
|
351
|
+
this.props.onChangeBlock(this.props.block, {
|
|
352
|
+
...this.props.data,
|
|
353
|
+
table: {
|
|
354
|
+
...table,
|
|
355
|
+
rows: [
|
|
356
|
+
...table.rows.slice(0, this.state.selected.row + 1),
|
|
357
|
+
emptyRow(table.rows[0].cells),
|
|
358
|
+
...table.rows.slice(this.state.selected.row + 1),
|
|
359
|
+
],
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Insert column before handler. Keeps the selected cell as selected after the
|
|
366
|
+
* operation is done.
|
|
367
|
+
* @returns {undefined}
|
|
368
|
+
*/
|
|
369
|
+
onInsertColBefore() {
|
|
370
|
+
const table = this.props.data.table;
|
|
371
|
+
this.props.onChangeBlock(this.props.block, {
|
|
372
|
+
...this.props.data,
|
|
373
|
+
table: {
|
|
374
|
+
...table,
|
|
375
|
+
rows: map(table.rows, (row, index) => ({
|
|
376
|
+
...row,
|
|
377
|
+
cells: [
|
|
378
|
+
...row.cells.slice(0, this.state.selected.cell),
|
|
379
|
+
emptyCell(table.rows[index].cells[this.state.selected.cell].type),
|
|
380
|
+
...row.cells.slice(this.state.selected.cell),
|
|
381
|
+
],
|
|
382
|
+
})),
|
|
383
|
+
},
|
|
384
|
+
});
|
|
385
|
+
this.setState({
|
|
386
|
+
selected: {
|
|
387
|
+
row: this.state.selected.row,
|
|
388
|
+
cell: this.state.selected.cell + 1,
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Insert column after handler
|
|
395
|
+
* @returns {undefined}
|
|
396
|
+
*/
|
|
397
|
+
onInsertColAfter() {
|
|
398
|
+
const table = this.props.data.table;
|
|
399
|
+
this.props.onChangeBlock(this.props.block, {
|
|
400
|
+
...this.props.data,
|
|
401
|
+
table: {
|
|
402
|
+
...table,
|
|
403
|
+
rows: map(table.rows, (row, index) => ({
|
|
404
|
+
...row,
|
|
405
|
+
cells: [
|
|
406
|
+
...row.cells.slice(0, this.state.selected.cell + 1),
|
|
407
|
+
emptyCell(table.rows[index].cells[this.state.selected.cell].type),
|
|
408
|
+
...row.cells.slice(this.state.selected.cell + 1),
|
|
409
|
+
],
|
|
410
|
+
})),
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Delete column handler. Changes the selected cell if the last table column
|
|
417
|
+
* is selected.
|
|
418
|
+
* @returns {undefined}
|
|
419
|
+
*/
|
|
420
|
+
onDeleteCol() {
|
|
421
|
+
const table = this.props.data.table;
|
|
422
|
+
|
|
423
|
+
if (this.state.selected.cell === table.rows[0].cells.length - 1) {
|
|
424
|
+
this.setState({
|
|
425
|
+
selected: {
|
|
426
|
+
row: this.state.selected.row,
|
|
427
|
+
cell: this.state.selected.cell - 1,
|
|
428
|
+
},
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
this.props.onChangeBlock(this.props.block, {
|
|
433
|
+
...this.props.data,
|
|
434
|
+
table: {
|
|
435
|
+
...table,
|
|
436
|
+
rows: map(table.rows, (row) => ({
|
|
437
|
+
...row,
|
|
438
|
+
cells: remove(
|
|
439
|
+
row.cells,
|
|
440
|
+
(cell, index) => index !== this.state.selected.cell,
|
|
441
|
+
),
|
|
442
|
+
})),
|
|
443
|
+
},
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Delete row handler. Changes the selected cell if the last table row is
|
|
449
|
+
* selected.
|
|
450
|
+
* @method onDeleteRow
|
|
451
|
+
* @returns {undefined}
|
|
452
|
+
*/
|
|
453
|
+
onDeleteRow() {
|
|
454
|
+
const table = this.props.data.table;
|
|
455
|
+
|
|
456
|
+
if (this.state.selected.row === table.rows.length - 1) {
|
|
457
|
+
this.setState({
|
|
458
|
+
selected: {
|
|
459
|
+
row: this.state.selected.row - 1,
|
|
460
|
+
cell: this.state.selected.cell,
|
|
461
|
+
},
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
this.props.onChangeBlock(this.props.block, {
|
|
466
|
+
...this.props.data,
|
|
467
|
+
table: {
|
|
468
|
+
...table,
|
|
469
|
+
rows: remove(
|
|
470
|
+
table.rows,
|
|
471
|
+
(row, index) => index !== this.state.selected.row,
|
|
472
|
+
),
|
|
473
|
+
},
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
componentDidUpdate(prevProps) {
|
|
478
|
+
if (prevProps.selected && !this.props.selected) {
|
|
479
|
+
this.setState({ selected: null });
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Render method.
|
|
485
|
+
* @method render
|
|
486
|
+
* @returns {string} Markup for the component.
|
|
487
|
+
*/
|
|
488
|
+
render() {
|
|
489
|
+
const headers = this.props.data.table?.rows?.[0]?.cells || [];
|
|
490
|
+
const rows =
|
|
491
|
+
this.props.data.table?.rows?.filter((_, index) => index > 0) || [];
|
|
492
|
+
const schema = TableSchema(this.props);
|
|
493
|
+
|
|
494
|
+
return (
|
|
495
|
+
// TODO: use slate-table instead of table, but first copy the CSS styles
|
|
496
|
+
// to the new name
|
|
497
|
+
<div className={cx('block table', { selected: this.props.selected })}>
|
|
498
|
+
{this.props.selected && (
|
|
499
|
+
<div className="toolbar">
|
|
500
|
+
<Button.Group>
|
|
501
|
+
<Button
|
|
502
|
+
icon
|
|
503
|
+
basic
|
|
504
|
+
onClick={this.onInsertRowBefore}
|
|
505
|
+
title={this.props.intl.formatMessage(messages.insertRowBefore)}
|
|
506
|
+
aria-label={this.props.intl.formatMessage(
|
|
507
|
+
messages.insertRowBefore,
|
|
508
|
+
)}
|
|
509
|
+
>
|
|
510
|
+
<Icon name={rowBeforeSVG} size="24px" />
|
|
511
|
+
</Button>
|
|
512
|
+
</Button.Group>
|
|
513
|
+
<Button.Group>
|
|
514
|
+
<Button
|
|
515
|
+
icon
|
|
516
|
+
basic
|
|
517
|
+
onClick={this.onInsertRowAfter}
|
|
518
|
+
title={this.props.intl.formatMessage(messages.insertRowAfter)}
|
|
519
|
+
aria-label={this.props.intl.formatMessage(
|
|
520
|
+
messages.insertRowAfter,
|
|
521
|
+
)}
|
|
522
|
+
>
|
|
523
|
+
<Icon name={rowAfterSVG} size="24px" />
|
|
524
|
+
</Button>
|
|
525
|
+
</Button.Group>
|
|
526
|
+
<Button.Group>
|
|
527
|
+
<Button
|
|
528
|
+
icon
|
|
529
|
+
basic
|
|
530
|
+
onClick={this.onDeleteRow}
|
|
531
|
+
disabled={
|
|
532
|
+
this.props.data.table &&
|
|
533
|
+
this.props.data.table.rows.length === 1
|
|
534
|
+
}
|
|
535
|
+
title={this.props.intl.formatMessage(messages.deleteRow)}
|
|
536
|
+
aria-label={this.props.intl.formatMessage(messages.deleteRow)}
|
|
537
|
+
>
|
|
538
|
+
<Icon name={rowDeleteSVG} size="24px" />
|
|
539
|
+
</Button>
|
|
540
|
+
</Button.Group>
|
|
541
|
+
<Button.Group>
|
|
542
|
+
<Button
|
|
543
|
+
icon
|
|
544
|
+
basic
|
|
545
|
+
onClick={this.onInsertColBefore}
|
|
546
|
+
title={this.props.intl.formatMessage(messages.insertColBefore)}
|
|
547
|
+
aria-label={this.props.intl.formatMessage(
|
|
548
|
+
messages.insertColBefore,
|
|
549
|
+
)}
|
|
550
|
+
>
|
|
551
|
+
<Icon name={colBeforeSVG} size="24px" />
|
|
552
|
+
</Button>
|
|
553
|
+
</Button.Group>
|
|
554
|
+
<Button.Group>
|
|
555
|
+
<Button
|
|
556
|
+
icon
|
|
557
|
+
basic
|
|
558
|
+
onClick={this.onInsertColAfter}
|
|
559
|
+
title={this.props.intl.formatMessage(messages.insertColAfter)}
|
|
560
|
+
aria-label={this.props.intl.formatMessage(
|
|
561
|
+
messages.insertColAfter,
|
|
562
|
+
)}
|
|
563
|
+
>
|
|
564
|
+
<Icon name={colAfterSVG} size="24px" />
|
|
565
|
+
</Button>
|
|
566
|
+
</Button.Group>
|
|
567
|
+
<Button.Group>
|
|
568
|
+
<Button
|
|
569
|
+
icon
|
|
570
|
+
basic
|
|
571
|
+
onClick={this.onDeleteCol}
|
|
572
|
+
disabled={
|
|
573
|
+
this.props.data.table &&
|
|
574
|
+
this.props.data.table.rows[0].cells.length === 1
|
|
575
|
+
}
|
|
576
|
+
title={this.props.intl.formatMessage(messages.deleteCol)}
|
|
577
|
+
aria-label={this.props.intl.formatMessage(messages.deleteCol)}
|
|
578
|
+
>
|
|
579
|
+
<Icon name={colDeleteSVG} size="24px" />
|
|
580
|
+
</Button>
|
|
581
|
+
</Button.Group>
|
|
582
|
+
</div>
|
|
583
|
+
)}
|
|
584
|
+
{this.props.data.table && (
|
|
585
|
+
<Table
|
|
586
|
+
fixed={this.props.data.table.fixed}
|
|
587
|
+
compact={this.props.data.table.compact}
|
|
588
|
+
basic={this.props.data.table.basic ? 'very' : false}
|
|
589
|
+
celled={this.props.data.table.celled}
|
|
590
|
+
inverted={this.props.data.table.inverted}
|
|
591
|
+
striped={this.props.data.table.striped}
|
|
592
|
+
className="slate-table-block"
|
|
593
|
+
>
|
|
594
|
+
{!this.props.data.table.hideHeaders ? (
|
|
595
|
+
<Table.Header>
|
|
596
|
+
<Table.Row textAlign="left">
|
|
597
|
+
{headers.map((cell, cellIndex) => (
|
|
598
|
+
<Table.HeaderCell
|
|
599
|
+
key={cell.key}
|
|
600
|
+
textAlign="left"
|
|
601
|
+
verticalAlign="middle"
|
|
602
|
+
>
|
|
603
|
+
<Cell
|
|
604
|
+
value={cell.value}
|
|
605
|
+
row={0}
|
|
606
|
+
cell={cellIndex}
|
|
607
|
+
onSelectCell={this.onSelectCell}
|
|
608
|
+
selected={
|
|
609
|
+
this.props.selected &&
|
|
610
|
+
this.state.selected &&
|
|
611
|
+
0 === this.state.selected.row &&
|
|
612
|
+
cellIndex === this.state.selected.cell
|
|
613
|
+
}
|
|
614
|
+
selectedCell={this.state.selected}
|
|
615
|
+
isTableBlockSelected={this.props.selected}
|
|
616
|
+
onAddBlock={this.props.onAddBlock}
|
|
617
|
+
onSelectBlock={this.props.onSelectBlock}
|
|
618
|
+
onChange={this.onChangeCell}
|
|
619
|
+
index={this.props.index}
|
|
620
|
+
/>
|
|
621
|
+
</Table.HeaderCell>
|
|
622
|
+
))}
|
|
623
|
+
</Table.Row>
|
|
624
|
+
</Table.Header>
|
|
625
|
+
) : (
|
|
626
|
+
''
|
|
627
|
+
)}
|
|
628
|
+
<Table.Body>
|
|
629
|
+
{map(rows, (row, rowIndex) => (
|
|
630
|
+
<Table.Row key={row.key}>
|
|
631
|
+
{map(row.cells, (cell, cellIndex) => (
|
|
632
|
+
<Table.Cell
|
|
633
|
+
key={cell.key}
|
|
634
|
+
textAlign="left"
|
|
635
|
+
verticalAlign="middle"
|
|
636
|
+
className={
|
|
637
|
+
this.props.selected &&
|
|
638
|
+
this.state.selected &&
|
|
639
|
+
rowIndex + 1 === this.state.selected.row &&
|
|
640
|
+
cellIndex === this.state.selected.cell &&
|
|
641
|
+
this.props.selected
|
|
642
|
+
? 'selected'
|
|
643
|
+
: ''
|
|
644
|
+
}
|
|
645
|
+
>
|
|
646
|
+
<Cell
|
|
647
|
+
value={cell.value}
|
|
648
|
+
row={rowIndex + 1}
|
|
649
|
+
cell={cellIndex}
|
|
650
|
+
onSelectCell={this.onSelectCell}
|
|
651
|
+
selected={
|
|
652
|
+
this.props.selected &&
|
|
653
|
+
this.state.selected &&
|
|
654
|
+
rowIndex + 1 === this.state.selected.row &&
|
|
655
|
+
cellIndex === this.state.selected.cell
|
|
656
|
+
}
|
|
657
|
+
selectedCell={this.state.selected}
|
|
658
|
+
isTableBlockSelected={this.props.selected}
|
|
659
|
+
onAddBlock={this.props.onAddBlock}
|
|
660
|
+
onSelectBlock={this.props.onSelectBlock}
|
|
661
|
+
onChange={this.onChangeCell}
|
|
662
|
+
index={this.props.index}
|
|
663
|
+
/>
|
|
664
|
+
</Table.Cell>
|
|
665
|
+
))}
|
|
666
|
+
</Table.Row>
|
|
667
|
+
))}
|
|
668
|
+
</Table.Body>
|
|
669
|
+
</Table>
|
|
670
|
+
)}
|
|
671
|
+
{this.props.selected && this.state.selected && this.state.isClient && (
|
|
672
|
+
<SidebarPortal selected={this.props.selected}>
|
|
673
|
+
<BlockDataForm
|
|
674
|
+
schema={schema}
|
|
675
|
+
title={schema.title}
|
|
676
|
+
onChangeField={(id, value) => {
|
|
677
|
+
this.props.onChangeBlock(this.props.block, {
|
|
678
|
+
...this.props.data,
|
|
679
|
+
[id]: value,
|
|
680
|
+
});
|
|
681
|
+
}}
|
|
682
|
+
onChangeBlock={this.props.onChangeBlock}
|
|
683
|
+
formData={this.props.data}
|
|
684
|
+
block={this.props.block}
|
|
685
|
+
blocksConfig={this.props.blocksConfig}
|
|
686
|
+
/>
|
|
687
|
+
</SidebarPortal>
|
|
688
|
+
)}
|
|
689
|
+
</div>
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
export default injectIntl(Edit);
|