Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(#2): add placeholder example #5

Merged
merged 1 commit into from
Jan 23, 2025
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
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@tiptap/extension-highlight": "^2.10.4",
"@tiptap/extension-image": "^2.10.4",
"@tiptap/extension-link": "^2.10.4",
"@tiptap/extension-placeholder": "^2.11.3",
"@tiptap/extension-subscript": "^2.10.4",
"@tiptap/extension-superscript": "^2.10.4",
"@tiptap/extension-text-align": "^2.10.4",
Expand Down
20 changes: 14 additions & 6 deletions examples/src/Custom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ import { RichTextEditor } from "react-dsfr-tiptap";
import { ControlImage, ControlLink, ControlUnlink, ControlYoutube } from "react-dsfr-tiptap/dialog";
import StarterKit from "@tiptap/starter-kit";
import Link from "@tiptap/extension-link";
import Placeholder from "@tiptap/extension-placeholder";

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

const Custom = () => {
const [content, setContent] = useState(`
<p>
this is a basic example of <strong>Tiptap</strong>. Sure, there are all kind of basic text styles you’d probably expect from a text editor?
</p>
`);
const [content, setContent] = useState("");

return (
<>
<RichTextEditor.Provider content={content} extensions={[StarterKit, Link]} onUpdate={({ editor }) => setContent(editor.getHTML())}>
<RichTextEditor.Provider
content={content}
extensions={[
StarterKit,
Link,
Placeholder.configure({
placeholder: "Write something …",
}),
]}
onUpdate={({ editor }) => setContent(editor.getHTML())}
>
<RichTextEditor.Menu first>
<RichTextEditor.Group>
<RichTextEditor.Bold />
Expand Down
7 changes: 7 additions & 0 deletions examples/src/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.tiptap p.is-editor-empty:first-child::before {
color: #adb5bd;
content: attr(data-placeholder);
float: left;
height: 0;
pointer-events: none;
}
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/react-dsfr-tiptap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ Les 2 composants `RichTextEditor` et `MarkdownEditor` fonctionne de la même man
| Props | Type | Valeur par défaut | Description |
| ----------------- | ------------------------------------------------------------------------------------- | ------------------- | ----------------------------------------------------------------- |
| `content` | `string` | `""` | Contenu de la zone de texte riche |
| `contentProps` | `IContentProps` | `{}` | Props pour le composant du contenu |
| `controlMap` | `Partial<Record<Control, (() => ReactNode) \| LazyExoticComponent<() => ReactNode>>>` | `{}` | Permet de configurer les composants des boutons préconfigurés |
| `controls` | `(Control \| (() => ReactNode) \| LazyExoticComponent<() => ReactNode>)[][]` | `defaultControls` | Permet de configurer les boutons du menu |
| `extensionLoader` | `Partial<Record<Extension, () => Promise<AnyExtension \| AnyExtension[]>>>` | `{}` | Permet de charger dynamiquement des extensions |
Expand Down
11 changes: 8 additions & 3 deletions packages/react-dsfr-tiptap/src/components/Content.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { fr } from "@codegouvfr/react-dsfr";
import { EditorContent } from "@tiptap/react";
import { EditorContent, EditorContentProps } from "@tiptap/react";
import { tss } from "tss-react";

import { useEditor } from "../contexts/editor";
import { ForwardedRef } from "react";

function Content() {
export interface IContentProps extends Omit<EditorContentProps, "editor" | "innerRef" | "ref"> {
ref?: ForwardedRef<HTMLDivElement | null>;
}

function Content(props: IContentProps) {
const editor = useEditor();
const { classes } = useStyles();

return <EditorContent className={classes.root} editor={editor} />;
return <EditorContent className={classes.root} editor={editor} {...props} />;
}

const useStyles = tss.withName(Content.name).create(() => ({
Expand Down
7 changes: 4 additions & 3 deletions packages/react-dsfr-tiptap/src/components/Loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import StarterKit from "@tiptap/starter-kit";
import { Control } from "../types/controls";
import { ControlComponent, richTextEditorControls } from "../utils/controls";

import RichTextEditorContent from "./Content";
import RichTextEditorContent, { IContentProps } from "./Content";
import RichTextEditorMenu from "./Menu";
import RichTextEditorProvider, { IProviderProps } from "./Provider";
import RichTextEditorGroup from "./Group";
Expand All @@ -24,6 +24,7 @@ export type Extension =
| "youtube";

export interface ILoaderProps extends Omit<IProviderProps, "children"> {
contentProps?: IContentProps;
controlMap?: Partial<Record<Control, ControlComponent>>;
controls: (Control | ControlComponent)[][];
extensionLoader?: Partial<Record<Extension, () => Promise<AnyExtension | AnyExtension[]>>>;
Expand Down Expand Up @@ -80,7 +81,7 @@ const extensionDefaultConfiguration = {
};

function Loader(props: ILoaderProps) {
const { controlMap = {}, controls, extensionLoader = {}, menu = "top", ...rest } = props;
const { contentProps = {}, controlMap = {}, controls, extensionLoader = {}, menu = "top", ...rest } = props;
const [extensions, setExtensions] = useState<Partial<Record<Extension, AnyExtension>>>(() =>
Object.fromEntries(props.extensions?.map((extension) => [extension.name, extension]) ?? [["starterKit", StarterKit]])
);
Expand Down Expand Up @@ -152,7 +153,7 @@ function Loader(props: ILoaderProps) {
<RichTextEditorGroup key={i}>{components}</RichTextEditorGroup>
))}
</RichTextEditorMenu>
{menu === "top" && <RichTextEditorContent />}
{menu === "top" && <RichTextEditorContent {...contentProps} />}
</RichTextEditorProvider>
);
}
Expand Down
Loading