-
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.
Merge pull request #152 from boostcampwm-2024/feature-fe-#146
에디터 업데이트 시 노드에 반영하는 기능 추가
- Loading branch information
Showing
7 changed files
with
133 additions
and
44 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
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
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
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
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,32 @@ | ||
import { useState } from "react"; | ||
import * as Y from "yjs"; | ||
import diff from "fast-diff"; | ||
|
||
function diffToDelta(diffResult) { | ||
return diffResult.map(([op, value]) => | ||
op === diff.INSERT | ||
? { insert: value } | ||
: op === diff.EQUAL | ||
? { retain: value.length } | ||
: op === diff.DELETE | ||
? { delete: value.length } | ||
: null, | ||
); | ||
} | ||
|
||
export const useYText = (ydoc: Y.Doc, currentPage: number) => { | ||
const yText = ydoc.getMap("title").get(`title_${currentPage}`) as Y.Text; | ||
|
||
const [input, setInput] = useState(yText.toString()); | ||
|
||
const setYText = (textNew: string) => { | ||
const delta = diffToDelta(diff(input, textNew)); | ||
yText.applyDelta(delta); | ||
}; | ||
|
||
yText.observe(() => { | ||
setInput(yText.toString()); | ||
}); | ||
|
||
return { input, setYText }; | ||
}; |
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,14 @@ | ||
import { create } from "zustand"; | ||
import * as Y from "yjs"; | ||
|
||
interface YDocStore { | ||
ydoc: Y.Doc; | ||
setYDoc: (ydoc: Y.Doc) => void; | ||
} | ||
|
||
const useYDocStore = create<YDocStore>((set) => ({ | ||
ydoc: new Y.Doc(), | ||
setYDoc: (ydoc: Y.Doc) => set({ ydoc }), | ||
})); | ||
|
||
export default useYDocStore; |
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