import { RichTextToolbarButton } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { registerFormatType } from '@wordpress/rich-text';
import { applyFormat } from '@wordpress/rich-text';
import { RichTextShortcut } from '@wordpress/block-editor';
import {
BlockControls
} from '@wordpress/block-editor';
import { Toolbar, Popover } from '@wordpress/components';
import { useState } from "@wordpress/element";
import { styles } from "@wordpress/icons";
import {
CustomSelectControl,
SelectControl,
} from '@wordpress/components';
import { useSelect } from "@wordpress/data/build-types";
import parse from 'html-react-parser';
import apiFetch from "@wordpress/api-fetch";
import { createReduxStore, register } from '@wordpress/data';
const DEFAULT_STATE = {
icons: {
wordpress: {
'star-empty': ''
}
},
};
const actions = {
setIcons( icons ) {
return {
type: 'SET_ICONS',
icons,
};
},
getIcons( path ) {
return {
type: 'GET_ICONS',
path,
};
},
};
const reducer = ( state = DEFAULT_STATE, action ) => {
switch ( action.type ) {
case 'SET_ICONS': {
return {
...state,
icons: action.icons,
};
}
default: {
return state;
}
}
};
const selectors = {
getIcons( state ) {
const { icons } = state;
return icons;
},
};
const controls = {
GET_ICONS( action ) {
return apiFetch( { path: action.path } );
},
};
const resolvers = {
* getIcons() {
const icons = yield actions.getIcons( '/blockify/v1/icons/' );
return actions.setIcons( icons );
},
};
register( createReduxStore( 'blockify/icons', {
reducer,
actions,
selectors,
controls,
resolvers,
} ) );
registerFormatType( 'blockify/icon', {
title: __( 'Icon', 'blockify' ),
tagName: 'span',
className: 'has-icon',
attributes: {
style: 'style',
class: 'class',
},
edit: ( { isActive, value, onChange } ) => {
let { icons } = useSelect( select => {
const iconData: any = select( 'blockify/icons' );
return {
icons: iconData.getIcons(),
};
}, [] ) ?? DEFAULT_STATE;
const [ icon, setIcon ] = useState( 'star-empty' );
const [ iconSet, setIconSet ] = useState( 'wordpress' );
const [ isOpen, setIsOpen ] = useState( false );
let iconSetOptions = [];
let iconOptions = {};
Object.keys( icons ).forEach( set => {
let label = set.split( '-' ).join( ' ' );
label = 'wordpress' === label ? 'WordPress' : label;
iconSetOptions.push( {
label: label,
value: set,
} );
iconOptions[set] = [];
Object.keys( icons[set] ).forEach( iconName => {
if ( iconName !== icon ) {
iconOptions[set].push( {
name: parse( icons[set][iconName] ),
key: iconName,
} );
}
} );
if ( icons?.[iconSet]?.[icon] ) {
iconOptions[set].unshift( {
name: parse( icons[iconSet][icon] ),
key: icon,
} );
}
} );
return (
setIsOpen( ! isOpen ) }
/>
{ isOpen &&
setIsOpen( false ) }
>
{
setIconSet( newIconSet );
onChange( applyFormat( value, {
type: 'blockify/icon',
attributes: {
class: 'has-gradient-text ' + 'icon-set-' + newIconSet
}
} ) )
} }
/>
{
setIcon( newIcon );
onChange( applyFormat( value, {
type: 'blockify/icon',
attributes: {
class: 'has-gradient-text ' + 'icon-' + newIcon
}
} ) )
} }
/>
}
);
}
} );