components
1 year ago
css
2 years ago
attributes.js
3 years ago
block.js
1 year ago
deprecated.js
3 years ago
edit.js
2 years ago
editor.scss
2 years ago
save.js
3 years ago
transforms.js
1 year ago
deprecated.js
135 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import classnames from 'classnames'; |
| 5 | import blockAttributes from './attributes'; |
| 6 | import sanitizeSVG from '../../utils/sanitize-svg'; |
| 7 | |
| 8 | import { |
| 9 | applyFilters, |
| 10 | } from '@wordpress/hooks'; |
| 11 | |
| 12 | import { |
| 13 | RichText, |
| 14 | } from '@wordpress/block-editor'; |
| 15 | |
| 16 | import { getBlockAttributes } from '../../block-context'; |
| 17 | import buttonContext from '../../block-context/button'; |
| 18 | |
| 19 | const allAttributes = Object.assign( |
| 20 | {}, |
| 21 | getBlockAttributes( |
| 22 | blockAttributes, |
| 23 | buttonContext, |
| 24 | generateBlocksDefaults.button |
| 25 | ), |
| 26 | ); |
| 27 | |
| 28 | const deprecated = [ |
| 29 | // v1 of button block. |
| 30 | { |
| 31 | attributes: { |
| 32 | ...allAttributes, |
| 33 | text: { |
| 34 | type: 'array', |
| 35 | source: 'children', |
| 36 | selector: '.gb-button .button-text', |
| 37 | default: 'Button', |
| 38 | }, |
| 39 | }, |
| 40 | supports: { |
| 41 | anchor: false, |
| 42 | className: false, |
| 43 | customClassName: false, |
| 44 | inserter: false, |
| 45 | reusable: false, |
| 46 | }, |
| 47 | migrate( attributes ) { |
| 48 | const oldClasses = attributes.cssClasses ? attributes.cssClasses : attributes.className; |
| 49 | const oldAnchor = attributes.elementId ? attributes.elementId : attributes.anchor; |
| 50 | |
| 51 | return { |
| 52 | ...attributes, |
| 53 | className: oldClasses, |
| 54 | anchor: oldAnchor, |
| 55 | cssClasses: '', |
| 56 | elementId: '', |
| 57 | }; |
| 58 | }, |
| 59 | save( { attributes } ) { |
| 60 | const { |
| 61 | uniqueId, |
| 62 | elementId, |
| 63 | cssClasses, |
| 64 | text, |
| 65 | url, |
| 66 | target, |
| 67 | relNoFollow, |
| 68 | relSponsored, |
| 69 | icon, |
| 70 | iconLocation, |
| 71 | removeText, |
| 72 | ariaLabel, |
| 73 | } = attributes; |
| 74 | |
| 75 | const relAttributes = []; |
| 76 | |
| 77 | if ( relNoFollow ) { |
| 78 | relAttributes.push( 'nofollow' ); |
| 79 | } |
| 80 | |
| 81 | if ( target ) { |
| 82 | relAttributes.push( 'noopener', 'noreferrer' ); |
| 83 | } |
| 84 | |
| 85 | if ( relSponsored ) { |
| 86 | relAttributes.push( 'sponsored' ); |
| 87 | } |
| 88 | |
| 89 | let htmlAttributes = { |
| 90 | id: !! elementId ? elementId : undefined, |
| 91 | className: classnames( { |
| 92 | 'gb-button': true, |
| 93 | [ `gb-button-${ uniqueId }` ]: true, |
| 94 | [ `${ cssClasses }` ]: '' !== cssClasses, |
| 95 | } ), |
| 96 | href: !! url ? url : undefined, |
| 97 | target: !! target ? '_blank' : undefined, |
| 98 | rel: relAttributes && relAttributes.length > 0 ? relAttributes.join( ' ' ) : undefined, |
| 99 | 'aria-label': !! ariaLabel ? ariaLabel : undefined, |
| 100 | }; |
| 101 | |
| 102 | htmlAttributes = applyFilters( 'generateblocks.frontend.htmlAttributes', htmlAttributes, 'generateblocks/button', attributes ); |
| 103 | |
| 104 | return ( |
| 105 | <a |
| 106 | { ...htmlAttributes } |
| 107 | > |
| 108 | { icon && 'left' === iconLocation && |
| 109 | <span |
| 110 | className="gb-icon" |
| 111 | dangerouslySetInnerHTML={ { __html: sanitizeSVG( icon ) } } |
| 112 | /> |
| 113 | } |
| 114 | { ! removeText && |
| 115 | <RichText.Content |
| 116 | tagName="span" |
| 117 | className="button-text" |
| 118 | value={ text } |
| 119 | key="button-text" |
| 120 | /> |
| 121 | } |
| 122 | { icon && 'right' === iconLocation && |
| 123 | <span |
| 124 | className="gb-icon" |
| 125 | dangerouslySetInnerHTML={ { __html: sanitizeSVG( icon ) } } |
| 126 | /> |
| 127 | } |
| 128 | </a> |
| 129 | ); |
| 130 | }, |
| 131 | }, |
| 132 | ]; |
| 133 | |
| 134 | export default deprecated; |
| 135 |