mirror of
https://github.com/outline/outline.git
synced 2026-11-30 17:10:36 +03:00
simplify mention code creation
This commit is contained in:
1 parent
9264d5ab00
commit
855e7fd30d
3 files changed
+18
-22
No files matched your search
@@ -2,9 +2,11 @@ import { observer } from "mobx-react";
|
||||
import { EmailIcon, LinkIcon } from "outline-icons";
|
||||
import React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { v4 } from "uuid";
|
||||
import { EmbedDescriptor } from "@shared/editor/embeds";
|
||||
import { MenuItem } from "@shared/editor/types";
|
||||
import Integration from "~/models/Integration";
|
||||
import useCurrentUser from "~/hooks/useCurrentUser";
|
||||
import useStores from "~/hooks/useStores";
|
||||
import SuggestionsMenu, {
|
||||
Props as SuggestionsMenuProps,
|
||||
@@ -22,6 +24,7 @@ type Props = Omit<
|
||||
export const PasteMenu = observer(({ pastedText, embeds, ...props }: Props) => {
|
||||
const { t } = useTranslation();
|
||||
const { integrations } = useStores();
|
||||
const user = useCurrentUser();
|
||||
|
||||
const url = pastedText ? new URL(pastedText) : undefined;
|
||||
|
||||
@@ -61,13 +64,20 @@ export const PasteMenu = observer(({ pastedText, embeds, ...props }: Props) => {
|
||||
name: "mention",
|
||||
title: t("Mention"),
|
||||
icon: <EmailIcon />,
|
||||
attrs: { type: mentionType },
|
||||
attrs: {
|
||||
id: v4(),
|
||||
type: mentionType,
|
||||
label: pastedText,
|
||||
href: pastedText,
|
||||
modelId: v4(),
|
||||
actorId: user.id,
|
||||
},
|
||||
appendSpace: true,
|
||||
});
|
||||
}
|
||||
|
||||
return menuItems;
|
||||
}, [embed, t, mentionType]);
|
||||
}, [t, embed, mentionType, pastedText, user]);
|
||||
|
||||
return (
|
||||
<SuggestionsMenu
|
||||
@@ -76,9 +86,7 @@ export const PasteMenu = observer(({ pastedText, embeds, ...props }: Props) => {
|
||||
filterable={false}
|
||||
renderMenuItem={(item, _index, options) => (
|
||||
<SuggestionsMenuItem
|
||||
onClick={() => {
|
||||
props.onSelect?.(item);
|
||||
}}
|
||||
onClick={options.onClick}
|
||||
selected={options.selected}
|
||||
title={item.title}
|
||||
icon={item.icon}
|
||||
|
||||
@@ -12,7 +12,6 @@ import filterExcessSeparators from "@shared/editor/lib/filterExcessSeparators";
|
||||
import { findParentNode } from "@shared/editor/queries/findParentNode";
|
||||
import { MenuItem } from "@shared/editor/types";
|
||||
import { depths, s } from "@shared/styles";
|
||||
import { MentionType } from "@shared/types";
|
||||
import { getEventFiles } from "@shared/utils/files";
|
||||
import { AttachmentValidation } from "@shared/validations";
|
||||
import Header from "~/components/ContextMenu/Header";
|
||||
@@ -234,11 +233,7 @@ function SuggestionsMenu<T extends MenuItem>(props: Props<T>) {
|
||||
const attrs =
|
||||
typeof item.attrs === "function" ? item.attrs(view.state) : item.attrs;
|
||||
|
||||
const skipMention =
|
||||
attrs?.type === MentionType.Issue ||
|
||||
attrs?.type === MentionType.PullRequest; // Mention node creation will be handled by PasteHandler.
|
||||
|
||||
if (item.name === "noop" || (item.name === "mention" && skipMention)) {
|
||||
if (item.name === "noop") {
|
||||
// Do nothing
|
||||
} else if (command) {
|
||||
command(attrs);
|
||||
|
||||
@@ -415,26 +415,19 @@ export default class PasteHandler extends Extension {
|
||||
});
|
||||
};
|
||||
|
||||
private insertMention = (attrs: Record<string, unknown>) => {
|
||||
private insertMention = () => {
|
||||
const { view } = this.editor;
|
||||
const { state } = view;
|
||||
const result = this.findPlaceholder(state, this.state.pastedText);
|
||||
|
||||
// Remove just the placeholder here.
|
||||
// Mention node will be created by SuggestionsMenu.
|
||||
if (result) {
|
||||
const tr = state.tr.deleteRange(result[0], result[1]);
|
||||
view.dispatch(
|
||||
tr.setSelection(TextSelection.near(tr.doc.resolve(result[0])))
|
||||
);
|
||||
}
|
||||
|
||||
this.editor.commands.mention({
|
||||
id: v4(),
|
||||
type: attrs.type as MentionType,
|
||||
label: this.state.pastedText,
|
||||
href: this.state.pastedText,
|
||||
modelId: v4(),
|
||||
actorId: stores.auth.currentUserId,
|
||||
});
|
||||
};
|
||||
|
||||
private removePlaceholder = () => {
|
||||
@@ -474,7 +467,7 @@ export default class PasteHandler extends Extension {
|
||||
}
|
||||
case "mention": {
|
||||
this.hidePasteMenu();
|
||||
this.insertMention(item.attrs as Record<string, unknown>);
|
||||
this.insertMention();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
Reference in new issue
Block a user