@wordpress/block-editor
Version:
8 lines (7 loc) • 8.54 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/link-control/link-preview.js"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\n\n/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport {\n\tButton,\n\tExternalLink,\n\t__experimentalTruncate as Truncate,\n\tFlex,\n} from '@wordpress/components';\nimport { useCopyToClipboard } from '@wordpress/compose';\nimport { filterURLForDisplay, safeDecodeURI } from '@wordpress/url';\nimport {\n\tIcon,\n\tglobe,\n\tinfo,\n\tlinkOff,\n\tpencil,\n\tcopySmall,\n} from '@wordpress/icons';\nimport { __unstableStripHTML as stripHTML } from '@wordpress/dom';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { store as preferencesStore } from '@wordpress/preferences';\n\n/**\n * Internal dependencies\n */\nimport { ViewerSlot } from './viewer-slot';\n\nimport useRichUrlData from './use-rich-url-data';\n\n/**\n * Filters the title for display. Removes the protocol and www prefix.\n *\n * @param {string} title The title to be filtered.\n *\n * @return {string} The filtered title.\n */\nfunction filterTitleForDisplay( title ) {\n\t// Derived from `filterURLForDisplay` in `@wordpress/url`.\n\treturn title\n\t\t.replace( /^[a-z\\-.\\+]+[0-9]*:(\\/\\/)?/i, '' )\n\t\t.replace( /^www\\./i, '' );\n}\n\nexport default function LinkPreview( {\n\tvalue,\n\tonEditClick,\n\thasRichPreviews = false,\n\thasUnlinkControl = false,\n\tonRemove,\n} ) {\n\tconst showIconLabels = useSelect(\n\t\t( select ) =>\n\t\t\tselect( preferencesStore ).get( 'core', 'showIconLabels' ),\n\t\t[]\n\t);\n\n\t// Avoid fetching if rich previews are not desired.\n\tconst showRichPreviews = hasRichPreviews ? value?.url : null;\n\n\tconst { richData, isFetching } = useRichUrlData( showRichPreviews );\n\n\t// Rich data may be an empty object so test for that.\n\tconst hasRichData = richData && Object.keys( richData ).length;\n\n\tconst displayURL =\n\t\t( value && filterURLForDisplay( safeDecodeURI( value.url ), 24 ) ) ||\n\t\t'';\n\n\t// url can be undefined if the href attribute is unset\n\tconst isEmptyURL = ! value?.url?.length;\n\n\tconst displayTitle =\n\t\t! isEmptyURL &&\n\t\tstripHTML( richData?.title || value?.title || displayURL );\n\n\tconst isUrlRedundant =\n\t\t! value?.url || filterTitleForDisplay( displayTitle ) === displayURL;\n\n\tlet icon;\n\n\tif ( richData?.icon ) {\n\t\ticon = <img src={ richData?.icon } alt=\"\" />;\n\t} else if ( isEmptyURL ) {\n\t\ticon = <Icon icon={ info } size={ 32 } />;\n\t} else {\n\t\ticon = <Icon icon={ globe } />;\n\t}\n\n\tconst { createNotice } = useDispatch( noticesStore );\n\tconst ref = useCopyToClipboard( value.url, () => {\n\t\tcreateNotice( 'info', __( 'Link copied to clipboard.' ), {\n\t\t\tisDismissible: true,\n\t\t\ttype: 'snackbar',\n\t\t} );\n\t} );\n\n\treturn (\n\t\t<Flex\n\t\t\trole=\"group\"\n\t\t\taria-label={ __( 'Manage link' ) }\n\t\t\tclassName={ clsx( 'block-editor-link-control__preview', {\n\t\t\t\t'is-current': true,\n\t\t\t\t'is-rich': hasRichData,\n\t\t\t\t'is-fetching': !! isFetching,\n\t\t\t\t'is-preview': true,\n\t\t\t\t'is-error': isEmptyURL,\n\t\t\t\t'is-url-title': displayTitle === displayURL,\n\t\t\t} ) }\n\t\t>\n\t\t\t<Flex gap={ 0 }>\n\t\t\t\t<Flex\n\t\t\t\t\tclassName=\"block-editor-link-control__link-information\"\n\t\t\t\t\trole=\"figure\"\n\t\t\t\t\taria-label={\n\t\t\t\t\t\t/* translators: Accessibility text for the link preview when editing a link. */\n\t\t\t\t\t\t__( 'Link information' )\n\t\t\t\t\t}\n\t\t\t\t\tjustify=\"start\"\n\t\t\t\t>\n\t\t\t\t\t<Flex\n\t\t\t\t\t\tclassName={ clsx(\n\t\t\t\t\t\t\t'block-editor-link-control__preview-icon',\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t'is-image': richData?.icon,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t) }\n\t\t\t\t\t\tjustify=\"center\"\n\t\t\t\t\t>\n\t\t\t\t\t\t{ icon }\n\t\t\t\t\t</Flex>\n\t\t\t\t\t<Flex\n\t\t\t\t\t\tclassName=\"block-editor-link-control__preview-details\"\n\t\t\t\t\t\tdirection=\"column\"\n\t\t\t\t\t\tgap={ 1 }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ ! isEmptyURL ? (\n\t\t\t\t\t\t\t<>\n\t\t\t\t\t\t\t\t<ExternalLink\n\t\t\t\t\t\t\t\t\tclassName=\"block-editor-link-control__preview-title\"\n\t\t\t\t\t\t\t\t\thref={ value.url }\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t<Truncate numberOfLines={ 1 }>\n\t\t\t\t\t\t\t\t\t\t{ displayTitle }\n\t\t\t\t\t\t\t\t\t</Truncate>\n\t\t\t\t\t\t\t\t</ExternalLink>\n\t\t\t\t\t\t\t\t{ ! isUrlRedundant && (\n\t\t\t\t\t\t\t\t\t<span className=\"block-editor-link-control__preview-info\">\n\t\t\t\t\t\t\t\t\t\t<Truncate numberOfLines={ 1 }>\n\t\t\t\t\t\t\t\t\t\t\t{ displayURL }\n\t\t\t\t\t\t\t\t\t\t</Truncate>\n\t\t\t\t\t\t\t\t\t</span>\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\t\t<span className=\"block-editor-link-control__preview-error-notice\">\n\t\t\t\t\t\t\t\t{ __( 'Link is empty' ) }\n\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t) }\n\t\t\t\t\t</Flex>\n\t\t\t\t</Flex>\n\t\t\t\t<Button\n\t\t\t\t\ticon={ pencil }\n\t\t\t\t\tlabel={ __( 'Edit link' ) }\n\t\t\t\t\tonClick={ onEditClick }\n\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\tshowTooltip={ ! showIconLabels }\n\t\t\t\t/>\n\t\t\t\t{ hasUnlinkControl && (\n\t\t\t\t\t<Button\n\t\t\t\t\t\ticon={ linkOff }\n\t\t\t\t\t\tlabel={ __( 'Remove link' ) }\n\t\t\t\t\t\tonClick={ onRemove }\n\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\tshowTooltip={ ! showIconLabels }\n\t\t\t\t\t/>\n\t\t\t\t) }\n\t\t\t\t<Button\n\t\t\t\t\ticon={ copySmall }\n\t\t\t\t\tlabel={ __( 'Copy link' ) }\n\t\t\t\t\tref={ ref }\n\t\t\t\t\taccessibleWhenDisabled\n\t\t\t\t\tdisabled={ isEmptyURL }\n\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\tshowTooltip={ ! showIconLabels }\n\t\t\t\t/>\n\t\t\t\t<ViewerSlot fillProps={ value } />\n\t\t\t</Flex>\n\t\t</Flex>\n\t);\n}\n"],
"mappings": ";AAGA,OAAO,UAAU;AAKjB,SAAS,UAAU;AACnB;AAAA,EACC;AAAA,EACA;AAAA,EACA,0BAA0B;AAAA,EAC1B;AAAA,OACM;AACP,SAAS,0BAA0B;AACnC,SAAS,qBAAqB,qBAAqB;AACnD;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,uBAAuB,iBAAiB;AACjD,SAAS,aAAa,iBAAiB;AACvC,SAAS,SAAS,oBAAoB;AACtC,SAAS,SAAS,wBAAwB;AAK1C,SAAS,kBAAkB;AAE3B,OAAO,oBAAoB;AAsDlB,SAuDF,UAvDE,KAuDF,YAvDE;AA7CT,SAAS,sBAAuB,OAAQ;AAEvC,SAAO,MACL,QAAS,+BAA+B,EAAG,EAC3C,QAAS,WAAW,EAAG;AAC1B;AAEe,SAAR,YAA8B;AAAA,EACpC;AAAA,EACA;AAAA,EACA,kBAAkB;AAAA,EAClB,mBAAmB;AAAA,EACnB;AACD,GAAI;AACH,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