-
-
Save byrnehollander/8e90fa235fec2fe8a66679f636bdc61d to your computer and use it in GitHub Desktop.
Attempt at Remirror + Yjs
This file contains hidden or 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
import React from 'react' | |
import useErrorBoundary from 'use-error-boundary' | |
import styled from 'styled-components' | |
import { Remirror, useRemirror, useRemirrorContext } from '@remirror/react' | |
import { YjsExtension } from '@remirror/extension-yjs' | |
import { IndexeddbPersistence } from 'y-indexeddb' | |
import { WebrtcProvider } from 'y-webrtc' | |
import { WebsocketProvider } from 'y-websocket' | |
import { Doc } from 'yjs' | |
const Container = styled.div` | |
width: 500px; | |
height: 500px; | |
` | |
const Editor = () => { | |
const { getRootProps } = useRemirrorContext() | |
return ( | |
<Container {...getRootProps()} /> | |
) | |
} | |
const webrtcProvider = (roomName, ydoc) => new WebrtcProvider(roomName, ydoc) | |
const websocketProvider = (roomName, ydoc) => new WebsocketProvider('wss://demos.yjs.dev', roomName, ydoc) | |
const indexeddbProvider = (roomName, ydoc) => new IndexeddbPersistence(roomName, ydoc) | |
const CollaborativeEditor = ({ roomName, editable }) => { | |
const { ErrorBoundary, didCatch, error } = useErrorBoundary() | |
const ydoc = new Doc() | |
const EXTENSIONS = () => [ | |
new YjsExtension({ getProvider: () => websocketProvider(roomName, ydoc) }), | |
new YjsExtension({ getProvider: () => webrtcProvider(roomName, ydoc) }), | |
new YjsExtension({ getProvider: () => indexeddbProvider(roomName, ydoc) }) | |
] | |
const extensionManager = useRemirror(EXTENSIONS) | |
return ( | |
<> | |
{ | |
didCatch | |
? ( | |
<p>An error has been caught: {error.message}</p> | |
) | |
: ( | |
<ErrorBoundary> | |
<Remirror editable={editable} manager={extensionManager}> | |
<Editor /> | |
</Remirror> | |
</ErrorBoundary> | |
) | |
} | |
</> | |
) | |
} | |
export default CollaborativeEditor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Relevant dependencies and their versions:
Error info:
Full version: