Skip to content

Commit

Permalink
POC: DON'T MERGE THIS. Allow tab keypress formatting in rich text fie…
Browse files Browse the repository at this point in the history
…lds in editor.
  • Loading branch information
jeryj committed Sep 6, 2023
1 parent 401cf4c commit 25a79f2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { useBeforeInputRules } from './use-before-input-rules';
import { useInputRules } from './use-input-rules';
import { useDelete } from './use-delete';
import { useEnter } from './use-enter';
import { useTab } from './use-tab';
import { useFormatTypes } from './use-format-types';
import { useRemoveBrowserShortcuts } from './use-remove-browser-shortcuts';
import { useShortcuts } from './use-shortcuts';
Expand Down Expand Up @@ -399,6 +400,10 @@ function RichTextWrapper(
disableLineBreaks,
onSplitAtEnd,
} ),
useTab( {
value,
onChange,
} ),
useFirefoxCompat(),
anchorRef,
] ) }
Expand Down
38 changes: 38 additions & 0 deletions packages/block-editor/src/components/rich-text/use-tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { insert } from '@wordpress/rich-text';
import { useRefEffect } from '@wordpress/compose';
import { TAB } from '@wordpress/keycodes';

export function useTab( props ) {
const propsRef = useRef( props );
propsRef.current = props;
return useRefEffect( ( element ) => {
function onKeyDown( event ) {
const { keyCode } = event;

if ( event.defaultPrevented ) {
return;
}

const { value, onChange } = propsRef.current;
const _value = { ...value };

if ( keyCode === TAB ) {
event.preventDefault();

const { start, end } = value;

onChange( insert( _value, '\t', start, end ) );
}
}

element.addEventListener( 'keydown', onKeyDown );
return () => {
element.removeEventListener( 'keydown', onKeyDown );
};
}, [] );

}
2 changes: 1 addition & 1 deletion packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ function filterRange( node, range, filter ) {
* @param {string} string
*/
function collapseWhiteSpace( string ) {
return string.replace( /[\n\r\t]+/g, ' ' );
return string.replace( /[\n\r]+/g, ' ' );
}

/**
Expand Down

0 comments on commit 25a79f2

Please sign in to comment.