diff options
Diffstat (limited to 'plugins/jetpack/extensions/blocks/contact-info/email')
4 files changed, 100 insertions, 0 deletions
diff --git a/plugins/jetpack/extensions/blocks/contact-info/email/edit.js b/plugins/jetpack/extensions/blocks/contact-info/email/edit.js new file mode 100644 index 00000000..5fd0ccac --- /dev/null +++ b/plugins/jetpack/extensions/blocks/contact-info/email/edit.js @@ -0,0 +1,15 @@ +/** + * Internal dependencies + */ +import save from './save'; +import simpleInput from '../../../shared/simple-input'; +import { __ } from '@wordpress/i18n'; + +const EmailEdit = props => { + const { setAttributes } = props; + return simpleInput( 'email', props, __( 'Email', 'jetpack' ), save, nextValue => + setAttributes( { email: nextValue } ) + ); +}; + +export default EmailEdit; diff --git a/plugins/jetpack/extensions/blocks/contact-info/email/editor.js b/plugins/jetpack/extensions/blocks/contact-info/email/editor.js new file mode 100644 index 00000000..403fddb8 --- /dev/null +++ b/plugins/jetpack/extensions/blocks/contact-info/email/editor.js @@ -0,0 +1,7 @@ +/** + * Internal dependencies + */ +import registerJetpackBlock from '../../../shared/register-jetpack-block'; +import { name, settings } from '.'; + +registerJetpackBlock( name, settings ); diff --git a/plugins/jetpack/extensions/blocks/contact-info/email/index.js b/plugins/jetpack/extensions/blocks/contact-info/email/index.js new file mode 100644 index 00000000..db086dd0 --- /dev/null +++ b/plugins/jetpack/extensions/blocks/contact-info/email/index.js @@ -0,0 +1,42 @@ +/** + * External dependencies + */ +import { __, _x } from '@wordpress/i18n'; +import { Path } from '@wordpress/components'; + +/** + * Internal dependencies + */ +import edit from './edit'; +import renderMaterialIcon from '../../../shared/render-material-icon'; +import save from './save'; + +const attributes = { + email: { + type: 'string', + default: '', + }, +}; + +export const name = 'email'; + +export const settings = { + title: __( 'Email Address', 'jetpack' ), + description: __( + 'Lets you add an email address with an automatically generated click-to-email link.', + 'jetpack' + ), + keywords: [ + 'e-mail', // not translatable on purpose + 'email', // not translatable on purpose + _x( 'message', 'block search term', 'jetpack' ), + ], + icon: renderMaterialIcon( + <Path d="M22 6c0-1.1-.9-2-2-2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6zm-2 0l-8 5-8-5h16zm0 12H4V8l8 5 8-5v10z" /> + ), + category: 'jetpack', + attributes, + edit, + save, + parent: [ 'jetpack/contact-info' ], +}; diff --git a/plugins/jetpack/extensions/blocks/contact-info/email/save.js b/plugins/jetpack/extensions/blocks/contact-info/email/save.js new file mode 100644 index 00000000..e0eb0204 --- /dev/null +++ b/plugins/jetpack/extensions/blocks/contact-info/email/save.js @@ -0,0 +1,36 @@ +/** + * External dependencies + */ +import emailValidator from 'email-validator'; +import { Fragment } from '@wordpress/element'; + +const renderEmail = inputText => { + const explodedInput = inputText.split( /(\s+)/ ).map( ( email, i ) => { + // Remove and punctuation from the end of the email address. + const emailToValidate = email.replace( /([.,/#!$%^&*;:{}=\-_`~()\][])+$/g, '' ); + if ( email.indexOf( '@' ) && emailValidator.validate( emailToValidate ) ) { + return email === emailToValidate ? ( + // Email. + <a href={ `mailto:${ email }` } key={ i }> + { email } + </a> + ) : ( + // Email with punctionation. + <Fragment key={ i }> + <a href={ `mailto:${ email }` } key={ i }> + { emailToValidate } + </a> + <Fragment>{ email.slice( -( email.length - emailToValidate.length ) ) }</Fragment> + </Fragment> + ); + } + // Just a plain string. + return <Fragment key={ i }>{ email }</Fragment>; + } ); + return explodedInput; +}; + +const save = ( { attributes: { email }, className } ) => + email && <div className={ className }>{ renderEmail( email ) }</div>; + +export default save; |