diff --git a/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/README.MD b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/README.MD new file mode 100644 index 00000000..526c969a --- /dev/null +++ b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/README.MD @@ -0,0 +1,11 @@ +# Tile block + +Add a tile block with an icon, a heading and a text paragraph. Features a Font Awesome icon picker component which uses the Font Awesome API to search for icons. Add an anchor link behind the heading to make the tile fully clickable. + +## Requirements + +Setup FontAwesome in your project [through a FontAwesome kit](https://fontawesome.com/docs/web/setup/get-started) or install it [using npm](https://fontawesome.com/docs/web/setup/packages). + +## Wait, where is the styling? + +Styling is situated in the `"/assets/scss/blocks/tile"` folder. diff --git a/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/block.json b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/block.json new file mode 100644 index 00000000..ecc1db22 --- /dev/null +++ b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/block.json @@ -0,0 +1,14 @@ +{ + "name": "openpdd/tile", + "category": "layout", + "attributes": { + "icon": { + "type": "string", + "default": "fal fa-envelope" + }, + "altText": { + "type": "string", + "default": "" + } + } +} diff --git a/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/icon-picker-control.js b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/icon-picker-control.js new file mode 100644 index 00000000..197f0844 --- /dev/null +++ b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/icon-picker-control.js @@ -0,0 +1,134 @@ +/** + * WordPress dependencies + */ +import { Button, Popover, SearchControl } from '@wordpress/components'; +import { useState } from '@wordpress/element'; +import { useDispatch } from '@wordpress/data'; +import { store as noticesStore } from '@wordpress/notices'; +import { __ } from '@wordpress/i18n'; + +const IconPickerControl = ( { onChange, icon } ) => { + const [ isOpen, setOpen ] = useState( false ); + const [ searchInput, setSearchInput ] = useState( '' ); + const [ searchResults, setSearchResults ] = useState( [] ); + + const { createNotice } = useDispatch( noticesStore ); + + const allowedStyles = [ 'solid', 'regular' ]; + + const searchFontAwesomeIcons = async ( searchValue ) => { + const response = await getFontAwesomeIcons( searchValue ); + if ( ! response ) return; + if ( response.errors ) return showErrorNotice(); + + const result = response.data.search.reduce( + ( iconResults, iconData ) => { + convertResponseToClassnames( iconData ).forEach( ( value ) => { + iconResults.push( value ); + } ); + + return iconResults; + }, + [] + ); + if ( ! result ) return; + + setSearchResults( () => result ); + setOpen( () => true ); + }; + + const getFontAwesomeIcons = ( search ) => { + const query = `{ search(version: "6.1.2", first: 100, query: "${ search }") { id styles } }`; + + return fetch( 'https://api.fontawesome.com', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + }, + body: JSON.stringify( { query } ), + } ) + .then( ( res ) => res.json() ) + .catch( () => showErrorNotice() ); + }; + + const convertResponseToClassnames = ( response ) => { + return response.styles + .filter( ( style ) => allowedStyles.includes( style ) ?? false ) + .map( ( style ) => `fa-${ style } fa-${ response.id }` ); + }; + + const showErrorNotice = () => { + createNotice( + 'error', + __( + 'Momenteel kunnen er geen iconen worden opgehaald, probeer het later nog een keer.', + 'owc-formulieren' + ), + { + isDismissible: true, + type: 'snackbar', + id: 'icon-picker-control-error', + } + ); + }; + + return ( + <> + { icon && ( + + ) } + { + setSearchInput( () => searchValue ); + searchFontAwesomeIcons( searchValue ); + } } + /> + { isOpen && searchInput && ( + setOpen( () => false ) } + focusOnMount={ false } + > +
+ { searchResults?.map( ( result, key ) => { + return ( +
+ +
+ ); + } ) } + + { ! searchResults.length && ( +

+ { __( + 'Er zijn geen iconen gevonden', + 'owc-formulieren' + ) } +

+ ) } +
+
+ ) } + + ); +}; + +export default IconPickerControl; diff --git a/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/icon.js b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/icon.js new file mode 100644 index 00000000..09c119f2 --- /dev/null +++ b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/icon.js @@ -0,0 +1,13 @@ +const Icon = ( props ) => { + const { attributes } = props; + const { icon, altText } = attributes; + + return ( + + ); +}; + +export default Icon; diff --git a/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/inspector.js b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/inspector.js new file mode 100644 index 00000000..f752ca7d --- /dev/null +++ b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/components/inspector.js @@ -0,0 +1,33 @@ +/** + * WordPress dependencies + */ +import { InspectorControls, FontSizePicker } from '@wordpress/block-editor'; +import { PanelBody, TextControl } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import IconPickerControl from './icon-picker-control'; + +const Inspector = ( props ) => { + const { setAttributes, attributes } = props; + const { icon } = attributes; + + return ( + + + + setAttributes( { + icon: result, + } ) + } + /> + + + ); +}; + +export default Inspector; diff --git a/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/edit.js b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/edit.js new file mode 100644 index 00000000..f915dc61 --- /dev/null +++ b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/edit.js @@ -0,0 +1,43 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import Inspector from './components/inspector'; +import Icon from './components/icon'; + +const Edit = ( props ) => { + const blockProps = useBlockProps( { + className: 'tile', + } ); + + const template = [ + [ + 'core/heading', + { placeholder: __( 'Titel', 'owc-formulieren' ), level: 3 }, + ], + [ + 'core/paragraph', + { + placeholder: __( + 'Introductie van maximaal 4 regels', + 'owc-formulieren' + ), + }, + ], + ]; + + return ( +
+ + + +
+ ); +}; + +export default Edit; diff --git a/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/index.js b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/index.js new file mode 100644 index 00000000..586ba445 --- /dev/null +++ b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/index.js @@ -0,0 +1,38 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { registerBlockType } from '@wordpress/blocks'; + +/** + * Internal dependencies + */ +import metadata from './block.json'; +import edit from './edit'; +import save from './save'; + +const { name, attributes } = metadata; + +const settings = { + apiVersion: 2, + title: __( 'Tegel' ), + description: __( + 'Toon een tegel met een icoon, koptekst en begeleidende tekst.' + ), + category: 'theme', + keywords: [ + __( 'fontawesome' ), + __( 'font awesome', 'owc-formulieren' ), + __( 'symbool', 'owc-formulieren' ), + ], + icon: { + src: 'image-filter', + background: '#fff', + foreground: '#000', + }, + edit, + save, + attributes, +}; + +registerBlockType( name, settings ); diff --git a/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/save.js b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/save.js new file mode 100644 index 00000000..2477983a --- /dev/null +++ b/htdocs/wp-content/themes/owc-formulieren/assets/js/editor/openpdd-tile/save.js @@ -0,0 +1,28 @@ +/** + * WordPress dependencies + */ +import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import Icon from './components/icon'; + +const Save = ( props ) => { + const { attributes } = props; + const { altText } = attributes; + + const blockProps = useBlockProps.save( { + className: 'tile', + } ); + + return ( +
+ + { altText && { altText } } + +
+ ); +}; + +export default Save;