Skip to content

Commit

Permalink
fix: revert fix to icon list item, use splitting instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Arukuen committed Jan 7, 2025
1 parent 81f1002 commit aff96fc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 47 deletions.
3 changes: 0 additions & 3 deletions src/block/icon-list-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import blockStyles from './style'
import { getUseSvgDef } from '../icon-list/util'
import {
convertToListItems,
useOnPaste,
useEnter,
} from './util'

Expand Down Expand Up @@ -110,7 +109,6 @@ const Edit = props => {
] )

const useEnterRef = useEnter( text, clientId )
const onPaste = useOnPaste( clientId, parentBlock?.clientId, attributes, setAttributes )

const onMerge = forward => {
mergeBlocks( forward )
Expand Down Expand Up @@ -173,7 +171,6 @@ const Edit = props => {
tagName="span"
className={ textClassNames }
onMerge={ onMerge }
onPaste={ onPaste }
onReplace={ onReplace
? ( blocks, ...args ) => {
onReplace(
Expand Down
47 changes: 3 additions & 44 deletions src/block/icon-list-item/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
* WordPress dependencies
*/
import { useRefEffect } from '@wordpress/compose'
import { useCallback, useRef } from '@wordpress/element'
import { useRef } from '@wordpress/element'
import {
useSelect, useDispatch, dispatch,
useSelect,
useDispatch,
} from '@wordpress/data'
import {
cloneBlock,
switchToBlockType,
createBlock,
getDefaultBlockName,
pasteHandler,
} from '@wordpress/blocks'
import { store as blockEditorStore } from '@wordpress/block-editor'
import { ENTER } from '@wordpress/keycodes'
Expand Down Expand Up @@ -123,44 +123,3 @@ export const useEnter = ( text, clientId ) => {
[ clientId ]
)
}

export const useOnPaste = ( clientId, parentClientId, attributes, setAttributes ) => {
const { insertBlocks } = useDispatch( blockEditorStore )
const { getBlockIndex } = useSelect( blockEditorStore )

return useCallback( event => {
event.preventDefault()
const html = event.clipboardData.getData( 'text/html' )
const plain = event.clipboardData.getData( 'text/plain' )

// Convert first to core/list block.
const list = pasteHandler( {
HTML: html,
plainText: plain,
mode: 'BLOCKS',
} )

// If list[0] has inner blocks, it has been converted to core/list block, else list has core/paragraph elements.
const items = list[ 0 ].innerBlocks.length ? list[ 0 ].innerBlocks : list

const content = items.map( item => item.attributes.content.toPlainText().replaceAll( '\n', '<br>' ) )

// If current icon list item has no text, use the first item as text.
if ( ! attributes.text ) {
const firstItem = content.shift()
setAttributes( { text: firstItem } )
}

// create new icon list items
const newBlocks = content.map( text => {
const block = createBlock( 'stackable/icon-list-item', {
...attributes,
text,
} )

return block
} )
dispatch( 'core/block-editor' ).__unstableMarkNextChangeAsNotPersistent()
insertBlocks( newBlocks, getBlockIndex( clientId ) + 1, parentClientId )
}, [ clientId, parentClientId, attributes, setAttributes ] )
}

0 comments on commit aff96fc

Please sign in to comment.