Created
October 13, 2020 22:50
-
-
Save cindywu/c23c97583409d71cd63dfbe2db9224f1 to your computer and use it in GitHub Desktop.
Editor.js function componenet wip
This file contains hidden or 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
function useForceUpdate() { | |
const [, setValue] = useState(0) | |
return () => setValue((value) => ++value) | |
} | |
export default function Editor(props) { | |
const { | |
post, | |
options, | |
onChange, | |
isEditable, | |
render, | |
field, | |
autoFocus, | |
} = props | |
console.log(field.toUpperCase() + ' EDITOR PROPS', props) | |
const forceUpdate = useForceUpdate() | |
const editorRef = useRef() | |
const getView = () => view | |
const view = new EditorView(null, { | |
state: EditorState.create({ | |
schema: options.schema, | |
doc: options.doc, | |
plugins: options.setupPlugins(getView), | |
field: field, | |
}), | |
dispatchTransaction: (transaction) => { | |
const oldComments = commentPluginKey.getState(view.state) | |
const { state, transactions } = view.state.applyTransaction(transaction) | |
view.updateState(state) | |
const newComments = commentPluginKey.getState(state) | |
if ( | |
transactions.some((tr) => tr.docChanged) || | |
newComments !== oldComments | |
) { | |
onChange(state.doc, state, field) | |
} | |
forceUpdate() | |
}, | |
editable: function (state) { | |
return isEditable | |
}, | |
handleDOMEvents: function (view, event) { | |
return true | |
}, | |
}) | |
// applyDevTools(view) | |
useEffect(() => { | |
editorRef.current.appendChild(view.dom) | |
if (autoFocus) view.focus() | |
}, []) | |
useEffect(() => { | |
view.state.doc = options.doc | |
const newState = EditorState.create({ | |
schema: options.schema, | |
doc: options.doc, | |
plugins: view.state.plugins, | |
field: field, | |
selection: view.state.selection, | |
}) | |
view.updateState(newState) | |
view.focus() | |
return () => view.destroy | |
}, [props]) // idk what goes here instead of props :( | |
const editor = <div ref={editorRef} /> | |
return render ? render({ editor, view: view }) : editor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment