-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
17 changed files
with
774 additions
and
1 deletion.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
docs/data/material/components/rich-text-field/TiptapRichTextField.js
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,49 @@ | ||
import RichTextField, { | ||
RichTextFieldStandardToolBar, | ||
RichTextFieldTiptapEditor, | ||
} from '@mui/lab/RichTextField'; | ||
import Stack from '@mui/material/Stack'; | ||
import Underline from '@tiptap/extension-underline'; | ||
import { useEditor } from '@tiptap/react'; | ||
import StarterKit from '@tiptap/starter-kit'; | ||
import * as React from 'react'; | ||
|
||
export default function TiptapRichTextField() { | ||
const [value, setValue] = React.useState(''); | ||
const [editor, setEditor] = React.useState(new RichTextFieldTiptapEditor(null)); | ||
|
||
const tiptapEditor = useEditor({ | ||
extensions: [ | ||
StarterKit.configure({ | ||
blockquote: false, | ||
code: false, | ||
codeBlock: false, | ||
dropcursor: false, | ||
gapcursor: false, | ||
heading: false, | ||
horizontalRule: false, | ||
strike: false, | ||
}), | ||
Underline, | ||
], | ||
}); | ||
|
||
React.useEffect(() => { | ||
setEditor(new RichTextFieldTiptapEditor(tiptapEditor)); | ||
}, [tiptapEditor]); | ||
|
||
return ( | ||
<Stack alignItems="center" direction="column" spacing={2}> | ||
<RichTextField | ||
label="Type here" | ||
editor={editor} | ||
onChange={setValue} | ||
variant="filled" | ||
value={value} | ||
> | ||
<RichTextFieldStandardToolBar /> | ||
</RichTextField> | ||
<div>{value}</div> | ||
</Stack> | ||
); | ||
} |
49 changes: 49 additions & 0 deletions
49
docs/data/material/components/rich-text-field/TiptapRichTextField.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,49 @@ | ||
import RichTextField, { | ||
RichTextFieldStandardToolBar, | ||
RichTextFieldTiptapEditor, | ||
} from '@mui/lab/RichTextField'; | ||
import Stack from '@mui/material/Stack'; | ||
import Underline from '@tiptap/extension-underline'; | ||
import { useEditor } from '@tiptap/react'; | ||
import StarterKit from '@tiptap/starter-kit'; | ||
import * as React from 'react'; | ||
|
||
export default function TiptapRichTextField() { | ||
const [value, setValue] = React.useState(''); | ||
const [editor, setEditor] = React.useState(new RichTextFieldTiptapEditor(null)); | ||
|
||
const tiptapEditor = useEditor({ | ||
extensions: [ | ||
StarterKit.configure({ | ||
blockquote: false, | ||
code: false, | ||
codeBlock: false, | ||
dropcursor: false, | ||
gapcursor: false, | ||
heading: false, | ||
horizontalRule: false, | ||
strike: false, | ||
}), | ||
Underline, | ||
], | ||
}); | ||
|
||
React.useEffect(() => { | ||
setEditor(new RichTextFieldTiptapEditor(tiptapEditor)); | ||
}, [tiptapEditor]); | ||
|
||
return ( | ||
<Stack alignItems="center" direction="column" spacing={2}> | ||
<RichTextField | ||
label="Type here" | ||
editor={editor} | ||
onChange={setValue} | ||
variant="filled" | ||
value={value} | ||
> | ||
<RichTextFieldStandardToolBar /> | ||
</RichTextField> | ||
<div>{value}</div> | ||
</Stack> | ||
); | ||
} |
10 changes: 10 additions & 0 deletions
10
docs/data/material/components/rich-text-field/TiptapRichTextField.tsx.preview
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,10 @@ | ||
<RichTextField | ||
label="Type here" | ||
editor={editor} | ||
onChange={setValue} | ||
variant="filled" | ||
value={value} | ||
> | ||
<RichTextFieldStandardToolBar /> | ||
</RichTextField> | ||
<div>{value}</div> |
19 changes: 19 additions & 0 deletions
19
docs/data/material/components/rich-text-field/rich-text-field.md
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,19 @@ | ||
--- | ||
product: material-ui | ||
title: React RichTextField component | ||
components: RichTextField | ||
githubLabel: 'component: rich text field' | ||
packageName: '@mui/lab' | ||
--- | ||
|
||
# Rich Text Field | ||
|
||
<p class="description">Text field component that supports advanced text editing like formatting</p> | ||
|
||
{{"component": "modules/components/ComponentLinkHeader.js"}} | ||
|
||
## Tiptap | ||
|
||
A rich text field using [tiptap](https://tiptap.dev) | ||
|
||
{{"demo": "TiptapRichTextField.js"}} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as React from 'react'; | ||
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import { | ||
demos, | ||
docs, | ||
demoComponents, | ||
} from 'docs/data/material/components/rich-text-field/rich-text-field.md?@mui/markdown'; | ||
|
||
export default function Page() { | ||
return <MarkdownDocs demos={demos} docs={docs} demoComponents={demoComponents} />; | ||
} |
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
78 changes: 78 additions & 0 deletions
78
packages/mui-lab/src/RichTextField/Editors/RichTextFieldTiptapEditor.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,78 @@ | ||
import '@tiptap/extension-underline'; | ||
import { Editor, EditorContent } from '@tiptap/react'; | ||
import '@tiptap/starter-kit'; | ||
import * as React from 'react'; | ||
import RichTextFieldEditor, { | ||
RichTextFieldEditorEvent, | ||
RichTextFieldEditorTextStyle, | ||
} from '../RichTextFieldEditor'; | ||
|
||
export default class RichTextFieldTiptapEditor implements RichTextFieldEditor { | ||
private editor: Editor | null; | ||
|
||
get hasContent(): boolean { | ||
return this.editor !== null && !this.editor.isEmpty; | ||
} | ||
|
||
constructor(editor: Editor | null) { | ||
this.editor = editor; | ||
} | ||
|
||
public clearStyles() { | ||
this.editor?.chain().focus().unsetAllMarks().clearNodes().run(); | ||
} | ||
|
||
public focus() { | ||
this.editor?.chain().focus(); | ||
} | ||
|
||
public getValue() { | ||
return this.editor?.getHTML() ?? ''; | ||
} | ||
|
||
public off(event: RichTextFieldEditorEvent, callback: () => void) { | ||
switch (event) { | ||
case RichTextFieldEditorEvent.change: | ||
this.editor?.off('update', callback); | ||
break; | ||
default: | ||
throw new Error(`Unsupported event ${event}`); | ||
} | ||
} | ||
|
||
public on(event: RichTextFieldEditorEvent, callback: () => void) { | ||
switch (event) { | ||
case RichTextFieldEditorEvent.change: | ||
this.editor?.on('update', callback); | ||
break; | ||
default: | ||
throw new Error(`Unsupported event ${event}`); | ||
} | ||
} | ||
|
||
public render() { | ||
return <EditorContent editor={this.editor} />; | ||
} | ||
|
||
public toggleStyle(style: RichTextFieldEditorTextStyle) { | ||
switch (style) { | ||
case RichTextFieldEditorTextStyle.bold: | ||
this.editor?.chain().focus().toggleBold().run(); | ||
break; | ||
case RichTextFieldEditorTextStyle.bulletList: | ||
this.editor?.chain().focus().toggleBulletList().run(); | ||
break; | ||
case RichTextFieldEditorTextStyle.italic: | ||
this.editor?.chain().focus().toggleItalic().run(); | ||
break; | ||
case RichTextFieldEditorTextStyle.orderedList: | ||
this.editor?.chain().focus().toggleOrderedList().run(); | ||
break; | ||
case RichTextFieldEditorTextStyle.underline: | ||
this.editor?.chain().focus().toggleUnderline().run(); | ||
break; | ||
default: | ||
throw new Error(`Unsupported style ${style}`); | ||
} | ||
} | ||
} |
Oops, something went wrong.