Skip to content

Commit f24aa76

Browse files
committed
fix: fixed regression
1 parent 89edd87 commit f24aa76

File tree

6 files changed

+30
-13
lines changed

6 files changed

+30
-13
lines changed

examples/src/Custom.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ const Custom = () => {
2727
<RichTextEditor.Provider
2828
content={content}
2929
extensions={[
30-
StarterKit.configure({
31-
link: false, // Disable link from StarterKit to avoid conflicts with our custom one
32-
}),
30+
StarterKit,
3331
Link,
3432
Placeholder.configure({
3533
placeholder: "Write something …",

examples/src/Markdown.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const extensionLoader = {
1111

1212
const initialContent = `
1313
# Tiptap Editor
14+
1415
The Tiptap Editor is a headless, framework-agnostic rich text editor that's customizable and extendable through extensions. Its headless nature means it comes without a set user interface, offering full design freedom (for a jumpstart, see linked [UI templates](#examples-codesandbox-and-ui-templates) below). Tiptap is based on the highly reliable [ProseMirror](https://github.com/ProseMirror/prosemirror) library.
1516
1617
Tiptap Editor is complemented by the collaboration open-source backend [Hocuspocus](https://github.com/ueberdosis/hocuspocus). Both the Editor and Hocuspocus form the foundation of the [Tiptap Suite](https://tiptap.dev/).

packages/react-dsfr-tiptap/src/components/Provider.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fr } from "@codegouvfr/react-dsfr";
22
import { useEditor, UseEditorOptions } from "@tiptap/react";
3-
import { ReactNode } from "react";
3+
import { ReactNode, useEffect, useMemo } from "react";
44
import { tss } from "tss-react";
55

66
import { editorContext } from "../contexts/editor";
@@ -10,13 +10,25 @@ export interface IProviderProps extends UseEditorOptions {
1010
}
1111

1212
function Provider(props: IProviderProps) {
13-
const { children, ...rest } = props;
13+
const { children, contentType, content, ...rest } = props;
1414
const editor = useEditor(rest);
1515
const { classes } = useStyles();
1616

17+
useEffect(() => {
18+
if (content != null && editor != null) {
19+
const oldContent = contentType === "markdown" ? editor.getMarkdown() : editor.getHTML();
20+
21+
if (oldContent !== content) {
22+
editor.commands.setContent(content, { contentType });
23+
}
24+
}
25+
}, [content, editor, contentType]);
26+
27+
const contextValue = useMemo(() => editor, [editor]);
28+
1729
return (
1830
<div className={classes.root}>
19-
<editorContext.Provider value={editor}>{children}</editorContext.Provider>
31+
<editorContext.Provider value={contextValue}>{children}</editorContext.Provider>
2032
</div>
2133
);
2234
}

packages/react-dsfr-tiptap/src/components/RichTextEditor.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@ describe("RichTextEditor component", () => {
1212
expect(title).toBeInTheDocument();
1313
expect(title instanceof HTMLHeadingElement).toBe(true);
1414
});
15+
16+
test("updates editor when content prop changes", async () => {
17+
const { rerender } = render(<RichTextEditor content="<p>First</p>" />);
18+
await waitFor(() => expect(screen.queryAllByRole("button").length).toBeGreaterThan(0));
19+
expect(screen.getByText("First")).toBeInTheDocument();
20+
21+
// Change content prop simulating a reset
22+
rerender(<RichTextEditor content="<h2>Second</h2>" />);
23+
// The new heading should appear, old text should be gone
24+
await waitFor(() => expect(screen.getByText("Second")).toBeInTheDocument());
25+
});
1526
});

packages/react-dsfr-tiptap/src/constants/richTextEditor.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,4 @@ export const richTextEditorDefaultControls: Control[][] = [
1010
["Undo", "Redo"],
1111
];
1212

13-
export const richTextEditorDefaultExtensions: AnyExtension[] = [
14-
StarterKit.configure({
15-
// Disable extensions from StarterKit to avoid conflicts with our custom ones
16-
link: false,
17-
underline: false,
18-
}),
19-
];
13+
export const richTextEditorDefaultExtensions: AnyExtension[] = [StarterKit];

packages/react-dsfr-tiptap/src/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import "@tiptap/extension-color";
22
import "@tiptap/extension-image";
33
import "@tiptap/extension-link";
44
import "@tiptap/extension-youtube";
5+
import "@tiptap/markdown";

0 commit comments

Comments
 (0)