fix react setup

This commit is contained in:
Philipp Kühn
2020-04-28 23:25:56 +02:00
parent 9e1d8f1943
commit bda4bbcd6b
5 changed files with 15 additions and 36 deletions

View File

@@ -0,0 +1,30 @@
import React, { useState, useRef, useEffect, createContext, useContext } from 'react'
import { Editor as Tiptap } from '@tiptap/core'
export const EditorContext = createContext({})
export const useEditor = () => useContext(EditorContext)
export const Editor = ({ value, onChange, children, ...props }) => {
const [editor, setEditor] = useState(null)
const editorRef = useRef(null)
useEffect(() => {
const e = new Tiptap({
element: editorRef.current,
content: value,
...props,
}).on('transaction', () => {
onChange(e.json())
})
setEditor(e)
}, [])
return (
<EditorContext.Provider value={editor}>
{editorRef.current && children}
<div ref={editorRef} />
</EditorContext.Provider>
)
}