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: Try debouncing useInput to improve performance and fix infinite loop #54819

Closed
wants to merge 2 commits into from
Closed
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
86 changes: 45 additions & 41 deletions packages/rich-text/src/component/use-input-and-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
import { debounce, useRefEffect } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -65,51 +65,55 @@ export function useInputAndSelection( props ) {

let isComposing = false;

function onInput( event ) {
// Do not trigger a change if characters are being composed.
// Browsers will usually emit a final `input` event when the
// characters are composed.
// As of December 2019, Safari doesn't support
// nativeEvent.isComposing.
if ( isComposing ) {
return;
}

let inputType;

if ( event ) {
inputType = event.inputType;
}
const onInput = debounce(
( event ) => {
// Do not trigger a change if characters are being composed.
// Browsers will usually emit a final `input` event when the
// characters are composed.
// As of December 2019, Safari doesn't support
// nativeEvent.isComposing.
if ( isComposing ) {
return;
}

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

// The browser formatted something or tried to insert HTML.
// Overwrite it. It will be handled later by the format library if
// needed.
if (
inputType &&
( inputType.indexOf( 'format' ) === 0 ||
INSERTION_INPUT_TYPES_TO_IGNORE.has( inputType ) )
) {
applyRecord( record.current );
return;
}
if ( event ) {
inputType = event.inputType;
}

const currentValue = createRecord();
const { start, activeFormats: oldActiveFormats = [] } =
record.current;
const { record, applyRecord, createRecord, handleChange } =
propsRef.current;

// Update the formats between the last and new caret position.
const change = updateFormats( {
value: currentValue,
start,
end: currentValue.start,
formats: oldActiveFormats,
} );
// The browser formatted something or tried to insert HTML.
// Overwrite it. It will be handled later by the format library if
// needed.
if (
inputType &&
( inputType.indexOf( 'format' ) === 0 ||
INSERTION_INPUT_TYPES_TO_IGNORE.has( inputType ) )
) {
applyRecord( record.current );
return;
}

handleChange( change );
}
const currentValue = createRecord();
const { start, activeFormats: oldActiveFormats = [] } =
record.current;

// Update the formats between the last and new caret position.
const change = updateFormats( {
value: currentValue,
start,
end: currentValue.start,
formats: oldActiveFormats,
} );

handleChange( change );
},
1,
{ leading: true, trailing: false }
);

/**
* Syncs the selection to local state. A callback for the
Expand Down
Loading