Add groups to mention menu

This commit is contained in:
Tom Moor committed 2025-01-09 22:34:36 -05:00
1 parent a2a84102a6
commit 22ff9d394b
4 files changed
+45 -19

No files matched your search

+5
View File
@@ -13,6 +13,7 @@ import Document from "~/models/Document";
import User from "~/models/User";
import { Avatar, AvatarSize } from "~/components/Avatar";
import Flex from "~/components/Flex";
import { DocumentSection, UserSection } from "~/actions/sections";
import useRequest from "~/hooks/useRequest";
import useStores from "~/hooks/useStores";
import { client } from "~/utils/ApiClient";
@@ -84,6 +85,7 @@ function MentionMenu({ search, isActive, ...rest }: Props) {
</Flex>
),
title: user.name,
section: UserSection,
appendSpace: true,
attrs: {
id: v4(),
@@ -102,6 +104,8 @@ function MentionMenu({ search, isActive, ...rest }: Props) {
<DocumentIcon />
),
title: doc.title,
subtitle: doc.collection?.name,
section: DocumentSection,
appendSpace: true,
attrs: {
id: v4(),
@@ -166,6 +170,7 @@ function MentionMenu({ search, isActive, ...rest }: Props) {
<SuggestionsMenuItem
onClick={options.onClick}
selected={options.selected}
subtitle={item.subtitle}
title={item.title}
icon={item.icon}
/>
+35 -18
View File
@@ -1,7 +1,8 @@
import commandScore from "command-score";
import capitalize from "lodash/capitalize";
import orderBy from "lodash/orderBy";
import * as React from "react";
import { Trans } from "react-i18next";
import { Trans, useTranslation } from "react-i18next";
import { VisuallyHidden } from "reakit/VisuallyHidden";
import { toast } from "sonner";
import styled from "styled-components";
@@ -13,6 +14,7 @@ import { MenuItem } from "@shared/editor/types";
import { depths, s } from "@shared/styles";
import { getEventFiles } from "@shared/utils/files";
import { AttachmentValidation } from "@shared/validations";
import Header from "~/components/ContextMenu/Header";
import { Portal } from "~/components/Portal";
import Scrollable from "~/components/Scrollable";
import useDictionary from "~/hooks/useDictionary";
@@ -80,6 +82,7 @@ export type Props<T extends MenuItem = MenuItem> = {
function SuggestionsMenu<T extends MenuItem>(props: Props<T>) {
const { view, commands } = useEditor();
const dictionary = useDictionary();
const { t } = useTranslation();
const hasActivated = React.useRef(false);
const pointerRef = React.useRef<{ clientX: number; clientY: number }>({
clientX: 0,
@@ -445,18 +448,20 @@ function SuggestionsMenu<T extends MenuItem>(props: Props<T>) {
});
return filterExcessSeparators(
filtered
.map((item) => ({
orderBy(
filtered.map((item) => ({
item,
section: "section" in item ? item.section?.({ t }) : "default",
score:
searchInput && item.title
? commandScore(item.title, searchInput)
: 0,
}))
.sort((a, b) => b.score - a.score)
.map(({ item }) => item)
})),
["section", "score"],
["asc", "desc"]
).map(({ item }) => item)
);
}, [commands, props]);
}, [t, commands, props]);
React.useEffect(() => {
const handleMouseDown = (event: MouseEvent) => {
@@ -555,6 +560,7 @@ function SuggestionsMenu<T extends MenuItem>(props: Props<T>) {
const { isActive, uploadFile } = props;
const items = filtered;
let previousHeading: string | undefined;
return (
<Portal>
@@ -614,18 +620,29 @@ function SuggestionsMenu<T extends MenuItem>(props: Props<T>) {
}
};
return (
<ListItem
key={index}
onPointerMove={handlePointerMove}
onPointerDown={handlePointerDown}
>
{props.renderMenuItem(item as any, index, {
selected: index === selectedIndex,
onClick: () => handleClickItem(item),
})}
</ListItem>
const currentHeading =
"section" in item ? item.section?.({ t }) : undefined;
const response = (
<>
{currentHeading !== previousHeading && (
<Header key={currentHeading}>{currentHeading}</Header>
)}
<ListItem
key={index}
onPointerMove={handlePointerMove}
onPointerDown={handlePointerDown}
>
{props.renderMenuItem(item as any, index, {
selected: index === selectedIndex,
onClick: () => handleClickItem(item),
})}
</ListItem>
</>
);
previousHeading = currentHeading;
return response;
})}
{items.length === 0 && (
<ListItem>
@@ -56,7 +56,7 @@ function SuggestionsMenuItem({
icon={icon}
>
{title}
{subtitle && <Subtitle $active={selected}>{subtitle}</Subtitle>}
{subtitle && <Subtitle $active={selected}>&middot; {subtitle}</Subtitle>}
{shortcut && <Shortcut $active={selected}>{shortcut}</Shortcut>}
</MenuItem>
);
+4
View File
@@ -1,3 +1,4 @@
import { TFunction } from "i18next";
import { Node as ProsemirrorNode } from "prosemirror-model";
import { EditorState } from "prosemirror-state";
import { EditorView } from "prosemirror-view";
@@ -15,10 +16,13 @@ export enum TableLayout {
fullWidth = "full-width",
}
type Section = ({ t }: { t: TFunction }) => string;
export type MenuItem = {
icon?: React.ReactElement;
name?: string;
title?: string;
section?: Section;
subtitle?: string;
shortcut?: string;
keywords?: string;