Skip to content

Commit 8b26e03

Browse files
committed
fix: controlled component for markdown editor
1 parent 691f0dc commit 8b26e03

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

examples/src/Custom.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@ import { ControlImage, ControlLink, ControlUnlink, ControlYoutube } from "react-
44
import StarterKit from "@tiptap/starter-kit";
55
import Link from "@tiptap/extension-link";
66
import Placeholder from "@tiptap/extension-placeholder";
7+
import Button from "@codegouvfr/react-dsfr/Button";
8+
import { fr } from "@codegouvfr/react-dsfr";
79

810
import { CustomControl1, CustomControl2, CustomControl3 } from "./TiptapCustomButtons";
911
import "./custom.css";
1012

13+
const initialContent = `<p>Hello, <strong>World!</strong></p>`;
14+
1115
const Custom = () => {
12-
const [content, setContent] = useState("");
16+
const [content, setContent] = useState(initialContent);
17+
18+
function reset() {
19+
setContent(initialContent);
20+
}
1321

1422
return (
1523
<>
24+
<Button className={fr.cx("fr-mt-2w")} type="button" onClick={reset}>
25+
Reset content
26+
</Button>
1627
<RichTextEditor.Provider
1728
content={content}
1829
extensions={[

examples/src/Markdown.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useState } from "react";
22
import { ControlImage, ControlLink, ControlUnlink } from "react-dsfr-tiptap/dialog";
33
import { MarkdownEditor } from "react-dsfr-tiptap/markdown";
4+
import Button from "@codegouvfr/react-dsfr/Button";
5+
import { fr } from "@codegouvfr/react-dsfr";
46

57
const extensionLoader = {
68
image: () => import("@tiptap/extension-image").then((module) => module.default),
@@ -25,8 +27,15 @@ Tiptap Editor is complemented by the collaboration open-source backend [Hocuspoc
2527
const Markdown = () => {
2628
const [content, setContent] = useState(initialContent);
2729

30+
function reset() {
31+
setContent(initialContent);
32+
}
33+
2834
return (
2935
<>
36+
<Button className={fr.cx("fr-mt-2w")} type="button" onClick={reset}>
37+
Reset content
38+
</Button>
3039
<MarkdownEditor
3140
controlMap={{ Link: ControlLink, Unlink: ControlUnlink, Image: ControlImage }}
3241
content={content}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ const MarkdownEditor = ((props: IMarkdownEditorProps) => {
3535
onContentUpdate?.(props.editor.storage.markdown.getMarkdown());
3636
}
3737

38-
return <RichTextEditorLoader controls={markdownEditorDefaultControls} extensions={markdownEditorDefaultExtensions} onUpdate={handleUpdate} {...rest} />;
38+
return (
39+
<RichTextEditorLoader
40+
controls={markdownEditorDefaultControls}
41+
extensions={markdownEditorDefaultExtensions}
42+
markdown
43+
onUpdate={handleUpdate}
44+
{...rest}
45+
/>
46+
);
3947
}) as IMarkdownEditor;
4048

4149
Object.entries(markdownControls).forEach(([key, component]) => {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ import { editorContext } from "../contexts/editor";
77

88
export interface IProviderProps extends UseEditorOptions {
99
children?: ReactNode;
10+
markdown?: boolean;
1011
}
1112

1213
function Provider(props: IProviderProps) {
13-
const { children, ...rest } = props;
14+
const { children, markdown, ...rest } = props;
1415
const { content } = rest;
1516
const editor = useEditor(rest);
1617
const { classes } = useStyles();
1718

1819
useEffect(() => {
1920
if (content != null && editor != null) {
20-
const html = editor.getHTML();
21-
if (html !== content) {
21+
const oldContent = markdown ? editor.storage.markdown.getMarkdown() : editor.getHTML();
22+
if (oldContent !== content) {
2223
editor.commands.setContent(content);
2324
}
2425
}
25-
}, [content, editor]);
26+
}, [content, editor, markdown]);
2627

2728
return (
2829
<div className={classes.root}>

0 commit comments

Comments
 (0)