-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexteditor.js
67 lines (60 loc) · 1.73 KB
/
Texteditor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, { useCallback, useEffect, useState } from "react";
import Quill from "quill";
import "quill/dist/quill.snow.css";
import { io } from "socket.io-client";
const TOOLBAR_OPTIONS = [
[{ header: [1, 2, 3, 4, 5, 6, false] }],
[{ font: [] }],
[{ list: "ordered" }, { list: "bullet" }],
["bold", "italic", "underline"],
[{ color: [] }, { background: [] }],
[{ script: "sub" }, { script: "super" }],
[{ align: [] }],
["image", "blockquote", "code-block"],
["clean"],
];
const Texteditor = () => {
const [socket, setSocket] = useState();
const [quill, setQuill] = useState();
useEffect(() => {
const s = io("http://localhost:3001");
setSocket(s);
return () => {
s.disconnect();
};
}, []);
useEffect(() => {
if (socket == null || quill == null) return;
const handler = delta => {
quill.updateContents(delta)
};
socket.on("receive-changes", handler);
return () => {
socket.off("receive-changes", handler);
};
}, [socket, quill]);
useEffect(() => {
if (socket == null || quill == null) return;
const handler = (delta, oldDelta, source) => {
if (source !== "user") return;
socket.emit("send-chnges", delta);
};
quill.on("text-change", handler);
return () => {
quill.off("text-change", handler);
};
}, [socket, quill]);
const wrapperRef = useCallback((wrapper) => {
if (wrapper == null) return;
wrapper.innerHTML = "";
const editor = document.createElement("div");
wrapper.append(editor);
const q = new Quill(editor, {
theme: "snow",
modules: { toolbar: TOOLBAR_OPTIONS },
});
setQuill(q);
}, []);
return <div className="container" ref={wrapperRef}></div>;
};
export default Texteditor;