mirror of
https://github.com/outline/outline.git
synced 2026-11-25 07:04:37 +03:00
hook up awareness to UI
fix header disappearing
This commit is contained in:
1 parent
cea1d808d1
commit
70ea77ce01
7 files changed
+77
-27
No files matched your search
@@ -25,19 +25,27 @@ export default class MultiplayerExtension extends Extension {
|
||||
provider.awareness.setLocalStateField("user", {
|
||||
color: user.color,
|
||||
name: user.name,
|
||||
id: user.id,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
provider.on("sync", () => {
|
||||
doc.once("update", () => {
|
||||
provider.once("sync", () => {
|
||||
const assignUser = (tr) => {
|
||||
const clientIds = Array.from(doc.store.clients.keys());
|
||||
|
||||
if (!clientIds.includes(doc.clientID)) {
|
||||
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);
|
||||
});
|
||||
|
||||
// const dbProvider = new IndexeddbPersistence(doc.documentId, doc);
|
||||
|
||||
@@ -252,7 +252,7 @@ class DataLoader extends React.Component<Props> {
|
||||
return (
|
||||
<SocketPresence
|
||||
documentId={document.id}
|
||||
userId={auth.user.id}
|
||||
userId={auth.user ? auth.user.id : undefined}
|
||||
isMultiplayer={team.multiplayerEditor}
|
||||
>
|
||||
{(multiplayer) => (
|
||||
|
||||
@@ -31,6 +31,7 @@ import KeyboardShortcutsButton from "./KeyboardShortcutsButton";
|
||||
import MarkAsViewed from "./MarkAsViewed";
|
||||
import References from "./References";
|
||||
import MultiplayerExtension from "multiplayer/MultiplayerExtension";
|
||||
import { WebsocketProvider } from "multiplayer/WebsocketProvider";
|
||||
import { type LocationWithState, type Theme } from "types";
|
||||
import { isCustomDomain } from "utils/domains";
|
||||
import { emojiToUrl } from "utils/emoji";
|
||||
@@ -62,7 +63,9 @@ type Props = {
|
||||
revision: Revision,
|
||||
readOnly: boolean,
|
||||
multiplayer: {
|
||||
provider: any,
|
||||
isConnected: boolean,
|
||||
isReconnecting: boolean,
|
||||
provider: ?WebsocketProvider,
|
||||
doc: Y.Doc,
|
||||
},
|
||||
onCreateLink: (title: string) => string,
|
||||
@@ -431,12 +434,16 @@ class DocumentScene extends React.Component<Props> {
|
||||
)}
|
||||
</Notice>
|
||||
)}
|
||||
{!multiplayer.isConnected && team.multiplayerEditor && (
|
||||
<Notice muted>
|
||||
Connection lost. Any edits will sync once you’re back online.{" "}
|
||||
{multiplayer.isReconnecting && "Trying to reconnect…"}
|
||||
</Notice>
|
||||
)}
|
||||
{team &&
|
||||
multiplayer &&
|
||||
!multiplayer.isConnected &&
|
||||
team.multiplayerEditor && (
|
||||
<Notice muted>
|
||||
Connection lost. Any edits will sync once you’re back
|
||||
online.{" "}
|
||||
{multiplayer.isReconnecting && "Trying to reconnect…"}
|
||||
</Notice>
|
||||
)}
|
||||
<React.Suspense fallback={<LoadingPlaceholder />}>
|
||||
<Flex auto={!readOnly}>
|
||||
{showContents && <Contents headings={headings} />}
|
||||
@@ -450,7 +457,7 @@ class DocumentScene extends React.Component<Props> {
|
||||
title={revision ? revision.title : this.title}
|
||||
document={document}
|
||||
value={readOnly ? value : undefined}
|
||||
defaultValue={value}
|
||||
defaultValue={team && team.multiplayerEditor ? "" : value}
|
||||
disableEmbeds={disableEmbeds}
|
||||
onImageUploadStart={this.onImageUploadStart}
|
||||
onImageUploadStop={this.onImageUploadStop}
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
} from "outline-icons";
|
||||
import { transparentize, darken } from "polished";
|
||||
import * as React from "react";
|
||||
import { Redirect } from "react-router-dom";
|
||||
import { withRouter, type RouterHistory } from "react-router-dom";
|
||||
import styled from "styled-components";
|
||||
import breakpoint from "styled-components-breakpoint";
|
||||
import AuthStore from "stores/AuthStore";
|
||||
@@ -50,6 +50,7 @@ type Props = {
|
||||
publishingIsDisabled: boolean,
|
||||
savingIsDisabled: boolean,
|
||||
onDiscard: () => void,
|
||||
history: RouterHistory,
|
||||
onSave: ({
|
||||
done?: boolean,
|
||||
publish?: boolean,
|
||||
@@ -61,7 +62,6 @@ type Props = {
|
||||
class Header extends React.Component<Props> {
|
||||
@observable isScrolled = false;
|
||||
@observable showShareModal = false;
|
||||
@observable redirectTo: ?string;
|
||||
|
||||
componentDidMount() {
|
||||
window.addEventListener("scroll", this.handleScroll);
|
||||
@@ -78,15 +78,17 @@ class Header extends React.Component<Props> {
|
||||
handleScroll = throttle(this.updateIsScrolled, 50);
|
||||
|
||||
handleEdit = () => {
|
||||
this.redirectTo = editDocumentUrl(this.props.document);
|
||||
this.props.history.push(editDocumentUrl(this.props.document));
|
||||
};
|
||||
|
||||
handleNewFromTemplate = () => {
|
||||
const { document } = this.props;
|
||||
const { document, history } = this.props;
|
||||
|
||||
this.redirectTo = newDocumentUrl(document.collectionId, {
|
||||
templateId: document.id,
|
||||
});
|
||||
history.push(
|
||||
newDocumentUrl(document.collectionId, {
|
||||
templateId: document.id,
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
handleSave = () => {
|
||||
@@ -116,8 +118,6 @@ class Header extends React.Component<Props> {
|
||||
};
|
||||
|
||||
render() {
|
||||
if (this.redirectTo) return <Redirect to={this.redirectTo} push />;
|
||||
|
||||
const {
|
||||
shares,
|
||||
document,
|
||||
@@ -425,4 +425,4 @@ const Title = styled.div`
|
||||
`};
|
||||
`;
|
||||
|
||||
export default inject("auth", "ui", "policies", "shares")(Header);
|
||||
export default inject("auth", "ui", "policies", "shares")(withRouter(Header));
|
||||
@@ -2,20 +2,23 @@
|
||||
import * as React from "react";
|
||||
import * as Y from "yjs";
|
||||
import { SocketContext } from "components/SocketProvider";
|
||||
import useStores from "hooks/useStores";
|
||||
import { WebsocketProvider } from "multiplayer/WebsocketProvider";
|
||||
|
||||
type Props = {
|
||||
children: ({
|
||||
provider: WebsocketProvider,
|
||||
provider: ?WebsocketProvider,
|
||||
isReconnecting: boolean,
|
||||
isConnected: boolean,
|
||||
doc: Y.Doc,
|
||||
}) => React.Node,
|
||||
isMultiplayer: boolean,
|
||||
documentId: string,
|
||||
userId: string,
|
||||
userId?: string,
|
||||
};
|
||||
|
||||
export default function SocketPresence(props: Props) {
|
||||
const { presence } = useStores();
|
||||
const context = React.useContext(SocketContext);
|
||||
const [isConnected, setConnected] = React.useState(
|
||||
context ? context.connected : false
|
||||
@@ -25,11 +28,17 @@ export default function SocketPresence(props: Props) {
|
||||
props.isMultiplayer ? new Y.Doc() : undefined
|
||||
);
|
||||
const [provider] = React.useState(() =>
|
||||
props.isMultiplayer
|
||||
props.isMultiplayer && props.userId
|
||||
? new WebsocketProvider(context, props.documentId, props.userId, doc)
|
||||
: undefined
|
||||
);
|
||||
|
||||
React.useEffect(() => {
|
||||
provider.awareness.on("update", () => {
|
||||
presence.updateFromAwareness(props.documentId, provider.awareness);
|
||||
});
|
||||
}, [provider.awareness]);
|
||||
|
||||
React.useEffect(() => {
|
||||
console.log("useEffect", context);
|
||||
if (!context) return;
|
||||
|
||||
@@ -34,6 +34,32 @@ export default class PresenceStore {
|
||||
this.data.set(documentId, existing);
|
||||
}
|
||||
|
||||
@action updateFromAwareness(documentId: string, awareness) {
|
||||
const existing = this.data.get(documentId) || new Map();
|
||||
const clients = Array.from(awareness.states.values());
|
||||
const userIds = clients.map((client) => client.user && client.user.id);
|
||||
|
||||
existing.forEach((value, key) => {
|
||||
if (!userIds.includes(key)) {
|
||||
existing.delete(key);
|
||||
}
|
||||
});
|
||||
|
||||
clients.forEach((client) => {
|
||||
if (!client.user) {
|
||||
return;
|
||||
}
|
||||
const userId = client.user.id;
|
||||
|
||||
existing.set(userId, {
|
||||
isEditing: !!client.cursor,
|
||||
userId,
|
||||
});
|
||||
});
|
||||
|
||||
this.data.set(documentId, existing);
|
||||
}
|
||||
|
||||
// called when a user presence message is received – user.presence websocket
|
||||
// message.
|
||||
// While in edit mode a message is sent every USER_PRESENCE_INTERVAL, if
|
||||
|
||||
@@ -121,12 +121,12 @@ export async function handleLeave(
|
||||
// TODO: store connections in redis?
|
||||
if (doc.conns.size === 0) {
|
||||
log(`all clients left doc (${documentId}), persisting…`);
|
||||
// TODO: write a revision
|
||||
|
||||
const document = await Document.findByPk(documentId);
|
||||
await documentUpdater({ document, ydoc: doc, userId, done: true });
|
||||
|
||||
doc.destroy();
|
||||
docs.delete(documentId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user