diff --git a/app/components/ContextMenu/MenuItem.tsx b/app/components/ContextMenu/MenuItem.tsx index 1febb9b7ee..36b12c5d9b 100644 --- a/app/components/ContextMenu/MenuItem.tsx +++ b/app/components/ContextMenu/MenuItem.tsx @@ -13,6 +13,7 @@ import MenuIconWrapper from "./MenuIconWrapper"; type Props = { id?: string; onClick?: (event: React.MouseEvent) => void | Promise; + onPointerMove?: (event: React.MouseEvent) => void | Promise; active?: boolean; selected?: boolean; disabled?: boolean; @@ -31,6 +32,7 @@ type Props = { const MenuItem = ( { onClick, + onPointerMove, children, active, selected, @@ -90,6 +92,7 @@ const MenuItem = ( return ( transparentize(0.5, props.theme.text)}; + } + @media (hover: none) and (pointer: coarse) { font-size: 16px; } diff --git a/app/editor/components/LinkEditor.tsx b/app/editor/components/LinkEditor.tsx index c9290c2c2a..baed135a00 100644 --- a/app/editor/components/LinkEditor.tsx +++ b/app/editor/components/LinkEditor.tsx @@ -1,15 +1,24 @@ -import { ArrowIcon, CloseIcon, OpenIcon } from "outline-icons"; +import { observer } from "mobx-react"; +import { ArrowIcon, CloseIcon, DocumentIcon, OpenIcon } from "outline-icons"; import { Mark } from "prosemirror-model"; import { Selection } from "prosemirror-state"; import { EditorView } from "prosemirror-view"; import * as React from "react"; +import { useEffect, useRef, useState } from "react"; import { toast } from "sonner"; import styled from "styled-components"; +import Icon from "@shared/components/Icon"; +import { hideScrollbars, s } from "@shared/styles"; import { isInternalUrl, sanitizeUrl } from "@shared/utils/urls"; import Flex from "~/components/Flex"; +import { ResizingHeightContainer } from "~/components/ResizingHeightContainer"; +import Scrollable from "~/components/Scrollable"; import { Dictionary } from "~/hooks/useDictionary"; -import Logger from "~/utils/Logger"; +import useRequest from "~/hooks/useRequest"; +import useStores from "~/hooks/useStores"; +import { client } from "~/utils/ApiClient"; import Input from "./Input"; +import SuggestionsMenuItem from "./SuggestionsMenuItem"; import ToolbarButton from "./ToolbarButton"; import Tooltip from "./Tooltip"; @@ -32,152 +41,87 @@ type Props = { view: EditorView; }; -type State = { - value: string; - previousValue: string; -}; +const LinkEditor: React.FC = ({ + mark, + from, + to, + dictionary, + onRemoveLink, + onSelectLink, + onClickLink, + view, +}) => { + const getHref = () => sanitizeUrl(mark?.attrs.href) ?? ""; + const initialValue = getHref(); + const initialSelectionLength = to - from; + const inputRef = useRef(null); + const discardRef = useRef(false); + const [query, setQuery] = useState(initialValue); + const [selectedIndex, setSelectedIndex] = useState(-1); + const { documents } = useStores(); -class LinkEditor extends React.Component { - discardInputValue = false; - initialValue = this.href; - initialSelectionLength = this.props.to - this.props.from; - inputRef = React.createRef(); + const trimmedQuery = query.trim(); + const results = trimmedQuery + ? documents.findByQuery(trimmedQuery, { maxResults: 25 }) + : []; - state: State = { - value: this.href, - previousValue: "", - }; + const { request } = useRequest( + React.useCallback(async () => { + const res = await client.post("/suggestions.mention", { query }); + res.data.documents.map(documents.add); + }, [query]) + ); - get href(): string { - return sanitizeUrl(this.props.mark?.attrs.href) ?? ""; - } - - componentDidMount(): void { - window.addEventListener("keydown", this.handleGlobalKeyDown); - } - - componentWillUnmount = () => { - window.removeEventListener("keydown", this.handleGlobalKeyDown); - - // If we discarded the changes then nothing to do - if (this.discardInputValue) { - return; + useEffect(() => { + if (trimmedQuery) { + void request(); } + }, [trimmedQuery, request]); - // If the link is the same as it was when the editor opened, nothing to do - if (this.state.value === this.initialValue) { - return; - } + useEffect(() => { + const handleGlobalKeyDown = (event: KeyboardEvent) => { + if (event.key === "k" && event.metaKey) { + inputRef.current?.select(); + } + }; - // If the link is totally empty or only spaces then remove the mark - const href = (this.state.value || "").trim(); - if (!href) { - return this.handleRemoveLink(); - } + window.addEventListener("keydown", handleGlobalKeyDown); + return () => { + window.removeEventListener("keydown", handleGlobalKeyDown); - this.save(href, href); - }; + // If we discarded the changes then nothing to do + if (discardRef.current) { + return; + } - handleGlobalKeyDown = (event: KeyboardEvent): void => { - if (event.key === "k" && event.metaKey) { - this.inputRef.current?.select(); - } - }; + // If the link is the same as it was when the editor opened, nothing to do + if (trimmedQuery === initialValue) { + return; + } - save = (href: string, title?: string): void => { + // If the link is totally empty or only spaces then remove the mark + if (!trimmedQuery) { + return handleRemoveLink(); + } + + save(trimmedQuery, trimmedQuery); + }; + }, [trimmedQuery, initialValue]); + + const save = (href: string, title?: string) => { href = href.trim(); if (href.length === 0) { return; } - this.discardInputValue = true; - const { from, to } = this.props; + discardRef.current = true; href = sanitizeUrl(href) ?? ""; - this.props.onSelectLink({ href, title, from, to }); + onSelectLink({ href, title, from, to }); }; - handleKeyDown = (event: React.KeyboardEvent): void => { - switch (event.key) { - case "Enter": { - event.preventDefault(); - const { value } = this.state; - - this.save(value, value); - - if (this.initialSelectionLength) { - this.moveSelectionToEnd(); - } - - return; - } - - case "Escape": { - event.preventDefault(); - - if (this.initialValue) { - this.setState({ value: this.initialValue }, this.moveSelectionToEnd); - } else { - this.handleRemoveLink(); - } - return; - } - } - }; - - handleSearch = async ( - event: React.ChangeEvent - ): Promise => { - const value = event.target.value; - - this.setState({ - value, - }); - - const trimmedValue = value.trim(); - - if (trimmedValue) { - try { - this.setState({ - previousValue: trimmedValue, - }); - } catch (err) { - Logger.error("Error searching for link", err); - } - } - }; - - handlePaste = (): void => { - setTimeout(() => this.save(this.state.value, this.state.value), 0); - }; - - handleOpenLink = (event: React.MouseEvent): void => { - event.preventDefault(); - - try { - this.props.onClickLink(this.href, event); - } catch (err) { - toast.error(this.props.dictionary.openLinkError); - } - }; - - handleRemoveLink = (): void => { - this.discardInputValue = true; - - const { from, to, mark, view, onRemoveLink } = this.props; - const { state, dispatch } = this.props.view; - - if (mark) { - dispatch(state.tr.removeMark(from, to, mark)); - } - - onRemoveLink?.(); - view.focus(); - }; - - moveSelectionToEnd = () => { - const { to, view } = this.props; + const moveSelectionToEnd = () => { const { state, dispatch } = view; const nextSelection = Selection.findFrom(state.tr.doc.resolve(to), 1, true); if (nextSelection) { @@ -186,47 +130,178 @@ class LinkEditor extends React.Component { view.focus(); }; - render() { - const { view, dictionary } = this.props; - const { value } = this.state; - const isInternal = isInternalUrl(value); + const handleKeyDown = (event: React.KeyboardEvent) => { + switch (event.key) { + case "ArrowDown": { + event.preventDefault(); + const maxIndex = results.length - 1; + setSelectedIndex((current) => (current >= maxIndex ? 0 : current + 1)); + return; + } + case "ArrowUp": { + event.preventDefault(); + const maxIndex = results.length - 1; + setSelectedIndex((current) => (current <= 0 ? maxIndex : current - 1)); + return; + } + case "Enter": { + event.preventDefault(); - return ( + if (selectedIndex >= 0 && results[selectedIndex]) { + const selectedDoc = results[selectedIndex]; + const href = selectedDoc.url; + save(href, selectedDoc.title); + } else { + save(trimmedQuery, trimmedQuery); + } + + if (initialSelectionLength) { + moveSelectionToEnd(); + } + return; + } + case "Escape": { + event.preventDefault(); + + if (initialValue) { + setQuery(initialValue); + moveSelectionToEnd(); + } else { + handleRemoveLink(); + } + return; + } + } + }; + + const handleSearch = async (event: React.ChangeEvent) => { + const newValue = event.target.value; + setQuery(newValue); + setSelectedIndex(-1); + }; + + const handlePaste = () => { + setTimeout(() => save(query, query), 0); + }; + + const handleOpenLink = (event: React.MouseEvent) => { + event.preventDefault(); + + try { + onClickLink(getHref(), event); + } catch (err) { + toast.error(dictionary.openLinkError); + } + }; + + const handleRemoveLink = () => { + discardRef.current = true; + + const { state, dispatch } = view; + if (mark) { + dispatch(state.tr.removeMark(from, to, mark)); + } + + onRemoveLink?.(); + view.focus(); + }; + + const isInternal = isInternalUrl(query); + const hasResults = !!results.length; + + return ( + <> - - + {isInternal ? : } {view.editable && ( - + )} - ); - } -} + + + {hasResults && ( + <> + {results.map((doc, index) => ( + { + save(doc.url, doc.title); + if (initialSelectionLength) { + moveSelectionToEnd(); + } + }} + onPointerMove={() => setSelectedIndex(index)} + selected={index === selectedIndex} + key={doc.id} + subtitle={doc.collection?.name} + title={doc.title} + icon={ + doc.icon ? ( + + ) : ( + + ) + } + /> + ))} + + )} + + + + ); +}; const Wrapper = styled(Flex)` pointer-events: all; gap: 8px; `; -export default LinkEditor; +const SearchResults = styled(Scrollable)<{ $hasResults: boolean }>` + background: ${s("menuBackground")}; + box-shadow: ${(props) => (props.$hasResults ? s("menuShadow") : "none")}; + clip-path: inset(0px -100px -100px -100px); + position: absolute; + top: 100%; + width: 100%; + height: auto; + left: 0; + margin-top: -6px; + border-radius: 0 0 4px 4px; + padding: ${(props) => (props.$hasResults ? "6px" : "0")}; + max-height: 240px; + pointer-events: all; + + ${hideScrollbars()} + + @media (hover: none) and (pointer: coarse) { + position: fixed; + top: auto; + bottom: 40px; + border-radius: 0; + max-height: 50vh; + padding: 8px 8px 4px; + } +`; + +export default observer(LinkEditor); diff --git a/app/editor/components/MentionMenu.tsx b/app/editor/components/MentionMenu.tsx index 10df7df1a0..661b6418f4 100644 --- a/app/editor/components/MentionMenu.tsx +++ b/app/editor/components/MentionMenu.tsx @@ -10,8 +10,6 @@ import Icon from "@shared/components/Icon"; import { MenuItem } from "@shared/editor/types"; import { MentionType } from "@shared/types"; import parseDocumentSlug from "@shared/utils/parseDocumentSlug"; -import Document from "~/models/Document"; -import User from "~/models/User"; import { Avatar, AvatarSize } from "~/components/Avatar"; import Flex from "~/components/Flex"; import { DocumentsSection, UserSection } from "~/actions/sections"; @@ -48,17 +46,11 @@ function MentionMenu({ search, isActive, ...rest }: Props) { const documentId = parseDocumentSlug(location.pathname); const maxResultsInSection = search ? 25 : 5; - const { loading, request } = useRequest<{ - documents: Document[]; - users: User[]; - }>( + const { loading, request } = useRequest( React.useCallback(async () => { const res = await client.post("/suggestions.mention", { query: search }); - - return { - documents: res.data.documents.map(documents.add), - users: res.data.users.map(users.add), - }; + res.data.documents.map(documents.add); + res.data.users.map(users.add); }, [search, documents, users]) ); diff --git a/app/editor/components/SuggestionsMenuItem.tsx b/app/editor/components/SuggestionsMenuItem.tsx index 0769475e89..0660485f54 100644 --- a/app/editor/components/SuggestionsMenuItem.tsx +++ b/app/editor/components/SuggestionsMenuItem.tsx @@ -11,6 +11,8 @@ export type Props = { disabled?: boolean; /** Callback when the item is clicked */ onClick: (event: React.SyntheticEvent) => void; + /** Callback when the item is hovered */ + onPointerMove?: (event: React.SyntheticEvent) => void; /** An optional icon for the item */ icon?: React.ReactNode; /** The title of the item */ @@ -25,6 +27,7 @@ function SuggestionsMenuItem({ selected, disabled, onClick, + onPointerMove, title, subtitle, shortcut, @@ -53,6 +56,7 @@ function SuggestionsMenuItem({ ref={ref} active={selected} onClick={disabled ? undefined : onClick} + onPointerMove={disabled ? undefined : onPointerMove} icon={icon} > {title}