diff --git a/src/edit.js b/src/edit.js new file mode 100644 index 0000000..e573471 --- /dev/null +++ b/src/edit.js @@ -0,0 +1,190 @@ +/* global Iconify */ + +import { __ } from '@wordpress/i18n'; + +import { RawHTML, Fragment, useState } from '@wordpress/element'; +import { BlockControls, InspectorControls } from '@wordpress/editor'; +import { + Button, + Modal, + RangeControl, + TextControl, + ToggleControl, + Toolbar, + ToolbarButton, + PanelBody, +} from '@wordpress/components'; + +export function edit({ className, attributes, setAttributes }) { + /* eslint-disable react-hooks/rules-of-hooks */ + const [modalIsOpen, setOpen] = useState(false); + const [newPrefix, setNewPrefix] = useState(attributes.prefix); + const [newName, setNewName] = useState(attributes.name); + /* eslint-enable react-hooks/rules-of-hooks */ + + const updateIcon = () => { + const iconString = `${newPrefix}:${newName}`; + if (!Iconify.iconExists(iconString)) { + // TODO loading indicator + Iconify.loadIcons([iconString], () => { + // TODO if icon not found + // TODO customisations object as renderHTML second argument + setSVG(Iconify.renderHTML(iconString), newPrefix, newName); + closeModal(); + }); + } else { + // TODO customisations object as renderHTML second argument + setSVG(Iconify.renderHTML(iconString), newPrefix, newName); + closeModal(); + } + }; + + const setSVG = (svg, prefix, name) => { + setAttributes({ + svg, + prefix, + name, + }); + }; + + const openModal = () => { + setOpen(true); + }; + + const closeModal = () => { + setOpen(false); + }; + + const InspectorControlComponent = () => ( + + + { + setAttributes({ fontSize: value }); + }} + /> + { + setAttributes({ + fontUnit: state ? 'em' : 'px', + fontSize: state + ? attributes.fontSize / 16 + : attributes.fontSize * 16, + }); + }} + /> + + + { + setAttributes({ isInline: state }); + }} + /> +

Color

+

Rotate

+

Flip

+

Etc

+
+
+ ); + + const BlockControlComponent = () => ( + + + + + + ); + + const ModalComponent = () => ( + +

+ TODO: Search for an icon +

+ { + setNewPrefix(value); + }} + /> + { + setNewName(value); + }} + /> + + + + +
+ ); + + return ( + + + + + + {attributes.svg} + + + {attributes.svg === '' && ( + + )} + + {attributes.svg !== '' && ( + + )} + + {modalIsOpen && } + + ); +} + diff --git a/src/index.js b/src/index.js index b33faeb..ecc1aa6 100644 --- a/src/index.js +++ b/src/index.js @@ -1,23 +1,13 @@ -/* global Iconify */ import './style.scss'; import './editor.scss'; -import { registerBlockType } from '@wordpress/blocks'; - -import { RawHTML, Fragment, useState } from '@wordpress/element'; -import { BlockControls, InspectorControls } from '@wordpress/editor'; -import { - Button, - Modal, - RangeControl, - TextControl, - ToggleControl, - Toolbar, - ToolbarButton, - PanelBody, -} from '@wordpress/components'; import { __ } from '@wordpress/i18n'; +import { edit } from './edit.js'; +import { save } from './save.js'; + +import { registerBlockType } from '@wordpress/blocks'; + registerBlockType('room-vw/iconify', { title: __('Iconify', 'room-vw'), icon: ( @@ -69,177 +59,7 @@ registerBlockType('room-vw/iconify', { }, }, - edit({ className, attributes, setAttributes }) { - /* eslint-disable react-hooks/rules-of-hooks */ - const [modalIsOpen, setOpen] = useState(false); - const [newPrefix, setNewPrefix] = useState(attributes.prefix); - const [newName, setNewName] = useState(attributes.name); - /* eslint-enable react-hooks/rules-of-hooks */ + edit, - const updateIcon = () => { - const iconString = `${newPrefix}:${newName}`; - if (!Iconify.iconExists(iconString)) { - // TODO loading indicator - Iconify.loadIcons([iconString], () => { - // TODO if icon not found - // TODO customisations object as renderHTML second argument - setSVG(Iconify.renderHTML(iconString), newPrefix, newName); - closeModal(); - }); - } else { - // TODO customisations object as renderHTML second argument - setSVG(Iconify.renderHTML(iconString), newPrefix, newName); - closeModal(); - } - }; - - const setSVG = (svg, prefix, name) => { - setAttributes({ - svg, - prefix, - name, - }); - }; - - const openModal = () => { - setOpen(true); - }; - - const closeModal = () => { - setOpen(false); - }; - - return ( - - - - { - setAttributes({ fontSize: value }); - }} - /> - { - setAttributes({ - fontUnit: state ? 'em' : 'px', - fontSize: state - ? attributes.fontSize / 16 - : attributes.fontSize * 16, - }); - }} - /> - - - { - setAttributes({ isInline: state }); - }} - /> -

Color

-

Rotate

-

Flip

-

Etc

-
-
- - - - - - - - - {attributes.svg} - - - {attributes.svg === '' && ( - - )} - - {attributes.svg !== '' && ( - - )} - - {modalIsOpen && ( - -

- TODO: Search for an icon -

- { - setNewPrefix(value); - }} - /> - { - setNewName(value); - }} - /> - - - - -
- )} -
- ); - }, - - save({ attributes }) { - return ( - - {attributes.svg} - - ); - }, + save, }); diff --git a/src/save.js b/src/save.js new file mode 100644 index 0000000..9280654 --- /dev/null +++ b/src/save.js @@ -0,0 +1,16 @@ +import { RawHTML } from '@wordpress/element'; + +export function save({ attributes }) { + return ( + + {attributes.svg} + + ); +}