@@ -13,7 +13,7 @@ import {
1313} from "lexical" ;
1414import { $createNodeFromSerializedNode } from "shared/converters/usfm/emptyUsfmNodes" ;
1515import { $isTypedMarkNode } from "shared/nodes/features/TypedMarkNode" ;
16- import { CharNode } from "shared/nodes/scripture/usj/CharNode" ;
16+ import { $isCharNode , CharNode } from "shared/nodes/scripture/usj/CharNode" ;
1717import { $isNoteNode } from "shared/nodes/scripture/usj/NoteNode" ;
1818import { getNextVerse } from "shared/nodes/scripture/usj/node.utils" ;
1919import { ParaNode } from "shared/nodes/scripture/usj/ParaNode" ;
@@ -270,22 +270,24 @@ function handleTextNode(
270270 const start = isFirst ? startOffset : 0 ;
271271 const end = isLast ? endOffset : textLength ;
272272
273- if ( start === 0 && end === 0 ) {
274- return ;
275- }
273+ if ( start === 0 && end === 0 ) return ;
276274
277275 const splitNodes = node . splitText ( start , end ) ;
278276
279- if ( splitNodes . length === 1 ) {
280- return splitNodes [ 0 ] ;
281- }
277+ if ( splitNodes . length === 1 ) return splitNodes [ 0 ] ;
282278
283- return splitNodes . length === 3 || isFirst || end === textLength ? splitNodes [ 1 ] : splitNodes [ 0 ] ;
279+ return splitNodes . length === 3 || end === textLength ? splitNodes [ 1 ] : splitNodes [ 0 ] ;
284280}
285281
286282function $wrapNode ( node : LexicalNode , wrapper : LexicalNode ) : void {
287283 if ( $isTextNode ( wrapper ) ) {
288- wrapper . setTextContent ( node . getTextContent ( ) ) ;
284+ let text = node . getTextContent ( ) ;
285+ // CharNodes can't start with a space.
286+ if ( $isTextNode ( node ) && $isCharNode ( wrapper ) && text . startsWith ( " " ) ) {
287+ wrapper . insertBefore ( $createTextNode ( " " ) ) ;
288+ text = text . trimStart ( ) ;
289+ }
290+ wrapper . setTextContent ( text ) ;
289291 node . remove ( ) ;
290292 } else if ( $isElementNode ( wrapper ) ) {
291293 wrapper . clear ( ) ;
0 commit comments