-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Replace Markdown-related libraries with TipTap (#229)
- Loading branch information
Showing
11 changed files
with
818 additions
and
3,213 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"namesake": minor | ||
--- | ||
|
||
Improve rich text editing experience |
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
Large diffs are not rendered by default.
Oops, something went wrong.
8 changes: 4 additions & 4 deletions
8
...RichTextEditor/RichTextEditor.stories.tsx → src/components/RichText/RichText.stories.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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { RichTextEditor } from "./RichTextEditor"; | ||
import { RichText } from "./RichText"; | ||
|
||
const meta = { | ||
component: RichTextEditor, | ||
component: RichText, | ||
parameters: { | ||
layout: "padded", | ||
}, | ||
} satisfies Meta<typeof RichTextEditor>; | ||
} satisfies Meta<typeof RichText>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
markdown: "hello", | ||
initialContent: "hello", | ||
}, | ||
}; |
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,95 @@ | ||
import { BubbleMenu, EditorContent, useEditor } from "@tiptap/react"; | ||
import StarterKit from "@tiptap/starter-kit"; | ||
import { Bold, Italic } from "lucide-react"; | ||
import { useEffect } from "react"; | ||
import { twMerge } from "tailwind-merge"; | ||
import { tv } from "tailwind-variants"; | ||
import { ReadingScore } from "../ReadingScore"; | ||
import { ToggleButton } from "../ToggleButton"; | ||
|
||
export interface RichTextProps { | ||
className?: string; | ||
showReadingScore?: boolean; | ||
initialContent?: string; | ||
onChange?: (content: string) => void; | ||
editable?: boolean; | ||
} | ||
|
||
export function RichText({ | ||
className, | ||
showReadingScore = false, | ||
initialContent, | ||
onChange, | ||
editable = true, | ||
}: RichTextProps) { | ||
const extensions = [StarterKit]; | ||
|
||
const editor = useEditor({ | ||
extensions, | ||
content: initialContent, | ||
editable, | ||
onUpdate: ({ editor }) => { | ||
if (onChange) { | ||
onChange(editor.getHTML()); | ||
} | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (editor && initialContent !== editor.getHTML()) { | ||
editor.commands.setContent(initialContent ?? ""); | ||
} | ||
}, [editor, initialContent]); | ||
|
||
if (!editor) { | ||
return null; | ||
} | ||
|
||
const styles = tv({ | ||
base: "w-full", | ||
variants: { | ||
editable: { | ||
true: "border border-gray-dim flex flex-col rounded-xl [&_.tiptap]:p-4 [&_.tiptap]:outline-none", | ||
}, | ||
}, | ||
}); | ||
|
||
return ( | ||
<div className={styles({ editable })}> | ||
<EditorContent | ||
editor={editor} | ||
className={twMerge("w-full prose dark:prose-invert", className)} | ||
/> | ||
{editable && ( | ||
<> | ||
<BubbleMenu | ||
editor={editor} | ||
className="bg-gray-1 dark:bg-graydark-2 p-1.5 rounded-xl shadow-md flex gap-1 items-center" | ||
> | ||
<ToggleButton | ||
onPress={() => editor.chain().focus().toggleBold().run()} | ||
isDisabled={!editor.can().chain().focus().toggleBold().run()} | ||
isSelected={editor.isActive("bold")} | ||
icon={Bold} | ||
aria-label="Toggle bold text" | ||
size="small" | ||
/> | ||
<ToggleButton | ||
onPress={() => editor.chain().focus().toggleItalic().run()} | ||
isDisabled={!editor.can().chain().focus().toggleItalic().run()} | ||
isSelected={editor.isActive("italic")} | ||
icon={Italic} | ||
aria-label="Toggle italic text" | ||
size="small" | ||
/> | ||
</BubbleMenu> | ||
{showReadingScore && ( | ||
<div className="border-t border-gray-dim"> | ||
<ReadingScore text={editor.state.doc.textContent} /> | ||
</div> | ||
)} | ||
</> | ||
)} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./RichText"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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