Skip to content

Commit

Permalink
Rich text: pad multiple spaces through en/em replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 20, 2023
1 parent 59c0356 commit 20c9c93
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ function findSelection( blocks ) {
return [];
}

function convertSpaces( value ) {
const { text, start } = value;
const lastTwoCharacters = text.slice( start - 2, start );

// Replace two spaces with an em space.
if ( lastTwoCharacters === ' ' ) {
return insert( value, '\u2002', start - 2, start );
}
// Replace an en space followed by a space with an em space.
else if ( lastTwoCharacters === '\u2002 ' ) {
return insert( value, '\u2003', start - 2, start );
}

return value;
}

export function useInputRules( props ) {
const {
__unstableMarkLastChangeAsPersistent,
Expand Down Expand Up @@ -122,7 +138,7 @@ export function useInputRules( props ) {

return accumlator;
},
preventEventDiscovery( value )
preventEventDiscovery( convertSpaces( value ) )
);

if ( transformed !== value ) {
Expand Down

0 comments on commit 20c9c93

Please sign in to comment.