|
| 1 | +import "@blocknote/core/fonts/inter.css"; |
| 2 | +import "@blocknote/mantine/style.css"; |
| 3 | +import { BlockNoteView } from "@blocknote/mantine"; |
| 4 | +import { useCreateBlockNote } from "@blocknote/react"; |
| 5 | + |
| 6 | +import YPartyKitProvider from "y-partykit/provider"; |
| 7 | +import * as Y from "yjs"; |
| 8 | +import "./styles.css"; |
| 9 | +import { useEffect, useState } from "react"; |
| 10 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 11 | +import { EditorView } from "prosemirror-view"; |
| 12 | + |
| 13 | +const params = new URLSearchParams(window.location.search); |
| 14 | +const ghostWritingRoom = params.get("room"); |
| 15 | +const ghostWriterIndex = parseInt(params.get("index") || "1"); |
| 16 | +const isGhostWriting = Boolean(ghostWritingRoom); |
| 17 | +const roomName = ghostWritingRoom || `ghost-writer-${Date.now()}`; |
| 18 | +// Sets up Yjs document and PartyKit Yjs provider. |
| 19 | +const doc = new Y.Doc(); |
| 20 | +const provider = new YPartyKitProvider( |
| 21 | + "blocknote-dev.yousefed.partykit.dev", |
| 22 | + // Use a unique name as a "room" for your application. |
| 23 | + roomName, |
| 24 | + doc, |
| 25 | +); |
| 26 | + |
| 27 | +/** |
| 28 | + * Y-prosemirror has an optimization, where it doesn't send awareness updates unless the editor is currently focused. |
| 29 | + * So, for the ghost writers, we override the hasFocus method to always return true. |
| 30 | + */ |
| 31 | +if (isGhostWriting) { |
| 32 | + EditorView.prototype.hasFocus = () => true; |
| 33 | +} |
| 34 | + |
| 35 | +const ghostContent = |
| 36 | + "This demo shows a two-way sync of documents. It allows you to test collaboration features, and see how stable the editor is. "; |
| 37 | + |
| 38 | +export default function App() { |
| 39 | + const [numGhostWriters, setNumGhostWriters] = useState(1); |
| 40 | + const [isPaused, setIsPaused] = useState(false); |
| 41 | + const editor = useCreateBlockNote({ |
| 42 | + collaboration: { |
| 43 | + // The Yjs Provider responsible for transporting updates: |
| 44 | + provider, |
| 45 | + // Where to store BlockNote data in the Y.Doc: |
| 46 | + fragment: doc.getXmlFragment("document-store"), |
| 47 | + // Information (name and color) for this user: |
| 48 | + user: { |
| 49 | + name: isGhostWriting |
| 50 | + ? `Ghost Writer #${ghostWriterIndex}` |
| 51 | + : "My Username", |
| 52 | + color: isGhostWriting ? "#CCCCCC" : "#00ff00", |
| 53 | + }, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + if (!isGhostWriting || isPaused) { |
| 59 | + return; |
| 60 | + } |
| 61 | + let index = 0; |
| 62 | + let timeout: NodeJS.Timeout; |
| 63 | + |
| 64 | + const scheduleNextChar = () => { |
| 65 | + const jitter = Math.random() * 200; // Random delay between 0-200ms |
| 66 | + timeout = setTimeout(() => { |
| 67 | + const firstBlock = editor.document?.[0]; |
| 68 | + if (firstBlock) { |
| 69 | + editor.insertInlineContent(ghostContent[index], { |
| 70 | + updateSelection: true, |
| 71 | + }); |
| 72 | + index = (index + 1) % ghostContent.length; |
| 73 | + } |
| 74 | + scheduleNextChar(); |
| 75 | + }, 50 + jitter); |
| 76 | + }; |
| 77 | + |
| 78 | + scheduleNextChar(); |
| 79 | + |
| 80 | + return () => clearTimeout(timeout); |
| 81 | + }, [editor, isPaused]); |
| 82 | + |
| 83 | + // Renders the editor instance. |
| 84 | + return ( |
| 85 | + <> |
| 86 | + {isGhostWriting ? ( |
| 87 | + <button onClick={() => setIsPaused((a) => !a)}> |
| 88 | + {isPaused ? "Resume Ghost Writer" : "Pause Ghost Writer"} |
| 89 | + </button> |
| 90 | + ) : ( |
| 91 | + <> |
| 92 | + <button onClick={() => setNumGhostWriters((a) => a + 1)}> |
| 93 | + Add a Ghost Writer |
| 94 | + </button> |
| 95 | + <button onClick={() => setNumGhostWriters((a) => a - 1)}> |
| 96 | + Remove a Ghost Writer |
| 97 | + </button> |
| 98 | + <button |
| 99 | + onClick={() => { |
| 100 | + window.open( |
| 101 | + `${window.location.origin}${window.location.pathname}?room=${roomName}&index=-1`, |
| 102 | + "_blank", |
| 103 | + ); |
| 104 | + }}> |
| 105 | + Ghost Writer in a new window |
| 106 | + </button> |
| 107 | + </> |
| 108 | + )} |
| 109 | + <BlockNoteView editor={editor} /> |
| 110 | + |
| 111 | + {!isGhostWriting && ( |
| 112 | + <div className="two-way-sync"> |
| 113 | + {Array.from({ length: numGhostWriters }).map((_, index) => ( |
| 114 | + <iframe |
| 115 | + src={`${window.location.origin}${ |
| 116 | + window.location.pathname |
| 117 | + }?room=${roomName}&index=${index + 1}&hideMenu=true`} |
| 118 | + title="ghost writer" |
| 119 | + className="ghost-writer" |
| 120 | + /> |
| 121 | + ))} |
| 122 | + </div> |
| 123 | + )} |
| 124 | + </> |
| 125 | + ); |
| 126 | +} |
0 commit comments