-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathuse-merge.js
More file actions
196 lines (175 loc) · 5.22 KB
/
Copy pathuse-merge.js
File metadata and controls
196 lines (175 loc) · 5.22 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
/**
* WordPress dependencies
*/
import { useRegistry, useDispatch, useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { isUnmodifiedBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import useOutdentListItem from './use-outdent-list-item';
export default function useMerge( clientId, onMerge ) {
const registry = useRegistry();
const {
getPreviousBlockClientId,
getNextBlockClientId,
getBlockOrder,
getBlockRootClientId,
getBlockName,
getBlock,
} = useSelect( blockEditorStore );
const { mergeBlocks, moveBlocksToPosition, removeBlock } =
useDispatch( blockEditorStore );
const outdentListItem = useOutdentListItem();
function getTrailingId( id ) {
const order = getBlockOrder( id );
if ( ! order.length ) {
return id;
}
return getTrailingId( order[ order.length - 1 ] );
}
function getParentListItemId( id ) {
const listId = getBlockRootClientId( id );
const parentListItemId = getBlockRootClientId( listId );
if ( ! parentListItemId ) {
return;
}
if ( getBlockName( parentListItemId ) !== 'core/list-item' ) {
return;
}
return parentListItemId;
}
/**
* Return the next list item with respect to the given list item. If none,
* return the next list item of the parent list item if it exists.
*
* @param {string} id A list item client ID.
* @return {?string} The client ID of the next list item.
*/
function _getNextId( id ) {
const next = getNextBlockClientId( id );
if ( next ) {
return next;
}
const parentListItemId = getParentListItemId( id );
if ( ! parentListItemId ) {
return;
}
return _getNextId( parentListItemId );
}
/**
* Given a client ID, return the client ID of the list item on the next
* line, regardless of indentation level.
*
* @param {string} id The client ID of the current list item.
* @return {?string} The client ID of the next list item.
*/
function getNextId( id ) {
const order = getBlockOrder( id );
// If the list item does not have a nested list, return the next list
// item.
if ( ! order.length ) {
return _getNextId( id );
}
// Get the first list item in the nested list.
return getBlockOrder( order[ 0 ] )[ 0 ];
}
/**
* Given a list item client ID, walk up through ancestor list items
* and return the client ID of the first block found after any
* ancestor list. This is used for forward merging when there are no
* more list items at any nesting level.
*
* @param {string} id A list item client ID.
*/
function getNextOuterBlockClientId( id ) {
let parentListItemId = getParentListItemId( id );
while ( parentListItemId ) {
const parentListId = getBlockRootClientId( parentListItemId );
const nextOuterBlockClientId = getNextBlockClientId( parentListId );
if ( nextOuterBlockClientId ) {
return nextOuterBlockClientId;
}
parentListItemId = getParentListItemId( parentListItemId );
}
}
return ( forward ) => {
function mergeWithNested( clientIdA, clientIdB ) {
registry.batch( () => {
// When merging a sub list item with a higher next list item, we
// also need to move any nested list items. Check if there's a
// listed list, and append its nested list items to the current
// list.
const [ nestedListClientId ] = getBlockOrder( clientIdB );
if ( nestedListClientId ) {
// If we are merging with the previous list item, and the
// previous list item does not have nested list, move the
// nested list to the previous list item.
if (
getPreviousBlockClientId( clientIdB ) === clientIdA &&
! getBlockOrder( clientIdA ).length
) {
moveBlocksToPosition(
[ nestedListClientId ],
clientIdB,
clientIdA
);
} else {
moveBlocksToPosition(
getBlockOrder( nestedListClientId ),
nestedListClientId,
getBlockRootClientId( clientIdA )
);
}
}
mergeBlocks( clientIdA, clientIdB );
} );
}
if ( forward ) {
const nextBlockClientId = getNextId( clientId );
if ( ! nextBlockClientId ) {
const nextOuterBlockClientId =
getNextOuterBlockClientId( clientId );
if ( nextOuterBlockClientId ) {
mergeBlocks(
getBlockRootClientId( clientId ),
nextOuterBlockClientId
);
return;
}
onMerge( forward );
return;
}
if ( getParentListItemId( nextBlockClientId ) ) {
outdentListItem( nextBlockClientId );
} else {
mergeWithNested( clientId, nextBlockClientId );
}
} else {
// Merging is only done from the top level. For lowel levels, the
// list item is outdented instead.
if ( getParentListItemId( clientId ) ) {
outdentListItem( clientId );
return;
}
const previousBlockClientId = getPreviousBlockClientId( clientId );
if ( previousBlockClientId ) {
const trailingId = getTrailingId( previousBlockClientId );
mergeWithNested( trailingId, clientId );
return;
}
const blockOrder = getBlockOrder( clientId );
if (
isUnmodifiedBlock( getBlock( clientId ), 'content' ) &&
blockOrder.length > 0
) {
registry.batch( () => {
outdentListItem( getBlockOrder( blockOrder[ 0 ] ) );
removeBlock( clientId, true );
} );
} else {
onMerge( forward );
}
}
};
}