-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
147 lines (134 loc) · 3.3 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
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
/**
* WordPress dependencies
*/
import { switchChildrenNodeName } from 'element';
import { find } from 'lodash';
/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType, query as hpq, createBlock } from '../../api';
import Editable from '../../editable';
const { children, prop } = hpq;
function execCommand( command ) {
return ( { editor } ) => {
if ( editor ) {
editor.execCommand( command );
}
};
}
function listIsActive( listType ) {
return ( { nodeName = 'OL', internalListType } ) => {
return listType === ( internalListType ? internalListType : nodeName );
};
}
function listSetType( listType, editorCommand ) {
return ( { internalListType, editor }, setAttributes ) => {
if ( internalListType ) {
// only change list types, don't toggle off internal lists
if ( internalListType !== listType ) {
if ( editor ) {
editor.execCommand( editorCommand );
}
}
} else {
setAttributes( { nodeName: listType } );
}
};
}
function findInternalListType( { parents } ) {
const list = find( parents, ( node ) => node.nodeName === 'UL' || node.nodeName === 'OL' );
return list ? list.nodeName : null;
}
registerBlockType( 'core/list', {
title: wp.i18n.__( 'List' ),
icon: 'editor-ul',
category: 'common',
attributes: {
nodeName: prop( 'ol,ul', 'nodeName' ),
values: children( 'ol,ul' ),
},
controls: [
{
icon: 'editor-ul',
title: wp.i18n.__( 'Convert to unordered' ),
isActive: listIsActive( 'UL' ),
onClick: listSetType( 'UL', 'InsertUnorderedList' ),
},
{
icon: 'editor-ol',
title: wp.i18n.__( 'Convert to ordered' ),
isActive: listIsActive( 'OL' ),
onClick: listSetType( 'OL', 'InsertOrderedList' ),
},
{
icon: 'editor-outdent',
title: wp.i18n.__( 'Outdent list item' ),
onClick: execCommand( 'Outdent' ),
},
{
icon: 'editor-indent',
title: wp.i18n.__( 'Indent list item' ),
onClick: execCommand( 'Indent' ),
},
],
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/text' ],
transform: ( { content } ) => {
return createBlock( 'core/list', {
nodeName: 'ul',
values: switchChildrenNodeName( content, 'li' ),
} );
},
},
],
to: [
{
type: 'block',
blocks: [ 'core/text' ],
transform: ( { values } ) => {
return createBlock( 'core/text', {
content: switchChildrenNodeName( values, 'p' ),
} );
},
},
],
},
edit( { attributes, setAttributes, focus, setFocus } ) {
const { nodeName = 'OL', values = [] } = attributes;
return (
<Editable
tagName={ nodeName.toLowerCase() }
getSettings={ ( settings ) => ( {
...settings,
plugins: ( settings.plugins || [] ).concat( 'lists' ),
lists_indent_on_tab: false,
} ) }
onSetup={ ( editor ) => {
editor.on( 'nodeChange', ( nodeInfo ) => {
setAttributes( { internalListType: findInternalListType( nodeInfo ) } );
} );
setAttributes( { editor } );
} }
onChange={ ( nextValues ) => {
setAttributes( { values: nextValues } );
} }
value={ values }
focus={ focus }
onFocus={ setFocus }
showAlignments
className="blocks-list" />
);
},
save( { attributes } ) {
const { nodeName = 'OL', values = [] } = attributes;
return wp.element.createElement(
nodeName.toLowerCase(),
null,
values
);
},
} );