UNPKG

8.19 kBJavaScriptView Raw
1import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
3import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
4import { createElement } from "@wordpress/element";
5
6/**
7 * External dependencies
8 */
9import { omit } from 'lodash';
10/**
11 * WordPress dependencies
12 */
13
14import { __ } from '@wordpress/i18n';
15import { createBlock, getPhrasingContentSchema, getBlockAttributes } from '@wordpress/blocks';
16import { RichText } from '@wordpress/block-editor';
17import { replace, join, split, create, toHTMLString, LINE_SEPARATOR } from '@wordpress/rich-text';
18import { G, Path, SVG } from '@wordpress/components';
19
20var listContentSchema = _objectSpread({}, getPhrasingContentSchema(), {
21 ul: {},
22 ol: {
23 attributes: ['type']
24 }
25}); // Recursion is needed.
26// Possible: ul > li > ul.
27// Impossible: ul > ul.
28
29
30['ul', 'ol'].forEach(function (tag) {
31 listContentSchema[tag].children = {
32 li: {
33 children: listContentSchema
34 }
35 };
36});
37var supports = {
38 className: false
39};
40var schema = {
41 ordered: {
42 type: 'boolean',
43 default: false
44 },
45 values: {
46 type: 'string',
47 source: 'html',
48 selector: 'ol,ul',
49 multiline: 'li',
50 default: ''
51 }
52};
53export var name = 'core/list';
54export var settings = {
55 title: __('List'),
56 description: __('Create a bulleted or numbered list.'),
57 icon: createElement(SVG, {
58 viewBox: "0 0 24 24",
59 xmlns: "http://www.w3.org/2000/svg"
60 }, createElement(G, null, createElement(Path, {
61 d: "M9 19h12v-2H9v2zm0-6h12v-2H9v2zm0-8v2h12V5H9zm-4-.5c-.828 0-1.5.672-1.5 1.5S4.172 7.5 5 7.5 6.5 6.828 6.5 6 5.828 4.5 5 4.5zm0 6c-.828 0-1.5.672-1.5 1.5s.672 1.5 1.5 1.5 1.5-.672 1.5-1.5-.672-1.5-1.5-1.5zm0 6c-.828 0-1.5.672-1.5 1.5s.672 1.5 1.5 1.5 1.5-.672 1.5-1.5-.672-1.5-1.5-1.5z"
62 }))),
63 category: 'common',
64 keywords: [__('bullet list'), __('ordered list'), __('numbered list')],
65 attributes: schema,
66 supports: supports,
67 transforms: {
68 from: [{
69 type: 'block',
70 isMultiBlock: true,
71 blocks: ['core/paragraph'],
72 transform: function transform(blockAttributes) {
73 return createBlock('core/list', {
74 values: toHTMLString({
75 value: join(blockAttributes.map(function (_ref) {
76 var content = _ref.content;
77 var value = create({
78 html: content
79 });
80
81 if (blockAttributes.length > 1) {
82 return value;
83 } // When converting only one block, transform
84 // every line to a list item.
85
86
87 return replace(value, /\n/g, LINE_SEPARATOR);
88 }), LINE_SEPARATOR),
89 multilineTag: 'li'
90 })
91 });
92 }
93 }, {
94 type: 'block',
95 blocks: ['core/quote'],
96 transform: function transform(_ref2) {
97 var value = _ref2.value;
98 return createBlock('core/list', {
99 values: toHTMLString({
100 value: create({
101 html: value,
102 multilineTag: 'p'
103 }),
104 multilineTag: 'li'
105 })
106 });
107 }
108 }, {
109 type: 'raw',
110 selector: 'ol,ul',
111 schema: {
112 ol: listContentSchema.ol,
113 ul: listContentSchema.ul
114 },
115 transform: function transform(node) {
116 return createBlock('core/list', _objectSpread({}, getBlockAttributes('core/list', node.outerHTML), {
117 ordered: node.nodeName === 'OL'
118 }));
119 }
120 }].concat(_toConsumableArray(['*', '-'].map(function (prefix) {
121 return {
122 type: 'prefix',
123 prefix: prefix,
124 transform: function transform(content) {
125 return createBlock('core/list', {
126 values: "<li>".concat(content, "</li>")
127 });
128 }
129 };
130 })), _toConsumableArray(['1.', '1)'].map(function (prefix) {
131 return {
132 type: 'prefix',
133 prefix: prefix,
134 transform: function transform(content) {
135 return createBlock('core/list', {
136 ordered: true,
137 values: "<li>".concat(content, "</li>")
138 });
139 }
140 };
141 }))),
142 to: [{
143 type: 'block',
144 blocks: ['core/paragraph'],
145 transform: function transform(_ref3) {
146 var values = _ref3.values;
147 return split(create({
148 html: values,
149 multilineTag: 'li',
150 multilineWrapperTags: ['ul', 'ol']
151 }), LINE_SEPARATOR).map(function (piece) {
152 return createBlock('core/paragraph', {
153 content: toHTMLString({
154 value: piece
155 })
156 });
157 });
158 }
159 }, {
160 type: 'block',
161 blocks: ['core/quote'],
162 transform: function transform(_ref4) {
163 var values = _ref4.values;
164 return createBlock('core/quote', {
165 value: toHTMLString({
166 value: create({
167 html: values,
168 multilineTag: 'li',
169 multilineWrapperTags: ['ul', 'ol']
170 }),
171 multilineTag: 'p'
172 })
173 });
174 }
175 }]
176 },
177 deprecated: [{
178 supports: supports,
179 attributes: _objectSpread({}, omit(schema, ['ordered']), {
180 nodeName: {
181 type: 'string',
182 source: 'property',
183 selector: 'ol,ul',
184 property: 'nodeName',
185 default: 'UL'
186 }
187 }),
188 migrate: function migrate(attributes) {
189 var nodeName = attributes.nodeName,
190 migratedAttributes = _objectWithoutProperties(attributes, ["nodeName"]);
191
192 return _objectSpread({}, migratedAttributes, {
193 ordered: 'OL' === nodeName
194 });
195 },
196 save: function save(_ref5) {
197 var attributes = _ref5.attributes;
198 var nodeName = attributes.nodeName,
199 values = attributes.values;
200 return createElement(RichText.Content, {
201 tagName: nodeName.toLowerCase(),
202 value: values
203 });
204 }
205 }],
206 merge: function merge(attributes, attributesToMerge) {
207 var values = attributesToMerge.values;
208
209 if (!values || values === '<li></li>') {
210 return attributes;
211 }
212
213 return _objectSpread({}, attributes, {
214 values: attributes.values + values
215 });
216 },
217 edit: function edit(_ref6) {
218 var attributes = _ref6.attributes,
219 insertBlocksAfter = _ref6.insertBlocksAfter,
220 setAttributes = _ref6.setAttributes,
221 mergeBlocks = _ref6.mergeBlocks,
222 onReplace = _ref6.onReplace,
223 className = _ref6.className;
224 var ordered = attributes.ordered,
225 values = attributes.values;
226 return createElement(RichText, {
227 identifier: "values",
228 multiline: "li",
229 tagName: ordered ? 'ol' : 'ul',
230 onChange: function onChange(nextValues) {
231 return setAttributes({
232 values: nextValues
233 });
234 },
235 value: values,
236 wrapperClassName: "block-library-list",
237 className: className,
238 placeholder: __('Write list…'),
239 onMerge: mergeBlocks,
240 unstableOnSplit: insertBlocksAfter ? function (before, after) {
241 for (var _len = arguments.length, blocks = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
242 blocks[_key - 2] = arguments[_key];
243 }
244
245 if (!blocks.length) {
246 blocks.push(createBlock('core/paragraph'));
247 }
248
249 if (after !== '<li></li>') {
250 blocks.push(createBlock('core/list', {
251 ordered: ordered,
252 values: after
253 }));
254 }
255
256 setAttributes({
257 values: before
258 });
259 insertBlocksAfter(blocks);
260 } : undefined,
261 onRemove: function onRemove() {
262 return onReplace([]);
263 },
264 onTagNameChange: function onTagNameChange(tag) {
265 return setAttributes({
266 ordered: tag === 'ol'
267 });
268 }
269 });
270 },
271 save: function save(_ref7) {
272 var attributes = _ref7.attributes;
273 var ordered = attributes.ordered,
274 values = attributes.values;
275 var tagName = ordered ? 'ol' : 'ul';
276 return createElement(RichText.Content, {
277 tagName: tagName,
278 value: values,
279 multiline: "li"
280 });
281 }
282};
283//# sourceMappingURL=index.js.map
\No newline at end of file