mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +03:00
refactor
This commit is contained in:
1 parent
a55163fb00
commit
8ed030e2ec
7 files changed
+114
-91
No files matched your search
@@ -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(() => {
|
||||
|
||||
@@ -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<?Error>();
|
||||
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 <Loading location={props.location} />;
|
||||
}
|
||||
|
||||
return <Document document={document} location={props.location} readOnly />;
|
||||
return (
|
||||
<Document document={document} location={props.location} isShare readOnly />
|
||||
);
|
||||
}
|
||||
@@ -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<Props> {
|
||||
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<Props> {
|
||||
) {
|
||||
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)
|
||||
);
|
||||
@@ -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<Props> {
|
||||
};
|
||||
|
||||
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<Props> {
|
||||
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<Props> {
|
||||
const showContents =
|
||||
(ui.tocVisible && readOnly) || (isShare && !!headings.length);
|
||||
|
||||
const EditorComponent = multiplayer ? MultiplayerEditor : Editor;
|
||||
|
||||
return (
|
||||
<ErrorBoundary>
|
||||
<Background
|
||||
@@ -359,7 +347,7 @@ class DocumentScene extends React.Component<Props> {
|
||||
auto
|
||||
>
|
||||
<Route
|
||||
path={`${match.url}/move`}
|
||||
path={`${document.url}/move`}
|
||||
component={() => (
|
||||
<DocumentMove document={document} onRequestClose={this.goBack} />
|
||||
)}
|
||||
@@ -456,10 +444,10 @@ class DocumentScene extends React.Component<Props> {
|
||||
<React.Suspense fallback={<LoadingPlaceholder />}>
|
||||
<Flex auto={!readOnly}>
|
||||
{showContents && <Contents headings={headings} />}
|
||||
<EditorComponent
|
||||
<Editor
|
||||
id={document.id}
|
||||
innerRef={this.editor}
|
||||
isShare={isShare}
|
||||
canShowHoverPreviews={!isShare}
|
||||
isDraft={document.isDraft}
|
||||
template={document.isTemplate}
|
||||
key={[injectTemplate, disableEmbeds].join("-")}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { observer } from "mobx-react";
|
||||
import * as React from "react";
|
||||
import Textarea from "react-autosize-textarea";
|
||||
import styled from "styled-components";
|
||||
import * as Y from "yjs";
|
||||
import { MAX_TITLE_LENGTH } from "shared/constants";
|
||||
import parseTitle from "shared/utils/parseTitle";
|
||||
import Document from "models/Document";
|
||||
@@ -12,18 +13,26 @@ import DocumentMetaWithViews from "components/DocumentMetaWithViews";
|
||||
import Editor from "components/Editor";
|
||||
import Flex from "components/Flex";
|
||||
import HoverPreview from "components/HoverPreview";
|
||||
import MultiplayerEditor from "./MultiplayerEditor";
|
||||
import { WebsocketProvider } from "multiplayer/WebsocketProvider";
|
||||
import { documentHistoryUrl } from "utils/routeHelpers";
|
||||
|
||||
type Props = {
|
||||
onChangeTitle: (event: SyntheticInputEvent<>) => 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<Props> {
|
||||
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 (
|
||||
<Flex auto column>
|
||||
@@ -114,17 +127,19 @@ class DocumentEditor extends React.Component<Props> {
|
||||
document={document}
|
||||
to={documentHistoryUrl(document)}
|
||||
/>
|
||||
<Editor
|
||||
<EditorComponent
|
||||
ref={innerRef}
|
||||
autoFocus={title && !this.props.defaultValue}
|
||||
placeholder="…the rest is up to you"
|
||||
onHoverLink={this.handleLinkActive}
|
||||
scrollTo={window.location.hash}
|
||||
readOnly={readOnly}
|
||||
multiplayer={multiplayer}
|
||||
grow
|
||||
{...this.props}
|
||||
{...rest}
|
||||
/>
|
||||
{!readOnly && <ClickablePadding onClick={this.focusAtEnd} grow />}
|
||||
{this.activeLinkEvent && !isShare && readOnly && (
|
||||
{this.activeLinkEvent && canShowHoverPreviews && readOnly && (
|
||||
<HoverPreview
|
||||
node={this.activeLinkEvent.target}
|
||||
event={this.activeLinkEvent}
|
||||
|
||||
@@ -1,38 +1,63 @@
|
||||
// @flow
|
||||
import * as React from "react";
|
||||
import Editor from "./Editor";
|
||||
import * as Y from "yjs";
|
||||
import Editor from "components/Editor";
|
||||
import useCurrentUser from "hooks/useCurrentUser";
|
||||
import MultiplayerExtension from "multiplayer/MultiplayerExtension";
|
||||
import { WebsocketProvider } from "multiplayer/WebsocketProvider";
|
||||
|
||||
type Props = {|
|
||||
onChangeTitle: (event: SyntheticInputEvent<>) => 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 ? (
|
||||
<Editor
|
||||
{...props}
|
||||
defaultValue={undefined}
|
||||
value={undefined}
|
||||
extensions={[
|
||||
new MultiplayerExtension({
|
||||
user,
|
||||
...multiplayer,
|
||||
}),
|
||||
]}
|
||||
/>
|
||||
) : (
|
||||
<Editor {...props} readOnly />
|
||||
const extensions = React.useMemo(() => {
|
||||
console.log("extensions");
|
||||
|
||||
return [
|
||||
new MultiplayerExtension({
|
||||
user,
|
||||
provider,
|
||||
doc,
|
||||
}),
|
||||
];
|
||||
}, [user, provider, doc]);
|
||||
|
||||
return (
|
||||
<span style={{ position: "relative" }}>
|
||||
{isRemoteSynced && (
|
||||
<Editor
|
||||
{...props}
|
||||
key="multiplayer"
|
||||
defaultValue={undefined}
|
||||
value={undefined}
|
||||
extensions={extensions}
|
||||
style={{ position: "absolute", width: "100%" }}
|
||||
/>
|
||||
)}
|
||||
{showCachedDocument && (
|
||||
<Editor
|
||||
{...props}
|
||||
style={{ position: "absolute", width: "100%" }}
|
||||
readOnly
|
||||
/>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user