UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 7.24 kB
{ "version": 3, "sources": ["../../../src/components/rich-text/multiline.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { forwardRef } from '@wordpress/element';\nimport deprecated from '@wordpress/deprecated';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { ENTER } from '@wordpress/keycodes';\nimport { create, split, toHTMLString } from '@wordpress/rich-text';\n\n/**\n * Internal dependencies\n */\nimport { RichTextWrapper } from './';\nimport { store as blockEditorStore } from '../../store';\nimport { useBlockEditContext } from '../block-edit';\nimport { getMultilineTag } from './utils';\n\nfunction RichTextMultiline(\n\t{\n\t\tchildren,\n\t\tidentifier,\n\t\ttagName: TagName = 'div',\n\t\tvalue = '',\n\t\tonChange,\n\t\tmultiline,\n\t\t...props\n\t},\n\tforwardedRef\n) {\n\tdeprecated( 'wp.blockEditor.RichText multiline prop', {\n\t\tsince: '6.1',\n\t\tversion: '6.3',\n\t\talternative: 'nested blocks (InnerBlocks)',\n\t\tlink: 'https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/nested-blocks-inner-blocks/',\n\t} );\n\n\tconst { clientId } = useBlockEditContext();\n\tconst { getSelectionStart, getSelectionEnd } =\n\t\tuseSelect( blockEditorStore );\n\tconst { selectionChange } = useDispatch( blockEditorStore );\n\n\tconst multilineTagName = getMultilineTag( multiline );\n\tvalue = value || `<${ multilineTagName }></${ multilineTagName }>`;\n\tconst padded = `</${ multilineTagName }>${ value }<${ multilineTagName }>`;\n\tconst values = padded.split(\n\t\t`</${ multilineTagName }><${ multilineTagName }>`\n\t);\n\n\tvalues.shift();\n\tvalues.pop();\n\n\tfunction _onChange( newValues ) {\n\t\tonChange(\n\t\t\t`<${ multilineTagName }>${ newValues.join(\n\t\t\t\t`</${ multilineTagName }><${ multilineTagName }>`\n\t\t\t) }</${ multilineTagName }>`\n\t\t);\n\t}\n\n\treturn (\n\t\t<TagName ref={ forwardedRef }>\n\t\t\t{ values.map( ( _value, index ) => {\n\t\t\t\treturn (\n\t\t\t\t\t<RichTextWrapper\n\t\t\t\t\t\tkey={ index }\n\t\t\t\t\t\tidentifier={ `${ identifier }-${ index }` }\n\t\t\t\t\t\ttagName={ multilineTagName }\n\t\t\t\t\t\tvalue={ _value }\n\t\t\t\t\t\tonChange={ ( newValue ) => {\n\t\t\t\t\t\t\tconst newValues = values.slice();\n\t\t\t\t\t\t\tnewValues[ index ] = newValue;\n\t\t\t\t\t\t\t_onChange( newValues );\n\t\t\t\t\t\t} }\n\t\t\t\t\t\tisSelected={ undefined }\n\t\t\t\t\t\tonKeyDown={ ( event ) => {\n\t\t\t\t\t\t\tif ( event.keyCode !== ENTER ) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tevent.preventDefault();\n\n\t\t\t\t\t\t\tconst { offset: start } = getSelectionStart();\n\t\t\t\t\t\t\tconst { offset: end } = getSelectionEnd();\n\n\t\t\t\t\t\t\t// Cannot split if there is no selection.\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\ttypeof start !== 'number' ||\n\t\t\t\t\t\t\t\ttypeof end !== 'number'\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst richTextValue = create( { html: _value } );\n\t\t\t\t\t\t\trichTextValue.start = start;\n\t\t\t\t\t\t\trichTextValue.end = end;\n\n\t\t\t\t\t\t\tconst array = split( richTextValue ).map( ( v ) =>\n\t\t\t\t\t\t\t\ttoHTMLString( { value: v } )\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\tconst newValues = values.slice();\n\t\t\t\t\t\t\tnewValues.splice( index, 1, ...array );\n\t\t\t\t\t\t\t_onChange( newValues );\n\t\t\t\t\t\t\tselectionChange(\n\t\t\t\t\t\t\t\tclientId,\n\t\t\t\t\t\t\t\t`${ identifier }-${ index + 1 }`,\n\t\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\t\t0\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} }\n\t\t\t\t\t\tonMerge={ ( forward ) => {\n\t\t\t\t\t\t\tconst newValues = values.slice();\n\t\t\t\t\t\t\tlet offset = 0;\n\t\t\t\t\t\t\tif ( forward ) {\n\t\t\t\t\t\t\t\tif ( ! newValues[ index + 1 ] ) {\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tnewValues.splice(\n\t\t\t\t\t\t\t\t\tindex,\n\t\t\t\t\t\t\t\t\t2,\n\t\t\t\t\t\t\t\t\tnewValues[ index ] + newValues[ index + 1 ]\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\toffset = newValues[ index ].length - 1;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tif ( ! newValues[ index - 1 ] ) {\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tnewValues.splice(\n\t\t\t\t\t\t\t\t\tindex - 1,\n\t\t\t\t\t\t\t\t\t2,\n\t\t\t\t\t\t\t\t\tnewValues[ index - 1 ] + newValues[ index ]\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\toffset = newValues[ index - 1 ].length - 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t_onChange( newValues );\n\t\t\t\t\t\t\tselectionChange(\n\t\t\t\t\t\t\t\tclientId,\n\t\t\t\t\t\t\t\t`${ identifier }-${\n\t\t\t\t\t\t\t\t\tindex - ( forward ? 0 : 1 )\n\t\t\t\t\t\t\t\t}`,\n\t\t\t\t\t\t\t\toffset,\n\t\t\t\t\t\t\t\toffset\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} }\n\t\t\t\t\t\t{ ...props }\n\t\t\t\t\t/>\n\t\t\t\t);\n\t\t\t} ) }\n\t\t</TagName>\n\t);\n}\n\nexport default forwardRef( RichTextMultiline );\n"], "mappings": ";;;;;;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