-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (73 loc) · 2.26 KB
/
index.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
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
import { createHigherOrderComponent } from '@wordpress/compose';
import { InspectorControls } from '@wordpress/block-editor';
import { Fragment } from '@wordpress/element';
import getSaveElement from './save';
/**
* Extend core table block settings
*
* @see https://developer.wordpress.org/block-editor/developers/filters/block-filters/#blocks-registerblocktype
*/
const registerBlockTypeHook = {
hookName: 'blocks.registerBlockType',
namespace: 'ups/extend/table-settings',
callback(settings, name) {
if (name !== 'core/table') {
return settings;
}
const updatedSettings = Object.assign({}, settings, {
supports: Object.assign({}, settings.supports, {
// Only allow center, wide, and full alignment options
align: ['center', 'wide', 'full'],
}),
});
return updatedSettings;
},
};
/**
* Extend core table block edit component
*
* @see https://developer.wordpress.org/block-editor/developers/filters/block-filters/#editor-blockedit
*/
const blockEditHook = {
hookName: 'editor.BlockEdit',
namespace: 'ups/extend/table-edit',
callback: createHigherOrderComponent(
BlockEdit => props => {
const { name } = props;
// Do nothing if it's another block than the table.
if (name !== 'core/table') {
return <BlockEdit {...props} />;
}
// Add a div above the Table settings panel
// so we can target the BlockEdit component with CSS
return (
<Fragment>
<InspectorControls>
<div className="block-editor-table"></div>
</InspectorControls>
<BlockEdit {...props} />
</Fragment>
);
},
'withInspectorControl',
),
};
/**
* Extend core table block save component
*
* @see https://developer.wordpress.org/block-editor/developers/filters/block-filters/#blocks-getsaveelement
*/
const blockEditSave = {
hookName: 'blocks.getSaveElement',
namespace: 'ups/extend/table-save',
callback: (element, blockType, attributes) => {
if (!element) {
return null;
} else if (blockType.name !== 'core/table') {
return element;
}
return getSaveElement(element, blockType, attributes);
},
};
export const hooks = [registerBlockTypeHook, blockEditHook, blockEditSave];
export const name = 'table';