|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; React; |
| 2 | +import { EditorState } from 'prosemirror-state'; |
| 3 | +import { EditorView } from 'prosemirror-view'; |
| 4 | +import { schema } from 'prosemirror-schema-basic'; |
| 5 | +import { baseKeymap } from "prosemirror-commands" |
| 6 | +import { undo, redo, history } from "prosemirror-history"; |
| 7 | +import { keymap } from "prosemirror-keymap"; |
| 8 | + |
| 9 | +import { Plugin } from 'prosemirror-state'; |
| 10 | +import ReactDOM from 'react-dom'; |
| 11 | +import { MenuView } from './MenuView'; |
| 12 | +import { debounce } from "lodash"; |
| 13 | + |
| 14 | +// const menuPlugin = new Plugin({ |
| 15 | +// view(editorView) { |
| 16 | +// let menuDiv = document.createElement('div'); |
| 17 | +// editorView.dom.parentNode.insertBefore(menuDiv, editorView.dom); |
| 18 | +// const update = () => { |
| 19 | +// ReactDOM.render( |
| 20 | +// <MenuView className="" editorView={editorView} />, |
| 21 | +// menuDiv |
| 22 | +// ); |
| 23 | +// }; |
| 24 | +// update(); |
| 25 | +// return { |
| 26 | +// update, |
| 27 | +// destroy() { |
| 28 | +// ReactDOM.unmountComponentAtNode(menuDiv); |
| 29 | +// menuDiv.remove(); |
| 30 | +// } |
| 31 | +// }; |
| 32 | +// } |
| 33 | +// }); |
| 34 | + |
| 35 | +const debouncedStateUpdate = debounce(({ state, editorState }) => { |
| 36 | + state.apply({ |
| 37 | + type: "update", |
| 38 | + args: {editorState}, |
| 39 | + }); |
| 40 | +}, 1000); |
| 41 | + |
| 42 | +export const TextEditor = ({ state }) => { |
| 43 | + console.log( |
| 44 | + "TextEditor", |
| 45 | + "state=" + JSON.stringify(state, null, 2) |
| 46 | + ); |
| 47 | + const [ editorView, setEditorView ] = useState(null); |
| 48 | + const editorRef = useRef(null); |
| 49 | + const plugins = [ |
| 50 | + history(), |
| 51 | + keymap({"Mod-z": undo, "Mod-y": redo}), |
| 52 | + keymap(baseKeymap), |
| 53 | + ]; |
| 54 | + useEffect(() => { |
| 55 | + if (!editorRef.current) { |
| 56 | + return; |
| 57 | + } |
| 58 | + const editorView = new EditorView(editorRef.current, { |
| 59 | + state: EditorState.create({ |
| 60 | + schema, |
| 61 | + plugins, |
| 62 | + }), |
| 63 | + dispatchTransaction(transaction) { |
| 64 | + const editorState = editorView.state.apply(transaction); |
| 65 | + editorView.updateState(editorState); |
| 66 | + debouncedStateUpdate({ |
| 67 | + state, |
| 68 | + editorState: editorState.toJSON() |
| 69 | + }); |
| 70 | + } |
| 71 | + }); |
| 72 | + setEditorView(editorView); |
| 73 | + editorView.focus(); |
| 74 | + return () => { |
| 75 | + if (editorView) { |
| 76 | + editorView.destroy(); |
| 77 | + } |
| 78 | + }; |
| 79 | + }, []); |
| 80 | + const { editorState } = state.data; |
| 81 | + useEffect(() => { |
| 82 | + if (editorState) { |
| 83 | + const newEditorState = EditorState.fromJSON({ |
| 84 | + schema, |
| 85 | + plugins, |
| 86 | + }, editorState); |
| 87 | + editorView.updateState(newEditorState); |
| 88 | + } |
| 89 | + }, [editorState]); |
| 90 | + return ( |
| 91 | + <div |
| 92 | + ref={editorRef} |
| 93 | + className="border border-gray-300 p-2 bg-white text-sm font-sans" |
| 94 | + > |
| 95 | + <p> |
| 96 | + <i>This is where you will ask for help from a Graffiticode language |
| 97 | + assistant. This feature is currently under construction.</i> |
| 98 | + </p> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +}; |
0 commit comments