Skip to content

Commit 48f5284

Browse files
committed
feat: add props to remove the empty paragraph
1 parent cc61359 commit 48f5284

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

examples/src/Tiptap.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const Tiptap = () => {
7171
]}
7272
extensionLoader={extensionLoader}
7373
onContentUpdate={setContent}
74+
removeEmptyParagraph
7475
/>
7576
<div className="fr-tiptap" dangerouslySetInnerHTML={{ __html: content }}></div>
7677
</>

packages/react-dsfr-tiptap/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Et utilisez la classe `fr-tiptap sur le conteneur qui va afficher le code HTML:
6363

6464
### Texte Riche
6565

66-
Une fois installée vous pouvez utilisé le composant `RichTextEditor` de cette manière:
66+
Vous pouvez utiliser le composant `RichTextEditor` de cette manière:
6767

6868
```tsx
6969
import { RichTextEditor } from "react-dsfr-tiptap";
@@ -307,7 +307,9 @@ Vous pouvez aussi réutiliser la configuration via cette variable `extensionLoad
307307

308308
### Props
309309

310-
Les 2 composants `RichTextEditor` et `MarkdownEditor` fonctionne de la même manière et ont les mêmes props:
310+
#### Props communes
311+
312+
Les 2 composants `RichTextEditor` et `MarkdownEditor` fonctionne globalement de la même manière et les props suivantes en commun:
311313

312314
| Props | Type | Valeur par défaut | Description |
313315
| ----------------- | ------------------------------------------------------------------------------------- | ------------------- | ----------------------------------------------------------------- |
@@ -348,6 +350,14 @@ Pour le composant `RichTextEditor`:
348350
```
349351
- `defaultExtensions` est égal à: `[require("@tiptap/starter-kit"), require("tiptap-markdown")]`
350352

353+
#### Props `RichTextEditor`
354+
355+
Le composant `RichTextEditor` possède en plus les props suivantes:
356+
357+
| Props | Type | Valeur par défaut | Description |
358+
| ---------------------- | --------- | ----------------- | ----------------------------------------------------------------------------------------------------------------- |
359+
| `removeEmptyParagraph` | `boolean` | `false` | Supprime le parapgraph vide `"<p></p>"` quand l'éditeur est complètement vide (via le callback `onContentUpdate`) |
360+
351361
### Ajout de boutons personnalisés
352362

353363
Vous pouvez développer et ajouter vos propres boutons dans le composant de texte riche.

packages/react-dsfr-tiptap/src/components/Content.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
.fr-tiptap li > p {
55
margin: 0;
66
}
7+
.fr-tiptap p:empty:before {
8+
content: " ";
9+
white-space: pre;
10+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import RichTextEditorGroup from "./Group";
1414
export interface IRichTextEditorProps extends Omit<ILoaderProps, "controls"> {
1515
controls?: (Control | ControlComponent)[][];
1616
onContentUpdate?: (content: string) => void;
17+
removeEmptyParagraph?: boolean;
1718
}
1819

1920
type RichTextEditorControls = {
@@ -28,11 +29,15 @@ interface IRichTextEditor extends RichTextEditorControls {
2829
Provider: typeof RichTextEditorProvider;
2930
}
3031
const RichTextEditor = ((props: IRichTextEditorProps) => {
31-
const { onContentUpdate, onUpdate, ...rest } = props;
32+
const { onContentUpdate, onUpdate, removeEmptyParagraph = false, ...rest } = props;
3233

3334
function handleUpdate(props: EditorEvents["update"]) {
3435
onUpdate?.(props);
35-
onContentUpdate?.(props.editor.getHTML());
36+
let content = props.editor.getHTML();
37+
if (removeEmptyParagraph && content === "<p></p>") {
38+
content = "";
39+
}
40+
onContentUpdate?.(content);
3641
}
3742

3843
return <RichTextEditorLoader controls={richTextEditorDefaultControls} extensions={richTextEditorDefaultExtensions} onUpdate={handleUpdate} {...rest} />;

0 commit comments

Comments
 (0)