Skip to content

Commit d4de257

Browse files
committed
implement text-editor to server side
1 parent 42bba7a commit d4de257

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

api/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import express from "express";
33
import {Server} from "socket.io";
44
import {createServer} from "http";
55
import cors from 'cors';
6-
import {YSocketIO} from 'y-socket.io';
6+
import { YSocketIO } from "y-socket.io/dist/server";
77

88
const app = express();
99
const server = createServer(app);
@@ -12,7 +12,7 @@ const io = new Server(server, {
1212
});
1313
const PORT = 8000;
1414

15-
const ysocketio = new YSocketIO(io, {});
15+
const ysocketio = new YSocketIO(io, {gcEnabled: true});
1616

1717
ysocketio.initialize();
1818

client/src/App.jsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ import Menu from "./components/Menu";
1313
import EraserCursor from "./components/EraserCursor";
1414
import TextEditor from "./components/TextEditor";
1515
import { useRecoilValue, useRecoilState } from "recoil";
16-
import { eraserState, cursorPosition, canvasColors, canvasState, showMenuState, showTextEditor } from "./atoms";
16+
import {
17+
eraserState,
18+
cursorPosition,
19+
canvasColors,
20+
canvasState,
21+
showMenuState,
22+
showTextEditor,
23+
docState,
24+
textState,
25+
} from "./atoms";
1726

1827
function App() {
1928
const [showMenu, setShowMenu] = useRecoilState(showMenuState);
@@ -26,9 +35,9 @@ function App() {
2635
const [penColor, setPenColor] = useState("#000000");
2736
const canvasColor = useRecoilValue(canvasColors);
2837
const [currentCanvas, setCanvas] = useRecoilState(canvasState);
29-
const [doc, setDoc] = useState(null);
38+
const [doc, setDoc] = useRecoilState(docState);
3039
const [provider, setProvider] = useState(null);
31-
const [text, setText] = useState('');
40+
const [text, setText] = useRecoilState(textState);
3241
const textEditor = useRecoilValue(showTextEditor);
3342

3443
useEffect(() => {
@@ -40,7 +49,7 @@ function App() {
4049
}, [doc]);
4150

4251
useEffect(() => {
43-
if (!!doc && !provider) {
52+
if (doc && !provider) {
4453
console.log("setting provider");
4554
const socketioprovider = new SocketIOProvider(
4655
"ws://localhost:8000",
@@ -64,7 +73,7 @@ function App() {
6473
yText.unobserve(observer);
6574
};
6675
}
67-
}, [provider]);
76+
}, [provider, doc]);
6877

6978
function toggleMenu() {
7079
setShowMenu(!showMenu);
@@ -204,8 +213,9 @@ function App() {
204213
{eraserMode && <EraserCursor></EraserCursor>}
205214
{showMenu && <Menu></Menu>}
206215
{textEditor && <TextEditor></TextEditor>}
216+
{console.log(text)}
207217
</div>
208218
);
209219
}
210220

211-
export default App;
221+
export default App;

client/src/atoms.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@ export const showMenuState = atom({
2828
export const showTextEditor = atom({
2929
key: "showTextEditor",
3030
default: false
31+
})
32+
33+
export const docState = atom({
34+
key: "docState",
35+
default: null
36+
})
37+
38+
export const textState = atom({
39+
key: "textState",
40+
default: null
3141
})

client/src/components/TextEditor.jsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import { useSetRecoilState } from "recoil";
2-
import { showTextEditor } from "../atoms";
1+
import { useSetRecoilState, useRecoilValue } from "recoil";
2+
import { showTextEditor, docState, textState } from "../atoms";
33

44
function TextEditor() {
55
const setTextEditorFalse = useSetRecoilState(showTextEditor);
6+
const doc = useRecoilValue(docState);
7+
const text = useRecoilValue(textState);
68

79
function removeTextEditor() {
810
setTextEditorFalse(false);
911
}
1012

13+
function handleChange(event) {
14+
const newText = event.target.value;
15+
const yText = doc.getText('text');
16+
yText.delete(0, yText.length);
17+
yText.insert(0, newText);
18+
}
19+
1120
return (
12-
<div className="w-96 h-96 absolute shadow rounded absolute">
13-
<div className="bg-gray bg-slate-800 w-96 h-14 content-center">
21+
<div className="w-96 h-96 absolute absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
22+
<div className="bg-gray bg-slate-800 w-96 h-14 content-center rounded-t-2xl">
1423
<h1 className="font-sans text-white text-lg justify-center flex">
1524
Plan your drawing
1625
</h1>
@@ -22,8 +31,10 @@ function TextEditor() {
2231
</h1>
2332
</div>
2433
<textarea
25-
className="w-96 h-80 border rounded absolute focus:outline-none focus:ring-0 focus:border-transparent"
34+
className="w-96 h-80 border rounded absolute focus:outline-none focus:ring-0 focus:border-transparent rounded-b-2xl p-2"
2635
placeholder="E.g. Make a painting with trees..."
36+
onChange={handleChange}
37+
value={text}
2738
></textarea>
2839
</div>
2940
);

0 commit comments

Comments
 (0)