Fix a bug when newly connected peer overwrites existing room state

This commit is contained in:
Pavel Murygin 2021-01-16 20:23:42 +03:00
parent e1574427a5
commit 18daaa3439
2 changed files with 18 additions and 3 deletions

View File

@ -120,6 +120,10 @@ body {
border: none; border: none;
outline: none; outline: none;
&:disabled {
background-color: $color-disabled;
}
} }
.text-input { .text-input {

View File

@ -17,11 +17,10 @@ const broadcastUpdates = _.debounce((text: string, syncClient: SyncClient) => {
export const CollaborativeEditor = () => { export const CollaborativeEditor = () => {
const client = useFluenceClient()!; const client = useFluenceClient()!;
const [text, setText] = useState(''); const [text, setText] = useState<string | null>(null);
const [syncClient, setSyncClient] = useState(new SyncClient()); const [syncClient, setSyncClient] = useState(new SyncClient());
useEffect(() => { useEffect(() => {
syncClient.syncDoc(initDoc());
syncClient.handleDocUpdate = (doc) => { syncClient.handleDocUpdate = (doc) => {
setText(doc.text.toString()); setText(doc.text.toString());
}; };
@ -48,6 +47,10 @@ export const CollaborativeEditor = () => {
for (let e of res) { for (let e of res) {
syncClient.receiveChanges(e.body); syncClient.receiveChanges(e.body);
} }
if (syncClient.getDoc() === undefined) {
syncClient.syncDoc(initDoc());
}
}); });
return () => { return () => {
@ -62,5 +65,13 @@ export const CollaborativeEditor = () => {
broadcastUpdates(newText, syncClient); broadcastUpdates(newText, syncClient);
}; };
return <textarea spellCheck={false} className="code-editor" value={text} onChange={handleTextUpdate} />; return (
<textarea
spellCheck={false}
className="code-editor"
disabled={text === null}
value={text ?? ''}
onChange={handleTextUpdate}
/>
);
}; };