From 28d5d0da5d2d497028ebcfeedddb803855945fc3 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 16 Jan 2025 23:18:03 -0500 Subject: [PATCH] Add supported embed detection --- app/components/ContextMenu/MenuItem.tsx | 2 +- app/editor/components/PasteMenu.tsx | 30 ++++++++++++++++--- app/editor/components/SuggestionsMenuItem.tsx | 2 +- app/editor/extensions/PasteHandler.tsx | 14 +++++---- app/types.ts | 12 ++++---- shared/editor/embeds/index.tsx | 4 +-- shared/editor/types/index.ts | 2 +- shared/i18n/locales/en_US/translation.json | 3 +- 8 files changed, 47 insertions(+), 22 deletions(-) diff --git a/app/components/ContextMenu/MenuItem.tsx b/app/components/ContextMenu/MenuItem.tsx index 96d8c964c1..1febb9b7ee 100644 --- a/app/components/ContextMenu/MenuItem.tsx +++ b/app/components/ContextMenu/MenuItem.tsx @@ -23,7 +23,7 @@ type Props = { as?: string | React.ComponentType; hide?: () => void; level?: number; - icon?: React.ReactElement; + icon?: React.ReactNode; children?: React.ReactNode; ref?: React.LegacyRef | undefined; }; diff --git a/app/editor/components/PasteMenu.tsx b/app/editor/components/PasteMenu.tsx index 1159c97706..3d12b40969 100644 --- a/app/editor/components/PasteMenu.tsx +++ b/app/editor/components/PasteMenu.tsx @@ -1,27 +1,48 @@ +import { LinkIcon } from "outline-icons"; import React from "react"; import { useTranslation } from "react-i18next"; +import { EmbedDescriptor } from "@shared/editor/embeds"; import SuggestionsMenu, { Props as SuggestionsMenuProps, } from "./SuggestionsMenu"; import SuggestionsMenuItem from "./SuggestionsMenuItem"; -type Props = Omit; +type Props = Omit< + SuggestionsMenuProps, + "renderMenuItem" | "items" | "embeds" +> & { + pastedText: string; + embeds: EmbedDescriptor[]; +}; -const PasteMenu = (props: Props) => { +const PasteMenu = ({ embeds, ...props }: Props) => { const { t } = useTranslation(); + const embed = React.useMemo(() => { + for (const e of embeds) { + const matches = e.matcher(props.pastedText); + if (matches) { + return e; + } + } + return; + }, [embeds, props.pastedText]); + const items = React.useMemo( () => [ { name: "link", - title: t("URL"), + title: t("Keep as link"), + icon: , }, { name: "embed", title: t("Embed"), + icon: embed?.icon, + keywords: embed?.keywords, }, ], - [t] + [embed, t] ); return ( @@ -35,6 +56,7 @@ const PasteMenu = (props: Props) => { }} selected={options.selected} title={item.title} + icon={item.icon} /> )} items={items} diff --git a/app/editor/components/SuggestionsMenuItem.tsx b/app/editor/components/SuggestionsMenuItem.tsx index a64d4a8df6..dcf99226f2 100644 --- a/app/editor/components/SuggestionsMenuItem.tsx +++ b/app/editor/components/SuggestionsMenuItem.tsx @@ -12,7 +12,7 @@ export type Props = { /** Callback when the item is clicked */ onClick: (event: React.SyntheticEvent) => void; /** An optional icon for the item */ - icon?: React.ReactElement; + icon?: React.ReactNode; /** The title of the item */ title: React.ReactNode; /** An optional subtitle for the item */ diff --git a/app/editor/extensions/PasteHandler.tsx b/app/editor/extensions/PasteHandler.tsx index 22fdeaa3ae..367d584891 100644 --- a/app/editor/extensions/PasteHandler.tsx +++ b/app/editor/extensions/PasteHandler.tsx @@ -65,11 +65,11 @@ export default class PasteHandler extends Extension { state: { open: boolean; query: string; - pasteData: Record; + pastedText: string; } = observable({ open: false, query: "", - pasteData: {}, + pastedText: "", }); get name() { @@ -288,14 +288,14 @@ export default class PasteHandler extends Extension { private showPasteMenu(text: string) { action(() => { - this.state.pasteData.text = text; + this.state.pastedText = text; this.state.open = true; })(); } private hidePasteMenu() { action(() => { - this.state.pasteData.text = ""; + this.state.pastedText = ""; this.state.open = false; })(); } @@ -344,6 +344,8 @@ export default class PasteHandler extends Extension { { @@ -356,10 +358,10 @@ export default class PasteHandler extends Extension { break; case "embed": - this.replaceOldLink(this.state.pasteData.text); + this.replaceOldLink(this.state.pastedText); this.editor.commands.embed({ - href: this.state.pasteData.text, + href: this.state.pastedText, }); this.hidePasteMenu(); diff --git a/app/types.ts b/app/types.ts index 4a2e1abca5..8431e63ad4 100644 --- a/app/types.ts +++ b/app/types.ts @@ -26,7 +26,7 @@ export type MenuItemButton = { visible?: boolean; selected?: boolean; disabled?: boolean; - icon?: React.ReactElement; + icon?: React.ReactNode; }; export type MenuItemWithChildren = { @@ -38,7 +38,7 @@ export type MenuItemWithChildren = { hover?: boolean; items: MenuItem[]; - icon?: React.ReactElement; + icon?: React.ReactNode; }; export type MenuSeparator = { @@ -59,7 +59,7 @@ export type MenuInternalLink = { visible?: boolean; selected?: boolean; disabled?: boolean; - icon?: React.ReactElement; + icon?: React.ReactNode; }; export type MenuExternalLink = { @@ -70,7 +70,7 @@ export type MenuExternalLink = { selected?: boolean; disabled?: boolean; level?: number; - icon?: React.ReactElement; + icon?: React.ReactNode; }; export type MenuItem = @@ -108,7 +108,7 @@ export type Action = { /** Higher number is higher in results, default is 0. */ priority?: number; iconInContextMenu?: boolean; - icon?: React.ReactElement | React.FC; + icon?: React.ReactNode; placeholder?: ((context: ActionContext) => string) | string; selected?: (context: ActionContext) => boolean; visible?: (context: ActionContext) => boolean; @@ -127,7 +127,7 @@ export type CommandBarAction = { shortcut: string[]; keywords: string; placeholder?: string; - icon?: React.ReactElement; + icon?: React.ReactNode; perform?: () => void; children?: string[]; parent?: string; diff --git a/shared/editor/embeds/index.tsx b/shared/editor/embeds/index.tsx index e20ddbcccb..a2cd5ce663 100644 --- a/shared/editor/embeds/index.tsx +++ b/shared/editor/embeds/index.tsx @@ -31,9 +31,9 @@ export type EmbedProps = { }; const Img = styled(Image)` - border-radius: 2px; + border-radius: 3px; background: #fff; - box-shadow: 0 0 0 1px #fff; + box-shadow: 0 0 0 1px ${(props) => props.theme.divider}; margin: 3px; width: 18px; height: 18px; diff --git a/shared/editor/types/index.ts b/shared/editor/types/index.ts index 17b3623524..1f8af8bc63 100644 --- a/shared/editor/types/index.ts +++ b/shared/editor/types/index.ts @@ -16,7 +16,7 @@ export enum TableLayout { } export type MenuItem = { - icon?: React.ReactElement; + icon?: React.ReactNode; name?: string; title?: string; subtitle?: string; diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index cdf82fad57..32bc3a2605 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -418,7 +418,7 @@ "Replace all": "Replace all", "{{ userName }} won't be notified, as they do not have access to this document": "{{ userName }} won't be notified, as they do not have access to this document", "Profile picture": "Profile picture", - "URL": "URL", + "Keep as link": "Keep as link", "Embed": "Embed", "Add column after": "Add column after", "Add column before": "Add column before", @@ -1108,6 +1108,7 @@ "Updating": "Updating", "Provide a descriptive name for this webhook and the URL we should send a POST request to when matching events are created.": "Provide a descriptive name for this webhook and the URL we should send a POST request to when matching events are created.", "A memorable identifer": "A memorable identifer", + "URL": "URL", "Signing secret": "Signing secret", "Subscribe to all events, groups, or individual events. We recommend only subscribing to the minimum amount of events that your application needs to function.": "Subscribe to all events, groups, or individual events. We recommend only subscribing to the minimum amount of events that your application needs to function.", "All events": "All events",