Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich text: avoid createRecord for delete handler #48413

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 3 additions & 21 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,6 @@ export function useRichText( {
const [ , forceRender ] = useReducer( () => ( {} ) );
const ref = useRef();

function createRecord() {
const {
ownerDocument: { defaultView },
} = ref.current;
const selection = defaultView.getSelection();
const range =
selection.rangeCount > 0 ? selection.getRangeAt( 0 ) : null;

return create( {
element: ref.current,
range,
multilineTag,
multilineWrapperTags:
multilineTag === 'li' ? [ 'ul', 'ol' ] : undefined,
__unstableIsEditableTree: true,
preserveWhiteSpace,
} );
}

function applyRecord( newRecord, { domOnly } = {} ) {
apply( {
value: newRecord,
Expand Down Expand Up @@ -228,17 +209,18 @@ export function useRichText( {
useSelectObject(),
useFormatBoundaries( { record, applyRecord } ),
useDelete( {
createRecord,
record,
handleChange,
multilineTag,
} ),
useInputAndSelection( {
record,
applyRecord,
createRecord,
handleChange,
isSelected,
onSelectionChange,
multilineTag,
preserveWhiteSpace,
} ),
useRefEffect( () => {
applyFromProps();
Expand Down
18 changes: 8 additions & 10 deletions packages/rich-text/src/component/use-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export function useDelete( props ) {
return useRefEffect( ( element ) => {
function onKeyDown( event ) {
const { keyCode } = event;
const { createRecord, handleChange, multilineTag } =
propsRef.current;
const { record, handleChange, multilineTag } = propsRef.current;

if ( event.defaultPrevented ) {
return;
Expand All @@ -29,13 +28,12 @@ export function useDelete( props ) {
return;
}

const currentValue = createRecord();
const { start, end, text } = currentValue;
const { start, end, text } = record;
const isReverse = keyCode === BACKSPACE;

// Always handle full content deletion ourselves.
if ( start === 0 && end !== 0 && end === text.length ) {
handleChange( remove( currentValue ) );
handleChange( remove( record ) );
event.preventDefault();
return;
}
Expand All @@ -46,13 +44,13 @@ export function useDelete( props ) {
// Check to see if we should remove the first item if empty.
if (
isReverse &&
currentValue.start === 0 &&
currentValue.end === 0 &&
isEmptyLine( currentValue )
record.start === 0 &&
record.end === 0 &&
isEmptyLine( record )
) {
newValue = removeLineSeparator( currentValue, ! isReverse );
newValue = removeLineSeparator( record, ! isReverse );
} else {
newValue = removeLineSeparator( currentValue, isReverse );
newValue = removeLineSeparator( record, isReverse );
}

if ( newValue ) {
Expand Down
31 changes: 21 additions & 10 deletions packages/rich-text/src/component/use-input-and-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useRefEffect } from '@wordpress/compose';
*/
import { getActiveFormats } from '../get-active-formats';
import { updateFormats } from '../update-formats';
import { create } from '../create';

/**
* All inserting input types that would insert HTML into the DOM.
Expand Down Expand Up @@ -62,9 +63,26 @@ export function useInputAndSelection( props ) {
return useRefEffect( ( element ) => {
const { ownerDocument } = element;
const { defaultView } = ownerDocument;
const selection = defaultView.getSelection();

let isComposing = false;

function createRecord() {
const { multilineTag, preserveWhiteSpace } = propsRef.current;
const range =
selection.rangeCount > 0 ? selection.getRangeAt( 0 ) : null;

return create( {
element,
range,
multilineTag,
multilineWrapperTags:
multilineTag === 'li' ? [ 'ul', 'ol' ] : undefined,
__unstableIsEditableTree: true,
preserveWhiteSpace,
} );
}

function onInput( event ) {
// Do not trigger a change if characters are being composed.
// Browsers will usually emit a final `input` event when the
Expand All @@ -81,8 +99,7 @@ export function useInputAndSelection( props ) {
inputType = event.inputType;
}

const { record, applyRecord, createRecord, handleChange } =
propsRef.current;
const { record, applyRecord, handleChange } = propsRef.current;

// The browser formatted something or tried to insert HTML.
// Overwrite it. It will be handled later by the format library if
Expand Down Expand Up @@ -118,13 +135,8 @@ export function useInputAndSelection( props ) {
* @param {Event} event
*/
function handleSelectionChange( event ) {
const {
record,
applyRecord,
createRecord,
isSelected,
onSelectionChange,
} = propsRef.current;
const { record, applyRecord, isSelected, onSelectionChange } =
propsRef.current;

// Check if the implementor disabled editing. `contentEditable`
// does disable input, but not text selection, so we must ignore
Expand All @@ -151,7 +163,6 @@ export function useInputAndSelection( props ) {
return;
}

const selection = defaultView.getSelection();
const { anchorNode, focusNode } = selection;

if (
Expand Down