Skip to content

Commit 314ee5a

Browse files
authored
platform: fix inserting CharNodes (#264)
- ensure leading space in CharNode is moved outside it - fix `$getTargetNode` when selection has no leading space
1 parent f0eb07e commit 314ee5a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

packages/platform/src/editor/adaptors/usj-marker-action.utils.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "lexical";
1414
import { $createNodeFromSerializedNode } from "shared/converters/usfm/emptyUsfmNodes";
1515
import { $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";
1717
import { $isNoteNode } from "shared/nodes/scripture/usj/NoteNode";
1818
import { getNextVerse } from "shared/nodes/scripture/usj/node.utils";
1919
import { 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

286282
function $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

Comments
 (0)