Skip to content

Commit a295c51

Browse files
authored
fix(frontend): Prevent rendering error if rich text contains tab characters (#66)
Lexical rich text supports entering tab characters. I just want to render these as normal space characters. !release
1 parent 20e4b53 commit a295c51

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

apps/frontend/app/components/rich-text/rich-text.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
unsupportedElementWithoutChildren,
1818
lineBreak,
1919
listitem,
20+
tab,
2021
} from "@fxmk/shared";
2122
import type { PropsWithChildren } from "react";
2223

@@ -305,6 +306,16 @@ test("line breaks are rendered as <br /> elements.", () => {
305306
`);
306307
});
307308

309+
test("tabs are rendered as whitespace.", () => {
310+
render(
311+
<RichText
312+
content={richTextRoot(paragraph(text("hello"), tab(), text("world")))}
313+
/>,
314+
);
315+
316+
expect(screen.getByText("hello world")).toBeInTheDocument();
317+
});
318+
308319
describe("custom elements", () => {
309320
test("if a custom bold element is specified, it is used for bold text nodes", () => {
310321
function CustomHighlight({ children }: PropsWithChildren) {

apps/frontend/app/components/rich-text/rich-text.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ function RenderedNode({ node, isLast }: { node: Node; isLast: boolean }) {
275275
return <elements.linebreak />;
276276
}
277277

278+
// Lexical tab nodes shall just render a normal space
279+
if (node.type === "tab") {
280+
return " ";
281+
}
282+
278283
return <RenderedElementNode node={node} isLast={isLast} />;
279284
}
280285

libs/shared/src/rich-text.builders.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type LinkElementNode,
1313
type LineBreakNode,
1414
type ParagraphElementNode,
15+
type TabNode,
1516
} from "./rich-text.model";
1617

1718
export function richTextRoot(...children: ElementNode[]): RichTextObject {
@@ -49,6 +50,10 @@ export function lineBreak(): LineBreakNode {
4950
return { type: "linebreak" };
5051
}
5152

53+
export function tab(): TabNode {
54+
return { type: "tab" };
55+
}
56+
5257
export function bold(t: string): TextNode {
5358
return text(t, { bold: true });
5459
}

libs/shared/src/rich-text.model.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ export type ElementNode =
2121
| HeadingElementNode
2222
| BlockElementNode;
2323

24-
export type Node = ElementNode | TextNode | LineBreakNode;
24+
export type Node = ElementNode | TextNode | LineBreakNode | TabNode;
2525

2626
export type LineBreakNode = { type: "linebreak" };
2727

28+
export type TabNode = { type: "tab" };
29+
2830
type ElementNodeWithChildren = { children: Node[] };
2931

3032
export type ListItemElementNode = ElementNodeWithChildren & {

0 commit comments

Comments
 (0)