UNPKG

@wordpress/block-library

Version:
283 lines (269 loc) 8.19 kB
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread"; import { createElement } from "@wordpress/element"; /** * External dependencies */ import { omit } from 'lodash'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { createBlock, getPhrasingContentSchema, getBlockAttributes } from '@wordpress/blocks'; import { RichText } from '@wordpress/block-editor'; import { replace, join, split, create, toHTMLString, LINE_SEPARATOR } from '@wordpress/rich-text'; import { G, Path, SVG } from '@wordpress/components'; var listContentSchema = _objectSpread({}, getPhrasingContentSchema(), { ul: {}, ol: { attributes: ['type'] } }); // Recursion is needed. // Possible: ul > li > ul. // Impossible: ul > ul. ['ul', 'ol'].forEach(function (tag) { listContentSchema[tag].children = { li: { children: listContentSchema } }; }); var supports = { className: false }; var schema = { ordered: { type: 'boolean', default: false }, values: { type: 'string', source: 'html', selector: 'ol,ul', multiline: 'li', default: '' } }; export var name = 'core/list'; export var settings = { title: __('List'), description: __('Create a bulleted or numbered list.'), icon: createElement(SVG, { viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/svg" }, createElement(G, null, createElement(Path, { 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" }))), category: 'common', keywords: [__('bullet list'), __('ordered list'), __('numbered list')], attributes: schema, supports: supports, transforms: { from: [{ type: 'block', isMultiBlock: true, blocks: ['core/paragraph'], transform: function transform(blockAttributes) { return createBlock('core/list', { values: toHTMLString({ value: join(blockAttributes.map(function (_ref) { var content = _ref.content; var value = create({ html: content }); if (blockAttributes.length > 1) { return value; } // When converting only one block, transform // every line to a list item. return replace(value, /\n/g, LINE_SEPARATOR); }), LINE_SEPARATOR), multilineTag: 'li' }) }); } }, { type: 'block', blocks: ['core/quote'], transform: function transform(_ref2) { var value = _ref2.value; return createBlock('core/list', { values: toHTMLString({ value: create({ html: value, multilineTag: 'p' }), multilineTag: 'li' }) }); } }, { type: 'raw', selector: 'ol,ul', schema: { ol: listContentSchema.ol, ul: listContentSchema.ul }, transform: function transform(node) { return createBlock('core/list', _objectSpread({}, getBlockAttributes('core/list', node.outerHTML), { ordered: node.nodeName === 'OL' })); } }].concat(_toConsumableArray(['*', '-'].map(function (prefix) { return { type: 'prefix', prefix: prefix, transform: function transform(content) { return createBlock('core/list', { values: "<li>".concat(content, "</li>") }); } }; })), _toConsumableArray(['1.', '1)'].map(function (prefix) { return { type: 'prefix', prefix: prefix, transform: function transform(content) { return createBlock('core/list', { ordered: true, values: "<li>".concat(content, "</li>") }); } }; }))), to: [{ type: 'block', blocks: ['core/paragraph'], transform: function transform(_ref3) { var values = _ref3.values; return split(create({ html: values, multilineTag: 'li', multilineWrapperTags: ['ul', 'ol'] }), LINE_SEPARATOR).map(function (piece) { return createBlock('core/paragraph', { content: toHTMLString({ value: piece }) }); }); } }, { type: 'block', blocks: ['core/quote'], transform: function transform(_ref4) { var values = _ref4.values; return createBlock('core/quote', { value: toHTMLString({ value: create({ html: values, multilineTag: 'li', multilineWrapperTags: ['ul', 'ol'] }), multilineTag: 'p' }) }); } }] }, deprecated: [{ supports: supports, attributes: _objectSpread({}, omit(schema, ['ordered']), { nodeName: { type: 'string', source: 'property', selector: 'ol,ul', property: 'nodeName', default: 'UL' } }), migrate: function migrate(attributes) { var nodeName = attributes.nodeName, migratedAttributes = _objectWithoutProperties(attributes, ["nodeName"]); return _objectSpread({}, migratedAttributes, { ordered: 'OL' === nodeName }); }, save: function save(_ref5) { var attributes = _ref5.attributes; var nodeName = attributes.nodeName, values = attributes.values; return createElement(RichText.Content, { tagName: nodeName.toLowerCase(), value: values }); } }], merge: function merge(attributes, attributesToMerge) { var values = attributesToMerge.values; if (!values || values === '<li></li>') { return attributes; } return _objectSpread({}, attributes, { values: attributes.values + values }); }, edit: function edit(_ref6) { var attributes = _ref6.attributes, insertBlocksAfter = _ref6.insertBlocksAfter, setAttributes = _ref6.setAttributes, mergeBlocks = _ref6.mergeBlocks, onReplace = _ref6.onReplace, className = _ref6.className; var ordered = attributes.ordered, values = attributes.values; return createElement(RichText, { identifier: "values", multiline: "li", tagName: ordered ? 'ol' : 'ul', onChange: function onChange(nextValues) { return setAttributes({ values: nextValues }); }, value: values, wrapperClassName: "block-library-list", className: className, placeholder: __('Write list…'), onMerge: mergeBlocks, unstableOnSplit: insertBlocksAfter ? function (before, after) { for (var _len = arguments.length, blocks = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { blocks[_key - 2] = arguments[_key]; } if (!blocks.length) { blocks.push(createBlock('core/paragraph')); } if (after !== '<li></li>') { blocks.push(createBlock('core/list', { ordered: ordered, values: after })); } setAttributes({ values: before }); insertBlocksAfter(blocks); } : undefined, onRemove: function onRemove() { return onReplace([]); }, onTagNameChange: function onTagNameChange(tag) { return setAttributes({ ordered: tag === 'ol' }); } }); }, save: function save(_ref7) { var attributes = _ref7.attributes; var ordered = attributes.ordered, values = attributes.values; var tagName = ordered ? 'ol' : 'ul'; return createElement(RichText.Content, { tagName: tagName, value: values, multiline: "li" }); } }; //# sourceMappingURL=index.js.map