|
| 1 | +import "@blocknote/core/fonts/inter.css"; |
| 2 | +import { withCollaboration, SuggestionsExtension } from "@blocknote/core/y"; |
| 3 | +import { localStorageEndpoints } from "./localStorageEndpoints.js"; |
| 4 | +import { VersioningExtension } from "@blocknote/core/extensions"; |
| 5 | +import { |
| 6 | + BlockNoteViewEditor, |
| 7 | + FloatingComposerController, |
| 8 | + useCreateBlockNote, |
| 9 | + useEditorState, |
| 10 | + useExtension, |
| 11 | + useExtensionState, |
| 12 | +} from "@blocknote/react"; |
| 13 | +import { BlockNoteView } from "@blocknote/mantine"; |
| 14 | +import "@blocknote/mantine/style.css"; |
| 15 | +import { useEffect, useMemo, useState } from "react"; |
| 16 | +import * as Y from "@y/y"; |
| 17 | +import { WebsocketProvider } from "@y/websocket"; |
| 18 | + |
| 19 | +import { getRandomColor, HARDCODED_USERS, MyUserType } from "./userdata"; |
| 20 | +import { SettingsSelect } from "./SettingsSelect"; |
| 21 | +import "./style.css"; |
| 22 | +import { |
| 23 | + DefaultThreadStoreAuth, |
| 24 | + CommentsExtension, |
| 25 | +} from "@blocknote/core/comments"; |
| 26 | +import { YjsThreadStore } from "@blocknote/core/y"; |
| 27 | + |
| 28 | +import { CommentsSidebar } from "./CommentsSidebar"; |
| 29 | +import { VersionHistorySidebar } from "./VersionHistorySidebar"; |
| 30 | +import { SuggestionActions } from "./SuggestionActions"; |
| 31 | +import { SuggestionActionsPopup } from "./SuggestionActionsPopup"; |
| 32 | + |
| 33 | +const roomName = "blocknote-versioning-example"; |
| 34 | +const doc = new Y.Doc(); |
| 35 | +const provider = new WebsocketProvider( |
| 36 | + "wss://demos.yjs.dev/ws", |
| 37 | + roomName, |
| 38 | + doc, |
| 39 | + { connect: false }, |
| 40 | +); |
| 41 | +provider.connectBc(); |
| 42 | +doc.on("update", () => { |
| 43 | + console.log("doc-update", doc.get().toJSON()); |
| 44 | +}); |
| 45 | + |
| 46 | +const suggestionModeDoc = new Y.Doc({ isSuggestionDoc: true }); |
| 47 | +suggestionModeDoc.on("update", () => { |
| 48 | + console.log("suggestion-update", suggestionModeDoc.get().toJSON()); |
| 49 | +}); |
| 50 | +const suggestionModeProvider = new WebsocketProvider( |
| 51 | + "wss://demos.yjs.dev/ws", |
| 52 | + roomName + "-suggestions", |
| 53 | + suggestionModeDoc, |
| 54 | + { connect: false }, |
| 55 | +); |
| 56 | +const suggestionModeAttributionManager = Y.createAttributionManagerFromDiff( |
| 57 | + doc, |
| 58 | + suggestionModeDoc, |
| 59 | + // { |
| 60 | + // attrs: [ |
| 61 | + // // Y.createAttributionItem("insert", ["John Doe"]), |
| 62 | + // // Y.createAttributionItem("delete", ["John Doe"]), |
| 63 | + // ], |
| 64 | + // }, |
| 65 | +); |
| 66 | +suggestionModeProvider.connectBc(); |
| 67 | + |
| 68 | +async function resolveUsers(userIds: string[]) { |
| 69 | + // fake a (slow) network request |
| 70 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 71 | + |
| 72 | + return HARDCODED_USERS.filter((user) => userIds.includes(user.id)); |
| 73 | +} |
| 74 | + |
| 75 | +export default function App() { |
| 76 | + const [activeUser, setActiveUser] = useState<MyUserType>(HARDCODED_USERS[0]); |
| 77 | + |
| 78 | + const threadStore = useMemo(() => { |
| 79 | + return new YjsThreadStore( |
| 80 | + activeUser.id, |
| 81 | + doc.get("threads"), |
| 82 | + new DefaultThreadStoreAuth(activeUser.id, activeUser.role), |
| 83 | + ); |
| 84 | + }, [doc, activeUser]); |
| 85 | + |
| 86 | + const editor = useCreateBlockNote( |
| 87 | + withCollaboration({ |
| 88 | + collaboration: { |
| 89 | + provider, |
| 90 | + suggestionDoc: suggestionModeDoc, |
| 91 | + attributionManager: suggestionModeAttributionManager, |
| 92 | + fragment: doc.get(), |
| 93 | + user: { color: getRandomColor(), name: activeUser.username }, |
| 94 | + versioningEndpoints: localStorageEndpoints, |
| 95 | + }, |
| 96 | + extensions: [CommentsExtension({ threadStore, resolveUsers })], |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + const { |
| 101 | + enableSuggestions, |
| 102 | + disableSuggestions, |
| 103 | + viewSuggestions, |
| 104 | + checkUnresolvedSuggestions, |
| 105 | + } = useExtension(SuggestionsExtension, { editor }); |
| 106 | + const hasUnresolvedSuggestions = useEditorState({ |
| 107 | + selector: () => checkUnresolvedSuggestions(), |
| 108 | + editor, |
| 109 | + }); |
| 110 | + |
| 111 | + const { previewedSnapshotId } = useExtensionState(VersioningExtension, { |
| 112 | + editor, |
| 113 | + }); |
| 114 | + |
| 115 | + const [editingMode, setEditingMode] = useState< |
| 116 | + "editing" | "suggestions" | "view-suggestions" |
| 117 | + >("editing"); |
| 118 | + useEffect(() => { |
| 119 | + if (editingMode !== "editing") { |
| 120 | + disableSuggestions(); |
| 121 | + setEditingMode("editing"); |
| 122 | + } |
| 123 | + }, [previewedSnapshotId]); |
| 124 | + const [sidebar, setSidebar] = useState<"comments" | "versionHistory">( |
| 125 | + "versionHistory", |
| 126 | + ); |
| 127 | + |
| 128 | + return ( |
| 129 | + <div className="wrapper"> |
| 130 | + <BlockNoteView |
| 131 | + className={"full-collaboration"} |
| 132 | + editor={editor} |
| 133 | + editable={ |
| 134 | + previewedSnapshotId === undefined && activeUser.role === "editor" |
| 135 | + } |
| 136 | + renderEditor={false} |
| 137 | + comments={sidebar !== "comments"} |
| 138 | + > |
| 139 | + <div className="layout"> |
| 140 | + <div className="editor-panel"> |
| 141 | + {previewedSnapshotId === undefined && ( |
| 142 | + <div className={"settings"}> |
| 143 | + <SettingsSelect |
| 144 | + label={"User"} |
| 145 | + items={HARDCODED_USERS.map((user) => ({ |
| 146 | + text: `${user.username} (${ |
| 147 | + user.role === "editor" ? "Editor" : "Commenter" |
| 148 | + })`, |
| 149 | + icon: null, |
| 150 | + onClick: () => { |
| 151 | + setActiveUser(user); |
| 152 | + }, |
| 153 | + isSelected: user.id === activeUser.id, |
| 154 | + }))} |
| 155 | + /> |
| 156 | + {activeUser.role === "editor" && ( |
| 157 | + <SettingsSelect |
| 158 | + label={"Mode"} |
| 159 | + items={[ |
| 160 | + { |
| 161 | + text: "Editing", |
| 162 | + icon: null, |
| 163 | + onClick: () => { |
| 164 | + disableSuggestions(); |
| 165 | + setEditingMode("editing"); |
| 166 | + }, |
| 167 | + isSelected: editingMode === "editing", |
| 168 | + }, |
| 169 | + { |
| 170 | + text: "Editing + Viewing Suggestions", |
| 171 | + icon: null, |
| 172 | + onClick: () => { |
| 173 | + viewSuggestions(); |
| 174 | + setEditingMode("view-suggestions"); |
| 175 | + }, |
| 176 | + isSelected: editingMode === "view-suggestions", |
| 177 | + }, |
| 178 | + { |
| 179 | + text: "Suggesting", |
| 180 | + icon: null, |
| 181 | + onClick: () => { |
| 182 | + enableSuggestions(); |
| 183 | + setEditingMode("suggestions"); |
| 184 | + }, |
| 185 | + isSelected: editingMode === "suggestions", |
| 186 | + }, |
| 187 | + ]} |
| 188 | + /> |
| 189 | + )} |
| 190 | + <SettingsSelect |
| 191 | + label={"Sidebar"} |
| 192 | + items={[ |
| 193 | + { |
| 194 | + text: "Version History", |
| 195 | + icon: null, |
| 196 | + onClick: () => setSidebar("versionHistory"), |
| 197 | + isSelected: sidebar === "versionHistory", |
| 198 | + }, |
| 199 | + { |
| 200 | + text: "Comments", |
| 201 | + icon: null, |
| 202 | + onClick: () => setSidebar("comments"), |
| 203 | + isSelected: sidebar === "comments", |
| 204 | + }, |
| 205 | + ]} |
| 206 | + /> |
| 207 | + {activeUser.role === "editor" && |
| 208 | + editingMode === "suggestions" && |
| 209 | + hasUnresolvedSuggestions && <SuggestionActions />} |
| 210 | + </div> |
| 211 | + )} |
| 212 | + <BlockNoteViewEditor /> |
| 213 | + <SuggestionActionsPopup /> |
| 214 | + {sidebar === "comments" && <FloatingComposerController />} |
| 215 | + </div> |
| 216 | + {sidebar === "comments" && <CommentsSidebar />} |
| 217 | + {sidebar === "versionHistory" && <VersionHistorySidebar />} |
| 218 | + </div> |
| 219 | + </BlockNoteView> |
| 220 | + </div> |
| 221 | + ); |
| 222 | +} |
0 commit comments