-
Notifications
You must be signed in to change notification settings - Fork 4k
/
edit.js
186 lines (171 loc) · 4.98 KB
/
edit.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
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
/**
* WordPress dependencies
*/
import {
BlockControls,
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { ToolbarButton } from '@wordpress/components';
import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import { isRTL, __ } from '@wordpress/i18n';
import {
formatListBullets,
formatListBulletsRTL,
formatListNumbered,
formatListNumberedRTL,
formatOutdent,
formatOutdentRTL,
} from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';
import { useCallback, useEffect, Platform } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import OrderedListSettings from './ordered-list-settings';
import { migrateToListV2 } from './utils';
import TagName from './tag-name';
const TEMPLATE = [ [ 'core/list-item' ] ];
const NATIVE_MARGIN_SPACING = 8;
/**
* At the moment, deprecations don't handle create blocks from attributes
* (like when using CPT templates). For this reason, this hook is necessary
* to avoid breaking templates using the old list block format.
*
* @param {Object} attributes Block attributes.
* @param {string} clientId Block client ID.
*/
function useMigrateOnLoad( attributes, clientId ) {
const registry = useRegistry();
const { updateBlockAttributes, replaceInnerBlocks } =
useDispatch( blockEditorStore );
useEffect( () => {
// As soon as the block is loaded, migrate it to the new version.
if ( ! attributes.values ) {
return;
}
const [ newAttributes, newInnerBlocks ] = migrateToListV2( attributes );
deprecated( 'Value attribute on the list block', {
since: '6.0',
version: '6.5',
alternative: 'inner blocks',
} );
registry.batch( () => {
updateBlockAttributes( clientId, newAttributes );
replaceInnerBlocks( clientId, newInnerBlocks );
} );
}, [ attributes.values ] );
}
function useOutdentList( clientId ) {
const { replaceBlocks, selectionChange } = useDispatch( blockEditorStore );
const { getBlockRootClientId, getBlockAttributes, getBlock } =
useSelect( blockEditorStore );
return useCallback( () => {
const parentBlockId = getBlockRootClientId( clientId );
const parentBlockAttributes = getBlockAttributes( parentBlockId );
// Create a new parent block without the inner blocks.
const newParentBlock = createBlock(
'core/list-item',
parentBlockAttributes
);
const { innerBlocks } = getBlock( clientId );
// Replace the parent block with a new parent block without inner blocks,
// and make the inner blocks siblings of the parent.
replaceBlocks( [ parentBlockId ], [ newParentBlock, ...innerBlocks ] );
// Select the last child of the list being outdent.
selectionChange( innerBlocks[ innerBlocks.length - 1 ].clientId );
}, [ clientId ] );
}
function IndentUI( { clientId } ) {
const outdentList = useOutdentList( clientId );
const canOutdent = useSelect(
( select ) => {
const { getBlockRootClientId, getBlockName } =
select( blockEditorStore );
return (
getBlockName( getBlockRootClientId( clientId ) ) ===
'core/list-item'
);
},
[ clientId ]
);
return (
<>
<ToolbarButton
icon={ isRTL() ? formatOutdentRTL : formatOutdent }
title={ __( 'Outdent' ) }
describedBy={ __( 'Outdent list item' ) }
disabled={ ! canOutdent }
onClick={ outdentList }
/>
</>
);
}
export default function Edit( { attributes, setAttributes, clientId, style } ) {
const { ordered, type, reversed, start } = attributes;
const blockProps = useBlockProps( {
style: {
...( Platform.isNative && style ),
listStyleType: ordered && type !== 'decimal' ? type : undefined,
},
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: [ 'core/list-item' ],
template: TEMPLATE,
templateLock: false,
templateInsertUpdatesSelection: true,
...( Platform.isNative && {
marginVertical: NATIVE_MARGIN_SPACING,
marginHorizontal: NATIVE_MARGIN_SPACING,
renderAppender: false,
} ),
__experimentalCaptureToolbars: true,
} );
useMigrateOnLoad( attributes, clientId );
const controls = (
<BlockControls group="block">
<ToolbarButton
icon={ isRTL() ? formatListBulletsRTL : formatListBullets }
title={ __( 'Unordered' ) }
describedBy={ __( 'Convert to unordered list' ) }
isActive={ ordered === false }
onClick={ () => {
setAttributes( { ordered: false } );
} }
/>
<ToolbarButton
icon={ isRTL() ? formatListNumberedRTL : formatListNumbered }
title={ __( 'Ordered' ) }
describedBy={ __( 'Convert to ordered list' ) }
isActive={ ordered === true }
onClick={ () => {
setAttributes( { ordered: true } );
} }
/>
<IndentUI clientId={ clientId } />
</BlockControls>
);
return (
<>
<TagName
ordered={ ordered }
reversed={ reversed }
start={ start }
{ ...innerBlocksProps }
/>
{ controls }
{ ordered && (
<OrderedListSettings
{ ...{
setAttributes,
reversed,
start,
type,
} }
/>
) }
</>
);
}