-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblock.js
101 lines (95 loc) · 2.92 KB
/
block.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
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import BlockIcon from '../block-icon';
/**
* Returns the client ID of the parent where a newly inserted block would be
* placed.
*
* @return {string} Client ID of the parent where a newly inserted block would
* be placed.
*/
function defaultGetBlockInsertionParentClientId() {
return select( 'core/editor' ).getBlockInsertionPoint().rootClientId;
}
/**
* Returns the inserter items for the specified parent block.
*
* @param {string} rootClientId Client ID of the block for which to retrieve
* inserter items.
*
* @return {Array<Editor.InserterItem>} The inserter items for the specified
* parent.
*/
function defaultGetInserterItems( rootClientId ) {
return select( 'core/editor' ).getInserterItems( rootClientId );
}
/**
* Returns the name of the currently selected block.
*
* @return {string?} The name of the currently selected block or `null` if no
* block is selected.
*/
function defaultGetSelectedBlockName() {
const selectedBlock = select( 'core/editor' ).getSelectedBlock();
return selectedBlock ? selectedBlock.name : null;
}
/**
* Creates a blocks repeater for replacing the current block with a selected block type.
*
* @return {Completer} A blocks completer.
*/
export function createBlockCompleter( {
// Allow store-based selectors to be overridden for unit test.
getBlockInsertionParentClientId = defaultGetBlockInsertionParentClientId,
getInserterItems = defaultGetInserterItems,
getSelectedBlockName = defaultGetSelectedBlockName,
} = {} ) {
return {
name: 'blocks',
className: 'editor-autocompleters__block',
triggerPrefix: '/',
options() {
const selectedBlockName = getSelectedBlockName();
return getInserterItems( getBlockInsertionParentClientId() ).filter(
// Avoid offering to replace the current block with a block of the same type.
( inserterItem ) => selectedBlockName !== inserterItem.name
);
},
getOptionKeywords( inserterItem ) {
const { title, keywords = [] } = inserterItem;
return [ ...keywords, title ];
},
getOptionLabel( inserterItem ) {
const { icon, title } = inserterItem;
return [
<BlockIcon key="icon" icon={ icon } showColors />,
title,
];
},
allowContext( before, after ) {
return ! ( /\S/.test( before ) || /\S/.test( after ) );
},
getOptionCompletion( inserterItem ) {
const { name, initialAttributes } = inserterItem;
return {
action: 'replace',
value: createBlock( name, initialAttributes ),
};
},
isOptionDisabled( inserterItem ) {
return inserterItem.isDisabled;
},
};
}
/**
* Creates a blocks repeater for replacing the current block with a selected block type.
*
* @return {Completer} A blocks completer.
*/
export default createBlockCompleter();