-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { BlockCRDT } from "@noctaCrdt/Crdt"; | ||
import { Char } from "@noctaCrdt/Node"; | ||
import { CharId } from "@noctaCrdt/NodeId"; | ||
import { useEffect, useState, useRef } from "react"; | ||
import { io, Socket } from "socket.io-client"; | ||
|
||
interface RemoteInsertOperation { | ||
node: Char; // Node<NodeId>μμ Charλ‘ λ³κ²½ | ||
} | ||
interface RemoteDeleteOperation { | ||
targetId: CharId; | ||
clock: number; | ||
} | ||
|
||
export const TestApp = () => { | ||
const contentRef = useRef<HTMLDivElement>(null); | ||
const crdtRef = useRef<BlockCRDT | null>(null); // CRDTλ₯Ό BlockCRDTλ‘ λ³κ²½ | ||
const [content, setContent] = useState(""); | ||
const socketRef = useRef<Socket | null>(null); | ||
|
||
useEffect(() => { | ||
const socket: Socket = io("http://localhost:3000"); | ||
socketRef.current = socket; | ||
|
||
socket.on("connect", () => { | ||
socket.emit("requestId"); | ||
}); | ||
|
||
socket.on("assignId", (id: number) => { | ||
crdtRef.current = new BlockCRDT(id); | ||
}); | ||
|
||
socket.on("document", (data: any) => { | ||
if (crdtRef.current) { | ||
crdtRef.current.clock = data.clock; | ||
crdtRef.current.client = data.client; | ||
|
||
if (data.LinkedList.head) { | ||
crdtRef.current.LinkedList.head = new CharId( // NodeIdλ₯Ό CharIdλ‘ λ³κ²½ | ||
data.LinkedList.head.clock, | ||
data.LinkedList.head.client, | ||
); | ||
} else { | ||
crdtRef.current.LinkedList.head = null; | ||
} | ||
|
||
crdtRef.current.LinkedList.nodeMap = {}; | ||
for (const key in data.LinkedList.nodeMap) { | ||
const node = data.LinkedList.nodeMap[key]; | ||
const reconstructedNode = new Char(node.value, new CharId(node.id.clock, node.id.client)); // Nodeλ₯Ό Charλ‘ λ³κ²½ | ||
reconstructedNode.next = node.next ? new CharId(node.next.clock, node.next.client) : null; | ||
reconstructedNode.prev = node.prev ? new CharId(node.prev.clock, node.prev.client) : null; | ||
crdtRef.current.LinkedList.nodeMap[key] = reconstructedNode; | ||
} | ||
|
||
setContent(crdtRef.current.read()); | ||
} | ||
}); | ||
|
||
socket.on("insert", (operation: RemoteInsertOperation) => { | ||
if (crdtRef.current) { | ||
crdtRef.current.remoteInsert(operation); | ||
setContent(crdtRef.current.read()); | ||
} | ||
}); | ||
|
||
socket.on("delete", (operation: RemoteDeleteOperation) => { | ||
if (crdtRef.current) { | ||
crdtRef.current.remoteDelete(operation); | ||
setContent(crdtRef.current.read()); | ||
} | ||
}); | ||
|
||
return () => { | ||
socket.disconnect(); | ||
}; | ||
}, []); | ||
// content μνκ° λ³κ²½λλ©΄ contentEditable divλ₯Ό μ λ°μ΄νΈ | ||
useEffect(() => { | ||
if (contentRef.current && contentRef.current.innerText !== content) { | ||
contentRef.current.innerText = content; | ||
} | ||
}, [content]); | ||
|
||
/** | ||
* λ λ¬Έμμ΄ κ°μ μ°¨μ΄ μΈλ±μ€λ₯Ό μ°Ύμ΅λλ€. | ||
* @param oldStr μ΄μ λ¬Έμμ΄ | ||
* @param newStr νμ¬ λ¬Έμμ΄ | ||
* @returns μ°¨μ΄κ° λ°μν μΈλ±μ€ | ||
*/ | ||
const findDifferenceIndex = (oldStr: string, newStr: string): number => { | ||
let i = 0; | ||
while (i < oldStr.length && i < newStr.length && oldStr[i] === newStr[i]) { | ||
i += 1; | ||
} | ||
return i; | ||
}; | ||
|
||
// μΊλΏ 컀μ μμΉλ‘ μΆκ° μΈλ±μ€λ₯Ό μ ν΄λ λ κ²κ°μλλ. | ||
|
||
// const handleCursorChange = () => { | ||
// if (!crdtRef.current || !contentRef.current || !socketRef.current) return; | ||
|
||
// const selection = window.getSelection(); | ||
// if (selection && selection.rangeCount > 0) { | ||
// const range = selection.getRangeAt(0); | ||
// const preCaretRange = range.cloneRange(); | ||
// preCaretRange.selectNodeContents(contentRef.current); | ||
// preCaretRange.setEnd(range.endContainer, range.endOffset); | ||
// const caretPosition = preCaretRange.toString().length; | ||
|
||
// const cursorData: CursorPosition = { | ||
// clientId: numericId, | ||
// position: caretPosition, | ||
// }; | ||
|
||
// // μλ²λ‘ 컀μ μμΉ μ ν | ||
// socketRef.current.emit("cursor", cursorData); | ||
// } | ||
// }; | ||
/** | ||
* μ¬μ©μ μ λ ₯ νΈλ€λ¬ | ||
*/ | ||
const handleInput = () => { | ||
if (!crdtRef.current || !contentRef.current || !socketRef.current) return; | ||
const newContent = contentRef.current.innerText || ""; | ||
const oldContent = crdtRef.current.read(); | ||
const diffIndex = findDifferenceIndex(oldContent, newContent); | ||
|
||
const trimmedNewContent = newContent.trim(); | ||
console.log("newContent:", JSON.stringify(newContent)); | ||
console.log("oldContent:", oldContent); | ||
console.log("newContent.length:", newContent.length, "oldContent.length:", oldContent.length); | ||
|
||
if (trimmedNewContent.length > oldContent.length) { | ||
// μ½μ μ΄ λ°μν κ²½μ° | ||
const insertedChar = newContent[diffIndex]; | ||
console.log(`μ½μ : '${insertedChar}' at index ${diffIndex}`); | ||
|
||
// CRDT λ‘컬 μ½μ μν | ||
const insertOp = crdtRef.current.localInsert(diffIndex, insertedChar); | ||
|
||
// μλ²λ‘ μ½μ μ°μ° μ ν | ||
socketRef.current.emit("insert", insertOp); | ||
|
||
// λ‘컬 ν μ€νΈ μ λ°μ΄νΈ | ||
setContent(crdtRef.current.read()); | ||
} else if (trimmedNewContent.length < oldContent.length) { | ||
// μμ κ° λ°μν κ²½μ° | ||
console.log(`μμ : at index ${diffIndex}`); | ||
|
||
// CRDT λ‘컬 μμ μν | ||
const deleteOp = crdtRef.current.localDelete(diffIndex); | ||
console.log(deleteOp, typeof deleteOp); | ||
// μλ²λ‘ μμ μ°μ° μ ν | ||
socketRef.current.emit("delete", deleteOp); | ||
|
||
// λ‘컬 ν μ€νΈ μ λ°μ΄νΈ | ||
setContent(crdtRef.current.read()); | ||
} else { | ||
// λ³κ²½μ΄ μκ±°λ, κ΅μ²΄ λ°μν κ²½μ° (λ¨μΌ κ΅μ²΄λ₯Ό μ²λ¦¬νλ €λ©΄ μΆκ° λ‘μ§ νμ) | ||
console.log("λ³κ²½ μμ λλ κ΅μ²΄ λ°μ."); | ||
// μΆκ° λ‘μ§ κ΅¬ν κ°λ₯ | ||
} | ||
}; | ||
|
||
return ( | ||
<div style={{ padding: "20px", maxWidth: "800px", margin: "0 auto" }}> | ||
<h1>μ€μκ° νΈμ§κΈ° (CRDT μ μ©)</h1> | ||
<p>ν΄λΌμ΄μΈνΈ μ«μID: {0}</p> | ||
<div | ||
contentEditable="true" | ||
ref={contentRef} | ||
onInput={handleInput} | ||
// onSelect={handleCursorChange} | ||
style={{ | ||
border: "1px solid #ddd", | ||
padding: "8px", | ||
minHeight: "150px", | ||
width: "100%", | ||
outline: "none", | ||
whiteSpace: "pre-wrap", | ||
}} | ||
/> | ||
<h2>CRDT μν</h2> | ||
<pre>{JSON.stringify(crdtRef.current?.LinkedList, null, 2)}</pre> | ||
</div> | ||
); | ||
}; |