-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTiptap.tsx
175 lines (166 loc) · 5.5 KB
/
Tiptap.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { useEditor, EditorContent, BubbleMenu, Editor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import { Paragraph } from "@tiptap/extension-paragraph"
import Placeholder from '@tiptap/extension-placeholder'
import { Heading } from '@tiptap/extension-heading'
import { useLocalStorageValue } from "@react-hookz/web"
import { CREATE_CACHE } from "../constants"
import { BulletList } from "@tiptap/extension-bullet-list"
import { OrderedList } from "@tiptap/extension-ordered-list"
import { ListItem } from "@tiptap/extension-list-item"
import { Image } from "@tiptap/extension-image"
import { UploadImageDialog } from "./UploadImageDialog"
import { useState } from "react"
import TurndownService from 'turndown'
import MarkdownIt from 'markdown-it'
import Link from '@tiptap/extension-link'
import { AddLinkDialog } from "./AddLinkDialog"
interface TiptapProps {
initValue: string
}
const turndownService = new TurndownService('commonmark', { headingStyle: 'atx' })
const md = new MarkdownIt()
export const Tiptap = ({ initValue }: TiptapProps) => {
const [openImageDialog, setOpenImageDialog] = useState(false)
const [openHyperLinkDialog, setOpenHyperDialog] = useState(false)
const [, setCache] = useLocalStorageValue<any>(CREATE_CACHE)
const editor = useEditor({
onUpdate: ({ editor : e }) => {
const html = e.getHTML()
const markdown = turndownService.turndown(html)
setCache(markdown)
},
extensions: [
BulletList,
OrderedList,
ListItem,
Image,
StarterKit,
Paragraph,
Link.configure({
autolink: false,
}),
Placeholder.configure({
placeholder: ({ node }) => {
if (node.type.name === 'heading') {
return 'What’s the title?'
}
return 'Can you add some further context?'
},
}),
Heading.configure({
levels: [1, 2],
}),
],
content: md.render(initValue ?? ''),
editorProps: {
attributes: {
class: 'focus:outline-none text-lg py-4',
style: 'min-height: 500px;'
},
},
})
const handleImage = (url) => {
if (url) {
editor.chain().focus().setImage({ src: url }).run()
}
setOpenImageDialog(false)
}
const handleHyperLink = (url) => {
if (url) {
editor.chain().focus().extendMarkRange('link').setLink({ href: url }).run()
}
setOpenHyperDialog(false)
}
return (
<div>
{editor && <BubbleMenu editor={editor}
pluginKey={'menu'}>
<MenuUI editor={editor}
openHyperLinkDialog={() => setOpenHyperDialog(true)} />
</BubbleMenu>}
<UploadImageDialog open={openImageDialog}
close={() => setOpenImageDialog(false)}
save={handleImage}/>
<AddLinkDialog open={openHyperLinkDialog}
close={() => setOpenHyperDialog(false)}
save={handleHyperLink}/>
<EditorContent editor={editor} />
<div className='fixed bottom-6 inset-x-0'>
<div className="flex justify-center">
<FixedMenuUI editor={editor}
openImageDialog={() => setOpenImageDialog(true)} />
</div>
</div>
</div>
)
}
const FixedMenuUI = ({ editor, openImageDialog }: {editor: Editor, openImageDialog: () => void}) => {
if (!editor) return null
return (
<div className=" justify-center bg-gray-100 rounded flex px-2 divide-x w-80">
<div className="p-2 text-gray-800">
<button
onClick={(e) => { editor.chain().focus().toggleBulletList().run(); e.preventDefault() }}
className={editor.isActive('bulletList') ? 'is-active' : ''}>
Bullet list
</button>
</div>
<div className="p-2 text-gray-800">
<button
onClick={(e) => {editor.chain().focus().toggleOrderedList().run(); e.preventDefault()}}
className={editor.isActive('orderedList') ? 'is-active' : ''}>
Order list
</button>
</div>
<div className="p-2 text-gray-800">
<button
onClick={(e) => { e.preventDefault(); openImageDialog()}}>
Image
</button>
</div>
</div>
)
}
const MenuUI = ({ editor, openHyperLinkDialog }: {editor: Editor, openHyperLinkDialog: () => void}) => {
if (!editor) return null
return (
<div className="shadow-md bg-gray-100 rounded flex px-2 divide-x">
<div className="p-2 text-gray-800">
<button
onClick={() => editor.chain().focus().toggleHeading({ level: 1 }).run()}
className={editor.isActive('h-1') ? 'is-active' : ''}>
h1
</button>
</div>
<div className="p-2 text-gray-800">
<button
onClick={() => editor.chain().focus().toggleHeading({ level: 2 }).run()}
className={editor.isActive('h-2') ? 'is-active' : ''}>
h2
</button>
</div>
<div className="p-2 text-gray-800">
<button
onClick={e => {e.preventDefault(); openHyperLinkDialog() }}
className={editor.isActive('link') ? 'is-active' : ''}>
link
</button>
</div>
<div className="p-2 text-gray-800">
<button
onClick={() => editor.chain().focus().toggleBold().run()}
className={editor.isActive('bold') ? 'is-active' : ''}>
bold
</button>
</div>
<div className="p-2 text-gray-800">
<button
onClick={() => editor.chain().focus().toggleItalic().run()}
className={editor.isActive('italic') ? 'is-active' : ''}>
italic
</button>
</div>
</div>
)
}