Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ export function RichTextWrapper(
return { isSelected: false };
}

const { getSelectionStart, getSelectionEnd } =
select( blockEditorStore );
const {
getSelectionStart,
getSelectionEnd,
getBlockEditingMode,
isNavigationMode,
} = select( blockEditorStore );
const selectionStart = getSelectionStart();
const selectionEnd = getSelectionEnd();

Expand All @@ -154,15 +158,19 @@ export function RichTextWrapper(
selectionStart: isSelected ? selectionStart.offset : undefined,
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
isContentOnlyWriteMode:
isNavigationMode() &&
getBlockEditingMode( clientId ) === 'contentOnly',
};
};
const { selectionStart, selectionEnd, isSelected } = useSelect( selector, [
clientId,
identifier,
instanceId,
originalIsSelected,
isBlockSelected,
] );
const { selectionStart, selectionEnd, isSelected, isContentOnlyWriteMode } =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you didn't introduce it in this PR, just wondering if there's a reason why selector is passed as a const here rather than just doing it inline like we usually do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. Seems odd.

useSelect( selector, [
clientId,
identifier,
instanceId,
originalIsSelected,
isBlockSelected,
] );

const { disableBoundBlock, bindingsPlaceholder, bindingsLabel } = useSelect(
( select ) => {
Expand Down Expand Up @@ -323,8 +331,9 @@ export function RichTextWrapper(
} = useFormatTypes( {
clientId,
identifier,
withoutInteractiveFormatting,
allowedFormats: adjustedAllowedFormats,
withoutInteractiveFormatting,
disableNoneEssentialFormatting: isContentOnlyWriteMode,
} );

function addEditorOnlyFormats( value ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as richTextStore } from '@wordpress/rich-text';

/**
* Internal dependencies
*/
import { essentialFormatKey } from '../../store/private-keys';

function formatTypesSelector( select ) {
return select( richTextStore ).getFormatTypes();
}
Expand Down Expand Up @@ -56,35 +61,53 @@ function getPrefixedSelectKeys( selected, prefix ) {
* This hook provides RichText with the `formatTypes` and its derived props from
* experimental format type settings.
*
* @param {Object} $0 Options
* @param {string} $0.clientId Block client ID.
* @param {string} $0.identifier Block attribute.
* @param {boolean} $0.withoutInteractiveFormatting Whether to clean the interactive formatting or not.
* @param {Array} $0.allowedFormats Allowed formats
* @param {Object} options Options
* @param {string} options.clientId Block client ID.
* @param {string} options.identifier Block attribute.
* @param {Array} options.allowedFormats Allowed formats
* @param {boolean} options.withoutInteractiveFormatting Whether to clean the interactive formatting or not.
* @param {boolean} options.disableNoneEssentialFormatting Whether to disable none-essential formatting or not.
*/
export function useFormatTypes( {
clientId,
identifier,
withoutInteractiveFormatting,
allowedFormats,
withoutInteractiveFormatting,
disableNoneEssentialFormatting = false,
} ) {
const allFormatTypes = useSelect( formatTypesSelector, [] );
const formatTypes = useMemo( () => {
return allFormatTypes.filter( ( { name, interactive, tagName } ) => {
if ( allowedFormats && ! allowedFormats.includes( name ) ) {
return false;
}
return allFormatTypes.filter(
( {
name,
interactive,
tagName,
[ essentialFormatKey ]: isEssential,
} ) => {
if ( allowedFormats && ! allowedFormats.includes( name ) ) {
return false;
}

if (
withoutInteractiveFormatting &&
( interactive || interactiveContentTags.has( tagName ) )
) {
return false;
}
if ( disableNoneEssentialFormatting && ! isEssential ) {
return false;
}

return true;
} );
}, [ allFormatTypes, allowedFormats, withoutInteractiveFormatting ] );
if (
withoutInteractiveFormatting &&
( interactive || interactiveContentTags.has( tagName ) )
) {
return false;
}

return true;
}
);
}, [
allFormatTypes,
allowedFormats,
disableNoneEssentialFormatting,
withoutInteractiveFormatting,
] );
const keyedSelected = useSelect(
( select ) =>
formatTypes.reduce( ( accumulator, type ) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
globalStylesLinksDataKey,
sectionRootClientIdKey,
mediaEditKey,
essentialFormatKey,
} from './store/private-keys';
import { requiresWrapperOnCopy } from './components/writing-flow/utils';
import { PrivateRichText } from './components/rich-text/';
Expand Down Expand Up @@ -101,4 +102,5 @@ lock( privateApis, {
CommentIconSlotFill,
CommentIconToolbarSlotFill,
mediaEditKey,
essentialFormatKey,
} );
1 change: 1 addition & 0 deletions packages/block-editor/src/store/private-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const selectBlockPatternsKey = Symbol( 'selectBlockPatternsKey' );
export const reusableBlocksSelectKey = Symbol( 'reusableBlocksSelect' );
export const sectionRootClientIdKey = Symbol( 'sectionRootClientIdKey' );
export const mediaEditKey = Symbol( 'mediaEditKey' );
export const essentialFormatKey = Symbol( 'essentialFormat' );
9 changes: 9 additions & 0 deletions packages/format-library/src/bold/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ import {
RichTextToolbarButton,
RichTextShortcut,
__unstableRichTextInputEvent,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { formatBold } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';

const { essentialFormatKey } = unlock( blockEditorPrivateApis );

const name = 'core/bold';
const title = __( 'Bold' );

Expand All @@ -18,6 +26,7 @@ export const bold = {
title,
tagName: 'strong',
className: null,
[ essentialFormatKey ]: true,
edit( { isActive, value, onChange, onFocus } ) {
function onToggle() {
onChange( toggleFormat( value, { type: name, title } ) );
Expand Down
9 changes: 9 additions & 0 deletions packages/format-library/src/italic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ import {
RichTextToolbarButton,
RichTextShortcut,
__unstableRichTextInputEvent,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { formatItalic } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';

const { essentialFormatKey } = unlock( blockEditorPrivateApis );

const name = 'core/italic';
const title = __( 'Italic' );

Expand All @@ -18,6 +26,7 @@ export const italic = {
title,
tagName: 'em',
className: null,
[ essentialFormatKey ]: true,
edit( { isActive, value, onChange, onFocus } ) {
function onToggle() {
onChange( toggleFormat( value, { type: name, title } ) );
Expand Down
5 changes: 5 additions & 0 deletions packages/format-library/src/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isURL, isEmail, isPhoneNumber } from '@wordpress/url';
import {
RichTextToolbarButton,
RichTextShortcut,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { decodeEntities } from '@wordpress/html-entities';
import { link as linkIcon } from '@wordpress/icons';
Expand All @@ -26,6 +27,9 @@ import { speak } from '@wordpress/a11y';
*/
import InlineLinkUI from './inline';
import { isValidHref } from './utils';
import { unlock } from '../lock-unlock';

const { essentialFormatKey } = unlock( blockEditorPrivateApis );

const name = 'core/link';
const title = __( 'Link' );
Expand Down Expand Up @@ -228,6 +232,7 @@ export const link = {
target: 'target',
rel: 'rel',
},
[ essentialFormatKey ]: true,
__unstablePasteRule( value, { html, plainText } ) {
const pastedText = ( html || plainText )
.replace( /<[^>]+>/g, '' )
Expand Down
Loading