From 8ed030e2ec610c0ef1441cebbf7b28fa06d291df Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 20 Nov 2020 00:05:25 -0800 Subject: [PATCH] refactor --- app/multiplayer/MultiplayerExtension.js | 42 +++++----- app/scenes/Document/Shared.js | 11 ++- app/scenes/Document/components/DataLoader.js | 14 +--- app/scenes/Document/components/Document.js | 28 ++----- app/scenes/Document/components/Editor.js | 27 +++++-- .../Document/components/MultiplayerEditor.js | 77 ++++++++++++------- .../Document/components/SocketPresence.js | 6 +- 7 files changed, 114 insertions(+), 91 deletions(-) diff --git a/app/multiplayer/MultiplayerExtension.js b/app/multiplayer/MultiplayerExtension.js index 3bd969fc8a..2ab0943c86 100644 --- a/app/multiplayer/MultiplayerExtension.js +++ b/app/multiplayer/MultiplayerExtension.js @@ -20,33 +20,27 @@ export default class MultiplayerExtension extends Extension { const { user, provider, doc } = this.options; const type = doc.get("prosemirror", Y.XmlFragment); - provider.on("status", ({ status }) => { - if (status === "connected") { - provider.awareness.setLocalStateField("user", { - color: user.color, - name: user.name, - id: user.id, - }); + const assignUser = (tr) => { + const clientIds = Array.from(doc.store.clients.keys()); + + if ( + tr.local && + tr.changed.size > 0 && + !clientIds.includes(doc.clientID) + ) { + const permanentUserData = new Y.PermanentUserData(doc); + permanentUserData.setUserMapping(doc, doc.clientID, user.id); + doc.off("afterTransaction", assignUser); } + }; + + provider.awareness.setLocalStateField("user", { + color: user.color, + name: user.name, + id: user.id, }); - provider.once("sync", () => { - const assignUser = (tr) => { - const clientIds = Array.from(doc.store.clients.keys()); - - if ( - tr.local && - tr.changed.size > 0 && - !clientIds.includes(doc.clientID) - ) { - const permanentUserData = new Y.PermanentUserData(doc); - permanentUserData.setUserMapping(doc, doc.clientID, user.id); - doc.off("afterTransaction", assignUser); - } - }; - - doc.on("afterTransaction", assignUser); - }); + doc.on("afterTransaction", assignUser); // const dbProvider = new IndexeddbPersistence(doc.documentId, doc); // dbProvider.whenSynced.then(() => { diff --git a/app/scenes/Document/Shared.js b/app/scenes/Document/Shared.js index 57ffa05c46..4e6d554a9f 100644 --- a/app/scenes/Document/Shared.js +++ b/app/scenes/Document/Shared.js @@ -1,6 +1,7 @@ // @flow import * as React from "react"; import { type Match } from "react-router-dom"; +import { useTheme } from "styled-components"; import Error404 from "scenes/Error404"; import ErrorOffline from "scenes/ErrorOffline"; import useStores from "../../hooks/useStores"; @@ -15,11 +16,17 @@ type Props = {| |}; export default function SharedEditor(props: Props) { + const theme = useTheme(); const [document, setDocument] = React.useState(); const [error, setError] = React.useState(); const { documents } = useStores(); const { shareId, documentSlug } = props.match.params; + // ensure the wider page color always matches the theme + React.useEffect(() => { + window.document.body.style.background = theme.background; + }, [theme]); + React.useEffect(() => { async function fetchData() { try { @@ -42,5 +49,7 @@ export default function SharedEditor(props: Props) { return ; } - return ; + return ( + + ); } diff --git a/app/scenes/Document/components/DataLoader.js b/app/scenes/Document/components/DataLoader.js index 2c830bc109..44679b65d2 100644 --- a/app/scenes/Document/components/DataLoader.js +++ b/app/scenes/Document/components/DataLoader.js @@ -7,7 +7,6 @@ import { observer, inject } from "mobx-react"; import * as React from "react"; import type { RouterHistory, Match } from "react-router-dom"; import { withRouter } from "react-router-dom"; -import { withTheme } from "styled-components"; import parseDocumentSlug from "shared/utils/parseDocumentSlug"; import AuthStore from "stores/AuthStore"; import DocumentsStore from "stores/DocumentsStore"; @@ -21,7 +20,7 @@ import Error404 from "scenes/Error404"; import ErrorOffline from "scenes/ErrorOffline"; import HideSidebar from "./HideSidebar"; import Loading from "./Loading"; -import { type LocationWithState, type Theme } from "types"; +import { type LocationWithState } from "types"; import { NotFoundError, OfflineError } from "utils/errors"; import isInternalUrl from "utils/isInternalUrl"; import { matchDocumentEdit, updateDocumentUrl } from "utils/routeHelpers"; @@ -35,7 +34,6 @@ type Props = {| policies: PoliciesStore, revisions: RevisionsStore, ui: UiStore, - theme: Theme, history: RouterHistory, children: (any) => React.Node, |}; @@ -50,7 +48,6 @@ class DataLoader extends React.Component { const { documents, match } = this.props; this.document = documents.getByUrl(match.params.documentSlug); this.loadDocument(); - this.updateBackground(); } componentDidUpdate(prevProps: Props) { @@ -75,13 +72,6 @@ class DataLoader extends React.Component { ) { this.loadRevision(); } - this.updateBackground(); - } - - updateBackground() { - // ensure the wider page color always matches the theme. This is to - // account for share links which don't sit in the wider Layout component - window.document.body.style.background = this.props.theme.background; } get isEditing() { @@ -272,5 +262,5 @@ export default withRouter( "revisions", "policies", "shares" - )(withTheme(DataLoader)) + )(DataLoader) ); diff --git a/app/scenes/Document/components/Document.js b/app/scenes/Document/components/Document.js index 6b801d4678..31c48d8174 100644 --- a/app/scenes/Document/components/Document.js +++ b/app/scenes/Document/components/Document.js @@ -6,7 +6,7 @@ import { InputIcon } from "outline-icons"; import * as React from "react"; import keydown from "react-keydown"; import { Prompt, Route, withRouter } from "react-router-dom"; -import type { RouterHistory, Match } from "react-router-dom"; +import type { RouterHistory } from "react-router-dom"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; import * as Y from "yjs"; @@ -30,14 +30,12 @@ import Editor from "./Editor"; import Header from "./Header"; import KeyboardShortcutsButton from "./KeyboardShortcutsButton"; import MarkAsViewed from "./MarkAsViewed"; -import MultiplayerEditor from "./MultiplayerEditor"; import References from "./References"; import { WebsocketProvider } from "multiplayer/WebsocketProvider"; import { type LocationWithState, type Theme } from "types"; import { isCustomDomain } from "utils/domains"; import { emojiToUrl } from "utils/emoji"; import { - collectionUrl, documentMoveUrl, documentHistoryUrl, editDocumentUrl, @@ -56,16 +54,17 @@ Are you sure you want to discard them? `; type Props = { - match: Match, history: RouterHistory, location: LocationWithState, abilities: Object, document: Document, revision: Revision, readOnly: boolean, + isShare?: boolean, multiplayer: { isConnected: boolean, isReconnecting: boolean, + isRemoteSynced: boolean, provider: ?WebsocketProvider, doc: Y.Doc, }, @@ -312,15 +311,7 @@ class DocumentScene extends React.Component { }; goBack = () => { - let url; - if (this.props.document.url) { - url = this.props.document.url; - } else if (this.props.match.params.id) { - url = collectionUrl(this.props.match.params.id); - } - if (url) { - this.props.history.push(url); - } + this.props.history.push(this.props.document.url); }; render() { @@ -328,14 +319,13 @@ class DocumentScene extends React.Component { document, revision, readOnly, + isShare, abilities = {}, auth, ui, - match, multiplayer, } = this.props; const team = auth.team; - const isShare = !!match.params.shareId; const value = revision ? revision.text : document.text; const injectTemplate = document.injectTemplate; @@ -348,8 +338,6 @@ class DocumentScene extends React.Component { const showContents = (ui.tocVisible && readOnly) || (isShare && !!headings.length); - const EditorComponent = multiplayer ? MultiplayerEditor : Editor; - return ( { auto > ( )} @@ -456,10 +444,10 @@ class DocumentScene extends React.Component { }> {showContents && } - ) => void, title: string, defaultValue: string, document: Document, isDraft: boolean, - isShare: boolean, + canShowHoverPreviews?: boolean, readOnly?: boolean, onSave: ({ publish?: boolean, done?: boolean, autosave?: boolean }) => mixed, innerRef: { current: any }, + multiplayer: { + isConnected: boolean, + isReconnecting: boolean, + isRemoteSynced: boolean, + provider: ?WebsocketProvider, + doc: Y.Doc, + }, }; @observer @@ -88,12 +97,16 @@ class DocumentEditor extends React.Component { title, onChangeTitle, isDraft, - isShare, + canShowHoverPreviews, readOnly, innerRef, + multiplayer, + ...rest } = this.props; + const { emoji } = parseTitle(title); const startsWithEmojiAndSpace = !!(emoji && title.startsWith(`${emoji} `)); + const EditorComponent = multiplayer ? MultiplayerEditor : Editor; return ( @@ -114,17 +127,19 @@ class DocumentEditor extends React.Component { document={document} to={documentHistoryUrl(document)} /> - {!readOnly && } - {this.activeLinkEvent && !isShare && readOnly && ( + {this.activeLinkEvent && canShowHoverPreviews && readOnly && ( ) => void, - title: string, - defaultValue: string, - document: Document, - isDraft: boolean, - readOnly?: boolean, - onSave: ({ publish?: boolean, done?: boolean, autosave?: boolean }) => mixed, - innerRef: { current: any }, -|}; +type Props = { + multiplayer: { + isConnected: boolean, + isReconnecting: boolean, + isRemoteSynced: boolean, + provider: ?WebsocketProvider, + doc: Y.Doc, + }, +}; export default function MultiplayerEditor({ multiplayer, ...props }: Props) { const user = useCurrentUser(); + const [showCachedDocument, setShowCachedDocument] = React.useState(true); + const { provider, doc, isRemoteSynced } = multiplayer; - console.log("isRemoteSynced", multiplayer.isRemoteSynced); + React.useEffect(() => { + if (isRemoteSynced) { + setTimeout(() => setShowCachedDocument(false), 100); + } + }, [showCachedDocument, isRemoteSynced]); - return multiplayer.isRemoteSynced ? ( - - ) : ( - + const extensions = React.useMemo(() => { + console.log("extensions"); + + return [ + new MultiplayerExtension({ + user, + provider, + doc, + }), + ]; + }, [user, provider, doc]); + + return ( + + {isRemoteSynced && ( + + )} + {showCachedDocument && ( + + )} + ); } diff --git a/app/scenes/Document/components/SocketPresence.js b/app/scenes/Document/components/SocketPresence.js index 530a35e0d1..83d87f6405 100644 --- a/app/scenes/Document/components/SocketPresence.js +++ b/app/scenes/Document/components/SocketPresence.js @@ -34,7 +34,9 @@ export default function SocketPresence(props: Props) { : undefined ); - provider.once("sync", () => setRemoteSynced(true)); + if (provider) { + provider.once("sync", () => setRemoteSynced(true)); + } React.useEffect(() => { return () => { @@ -62,7 +64,6 @@ export default function SocketPresence(props: Props) { }, [presence, props.documentId, awareness]); React.useEffect(() => { - console.log("useEffect", context); if (!context) return; const emitJoin = () => { @@ -91,6 +92,7 @@ export default function SocketPresence(props: Props) { return () => { if (!context) return; + context.emit("leave", { documentId: props.documentId }); context.off("authenticated", emitJoin); context.off("connect", updateStatus);