Skip to content

Commit bc2b83b

Browse files
committed
Send text to all the clients
1 parent 03dfaa2 commit bc2b83b

File tree

4 files changed

+19
-56
lines changed

4 files changed

+19
-56
lines changed

api/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ io.on('connection', (socket) => {
4343
socket.on('close-text-editor', data => {
4444
socket.broadcast.emit("close-text-editor", data);
4545
})
46+
47+
socket.on("text-updated", (data) => {
48+
socket.broadcast.emit("text-updated", data);
49+
});
4650
})
4751

4852
server.listen(PORT, ()=>{

client/src/App.jsx

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
22
import { useEffect, useRef, useState } from "react";
33
import "./App.css";
4-
import * as Y from "yjs";
5-
import { SocketIOProvider } from "y-socket.io";
64
import socket from "./socket";
75

86
import Sidebar from "./components/Sidebar";
@@ -18,7 +16,6 @@ import {
1816
canvasState,
1917
showMenuState,
2018
showTextEditor,
21-
docState,
2219
textEditorInput
2320
} from "./atoms";
2421

@@ -35,46 +32,9 @@ function App() {
3532
const [penColor, setPenColor] = useState("#000000");
3633
const canvasColor = useRecoilValue(canvasColors);
3734
const [currentCanvas, setCanvas] = useRecoilState(canvasState);
38-
const [doc, setDoc] = useRecoilState(docState);
39-
const [provider, setProvider] = useState(null);
4035
const setTextEditorInput = useSetRecoilState(textEditorInput);
4136
const textEditor = useRecoilValue(showTextEditor);
4237

43-
useEffect(() => {
44-
if (!doc) {
45-
console.log("setting doc");
46-
const _doc = new Y.Doc();
47-
setDoc(_doc);
48-
}
49-
}, [doc]);
50-
51-
useEffect(() => {
52-
if (provider) {
53-
const yText = doc.getText("text");
54-
const observer = () => {
55-
setTextEditorInput(yText.toString());
56-
};
57-
yText.observe(observer);
58-
return () => {
59-
yText.unobserve(observer);
60-
};
61-
}
62-
}, [provider, doc]);
63-
64-
useEffect(() => {
65-
if (doc && !provider) {
66-
console.log("setting provider");
67-
const socketioprovider = new SocketIOProvider(
68-
"ws://localhost:8000",
69-
"text-editor",
70-
doc,
71-
{ autoConnect: true }
72-
);
73-
setProvider(socketioprovider);
74-
socketioprovider.connect();
75-
}
76-
}, [doc, provider]);
77-
7838

7939
function toggleMenu() {
8040
setShowMenu(!showMenu);

client/src/atoms.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ export const showTextEditor = atom({
3030
default: false
3131
})
3232

33-
export const docState = atom({
34-
key: "docState",
35-
default: null
36-
})
37-
3833

3934
export const textEditorInput = atom({
4035
key: "textEditorInput",

client/src/components/TextEditor.jsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
import { useSetRecoilState, useRecoilValue, useRecoilState } from "recoil";
2-
import { showTextEditor, docState, textEditorInput } from "../atoms";
2+
import { showTextEditor, textEditorInput } from "../atoms";
33
import socket from "../socket";
44
import { useCallback, useEffect, useRef } from "react";
55

66
function TextEditor() {
77
const setTextEditorFalse = useSetRecoilState(showTextEditor);
8-
const doc = useRecoilValue(docState);
9-
const text = useRecoilValue(textEditorInput);
8+
// const text = useRecoilValue(textEditorInput);
109
const [input, setInput] = useRecoilState(textEditorInput)
1110
const isRendering = useRef(false);
1211

1312
function removeTextEditor() {
13+
setTextEditorFalse(false);
1414
if (!isRendering.current) {
1515
socket.emit("close-text-editor");
1616
}
17-
setTextEditorFalse(false);
1817
}
1918

2019
const handleRemoveTextEditor = useCallback((rendering) => {
2120
isRendering.current = rendering;
2221
removeTextEditor();
2322
}, []);
2423

24+
const handleTextEditorUpdate = useCallback((data) => {
25+
setInput(data);
26+
}, []);
27+
2528
useEffect(() => {
2629
socket.on("close-text-editor", () => {
2730
handleRemoveTextEditor(true);
2831
});
2932

33+
socket.on("text-updated", data => {
34+
handleTextEditorUpdate(data);
35+
});
36+
3037
return () => {
3138
socket.off("close-text-editor", handleRemoveTextEditor);
3239
};
3340
}, []);
3441

3542
function handleChange(event) {
36-
setInput(event.target.value);
37-
if(!doc) return;
38-
const newText = input;
39-
const yText = doc.getText("text");
40-
yText.delete(0, yText.length);
41-
yText.insert(0, newText);
43+
const value = event.target.value;
44+
setInput(value); // this is asynchronous
45+
socket.emit("text-updated", value);
4246
}
4347

4448
return (
@@ -58,7 +62,7 @@ function TextEditor() {
5862
className="w-96 h-80 border rounded absolute focus:outline-none focus:ring-0 focus:border-transparent rounded-b-2xl p-2"
5963
placeholder="E.g. Make a painting with trees..."
6064
onChange={handleChange}
61-
value={text}
65+
value={input}
6266
></textarea>
6367
</div>
6468
);

0 commit comments

Comments
 (0)