Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getMarker, GetMarkerAction, ScriptureReference } from "shared";

// getMarker() takes a marker string and gets its data from a usfm markers map object that is merged with overwrites that fit the PERF editor context.
// getMarkerAction() returns a function to generate a LexicalNode and insert it in the editor, this lexical node is a custom node made for the PERF editor
//NOTE: You can create your own typeahead plugin by creating your own getMarker() and getMarkerAction() functions adapted to your editor needs.
export default function useUsfmMakersForMenu({
// NOTE: You can create your own typeahead plugin by creating your own getMarker() and getMarkerAction() functions adapted to your editor needs.
export default function useUsfmMarkersForMenu({
scriptureReference,
contextMarker,
getMarkerAction,
Expand Down
4 changes: 2 additions & 2 deletions libs/shared-react/src/plugins/PerfTypeahead/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getUsfmMarkerAction, ScriptureReference } from "shared";
import useUsfmMakersForMenu from "../PerfNodesItems/useUsfmMarkersForMenu";
import useUsfmMarkersForMenu from "../PerfNodesItems/useUsfmMarkersForMenu";
import TypeaheadPlugin from "../Typeahead/TypeaheadPlugin";

export default function PerfTypeaheadPlugin({
Expand All @@ -11,7 +11,7 @@ export default function PerfTypeaheadPlugin({
scriptureReference: ScriptureReference;
contextMarker: string;
}) {
const { markersMenuItems } = useUsfmMakersForMenu({
const { markersMenuItems } = useUsfmMarkersForMenu({
scriptureReference,
contextMarker,
getMarkerAction: getUsfmMarkerAction,
Expand Down
4 changes: 2 additions & 2 deletions libs/shared-react/src/plugins/UsfmNodesMenuPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GetMarkerAction, ScriptureReference } from "shared";
import useUsfmMakersForMenu from "./PerfNodesItems/useUsfmMarkersForMenu";
import useUsfmMarkersForMenu from "./PerfNodesItems/useUsfmMarkersForMenu";
import NodesMenu from "./NodesMenu";

export default function UsfmNodesMenuPlugin({
Expand All @@ -13,7 +13,7 @@ export default function UsfmNodesMenuPlugin({
contextMarker: string | undefined;
getMarkerAction: GetMarkerAction;
}) {
const { markersMenuItems } = useUsfmMakersForMenu({
const { markersMenuItems } = useUsfmMarkersForMenu({
scriptureReference,
contextMarker,
getMarkerAction,
Expand Down
66 changes: 66 additions & 0 deletions libs/shared-react/src/plugins/usj/TextSpacingPlugin.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,72 @@ describe("TextSpacingPlugin", () => {
});
});

it("should not add trailing space when next sibling is a CharNode", async () => {
const { editor } = await testEnvironment(() => {
$getRoot().append(
$createParaNode().append(
$createTextNode("abc"),
$createCharNode("nd").append($createTextNode("xyz")),
),
);
});

editor.getEditorState().read(() => {
const para = $getRoot().getFirstChild();
if (!$isParaNode(para)) throw new Error("Expected a ParaNode");
expect(para.getChildren()).toHaveLength(2);
const textNode = para.getChildAtIndex(0);
if (!$isTextNode(textNode)) throw new Error("Expected a TextNode");
expect(textNode.getTextContent()).toBe("abc");
const charNode = para.getChildAtIndex(1);
if (!$isCharNode(charNode)) throw new Error("Expected a CharNode");
expect(charNode.getTextContent()).toBe("xyz");
});
});

it("should not add trailing space when next sibling is a TypedMarkNode", async () => {
const { editor } = await testEnvironment(() => {
$getRoot().append(
$createParaNode().append(
$createTextNode("abc"),
$createTypedMarkNode({ testType1: ["testID1"] }).append($createTextNode("marked")),
),
);
});

editor.getEditorState().read(() => {
const para = $getRoot().getFirstChild();
if (!$isParaNode(para)) throw new Error("Expected a ParaNode");
expect(para.getChildren()).toHaveLength(2);
const textNode = para.getChildAtIndex(0);
if (!$isTextNode(textNode)) throw new Error("Expected a TextNode");
expect(textNode.getTextContent()).toBe("abc");
const markNode = para.getChildAtIndex(1);
if (!$isTypedMarkNode(markNode)) throw new Error("Expected a TypedMarkNode");
expect(markNode.getTextContent()).toBe("marked");
});
});

it("should preserve space-only TextNode when next sibling is a CharNode", async () => {
const { editor } = await testEnvironment(() => {
$getRoot().append(
$createParaNode().append(
$createTextNode(" "),
$createCharNode("nd").append($createTextNode("xyz")),
),
);
});

editor.getEditorState().read(() => {
const para = $getRoot().getFirstChild();
if (!$isParaNode(para)) throw new Error("Expected a ParaNode");
expect(para.getChildren()).toHaveLength(2);
const spaceNode = para.getChildAtIndex(0);
if (!$isTextNode(spaceNode)) throw new Error("Expected a TextNode");
expect(spaceNode.getTextContent()).toBe(" ");
});
});

it("should add a space if typing before an initial verse in a para", async () => {
const { editor } = await testEnvironment();

Expand Down
7 changes: 7 additions & 0 deletions libs/shared-react/src/plugins/usj/TextSpacingPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ function useTextSpacing(editor: LexicalEditor) {
* Adds a space to the end of a TextNode if it doesn't precede a note or isn't inside a CharNode,
* TypedMarkNode, or UnknownNode. It doesn't add a space if the text node is not editable. It
* removes a TextNode with only a space if it is not followed by a verse node.
*
* When the next sibling is a CharNode or TypedMarkNode (inline marker or annotation), we skip
* both adding trailing space and the space-only cleanup below, so spacing before inline content
* is left unchanged.
*
* @param node - TextNode that might need updating.
*/
function $textNodeTrailingSpaceTransform(node: TextNode): void {
Expand All @@ -65,7 +70,9 @@ function $textNodeTrailingSpaceTransform(node: TextNode): void {
(text.endsWith(" ") && text.length > 1) ||
$isNoteNode(nextSibling) ||
$isCharNode(parent) ||
$isCharNode(nextSibling) ||
$isTypedMarkNode(parent) ||
$isTypedMarkNode(nextSibling) ||
$isUnknownNode(parent)
)
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/scribe/src/editor/plugins/UsfmNodesMenuPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GetMarkerAction, ScriptureReference } from "shared";
import useUsfmMakersForMenu from "@/hooks/useUsfmMarkersForMenu";
import useUsfmMarkersForMenu from "@/hooks/useUsfmMarkersForMenu";
import NodesMenu from "@/components/NodesMenu/NodesMenu";

export default function UsfmNodesMenuPlugin({
Expand All @@ -15,7 +15,7 @@ export default function UsfmNodesMenuPlugin({
getMarkerAction: GetMarkerAction;
autoNumbering?: boolean;
}) {
const { markersMenuItems } = useUsfmMakersForMenu({
const { markersMenuItems } = useUsfmMarkersForMenu({
scriptureReference,
contextMarker,
getMarkerAction,
Expand Down
4 changes: 2 additions & 2 deletions packages/scribe/src/hooks/useUsfmMarkersForMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getMarker, GetMarkerAction, ScriptureReference } from "shared";

// getMarker() takes a marker string and gets its data from a usfm markers map object that is merged with overwrites that fit the PERF editor context.
// getMarkerAction() returns a function to generate a LexicalNode and insert it in the editor, this lexical node is a custom node made for the PERF editor
//NOTE: You can create your own typeahead plugin by creating your own getMarker() and getMarkerAction() functions adapted to your editor needs.
export default function useUsfmMakersForMenu({
// NOTE: You can create your own typeahead plugin by creating your own getMarker() and getMarkerAction() functions adapted to your editor needs.
export default function useUsfmMarkersForMenu({
scriptureReference,
contextMarker,
getMarkerAction,
Expand Down
Loading