Revert "refactor: cleanup"

This reverts commit 9a0a98eff2.
This commit is contained in:
Apoorv Mishra committed 2023-01-28 21:19:22 +05:30
1 parent 9a0a98eff2
commit b301210a3b
4 files changed
+252 -2

No files matched your search

+46
View File
@@ -5,6 +5,7 @@ import {
StarredIcon,
PrintIcon,
UnstarredIcon,
DocumentIcon,
NewDocumentIcon,
ShapesIcon,
ImportIcon,
@@ -16,6 +17,7 @@ import {
TrashIcon,
CrossIcon,
ArchiveIcon,
ShuffleIcon,
HistoryIcon,
LightBulbIcon,
UnpublishIcon,
@@ -41,6 +43,30 @@ import {
searchPath,
} from "~/utils/routeHelpers";
export const openDocument = createAction({
name: ({ t }) => t("Open document"),
section: DocumentSection,
shortcut: ["o", "d"],
keywords: "go to",
icon: <DocumentIcon />,
children: ({ stores }) => {
const paths = stores.collections.pathsToDocuments;
return paths
.filter((path) => path.type === "document")
.map((path) => ({
// Note: using url which includes the slug rather than id here to bust
// cache if the document is renamed
id: path.url,
name: path.title,
icon: () =>
stores.documents.get(path.id)?.isStarred ? <StarredIcon /> : null,
section: DocumentSection,
perform: () => history.push(path.url),
}));
},
});
export const createDocument = createAction({
name: ({ t }) => t("New document"),
section: DocumentSection,
@@ -508,6 +534,24 @@ export const createTemplate = createAction({
},
});
export const openRandomDocument = createAction({
id: "random",
section: DocumentSection,
name: ({ t }) => t(`Open random document`),
icon: <ShuffleIcon />,
perform: ({ stores, activeDocumentId }) => {
const documentPaths = stores.collections.pathsToDocuments.filter(
(path) => path.type === "document" && path.id !== activeDocumentId
);
const documentPath =
documentPaths[Math.round(Math.random() * documentPaths.length)];
if (documentPath) {
history.push(documentPath.url);
}
},
});
export const searchDocumentsForQuery = (searchQuery: string) =>
createAction({
id: "search",
@@ -681,6 +725,7 @@ export const openDocumentInsights = createAction({
});
export const rootDocumentActions = [
openDocument,
archiveDocument,
createDocument,
createTemplate,
@@ -695,6 +740,7 @@ export const rootDocumentActions = [
unsubscribeDocument,
duplicateDocument,
moveDocument,
openRandomDocument,
permanentlyDeleteDocument,
printDocument,
pinDocumentToCollection,
+122
View File
@@ -0,0 +1,122 @@
import { observer } from "mobx-react";
import { GoToIcon } from "outline-icons";
import * as React from "react";
import styled from "styled-components";
import { DocumentPath } from "~/stores/CollectionsStore";
import Collection from "~/models/Collection";
import Document from "~/models/Document";
import Flex from "~/components/Flex";
import CollectionIcon from "~/components/Icons/CollectionIcon";
type Props = {
result: DocumentPath;
document?: Document | null | undefined;
collection: Collection | null | undefined;
onSuccess?: () => void;
style?: React.CSSProperties;
ref?: (element: React.ElementRef<"div"> | null | undefined) => void;
};
@observer
class PathToDocument extends React.Component<Props> {
handleClick = async (ev: React.SyntheticEvent) => {
ev.preventDefault();
const { document, result, onSuccess } = this.props;
if (!document) {
return;
}
if (result.type === "document") {
await document.move(result.collectionId, result.id);
} else {
await document.move(result.collectionId);
}
if (onSuccess) {
onSuccess();
}
};
render() {
const { result, collection, document, ref, style } = this.props;
const Component = document ? ResultWrapperLink : ResultWrapper;
if (!result) {
return <div />;
}
return (
// @ts-expect-error ts-migrate(2604) FIXME: JSX element type 'Component' does not have any con... Remove this comment to see the full error message
<Component
ref={ref}
onClick={this.handleClick}
href=""
style={style}
role="option"
selectable
>
{collection && <CollectionIcon collection={collection} />}
&nbsp;
{result.path
.map((doc) => <Title key={doc.id}>{doc.title}</Title>)
// @ts-expect-error ts-migrate(2739) FIXME: Type 'Element[]' is missing the following properti... Remove this comment to see the full error message
.reduce((prev, curr) => [prev, <StyledGoToIcon />, curr])}
{document && (
<DocumentTitle>
{" "}
<StyledGoToIcon /> <Title>{document.title}</Title>
</DocumentTitle>
)}
</Component>
);
}
}
const DocumentTitle = styled(Flex)``;
const Title = styled.span`
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
`;
const StyledGoToIcon = styled(GoToIcon)`
fill: ${(props) => props.theme.divider};
`;
const ResultWrapper = styled.div`
display: flex;
margin-bottom: 10px;
user-select: none;
color: ${(props) => props.theme.text};
cursor: default;
svg {
flex-shrink: 0;
}
`;
const ResultWrapperLink = styled(ResultWrapper.withComponent("a"))`
padding: 8px 4px;
${DocumentTitle} {
display: none;
}
svg {
flex-shrink: 0;
}
&:hover,
&:active,
&:focus {
background: ${(props) => props.theme.listItemHoverBackground};
outline: none;
${DocumentTitle} {
display: flex;
}
}
`;
export default PathToDocument;
+82 -2
View File
@@ -1,13 +1,34 @@
import invariant from "invariant";
import { find } from "lodash";
import { concat, find, last } from "lodash";
import { computed, action } from "mobx";
import { CollectionPermission, FileOperationFormat } from "@shared/types";
import {
CollectionPermission,
FileOperationFormat,
NavigationNode,
} from "@shared/types";
import Collection from "~/models/Collection";
import { client } from "~/utils/ApiClient";
import { AuthorizationError, NotFoundError } from "~/utils/errors";
import BaseStore from "./BaseStore";
import RootStore from "./RootStore";
enum DocumentPathItemType {
Collection = "collection",
Document = "document",
}
export type DocumentPathItem = {
type: DocumentPathItemType;
id: string;
collectionId: string;
title: string;
url: string;
};
export type DocumentPath = DocumentPathItem & {
path: DocumentPathItem[];
};
export default class CollectionsStore extends BaseStore<Collection> {
constructor(rootStore: RootStore) {
super(rootStore, Collection);
@@ -35,6 +56,52 @@ export default class CollectionsStore extends BaseStore<Collection> {
});
}
/**
* List of paths to each of the documents, where paths are composed of id and title/name pairs
*/
@computed
get pathsToDocuments(): DocumentPath[] {
const results: DocumentPathItem[][] = [];
const travelDocuments = (
documentList: NavigationNode[],
collectionId: string,
path: DocumentPathItem[]
) =>
documentList.forEach((document: NavigationNode) => {
const { id, title, url } = document;
const node = {
type: DocumentPathItemType.Document,
id,
collectionId,
title,
url,
};
results.push(concat(path, node));
travelDocuments(document.children, collectionId, concat(path, [node]));
});
if (this.isLoaded) {
this.data.forEach((collection) => {
const { id, name, url } = collection;
const node = {
type: DocumentPathItemType.Collection,
id,
collectionId: id,
title: name,
url,
};
results.push([node]);
travelDocuments(collection.documents, id, [node]);
});
}
return results.map((result) => {
const tail = last(result) as DocumentPathItem;
return { ...tail, path: result };
});
}
@action
import = async (attachmentId: string, format?: string) => {
await client.post("/collections.import", {
@@ -128,6 +195,19 @@ export default class CollectionsStore extends BaseStore<Collection> {
await star?.delete();
};
getPathForDocument(documentId: string): DocumentPath | undefined {
return this.pathsToDocuments.find((path) => path.id === documentId);
}
titleForDocument(documentUrl: string): string | undefined {
const path = this.pathsToDocuments.find((path) => path.url === documentUrl);
if (path) {
return path.title;
}
return;
}
getByUrl(url: string): Collection | null | undefined {
return find(this.orderedData, (col: Collection) => url.endsWith(col.urlId));
}
@@ -11,6 +11,7 @@
"Delete IndexedDB cache": "Delete IndexedDB cache",
"IndexedDB cache deleted": "IndexedDB cache deleted",
"Developer": "Developer",
"Open document": "Open document",
"New document": "New document",
"Publish": "Publish",
"Document published": "Document published",
@@ -41,6 +42,7 @@
"Import document": "Import document",
"Templatize": "Templatize",
"Create template": "Create template",
"Open random document": "Open random document",
"Search documents for \"{{searchQuery}}\"": "Search documents for \"{{searchQuery}}\"",
"Move": "Move",
"Move {{ documentType }}": "Move {{ documentType }}",