-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: preserve multiple spaces on front-end #56280
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { useRefEffect } from '@wordpress/compose'; | |
*/ | ||
import { getActiveFormats } from '../get-active-formats'; | ||
import { updateFormats } from '../update-formats'; | ||
import { isCollapsed } from '../is-collapsed'; | ||
|
||
/** | ||
* All inserting input types that would insert HTML into the DOM. | ||
|
@@ -96,15 +97,59 @@ export function useInputAndSelection( props ) { | |
return; | ||
} | ||
|
||
const currentValue = createRecord(); | ||
const { start, activeFormats: oldActiveFormats = [] } = | ||
const newValue = createRecord(); | ||
const { start: oldStart, activeFormats: oldActiveFormats = [] } = | ||
record.current; | ||
|
||
const SP = ' '; | ||
const NBSP = '\u00a0'; | ||
const isSpOrNbsp = ( char ) => char === SP || char === NBSP; | ||
|
||
// When inserting multiple spaces, alternate between a normal space | ||
// and a non-breaking space. This is to prevent browsers from | ||
// collapsing multiple spaces into one. | ||
if ( | ||
isCollapsed( newValue ) && | ||
oldStart !== newValue.start && | ||
( oldStart < newValue.start | ||
? // Check if the inserted character is a space. | ||
newValue.text.slice( oldStart, newValue.start ) === SP | ||
: // Check if the deleted character is a space or | ||
// non-breaking space (in both cases we need to fix the | ||
// alternating pattern). | ||
isSpOrNbsp( | ||
record.current.text.slice( | ||
newValue.start, | ||
oldStart | ||
) | ||
) ) | ||
) { | ||
const text = newValue.text; | ||
let startOffset = newValue.start; | ||
let endOffset = newValue.start; | ||
|
||
// We need to make sure the alternating pattern is maintained, | ||
// so replace all spaces before and after the selection. | ||
while ( isSpOrNbsp( text[ startOffset - 1 ] ) ) { | ||
startOffset--; | ||
} | ||
while ( isSpOrNbsp( text[ endOffset ] ) ) { | ||
endOffset++; | ||
} | ||
|
||
newValue.text = | ||
text.slice( 0, startOffset ) + | ||
Array.from( { length: endOffset - startOffset } ) | ||
.map( ( _, index ) => ( index % 2 === 0 ? SP : NBSP ) ) | ||
.join( '' ) + | ||
text.slice( endOffset ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it make sense here to use a regular expression to handle this? // Replace all successive spaces with an alternating pattern of space/nbsp.
newValue.text = newValue.replace( /[ \u00a0]+/g, match => ''.padStart( match.length, ' \u00a0' ) ); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we want to be replacing all spaces. We shouldn't modify existing content too much imo, only the spaces around the selection. |
||
} | ||
|
||
// Update the formats between the last and new caret position. | ||
const change = updateFormats( { | ||
value: currentValue, | ||
start, | ||
end: currentValue.start, | ||
value: newValue, | ||
start: oldStart, | ||
end: newValue.start, | ||
formats: oldActiveFormats, | ||
} ); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yikes. this seems good, but dang - so hard to make an editor in the browser!