diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index 2988a7673d..ba9b0a3ddc 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -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: , + 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 ? : 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: , + 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, diff --git a/app/components/PathToDocument.tsx b/app/components/PathToDocument.tsx new file mode 100644 index 0000000000..d35b1d149e --- /dev/null +++ b/app/components/PathToDocument.tsx @@ -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 { + 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
; + } + + 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 + + {collection && } +   + {result.path + .map((doc) => {doc.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, , curr])} + {document && ( + + {" "} + {document.title} + + )} + + ); + } +} + +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; diff --git a/app/stores/CollectionsStore.ts b/app/stores/CollectionsStore.ts index 74743cf44c..31c1aab30e 100644 --- a/app/stores/CollectionsStore.ts +++ b/app/stores/CollectionsStore.ts @@ -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 { constructor(rootStore: RootStore) { super(rootStore, Collection); @@ -35,6 +56,52 @@ export default class CollectionsStore extends BaseStore { }); } + /** + * 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 { 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)); } diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index ca1f845e69..02bee0c31a 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -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 }}",