Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jdamner committed Jun 19, 2024
1 parent 67c50c8 commit a6028a6
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 96 deletions.
File renamed without changes
72 changes: 16 additions & 56 deletions packages/iconography/src/block/Edit.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import React from 'react';

/* WordPress Dependencies */
import {
BlockControls,
InspectorControls,
useBlockProps,
} from '@wordpress/block-editor';
import { useBlockProps } from '@wordpress/block-editor';
import { store as RichTextStore } from '@wordpress/rich-text';
import { useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';
import {
Button,
Panel,
PanelBody,
ToolbarButton,
Icon,
} from '@wordpress/components';
import { Icon, Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { replace } from '@wordpress/icons';

/* Internal deps */
import { IconModal } from '../IconModal';
import { IconToolbarButton } from '../shared';
import './style.scss';

/* Types */
Expand All @@ -33,7 +22,6 @@ export const Edit = ( {
attributes,
setAttributes,
}: BlockEditProps< Attributes > ) => {
const [ showIconModal, setShowIconModal ] = useState( false );
const blockProps = useBlockProps();
const { getFormatType } = useSelect(
( select ) =>
Expand Down Expand Up @@ -61,55 +49,27 @@ export const Edit = ( {
const TagName =
( attributes.iconTag as keyof HTMLElementTagNameMap ) ?? 'span';

const buttonText = attributes.iconContent
? __( 'Change Icon', 'boxuk' )
: __( 'Select Icon', 'boxuk' );

const ShowModalButton = () => (
<Button
variant={ 'secondary' }
onClick={ () => setShowIconModal( ! showIconModal ) }
text={ buttonText }
icon={ <Icon icon={ replace } /> }
/>
);

return (
<>
<InspectorControls group="list">
<Panel>
<PanelBody>
<ShowModalButton />
</PanelBody>
</Panel>
</InspectorControls>
<BlockControls group="inline">
<ToolbarButton
icon={ <Icon icon={ replace } /> }
onClick={ () => setShowIconModal( ! showIconModal ) }
title={ buttonText }
/>
</BlockControls>
<IconToolbarButton
icon={ <Icon icon={ replace } /> }
onChange={ handleChange }
value={ {
text: '',
formats: [],
replacements: [],
start: 0,
end: 0,
} }
initialOpen={ ! attributes.iconContent }
/>
<div { ...blockProps }>
{ attributes.iconContent && (
<TagName className={ attributes.iconClass ?? '' }>
{ attributes.iconContent }
</TagName>
) }
{ ! attributes.iconContent && <ShowModalButton /> }
{ showIconModal && (
<IconModal
onRequestClose={ () => setShowIconModal( false ) }
onChange={ handleChange }
value={ {
text: '',
formats: [],
replacements: [],
start: 0,
end: 0,
} }
/>
) }
{ ! attributes.iconContent && <Spinner /> }
</div>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/iconography/src/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { registerBlockType } from '@wordpress/blocks';
import metadata from './block.json';
import { Edit } from './Edit';
import { Save } from './Save';
import { ReactComponent as AddReactionOutlined } from '../AddReactionOutlined.svg';
import { ReactComponent as AddReactionOutlined } from '../Icon.svg';

registerBlockType( metadata, {
icon: AddReactionOutlined,
Expand Down
14 changes: 14 additions & 0 deletions packages/iconography/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* WordPress Dependencies */
import domReady from '@wordpress/dom-ready';

/* Internal deps */
import registerInlineIconography from './registerInlineIconography';

domReady( () => {
registerInlineIconography();
} );

/* Export for other packages to consume */
export * from './types';
export * from './shared';
export * from './utils';
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import React, { ComponentProps } from 'react';

/* WordPress Dependencies */
import domReady from '@wordpress/dom-ready';
import { registerFormatType } from '@wordpress/rich-text';
import { BlockControls } from '@wordpress/block-editor';

/* Internal deps */
import { IconToolbarButton } from './IconToolbarButton';
import { IconToolbarButton } from './shared';
import { getIconGroups, selectIconAtCurrentCursor } from './utils';

/* Types */
import type { IconGroup } from './types';

export const handleKeyDown =
const handleKeyDown =
( iconGroups: IconGroup[] | undefined ) => ( event: KeyboardEvent ) => {
switch ( event.key ) {
case 'ArrowLeft':
Expand All @@ -25,7 +23,7 @@ export const handleKeyDown =
}
};

export const handleKeyUp =
const handleKeyUp =
( iconGroups: IconGroup[] | undefined ) => ( event: KeyboardEvent ) => {
if ( 'ArrowRight' === event.key ) {
const { selection, icon } = selectIconAtCurrentCursor( iconGroups );
Expand All @@ -51,7 +49,7 @@ export const handleKeyEvent =
}
};

export const registerIconography = () => {
export const registerInlineIconography = () => {
const iconGroups = getIconGroups();
if ( ! iconGroups ) {
return;
Expand All @@ -61,11 +59,7 @@ export const registerIconography = () => {
if ( index === 0 ) {
iconGroup.edit = (
props: ComponentProps< typeof IconToolbarButton >
) => (
<BlockControls group="inline">
<IconToolbarButton { ...props } />
</BlockControls>
);
) => <IconToolbarButton { ...props } />;
}
registerFormatType( iconGroup.name, iconGroup );
} );
Expand All @@ -76,6 +70,4 @@ export const registerIconography = () => {
document.addEventListener( 'keyup', handleKeyEvent( iconGroups ) );
};

domReady( () => {
registerIconography();
} );
export default registerInlineIconography;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useState, useMemo } from '@wordpress/element';

/* Internal Dependencies */
import { PlaceholderIconPanel } from './PlaceholderIconPanel';
import { getIconGroups } from './utils';
import { getIconGroups } from '../utils';

/* Types */
import type { RichTextValue } from '@wordpress/rich-text';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, jest, test } from '@jest/globals';
import { render, screen } from '@testing-library/react';
import { generateRichTextFormat } from '../utils';

import { IconPanel } from '../IconPanel';
import { IconPanel } from './IconPanel';

jest.mock( '../utils', () => ( {
generateRichTextFormat: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
} from '@wordpress/components';

/* Internal Dependencies */
import { generateRichTextFormat } from './utils';
import { generateRichTextFormat } from '../utils';

/* Types */
import type { IconGroup } from './types';
import type { IconGroup } from '../types';
import type { RichTextValue } from '@wordpress/rich-text';

export type IconPanelProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import React from 'react';
import React, { ComponentProps } from 'react';

/* WordPress Dependencies */
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { BlockControls } from '@wordpress/block-editor';
import { ToolbarButton } from '@wordpress/components';

/* Internal Dependencies */
import { ReactComponent as AddReactionOutlined } from './AddReactionOutlined.svg';
import { ReactComponent as Icon } from '../Icon.svg';
import { IconModal } from './IconModal';

/* Types */
import type { RichTextValue } from '@wordpress/rich-text';
import { IconModal } from './IconModal';

export type IconToolbarButtonProps = {
onChange: ( value: RichTextValue ) => void;
value: RichTextValue;
icon: ComponentProps< typeof ToolbarButton >[ 'icon' ];
initialOpen?: boolean;
};

export const IconToolbarButton = ( {
onChange,
value,
icon = <Icon />,
initialOpen = false,
}: IconToolbarButtonProps ) => {
const [ open, setOpen ] = useState( false );
const [ open, setOpen ] = useState( initialOpen );

return (
<>
<BlockControls group="inline">
<ToolbarButton
icon={ <AddReactionOutlined /> }
icon={ icon }
label={ __( 'Add an icon', 'boxuk' ) }
onClick={ () => setOpen( ! open ) }
/>
Expand All @@ -37,6 +42,6 @@ export const IconToolbarButton = ( {
value={ value }
/>
) }
</>
</BlockControls>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@wordpress/components';

/* Types */
import type { IconGroup } from './types';
import type { IconGroup } from '../types';
import type { RichTextValue } from '@wordpress/rich-text';

export type IconPanelProps = {
Expand Down
6 changes: 6 additions & 0 deletions packages/iconography/src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IconModal } from './IconModal';
import { IconPanel } from './IconPanel';
import { IconToolbarButton } from './IconToolbarButton';
import { PlaceholderIconPanel } from './PlaceholderIconPanel';

export { IconModal, IconPanel, IconToolbarButton, PlaceholderIconPanel };
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { registerFormatType } from '@wordpress/rich-text';

/* Internal Dependencies */
import { selectIconAtCurrentCursor, getIconGroups } from '../utils';
import { handleKeyEvent, registerIconography } from '../';
import { handleKeyEvent, registerInlineIconography } from '../registerInlineIconography';

Check failure on line 6 in packages/iconography/src/tests/registerInlineIconography.test.ts

View workflow job for this annotation

GitHub Actions / Test JS (node 20)

Replace `·handleKeyEvent,·registerInlineIconography·` with `⏎↹handleKeyEvent,⏎↹registerInlineIconography,⏎`

Check failure on line 6 in packages/iconography/src/tests/registerInlineIconography.test.ts

View workflow job for this annotation

GitHub Actions / Test JS (node 22)

Replace `·handleKeyEvent,·registerInlineIconography·` with `⏎↹handleKeyEvent,⏎↹registerInlineIconography,⏎`

jest.mock( '@wordpress/rich-text', () => ( {
registerFormatType: jest.fn(),
} ) );

jest.mock( '../IconToolbarButton', () => ( {
jest.mock( '../shared', () => ( {
IconToolbarButton: jest.fn(),
} ) );

Expand All @@ -26,6 +26,7 @@ jest.mock( '../utils', () => ( {

describe( 'registering iconography', () => {
test( 'should register all 3 types by default', () => {
registerInlineIconography();
expect( getIconGroups ).toBeCalledTimes( 1 );
// just by having imported the file, the registerFormatType should be called 3 times as per the 3 mocked values.
expect( registerFormatType ).toBeCalledTimes( 3 );
Expand All @@ -35,7 +36,7 @@ describe( 'registering iconography', () => {
jest.clearAllMocks();
getIconGroups.mockReturnValue( undefined );
expect( registerFormatType ).toBeCalledTimes( 0 );
registerIconography();
registerInlineIconography();
} );
} );

Expand Down
2 changes: 1 addition & 1 deletion packages/iconography/src/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IconGroup } from '../types';

jest.mock( '@wordpress/rich-text', () => ( {} ) );

jest.mock( '../IconToolbarButton', () => ( {
jest.mock( '../shared', () => ( {
IconToolbarButton: jest.fn(),
} ) );

Expand Down
8 changes: 0 additions & 8 deletions packages/iconography/svg.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/iconography/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module.exports = {
...defaultConfig,
entry: {
...defaultConfig.entry(),
index: './src/index.tsx',
index: './src/index.ts',
},
};

0 comments on commit a6028a6

Please sign in to comment.