Forked from byrnehollander/CollaborativeEditor.js
Last active
February 17, 2021 07:24
-
-
Save ifiokjr/d3e52b9d9b2884f59ada1b3a69763104 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 = () => [ | |
// Only one extension of a given type is supported and the highest priority | |
// will win (i.e. the first one). The rest will be deduped. | |
new YjsExtension({ getProvider: () => websocketProvider(roomName, ydoc) }), | |
new YjsExtension({ getProvider: () => webrtcProvider(roomName, ydoc) }), | |
new YjsExtension({ getProvider: () => indexeddbProvider(roomName, ydoc) }) | |
] | |
const { manager } = useRemirror({ extensions }) | |
return ( | |
<> | |
{ | |
didCatch | |
? ( | |
<p>An error has been caught: {error.message}</p> | |
) | |
: ( | |
<ErrorBoundary> | |
<Remirror editable={editable} manager={manager}> | |
<Editor /> | |
</Remirror> | |
</ErrorBoundary> | |
) | |
} | |
</> | |
) | |
} | |
export default CollaborativeEditor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment