Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if ( originalIsSelected === undefined ) {
isSelected = (
selectionStart.clientId === clientId &&
selectionStart.attributeKey === identifier
);
} else if ( originalIsSelected ) {
isSelected = selectionStart.clientId === clientId;
}
let extraProps = {};
if ( Platform.OS === 'native' ) {
// If the block of this RichText is unmodified then it's a candidate for replacing when adding a new block.
// In order to fix https://github.com/wordpress-mobile/gutenberg-mobile/issues/1126, let's blur on unmount in that case.
// This apparently assumes functionality the BlockHlder actually
const block = clientId && __unstableGetBlockWithoutInnerBlocks( clientId );
const shouldBlurOnUnmount = block && isSelected && isUnmodifiedDefaultBlock( block );
extraProps = {
shouldBlurOnUnmount,
};
}
return {
canUserUseUnfilteredHTML: __experimentalCanUserUseUnfilteredHTML,
isCaretWithinFormattedText: isCaretWithinFormattedText(),
selectionStart: isSelected ? selectionStart.offset : undefined,
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
didAutomaticChange: didAutomaticChange(),
disabled: isMultiSelecting() || hasMultiSelection(),
...extraProps,
};
};
return 0;
}
// If the clientId is defined, we insert at the position of the block.
if ( clientId ) {
return getBlockIndex( clientId, destinationRootClientId );
}
// If there a selected block,
const end = getBlockSelectionEnd();
// `end` argument (id) can refer to the component which is removed
// due to pressing `undo` button, that's why we need to check
// if `getBlock( end) is valid, otherwise `null` is passed
if ( ! isAppender && end && getBlock( end ) ) {
// and the last selected block is unmodified (empty), it will be replaced
if ( isUnmodifiedDefaultBlock( getBlock( end ) ) ) {
return getBlockIndex( end, destinationRootClientId );
}
// we insert after the selected block.
return getBlockIndex( end, destinationRootClientId ) + 1;
}
// Otherwise, we insert at the end of the current rootClientId
return getBlockOrder( destinationRootClientId ).length;
}
const selectionStart = getSelectionStart();
const selectionEnd = getSelectionEnd();
if ( isSelected === undefined ) {
isSelected = (
selectionStart.clientId === clientId &&
selectionStart.attributeKey === identifier
);
}
// If the block of this RichText is unmodified then it's a candidate for replacing when adding a new block.
// In order to fix https://github.com/wordpress-mobile/gutenberg-mobile/issues/1126, let's blur on unmount in that case.
// This apparently assumes functionality the BlockHlder actually
const block = clientId && __unstableGetBlockWithoutInnerBlocks( clientId );
const shouldBlurOnUnmount = block && isSelected && isUnmodifiedDefaultBlock( block );
return {
formatTypes: getFormatTypes(),
selectionStart: isSelected ? selectionStart.offset : undefined,
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
blockIsSelected,
shouldBlurOnUnmount,
};
} ),
withDispatch( ( dispatch, {
return {
nextBlockClientId: getNextBlockClientId(clientId),
isPartOfMultiSelection: isBlockMultiSelected(clientId) || isAncestorMultiSelected(clientId),
isFirstMultiSelected: isFirstMultiSelectedBlock(clientId),
isMultiSelecting: isMultiSelecting(),
hasSelectedInnerBlock: hasSelectedInnerBlock(clientId, false),
// We only care about this prop when the block is selected
// Thus to avoid unnecessary rerenders we avoid updating the prop if the block is not selected.
isTypingWithinBlock: (isSelected || isParentOfSelectedBlock) && isTyping(),
order: getBlockIndex(clientId, rootClientId),
meta: getEditedPostAttribute('meta'),
mode: getBlockMode(clientId),
isSelectionEnabled: isSelectionEnabled(),
initialPosition: getSelectedBlocksInitialCaretPosition(),
isEmptyDefaultBlock: block && isUnmodifiedDefaultBlock(block),
isPreviousBlockADefaultEmptyBlock: previousBlock && isUnmodifiedDefaultBlock(previousBlock),
isMovable: 'all' !== templateLock,
isLocked: !! templateLock,
isFocusMode: focusMode && isLargeViewport,
hasFixedToolbar: hasFixedToolbar && isLargeViewport,
previousBlockClientId,
block,
isSelected,
isParentOfSelectedBlock,
};
});
alternative: 'getEditorBlocks',
hint: 'Blocks serialization pre-processing occurs at save time',
} );
const blocks = state.editor.present.blocks.value;
// WARNING: Any changes to the logic of this function should be verified
// against the implementation of isEditedPostEmpty, which bypasses this
// function for performance' sake, in an assumption of this current logic
// being irrelevant to the optimized condition of emptiness.
// A single unmodified default block is assumed to be equivalent to an
// empty post.
const isSingleUnmodifiedDefaultBlock = (
blocks.length === 1 &&
isUnmodifiedDefaultBlock( blocks[ 0 ] )
);
if ( isSingleUnmodifiedDefaultBlock ) {
return [];
}
return blocks;
}
const { name, attributes, isValid } = block || {};
return {
isPartOfMultiSelection:
isBlockMultiSelected( clientId ) || isAncestorMultiSelected( clientId ),
isFirstMultiSelected: isFirstMultiSelectedBlock( clientId ),
// We only care about this prop when the block is selected
// Thus to avoid unnecessary rerenders we avoid updating the prop if the block is not selected.
isTypingWithinBlock:
( isSelected || isParentOfSelectedBlock ) && isTyping(),
isCaretWithinFormattedText: isCaretWithinFormattedText(),
mode: getBlockMode( clientId ),
isSelectionEnabled: isSelectionEnabled(),
initialPosition: isSelected ? getSelectedBlocksInitialCaretPosition() : null,
isEmptyDefaultBlock:
name && isUnmodifiedDefaultBlock( { name, attributes } ),
isMovable: 'all' !== templateLock,
isLocked: !! templateLock,
isFocusMode: focusMode && isLargeViewport,
hasFixedToolbar: hasFixedToolbar && isLargeViewport,
isLast: index === blockOrder.length - 1,
isNavigationMode: isNavigationMode(),
isRTL,
// Users of the editor.BlockListBlock filter used to be able to access the block prop
// Ideally these blocks would rely on the clientId prop only.
// This is kept for backward compatibility reasons.
block,
name,
attributes,
isValid,
isReplaceable( block: ?BlockType ) {
if ( ! block ) {
return false;
}
return isUnmodifiedDefaultBlock( block );
}
const {
getCurrentPostType,
} = select( 'core/editor' );
const {
getBlockOrder,
getBlock,
getSettings,
} = select( 'core/block-editor' );
const isPageTemplatesEnabled = getSettings().__experimentalEnablePageTemplates;
const blocks = getBlockOrder();
const isEmptyBlockList = blocks.length === 0;
const firstBlock = ! isEmptyBlockList && getBlock( blocks[ 0 ] );
const isOnlyUnmodifiedDefault = blocks.length === 1 && isUnmodifiedDefaultBlock( firstBlock );
const isEmptyContent = isEmptyBlockList || isOnlyUnmodifiedDefault;
const isPage = getCurrentPostType() === 'page';
return isPageTemplatesEnabled && isEmptyContent && isPage;
}, [] );
};
onSelect( item ) {
const {
replaceBlocks,
insertBlock,
} = dispatch( 'core/block-editor' );
const {
getSelectedBlock,
} = select( 'core/block-editor' );
const { isAppender } = ownProps;
const { name, initialAttributes } = item;
const selectedBlock = getSelectedBlock();
const insertedBlock = createBlock( name, initialAttributes );
if ( ! isAppender && selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) {
replaceBlocks( selectedBlock.clientId, insertedBlock );
} else {
const rootClientId = ALLOWED_TOP_LEVEL_BLOCKS.includes( name ) ? undefined : ownProps.destinationRootClientId;
insertBlock(
insertedBlock,
getInsertionIndex( name ),
rootClientId,
);
}
ownProps.onSelect();
},
};
isReplaceable( block ) {
if ( ! block ) {
return false;
}
return isUnmodifiedDefaultBlock( block );
}