Skip to content

Commit

Permalink
Rich text: Try debouncing useInput to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Sep 26, 2023
1 parent 9660466 commit 245bf40
Showing 1 changed file with 45 additions and 41 deletions.
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 );
},
50,
{ leading: true, trailing: false }
);

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

0 comments on commit 245bf40

Please sign in to comment.