Skip to content

Commit 67d4e89

Browse files
committed
Fix build
1 parent 5aac707 commit 67d4e89

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

src/components/HelpPanel.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { useState, useEffect} from 'react';
2+
import useSWR from 'swr';
3+
//import { getData } from '../utils/swr/fetchers';
4+
//import { Form } from "@graffiticode/l0002";
5+
import { TextEditor } from "./TextEditor";
6+
7+
const isNullOrEmptyObject = (obj) => !obj || Object.keys(obj).length === 0;
8+
9+
export const HelpPanel = ({
10+
id,
11+
user,
12+
}) => {
13+
console.log(
14+
"HelpPanel",
15+
"id=" + JSON.stringify(id, null, 2)
16+
);
17+
const [ data, setData ] = useState({});
18+
// const [ doGetData, setDoGetData ] = useState(false);
19+
20+
// useEffect(() => {
21+
// setDoGetData(true);
22+
// }, [id]);
23+
24+
// const getDataResp = useSWR(
25+
// doGetData && { user, id } || null,
26+
// getData
27+
// );
28+
29+
// if (getDataResp.data) {
30+
// const data = getDataResp.data;
31+
// setData(data);
32+
// setDoGetData(false);
33+
// }
34+
35+
return (
36+
<TextEditor
37+
state={{
38+
apply: () => {},
39+
data
40+
}}
41+
/>
42+
);
43+
}

src/components/TextEditor.tsx

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)