Skip to content

Commit df66106

Browse files
committed
improved colab feature for separate doc
1 parent d73ace0 commit df66106

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

package-lock.json

Lines changed: 64 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
"react": "^18.2.0",
1717
"react-dom": "^18.2.0",
1818
"react-quill": "^2.0.0",
19-
"socket.io-client": "^4.7.2"
19+
"react-router-dom": "^6.15.0",
20+
"socket.io-client": "^4.7.2",
21+
"uuid": "^9.0.0"
2022
},
2123
"devDependencies": {
2224
"@types/react": "^18.2.15",
2325
"@types/react-dom": "^18.2.7",
26+
"@types/uuid": "^9.0.3",
2427
"@typescript-eslint/eslint-plugin": "^6.0.0",
2528
"@typescript-eslint/parser": "^6.0.0",
2629
"@vitejs/plugin-react-swc": "^3.3.2",

src/App.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import './App.css'
22
import Editor from './components/Editor';
3+
import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom';
4+
import {v4 as uuidV4} from 'uuid';
35

46
function App() {
57

68
return (
7-
<Editor />
9+
<Router>
10+
<Routes>
11+
<Route path="/" element={<Navigate to={`/document/${uuidV4()}`}/>} />
12+
<Route path="/document/:id" element={<Editor />} />
13+
</Routes>
14+
</Router>
815
)
916
}
1017

src/components/Editor.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { modules, formats } from '../utils/QuillConfig';
44
import 'react-quill/dist/quill.snow.css';
55
import { Box } from '@mui/material';
66
import { io, Socket } from 'socket.io-client';
7+
import { useParams } from 'react-router-dom';
78

89
function Editor() {
910
const [value, setValue] = useState('');
1011
const [socket, setSocket] = useState<Socket>();
1112
const quillRef = useRef(null);
13+
const { id: documentId } = useParams();
1214

13-
1415
useEffect(() => {
1516
const s = io('http://localhost:9000');
1617
setSocket(s);
@@ -19,6 +20,20 @@ function Editor() {
1920
}
2021
},[]);
2122

23+
useEffect(() => {
24+
quillRef.current?.getEditor().disable();
25+
quillRef.current?.getEditor().setText('Loading...');
26+
},[]);
27+
28+
useEffect(() => {
29+
if(socket == null || quillRef == null) return;
30+
socket.once('load-document', document => {
31+
quillRef.current?.getEditor().setContents(document);
32+
quillRef.current?.getEditor().enable();
33+
});
34+
socket.emit('get-document', documentId);
35+
},[socket, quillRef]);
36+
2237
useEffect(() => {
2338
if(socket == null || quillRef == null) return;
2439

0 commit comments

Comments
 (0)