Skip to content

Commit 9433642

Browse files
authored
Enable changing icon in block editor (#262)
* enable change of icon in the IconModifier modal * WIP: first draft of adding a pencil icon * fix the layout of the pencil * make the pencil invisible until hover * get rid of the extra state for size in IconModifier * Fix icon layer replacement when changing icon in IconModifier * rebuild block-editor bundle * remove the pencil icon * refactor the IconChooserModal stuff for changing the icon * rebuild icon-chooser bundle * rebuild block-editor bundle * remove the position: relative * rebuild block-editor bundle
1 parent dfa22a5 commit 9433642

File tree

10 files changed

+43
-18
lines changed

10 files changed

+43
-18
lines changed

block-editor/build/index-rtl.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

block-editor/build/index.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives', 'wp-rich-text'), 'version' => '987f5e49022d05fb19a8');
1+
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n', 'wp-primitives', 'wp-rich-text'), 'version' => '17f5c0bec291cb2ddb3f');

block-editor/build/index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

block-editor/build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

block-editor/src/edit.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ export function Edit(props) {
4646

4747
if (!iconDefinition) return
4848

49-
const layer = {
50-
iconDefinition,
51-
...defaultStylingParams
52-
}
49+
const { replace, append } = layerParams
5350

5451
const newIconLayers = [...iconLayers]
55-
const { replace, append } = layerParams
5652

5753
if (append) {
54+
const layer = {
55+
iconDefinition,
56+
...defaultStylingParams
57+
}
58+
5859
newIconLayers.push(layer)
5960
} else if (Number.isInteger(replace) && replace < iconLayers.length) {
60-
newIconLayers[replace] = layer
61+
const oldLayer = iconLayers[replace] || {}
62+
const newLayer = {...oldLayer, iconDefinition }
63+
newIconLayers[replace] = newLayer
6164
}
6265

6366
setAttributes({ iconLayers: newIconLayers })
@@ -119,6 +122,7 @@ export function Edit(props) {
119122
setAttributes={setAttributes}
120123
IconChooserModal={IconChooserModal}
121124
prepareHandleSelect={prepareHandleSelect}
125+
iconChooserOpenEvent={openIconChooserForAddLayerEvent}
122126
/>
123127
</Modal>
124128
)}

block-editor/src/iconModifier.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@ const SettingsTabPanel = ({ onSelect, onSizeChange, setColor, setAnimation, upda
4242
if ('object' !== typeof currentIconLayer) return
4343
const currentTransform = currentIconLayer?.transform
4444
const currentRotate = currentTransform?.rotate
45-
const currentSize = currentIconLayer?.style?.fontSize
46-
const [size, setSize] = useState(currentSize || DEFAULT_SIZE)
45+
const currentSize = currentIconLayer?.style?.fontSize || DEFAULT_SIZE
4746

4847
const updateSize = (size) => {
4948
const newSize = size || DEFAULT_SIZE
50-
setSize(newSize)
5149
onSizeChange(newSize)
5250
}
5351

@@ -195,7 +193,7 @@ const SettingsTabPanel = ({ onSelect, onSizeChange, setColor, setAnimation, upda
195193
<div className="fawp-icon-styling-tab-content fawp-icon-styling-size fawp-tab-content">
196194
<div className="">
197195
<FontSizePicker
198-
value={size}
196+
value={currentSize}
199197
fontSizes={[
200198
{
201199
name: 'tiny',
@@ -374,7 +372,16 @@ const SettingsTabPanel = ({ onSelect, onSizeChange, setColor, setAnimation, upda
374372
)
375373
}
376374

377-
export default function ({ attributes, setAttributes, context }) {
375+
export default function (params) {
376+
const {
377+
attributes,
378+
setAttributes,
379+
context,
380+
iconChooserOpenEvent,
381+
IconChooserModal,
382+
prepareHandleSelect
383+
} = params
384+
378385
const iconLayers = attributes.iconLayers || []
379386

380387
const updateSize = (size) => {
@@ -459,6 +466,7 @@ export default function ({ attributes, setAttributes, context }) {
459466
<div
460467
className="fawp-icon-modifier-preview"
461468
style={contextStyle}
469+
onClick={() => document.dispatchEvent(iconChooserOpenEvent)}
462470
>
463471
{renderIcon(attributes)}
464472
</div>
@@ -473,6 +481,11 @@ export default function ({ attributes, setAttributes, context }) {
473481
setAnimation={setAnimation}
474482
/>
475483
</div>
484+
<IconChooserModal
485+
title={__('Change Font Awesome Icon', 'font-awesome')}
486+
onSubmit={prepareHandleSelect({ replace: 0 })}
487+
openEvent={iconChooserOpenEvent}
488+
/>
476489
</div>
477490
)
478491
}

block-editor/src/index.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@
8282
padding: var(--spacing-md-fa);
8383
}
8484

85+
.fawp-icon-modifier-preview:hover {
86+
background: var(--gray7-fa);
87+
cursor: pointer;
88+
border-radius: var(--border-radius-fa);
89+
}
90+
8591
.fawp-icon-modifier-preview div {
8692
display: flex;
8793
margin: auto;

icon-chooser/build/index.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-components', 'wp-i18n'), 'version' => '9291d79bfa031a0e70d7');
1+
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-components', 'wp-i18n'), 'version' => '1ffd38231f1a41762e04');

icon-chooser/build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

icon-chooser/src/IconChooserModal.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ export default function (params) {
3030

3131
const isProCdn = !!pro && !kitToken;
3232

33+
const title = props.title || __("Add a Font Awesome Icon", "font-awesome")
34+
3335
return (
3436
<>
3537
{isOpen && (
36-
<Modal title="Add a Font Awesome Icon" onRequestClose={closeModal}>
38+
<Modal title={title} onRequestClose={closeModal}>
3739
{isProCdn && (
3840
<div
3941
style={{

0 commit comments

Comments
 (0)