UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 13 kB
{ "version": 3, "sources": ["../../src/post-excerpt/edit.js"], "sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\n\n/**\n * WordPress dependencies\n */\nimport { useEntityProp, store as coreStore } from '@wordpress/core-data';\nimport { useMemo } from '@wordpress/element';\nimport {\n\tAlignmentToolbar,\n\tBlockControls,\n\tInspectorControls,\n\tRichText,\n\tWarning,\n\tuseBlockProps,\n\tuseBlockEditingMode,\n} from '@wordpress/block-editor';\nimport {\n\tToggleControl,\n\tRangeControl,\n\t__experimentalToolsPanel as ToolsPanel,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\nimport { __, _x } from '@wordpress/i18n';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport {\n\tuseCanEditEntity,\n\tuseToolsPanelDropdownMenuProps,\n} from '../utils/hooks';\n\nconst ELLIPSIS = '\u2026';\n\nexport default function PostExcerptEditor( {\n\tattributes: { textAlign, moreText, showMoreOnNewLine, excerptLength },\n\tsetAttributes,\n\tisSelected,\n\tcontext: { postId, postType, queryId },\n} ) {\n\tconst blockEditingMode = useBlockEditingMode();\n\tconst showControls = blockEditingMode === 'default';\n\tconst isDescendentOfQueryLoop = Number.isFinite( queryId );\n\tconst userCanEdit = useCanEditEntity( 'postType', postType, postId );\n\tconst [\n\t\trawExcerpt,\n\t\tsetExcerpt,\n\t\t{ rendered: renderedExcerpt, protected: isProtected } = {},\n\t] = useEntityProp( 'postType', postType, 'excerpt', postId );\n\n\tconst dropdownMenuProps = useToolsPanelDropdownMenuProps();\n\n\t/**\n\t * Check if the post type supports excerpts.\n\t * Add an exception and return early for the \"page\" post type,\n\t * which is registered without support for the excerpt UI,\n\t * but supports saving the excerpt to the database.\n\t * See: https://core.trac.wordpress.org/browser/branches/6.1/src/wp-includes/post.php#L65\n\t * Without this exception, users that have excerpts saved to the database will\n\t * not be able to edit the excerpts.\n\t */\n\tconst postTypeSupportsExcerpts = useSelect(\n\t\t( select ) => {\n\t\t\tif ( postType === 'page' ) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn !! select( coreStore ).getPostType( postType )?.supports\n\t\t\t\t?.excerpt;\n\t\t},\n\t\t[ postType ]\n\t);\n\n\t/**\n\t * The excerpt is editable if:\n\t * - The user can edit the post\n\t * - It is not a descendent of a Query Loop block\n\t * - The post type supports excerpts\n\t */\n\tconst isEditable =\n\t\tuserCanEdit && ! isDescendentOfQueryLoop && postTypeSupportsExcerpts;\n\n\tconst blockProps = useBlockProps( {\n\t\tclassName: clsx( {\n\t\t\t[ `has-text-align-${ textAlign }` ]: textAlign,\n\t\t} ),\n\t} );\n\n\t/**\n\t * translators: If your word count is based on single characters (e.g. East Asian characters),\n\t * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.\n\t * Do not translate into your own language.\n\t */\n\tconst wordCountType = _x( 'words', 'Word count type. Do not translate!' );\n\n\t/**\n\t * When excerpt is editable, strip the html tags from\n\t * rendered excerpt. This will be used if the entity's\n\t * excerpt has been produced from the content.\n\t */\n\tconst strippedRenderedExcerpt = useMemo( () => {\n\t\tif ( ! renderedExcerpt ) {\n\t\t\treturn '';\n\t\t}\n\t\tconst document = new window.DOMParser().parseFromString(\n\t\t\trenderedExcerpt,\n\t\t\t'text/html'\n\t\t);\n\t\treturn document.body.textContent || document.body.innerText || '';\n\t}, [ renderedExcerpt ] );\n\n\tif ( ! postType || ! postId ) {\n\t\treturn (\n\t\t\t<>\n\t\t\t\t<BlockControls>\n\t\t\t\t\t<AlignmentToolbar\n\t\t\t\t\t\tvalue={ textAlign }\n\t\t\t\t\t\tonChange={ ( newAlign ) =>\n\t\t\t\t\t\t\tsetAttributes( { textAlign: newAlign } )\n\t\t\t\t\t\t}\n\t\t\t\t\t/>\n\t\t\t\t</BlockControls>\n\t\t\t\t<div { ...blockProps }>\n\t\t\t\t\t<p>{ __( 'This block will display the excerpt.' ) }</p>\n\t\t\t\t</div>\n\t\t\t</>\n\t\t);\n\t}\n\tif ( isProtected && ! userCanEdit ) {\n\t\treturn (\n\t\t\t<div { ...blockProps }>\n\t\t\t\t<Warning>\n\t\t\t\t\t{ __(\n\t\t\t\t\t\t'The content is currently protected and does not have the available excerpt.'\n\t\t\t\t\t) }\n\t\t\t\t</Warning>\n\t\t\t</div>\n\t\t);\n\t}\n\tconst readMoreLink = (\n\t\t<RichText\n\t\t\tidentifier=\"moreText\"\n\t\t\tclassName=\"wp-block-post-excerpt__more-link\"\n\t\t\ttagName=\"a\"\n\t\t\taria-label={ __( '\u201CRead more\u201D link text' ) }\n\t\t\tplaceholder={ __( 'Add \"read more\" link text' ) }\n\t\t\tvalue={ moreText }\n\t\t\tonChange={ ( newMoreText ) =>\n\t\t\t\tsetAttributes( { moreText: newMoreText } )\n\t\t\t}\n\t\t\twithoutInteractiveFormatting\n\t\t/>\n\t);\n\tconst excerptClassName = clsx( 'wp-block-post-excerpt__excerpt', {\n\t\t'is-inline': ! showMoreOnNewLine,\n\t} );\n\n\t/**\n\t * The excerpt length setting needs to be applied to both\n\t * the raw and the rendered excerpt depending on which is being used.\n\t */\n\tconst rawOrRenderedExcerpt = (\n\t\trawExcerpt || strippedRenderedExcerpt\n\t).trim();\n\n\tlet trimmedExcerpt = '';\n\tif ( wordCountType === 'words' ) {\n\t\ttrimmedExcerpt = rawOrRenderedExcerpt\n\t\t\t.split( /\\s+/, excerptLength )\n\t\t\t.join( ' ' );\n\t} else if ( wordCountType === 'characters_excluding_spaces' ) {\n\t\t/*\n\t\t * 1. Split the excerpt at the character limit,\n\t\t * then join the substrings back into one string.\n\t\t * 2. Count the number of spaces in the excerpt\n\t\t * by comparing the lengths of the string with and without spaces.\n\t\t * 3. Add the number to the length of the visible excerpt,\n\t\t * so that the spaces are excluded from the word count.\n\t\t */\n\t\tconst excerptWithSpaces = rawOrRenderedExcerpt\n\t\t\t.split( '', excerptLength )\n\t\t\t.join( '' );\n\n\t\tconst numberOfSpaces =\n\t\t\texcerptWithSpaces.length -\n\t\t\texcerptWithSpaces.replaceAll( ' ', '' ).length;\n\n\t\ttrimmedExcerpt = rawOrRenderedExcerpt\n\t\t\t.split( '', excerptLength + numberOfSpaces )\n\t\t\t.join( '' );\n\t} else if ( wordCountType === 'characters_including_spaces' ) {\n\t\ttrimmedExcerpt = rawOrRenderedExcerpt\n\t\t\t.split( '', excerptLength )\n\t\t\t.join( '' );\n\t}\n\n\tconst isTrimmed = trimmedExcerpt !== rawOrRenderedExcerpt;\n\n\tconst excerptContent = isEditable ? (\n\t\t<RichText\n\t\t\tclassName={ excerptClassName }\n\t\t\taria-label={ __( 'Excerpt text' ) }\n\t\t\tvalue={\n\t\t\t\tisSelected\n\t\t\t\t\t? rawOrRenderedExcerpt\n\t\t\t\t\t: ( ! isTrimmed\n\t\t\t\t\t\t\t? rawOrRenderedExcerpt\n\t\t\t\t\t\t\t: trimmedExcerpt + ELLIPSIS ) ||\n\t\t\t\t\t __( 'No excerpt found' )\n\t\t\t}\n\t\t\tonChange={ setExcerpt }\n\t\t\ttagName=\"p\"\n\t\t/>\n\t) : (\n\t\t<p className={ excerptClassName }>\n\t\t\t{ ! isTrimmed\n\t\t\t\t? rawOrRenderedExcerpt || __( 'No excerpt found' )\n\t\t\t\t: trimmedExcerpt + ELLIPSIS }\n\t\t</p>\n\t);\n\treturn (\n\t\t<>\n\t\t\t{ showControls && (\n\t\t\t\t<BlockControls>\n\t\t\t\t\t<AlignmentToolbar\n\t\t\t\t\t\tvalue={ textAlign }\n\t\t\t\t\t\tonChange={ ( newAlign ) =>\n\t\t\t\t\t\t\tsetAttributes( { textAlign: newAlign } )\n\t\t\t\t\t\t}\n\t\t\t\t\t/>\n\t\t\t\t</BlockControls>\n\t\t\t) }\n\t\t\t<InspectorControls>\n\t\t\t\t<ToolsPanel\n\t\t\t\t\tlabel={ __( 'Settings' ) }\n\t\t\t\t\tresetAll={ () => {\n\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\tshowMoreOnNewLine: true,\n\t\t\t\t\t\t\texcerptLength: 55,\n\t\t\t\t\t\t} );\n\t\t\t\t\t} }\n\t\t\t\t\tdropdownMenuProps={ dropdownMenuProps }\n\t\t\t\t>\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\thasValue={ () => showMoreOnNewLine !== true }\n\t\t\t\t\t\tlabel={ __( 'Show link on new line' ) }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( { showMoreOnNewLine: true } )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t>\n\t\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\t\tlabel={ __( 'Show link on new line' ) }\n\t\t\t\t\t\t\tchecked={ showMoreOnNewLine }\n\t\t\t\t\t\t\tonChange={ ( newShowMoreOnNewLine ) =>\n\t\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\t\tshowMoreOnNewLine: newShowMoreOnNewLine,\n\t\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\thasValue={ () => excerptLength !== 55 }\n\t\t\t\t\t\tlabel={ __( 'Max number of words' ) }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( { excerptLength: 55 } )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t>\n\t\t\t\t\t\t<RangeControl\n\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\tlabel={ __( 'Max number of words' ) }\n\t\t\t\t\t\t\tvalue={ excerptLength }\n\t\t\t\t\t\t\tonChange={ ( value ) => {\n\t\t\t\t\t\t\t\tsetAttributes( { excerptLength: value } );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\tmin=\"10\"\n\t\t\t\t\t\t\tmax=\"100\"\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\t\t\t\t</ToolsPanel>\n\t\t\t</InspectorControls>\n\t\t\t<div { ...blockProps }>\n\t\t\t\t{ excerptContent }\n\t\t\t\t{ ! showMoreOnNewLine && ' ' }\n\t\t\t\t{ showMoreOnNewLine ? (\n\t\t\t\t\t<p className=\"wp-block-post-excerpt__more-text\">\n\t\t\t\t\t\t{ readMoreLink }\n\t\t\t\t\t</p>\n\t\t\t\t) : (\n\t\t\t\t\treadMoreLink\n\t\t\t\t) }\n\t\t\t</div>\n\t\t</>\n\t);\n}\n"], "mappings": ";AAGA,OAAO,UAAU;AAKjB,SAAS,eAAe,SAAS,iBAAiB;AAClD,SAAS,eAAe;AACxB;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP;AAAA,EACC;AAAA,EACA;AAAA,EACA,4BAA4B;AAAA,EAC5B,gCAAgC;AAAA,OAC1B;AACP,SAAS,IAAI,UAAU;AACvB,SAAS,iBAAiB;AAK1B;AAAA,EACC;AAAA,EACA;AAAA,OACM;AAkFJ,mBAEE,KAFF;AAhFH,IAAM,WAAW;AAEF,SAAR,kBAAoC;AAAA,EAC1C,YAAY,EAAE,WAAW,UAAU,mBAAmB,cAAc;AAAA,EACpE;AAAA,EACA;AAAA,EACA,SAAS,EAAE,QAAQ,UAAU,QAAQ;AACtC,GAAI;AACH,QAAM,mBAAmB,oBAAoB;AAC7C,QAAM,eAAe,qBAAqB;AAC1C,QAAM,0BAA0B,OAAO,SAAU,OAAQ;AACzD,QAAM,cAAc,iBAAkB,YAAY,UAAU,MAAO;AACnE,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA,EAAE,UAAU,iBAAiB,WAAW,YAAY,IAAI,CAAC;AAAA,EAC1D,IAAI,cAAe,YAAY,UAAU,WAAW,MAAO;AAE3D,QAAM,oBAAoB,+BAA+B;AAWzD,QAAM,2BA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