Version History (#768)

* Stash. Super rough progress

* Stash

* 'h' how toggles history panel
Add documents.restore endpoint

* Add tests for documents.restore endpoint

* Document restore endpoint

* Tiding, RevisionMenu, remove scroll dep

* Add history menu item

* Paginate loading

* Fixed: Error boundary styling
Select first revision faster

* Diff summary, styling

* Add history loading placeholder
Fix move modal not opening

* Fixes: Refreshing page on specific revision

* documentation for document.revision

* Better handle versions with no text changes (will no longer be created)
This commit is contained in:
Tom Moor
2018-09-29 21:24:07 -07:00
committed by GitHub
parent 7973bfeca2
commit d0bee23432
28 changed files with 794 additions and 85 deletions

View File

@@ -7,7 +7,6 @@ import ApiKeysStore from 'stores/ApiKeysStore';
import UsersStore from 'stores/UsersStore';
import CollectionsStore from 'stores/CollectionsStore';
import IntegrationsStore from 'stores/IntegrationsStore';
import CacheStore from 'stores/CacheStore';
import LoadingIndicator from 'components/LoadingIndicator';
type Props = {
@@ -29,7 +28,6 @@ const Auth = observer(({ auth, children }: Props) => {
// will get overridden on route change
if (!authenticatedStores) {
// Stores for authenticated user
const cache = new CacheStore(user.id);
authenticatedStores = {
integrations: new IntegrationsStore({
ui: stores.ui,
@@ -39,7 +37,6 @@ const Auth = observer(({ auth, children }: Props) => {
collections: new CollectionsStore({
ui: stores.ui,
teamId: team.id,
cache,
}),
};

View File

@@ -23,11 +23,13 @@ class Avatar extends React.Component<Props> {
};
render() {
const { src, ...rest } = this.props;
return (
<CircleImg
size={this.props.size}
onError={this.handleError}
src={this.error ? placeholder : this.props.src}
src={this.error ? placeholder : src}
{...rest}
/>
);
}

View File

@@ -0,0 +1,138 @@
// @flow
import * as React from 'react';
import { withRouter } from 'react-router-dom';
import { observable, action } from 'mobx';
import { observer, inject } from 'mobx-react';
import styled from 'styled-components';
import Waypoint from 'react-waypoint';
import ArrowKeyNavigation from 'boundless-arrow-key-navigation';
import { DEFAULT_PAGINATION_LIMIT } from 'stores/DocumentsStore';
import Document from 'models/Document';
import RevisionsStore from 'stores/RevisionsStore';
import Flex from 'shared/components/Flex';
import { ListPlaceholder } from 'components/LoadingPlaceholder';
import Revision from './components/Revision';
import { documentHistoryUrl } from 'utils/routeHelpers';
type Props = {
match: Object,
document: Document,
revisions: RevisionsStore,
revision?: Object,
history: Object,
};
@observer
class DocumentHistory extends React.Component<Props> {
@observable isLoaded: boolean = false;
@observable isFetching: boolean = false;
@observable offset: number = 0;
@observable allowLoadMore: boolean = true;
async componentDidMount() {
this.selectFirstRevision();
await this.loadMoreResults();
this.selectFirstRevision();
}
fetchResults = async () => {
this.isFetching = true;
const limit = DEFAULT_PAGINATION_LIMIT;
const results = await this.props.revisions.fetchPage({
limit,
offset: this.offset,
id: this.props.document.id,
});
if (
results &&
(results.length === 0 || results.length < DEFAULT_PAGINATION_LIMIT)
) {
this.allowLoadMore = false;
} else {
this.offset += DEFAULT_PAGINATION_LIMIT;
}
this.isLoaded = true;
this.isFetching = false;
};
selectFirstRevision = () => {
const revisions = this.revisions;
if (revisions.length && !this.props.revision) {
this.props.history.replace(
documentHistoryUrl(this.props.document, this.revisions[0].id)
);
}
};
@action
loadMoreResults = async () => {
// Don't paginate if there aren't more results or were in the middle of fetching
if (!this.allowLoadMore || this.isFetching) return;
await this.fetchResults();
};
get revisions() {
return this.props.revisions.getDocumentRevisions(this.props.document.id);
}
render() {
const showLoading = !this.isLoaded && this.isFetching;
const maxChanges = this.revisions.reduce((acc, change) => {
if (acc < change.diff.added + change.diff.removed) {
return change.diff.added + change.diff.removed;
}
return acc;
}, 0);
return (
<Wrapper column>
{showLoading ? (
<Loading>
<ListPlaceholder count={5} />
</Loading>
) : (
<ArrowKeyNavigation
mode={ArrowKeyNavigation.mode.VERTICAL}
defaultActiveChildIndex={0}
>
{this.revisions.map((revision, index) => (
<Revision
key={revision.id}
revision={revision}
document={this.props.document}
maxChanges={maxChanges}
showMenu={index !== 0}
/>
))}
</ArrowKeyNavigation>
)}
{this.allowLoadMore && (
<Waypoint key={this.offset} onEnter={this.loadMoreResults} />
)}
</Wrapper>
);
}
}
const Loading = styled.div`
margin: 0 16px;
`;
const Wrapper = styled(Flex)`
position: fixed;
top: 0;
right: 0;
bottom: 0;
min-width: ${props => props.theme.sidebarWidth};
border-left: 1px solid ${props => props.theme.slateLight};
overflow: scroll;
overscroll-behavior: none;
`;
export default withRouter(inject('revisions')(DocumentHistory));

View File

@@ -0,0 +1,58 @@
// @flow
import * as React from 'react';
import styled from 'styled-components';
import Flex from 'shared/components/Flex';
type Props = {
added: number,
removed: number,
max: number,
color?: string,
width: number,
};
export default function DiffSummary({
added,
removed,
max,
color,
width = 180,
}: Props) {
const summary = [];
if (added) summary.push(`+${added}`);
if (removed) summary.push(`-${removed}`);
const hasChanges = !!summary.length;
return (
<Flex align="center">
{hasChanges && (
<Diff>
<Bar color={color} style={{ width: `${added / max * width}px` }} />
<Bar color={color} style={{ width: `${removed / max * width}px` }} />
</Diff>
)}
<Summary>{hasChanges ? summary.join(', ') : 'No changes'}</Summary>
</Flex>
);
}
const Summary = styled.div`
display: inline-block;
font-size: 10px;
opacity: 0.5;
flex-grow: 100;
text-transform: uppercase;
`;
const Diff = styled(Flex)`
height: 6px;
margin-right: 2px;
`;
const Bar = styled.div`
display: inline-block;
background: ${props => props.color || props.theme.text};
height: 100%;
opacity: 0.3;
margin-right: 1px;
`;

View File

@@ -0,0 +1,80 @@
// @flow
import * as React from 'react';
import { NavLink } from 'react-router-dom';
import styled, { withTheme } from 'styled-components';
import format from 'date-fns/format';
import { MoreIcon } from 'outline-icons';
import Flex from 'shared/components/Flex';
import Time from 'shared/components/Time';
import Avatar from 'components/Avatar';
import RevisionMenu from 'menus/RevisionMenu';
import DiffSummary from './DiffSummary';
import { documentHistoryUrl } from 'utils/routeHelpers';
class Revision extends React.Component<*> {
render() {
const { revision, document, maxChanges, showMenu, theme } = this.props;
return (
<StyledNavLink
to={documentHistoryUrl(document, revision.id)}
activeStyle={{ background: theme.primary, color: theme.white }}
>
<Author>
<StyledAvatar src={revision.createdBy.avatarUrl} />{' '}
{revision.createdBy.name}
</Author>
<Meta>
<Time dateTime={revision.createdAt}>
{format(revision.createdAt, 'MMMM Do, YYYY h:mm a')}
</Time>
</Meta>
<DiffSummary {...revision.diff} max={maxChanges} />
{showMenu && (
<StyledRevisionMenu
document={document}
revision={revision}
label={<MoreIcon color={theme.white} />}
/>
)}
</StyledNavLink>
);
}
}
const StyledAvatar = styled(Avatar)`
border-color: transparent;
margin-right: 4px;
`;
const StyledRevisionMenu = styled(RevisionMenu)`
position: absolute;
right: 16px;
top: 16px;
`;
const StyledNavLink = styled(NavLink)`
color: ${props => props.theme.text};
display: block;
padding: 16px;
font-size: 15px;
position: relative;
height: 100px;
`;
const Author = styled(Flex)`
font-weight: 500;
padding: 0;
margin: 0;
`;
const Meta = styled.p`
font-size: 14px;
opacity: 0.75;
margin: 0 0 2px;
padding: 0;
`;
export default withTheme(Revision);

View File

@@ -0,0 +1,3 @@
// @flow
import DocumentHistory from './DocumentHistory';
export default DocumentHistory;

View File

@@ -77,6 +77,7 @@ const Pre = styled.pre`
padding: 16px;
border-radius: 4px;
font-size: 12px;
white-space: pre-wrap;
`;
export default ErrorBoundary;