generated from betagouv/template-nextjs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
846 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/frontend/src/features/text-editor/TextEditor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import Link from "@tiptap/extension-link"; | ||
import { Placeholder } from "@tiptap/extension-placeholder"; | ||
import { EditorContent, useEditor } from "@tiptap/react"; | ||
import StarterKit from "@tiptap/starter-kit"; | ||
import { useEffect } from "react"; | ||
import { css } from "#styled-system/css"; | ||
import { TextEditorToolbar } from "./TextEditorToolbar"; | ||
|
||
interface Props { | ||
defaultValue?: string; | ||
onChange?: (value: string) => void; | ||
autoFocus?: boolean; | ||
readOnly?: boolean; | ||
placeholder?: string; | ||
hasSubmitted?: boolean; | ||
} | ||
|
||
export const TextEditor = (props: Props) => { | ||
const { defaultValue, onChange, autoFocus, readOnly, placeholder, hasSubmitted } = props; | ||
|
||
const editor = useEditor({ | ||
autofocus: autoFocus ?? false, | ||
editable: !readOnly, | ||
extensions: [ | ||
StarterKit.configure({ | ||
codeBlock: false, | ||
code: { | ||
HTMLAttributes: { | ||
class: "inline", | ||
}, | ||
}, | ||
}), | ||
Placeholder.configure({ | ||
placeholder, | ||
}), | ||
Link.configure({ | ||
autolink: true, | ||
linkOnPaste: true, | ||
}), | ||
], | ||
content: defaultValue, | ||
onUpdate({ editor }) { | ||
onChange?.(editor.getHTML()); | ||
}, | ||
}); | ||
|
||
const isEditorActive = !!editor; | ||
|
||
// biome-ignore lint/correctness/useExhaustiveDependencies: | ||
useEffect(() => { | ||
if (hasSubmitted && isEditorActive) { | ||
editor.commands.clearContent(true); | ||
} | ||
}, [isEditorActive, hasSubmitted]); | ||
|
||
return ( | ||
<div className="text-edito"> | ||
<EditorContent | ||
className={css({ | ||
minH: "160px", | ||
"& > div": { | ||
outline: "none", | ||
roundedTop: "md", | ||
borderWidth: "1px", | ||
minH: "160px", | ||
maxH: "240px", | ||
py: "2", | ||
px: "2", | ||
fontSize: "13px", | ||
overflowY: "auto", | ||
_focusVisible: { | ||
outlineWidth: "1px", | ||
borderColor: "purple.500", | ||
outlineColor: "purple.500", | ||
outlineStyle: "solid", | ||
}, | ||
"& strong": { | ||
fontWeight: "semibold", | ||
}, | ||
"& em": { | ||
fontStyle: "italic", | ||
}, | ||
"& :where(ul, ol, li)": { | ||
listStyle: "unset", | ||
}, | ||
"& :where(ul, ol)": { | ||
paddingStart: "1rem", | ||
}, | ||
"& blockquote": { | ||
borderLeftWidth: "3px", | ||
borderColor: "gray.300", | ||
paddingLeft: "4", | ||
}, | ||
"& a": { | ||
color: "purple.600", | ||
}, | ||
"& code.inline": { | ||
rounded: "2px", | ||
borderWidth: "1px", | ||
mb: "1px", | ||
padding: "2px 3px 1px", | ||
fontFamily: "mono", | ||
fontSize: "0.9em", | ||
bg: "gray.600", | ||
}, | ||
}, | ||
})} | ||
editor={editor} | ||
/> | ||
{editor && <TextEditorToolbar editor={editor} />} | ||
</div> | ||
); | ||
}; |
134 changes: 134 additions & 0 deletions
134
packages/frontend/src/features/text-editor/TextEditorToolbar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import type { Editor } from "@tiptap/react"; | ||
import { LuBold, LuCode, LuItalic, LuLink, LuList, LuListOrdered, LuQuote, LuStrikethrough } from "react-icons/lu"; | ||
import { cva } from "#styled-system/css"; | ||
import { hstack } from "#styled-system/patterns"; | ||
|
||
const toolbar = hstack({ | ||
gap: "0", | ||
roundedBottom: "sm", | ||
borderWidth: "1px", | ||
mt: "-1px", | ||
lineHeight: "0", | ||
transition: "all 0.15s", | ||
}); | ||
|
||
const toolbarButtonRecipe = cva({ | ||
base: { | ||
display: "flex", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
w: "8", | ||
h: "8", | ||
}, | ||
variants: { | ||
active: { | ||
true: { | ||
color: "gray.100", | ||
bg: "gray.600", | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
interface Props { | ||
editor: Editor; | ||
} | ||
|
||
export const TextEditorToolbar = (props: Props) => { | ||
const { editor } = props; | ||
|
||
return ( | ||
<div className={toolbar}> | ||
<button | ||
type="button" | ||
onPointerDown={(event) => event.preventDefault()} | ||
onClick={() => editor.chain().focus().toggleBold().run()} | ||
className={toolbarButtonRecipe({ | ||
active: editor.isActive("bold"), | ||
})} | ||
> | ||
<LuBold /> | ||
</button> | ||
<button | ||
type="button" | ||
onPointerDown={(event) => event.preventDefault()} | ||
onClick={() => editor.chain().focus().toggleItalic().run()} | ||
className={toolbarButtonRecipe({ | ||
active: editor.isActive("italic"), | ||
})} | ||
> | ||
<LuItalic /> | ||
</button> | ||
<button | ||
type="button" | ||
onPointerDown={(event) => event.preventDefault()} | ||
onClick={() => editor.chain().focus().toggleStrike().run()} | ||
className={toolbarButtonRecipe({ | ||
active: editor.isActive("strike"), | ||
})} | ||
> | ||
<LuStrikethrough /> | ||
</button> | ||
<button | ||
type="button" | ||
onPointerDown={(event) => event.preventDefault()} | ||
onClick={() => editor.chain().focus().toggleBulletList().run()} | ||
className={toolbarButtonRecipe({ | ||
active: editor.isActive("bulletList"), | ||
})} | ||
> | ||
<LuList /> | ||
</button> | ||
<button | ||
type="button" | ||
onPointerDown={(event) => event.preventDefault()} | ||
onClick={() => editor.chain().focus().toggleOrderedList().run()} | ||
className={toolbarButtonRecipe({ | ||
active: editor.isActive("orderedList"), | ||
})} | ||
> | ||
<LuListOrdered /> | ||
</button> | ||
<button | ||
type="button" | ||
onPointerDown={(event) => event.preventDefault()} | ||
onClick={() => editor.chain().focus().toggleBlockquote().run()} | ||
className={toolbarButtonRecipe({ | ||
active: editor.isActive("blockquote"), | ||
})} | ||
> | ||
<LuQuote /> | ||
</button> | ||
<button | ||
type="button" | ||
onPointerDown={(event) => event.preventDefault()} | ||
onClick={() => editor.chain().focus().toggleCode().run()} | ||
className={toolbarButtonRecipe({ | ||
active: editor.isActive("code"), | ||
})} | ||
> | ||
<LuCode /> | ||
</button> | ||
<button | ||
type="button" | ||
onPointerDown={(event) => event.preventDefault()} | ||
onClick={() => { | ||
const previousUrl = editor.getAttributes("link").href; | ||
const url = window.prompt("URL", previousUrl); | ||
if (url === null) return; // cancelled | ||
if (url === "") { | ||
// empty | ||
editor.chain().focus().extendMarkRange("link").unsetLink().run(); | ||
return; | ||
} | ||
editor.chain().focus().extendMarkRange("link").setLink({ href: url }).run(); | ||
}} | ||
className={toolbarButtonRecipe({ | ||
active: editor.isActive("link"), | ||
})} | ||
> | ||
<LuLink /> | ||
</button> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.