diff --git a/app/models/Document.js b/app/models/Document.js index 8aee00c40a..21f5ef54fb 100644 --- a/app/models/Document.js +++ b/app/models/Document.js @@ -297,6 +297,7 @@ export default class Document extends BaseModel { done: options.done, autosave: options.autosave, collectionId: this.collectionId, + parentDocumentId: this.parentDocumentId, }); } diff --git a/app/scenes/Document/components/Document.js b/app/scenes/Document/components/Document.js index 5985daa319..ff9934916e 100644 --- a/app/scenes/Document/components/Document.js +++ b/app/scenes/Document/components/Document.js @@ -196,6 +196,7 @@ class DocumentScene extends React.Component { publish?: boolean, autosave?: boolean, collectionId?: string, + parentDocumentId?: string, } = {} ) => { const { document } = this.props; @@ -213,6 +214,7 @@ class DocumentScene extends React.Component { // prevent autosave if nothing has changed if ( !options.collectionId && + !options.parentDocumentId && options.autosave && document.text.trim() === text.trim() && document.title.trim() === title.trim() @@ -223,6 +225,8 @@ class DocumentScene extends React.Component { document.text = text; if (options.collectionId) document.collectionId = options.collectionId; + if (options.parentDocumentId) + document.parentDocumentId = options.parentDocumentId; let isNew = !document.id; this.isSaving = true; diff --git a/app/scenes/Document/components/Header.js b/app/scenes/Document/components/Header.js index a600bce6e0..60ed71bb86 100644 --- a/app/scenes/Document/components/Header.js +++ b/app/scenes/Document/components/Header.js @@ -1,23 +1,37 @@ // @flow +import { Search } from "js-search"; +import { last } from "lodash"; import { observer } from "mobx-react"; import { TableOfContentsIcon, EditIcon, PlusIcon, MoreIcon, + SearchIcon, } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; -import styled from "styled-components"; +import AutoSizer from "react-virtualized-auto-sizer"; +import { FixedSizeList as List } from "react-window"; +import { + Dialog, + DialogBackdrop, + DialogDisclosure, + useDialogState, +} from "reakit"; +import styled, { useTheme } from "styled-components"; +import breakpoint from "styled-components-breakpoint"; +import { type DocumentPath } from "stores/CollectionsStore"; import Document from "models/Document"; import { Action, Separator } from "components/Actions"; import Badge from "components/Badge"; import Button from "components/Button"; import Collaborators from "components/Collaborators"; import DocumentBreadcrumb from "components/DocumentBreadcrumb"; -import FilterOptions from "components/FilterOptions"; +import Flex from "components/Flex"; import Header from "components/Header"; +import PathToDocument from "components/PathToDocument"; import Tooltip from "components/Tooltip"; import PublicBreadcrumb from "./PublicBreadcrumb"; import ShareButton from "./ShareButton"; @@ -66,9 +80,10 @@ function DocumentHeader({ headings, }: Props) { const { t } = useTranslation(); - const { auth, ui, policies, collections } = useStores(); + const { auth, ui, policies, collections, documents } = useStores(); const isMobile = useMobile(); - + const dialog = useDialogState({ modal: false }); + const theme = useTheme(); const handleSave = React.useCallback(() => { onSave({ done: true }); }, [onSave]); @@ -84,33 +99,90 @@ function DocumentHeader({ const canToggleEmbeds = auth.team && auth.team.documentEmbeds; const canEdit = can.update && !isEditing; const hasCollection = collections.get(document.computedCollectionId); - const [chosenCollection, setChosenCollection] = React.useState(""); + const [searchTerm, setSearchTerm] = React.useState(); - const updateCollection = React.useCallback(async () => { - if (!chosenCollection) { - return; + const handleFilter = (ev) => { + setSearchTerm(ev.target.value); + }; + + const searchIndex = React.useMemo(() => { + const paths = collections.pathsToDocuments; + const index = new Search("id"); + index.addIndex("title"); + + // Build index + const indexeableDocuments = []; + paths.forEach((path) => { + const doc = documents.get(path.id); + if (!doc || !doc.isTemplate) { + indexeableDocuments.push(path); + } + }); + index.addDocuments(indexeableDocuments); + + return index; + }, [documents, collections.pathsToDocuments]); + + const results: DocumentPath[] = React.useMemo(() => { + const onlyShowCollections = document.isTemplate; + let results = []; + if (collections.isLoaded) { + if (searchTerm) { + results = searchIndex.search(searchTerm); + } else { + results = searchIndex._documents; + } } - onSave({ done: true, collectionId: chosenCollection }); - }, [chosenCollection, onSave]); - React.useEffect(() => { - updateCollection(); - }, [updateCollection]); + if (onlyShowCollections) { + results = results.filter((result) => result.type === "collection"); + } else { + // Exclude root from search results if document is already at the root + if (!document.parentDocumentId) { + results = results.filter( + (result) => result.id !== document.collectionId + ); + } - const options = React.useMemo(() => { - const collectionOptions = collections.orderedData.map((collection) => ({ - key: collection.id, - label: collection.name, - })); + // Exclude document if on the path to result, or the same result + results = results.filter( + (result) => + !result.path.map((doc) => doc.id).includes(document.id) && + last(result.path.map((doc) => doc.id)) !== document.parentDocumentId + ); + } - return [ - { - key: "", - label: t("Choose collection"), - }, - ...collectionOptions, - ]; - }, [collections.orderedData, t]); + return results; + }, [document, collections, searchTerm, searchIndex]); + + const handleSuccess = async (result: DocumentPath) => { + if (!document) return; + + if (result.type === "collection") { + onSave({ done: true, collectionId: result.collectionId }); + } else { + onSave({ + done: true, + collectionId: result.collectionId, + parentDocumentId: result.id, + }); + } + }; + + const row = ({ index, data, style }) => { + const result = data[index]; + return ( + + ); + }; + + const data = results; const toc = ( )} {can.update && isDraft && !isRevision && !hasCollection && ( - - setChosenCollection(key)} - defaultLabel={t("Choose collection")} - selectedPrefix={`${t("Collection")}:`} - /> - + + + + Choose collection + + + + + + + + + + + + + {({ width, height }) => { + return ( + + data[index].id} + > + {row} + + + ); + }} + + + + + + + )} {isEditing && ( <> @@ -327,4 +446,74 @@ const TocWrapper = styled(Action)` left: 42px; `; +const Content = styled.div` + background: ${(props) => props.theme.background}; + width: 25vw; + height: 40vh; + border-radius: 8px; + position: absolute; + padding: 10px; + top: 5px; + right: -20vh; + box-shadow: ${(props) => props.theme.menuShadow}; +`; + +const Input = styled.input` + width: 100%; + padding: 10px 10px 10px 40px; + font-size: 16px; + font-weight: 400; + outline: none; + border: 0; + background: ${(props) => props.theme.sidebarBackground}; + transition: ${(props) => props.theme.backgroundTransition}; + border-radius: 4px; + + color: ${(props) => props.theme.text}; + + ::-webkit-search-cancel-button { + -webkit-appearance: none; + } + ::-webkit-input-placeholder { + color: ${(props) => props.theme.placeholder}; + } + :-moz-placeholder { + color: ${(props) => props.theme.placeholder}; + } + ::-moz-placeholder { + color: ${(props) => props.theme.placeholder}; + } + :-ms-input-placeholder { + color: ${(props) => props.theme.placeholder}; + } +`; + +const StyledIcon = styled(SearchIcon)` + position: absolute; + left: 12px; +`; + +const Wrapper = styled.div` + position: relative; +`; + +const Position = styled.div` + position: absolute; + z-index: ${(props) => props.theme.depths.menu}; + + ${breakpoint("mobile", "tablet")` + position: fixed !important; + transform: none !important; + top: auto !important; + right: 8px !important; + bottom: 16px !important; + left: 8px !important; + `}; +`; + +const Results = styled.div` + padding: 8px 0; + height: calc(100% - 46px); +`; + export default observer(DocumentHeader); diff --git a/server/api/documents.js b/server/api/documents.js index 77cf3d58ba..e6ab6c9c30 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -1000,6 +1000,7 @@ router.post("documents.update", auth(), async (ctx) => { templateId, append, collectionId, + parentDocumentId, } = ctx.body; const editorVersion = ctx.headers["x-editor-version"]; @@ -1035,6 +1036,7 @@ router.post("documents.update", auth(), async (ctx) => { method: ["withMembership", user.id], }).findByPk(collectionId); document.collectionId = collectionId; + if (parentDocumentId) document.parentDocumentId = parentDocumentId; } else { collection = document.collection; } diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 4b65ec8753..01518f7560 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -327,6 +327,7 @@ "Show contents": "Show contents", "Edit {{noun}}": "Edit {{noun}}", "Archived": "Archived", + "Search collections & documents": "Search collections & documents", "Save Draft": "Save Draft", "Done Editing": "Done Editing", "New from template": "New from template", @@ -351,7 +352,6 @@ "Document moved": "Document moved", "Current location": "Current location", "Choose a new location": "Choose a new location", - "Search collections & documents": "Search collections & documents", "Couldn’t create the document, try again?": "Couldn’t create the document, try again?", "Document permanently deleted": "Document permanently deleted", "Are you sure you want to permanently delete the {{ documentTitle }} document? This action is immediate and cannot be undone.": "Are you sure you want to permanently delete the {{ documentTitle }} document? This action is immediate and cannot be undone.",