Skip to content

Commit

Permalink
feat: hyperlink edit popover modal
Browse files Browse the repository at this point in the history
  • Loading branch information
YvesRijckaert committed Nov 13, 2023
1 parent 4eb475c commit 2af942c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as React from 'react';

import { FieldAppSDK, Link } from '@contentful/app-sdk';
import { Tooltip, TextLink } from '@contentful/f36-components';
import { TextLink, Popover, IconButton, Tooltip, Flex, Text } from '@contentful/f36-components';
import { EditIcon, LinkIcon } from '@contentful/f36-icons';
import { Path } from 'slate';

import { useContentfulEditor } from '../../../ContentfulEditorProvider';
import { fromDOMPoint } from '../../../internal';
import { findNodePath } from '../../../internal/queries';
import { Element, RenderElementProps } from '../../../internal/types';
import { useSdkContext } from '../../../SdkProvider';
import { addOrEditLink } from '../HyperlinkModal';
Expand All @@ -30,37 +32,63 @@ type HyperlinkElementProps = {

export function UrlHyperlink(props: HyperlinkElementProps) {
const editor = useContentfulEditor();
const { focus } = editor.selection;
const sdk: FieldAppSDK = useSdkContext();
const { uri } = props.element.data;
const pathToElement = findNodePath(editor, props.element);

function handleClick(event: React.MouseEvent<HTMLAnchorElement>) {
event.preventDefault();
event.stopPropagation();
const isLinkFocused = pathToElement && Path.isChild(focus.path, pathToElement);

function handleClick() {
if (!editor) return;
const p = fromDOMPoint(editor, [event.target as Node, 0], {
exactMatch: false,
suppressThrow: false,
});
addOrEditLink(editor, sdk, editor.tracking.onViewportAction, p?.path);
addOrEditLink(editor, sdk, editor.tracking.onViewportAction);
}

return (
<Tooltip
usePortal
content={uri}
targetWrapperClassName={styles.hyperlinkWrapper}
placement="bottom"
maxWidth="min-content"
>
<TextLink
as="a"
href={uri}
rel="noopener noreferrer"
onClick={handleClick}
className={styles.hyperlink}
>
{props.children}
</TextLink>
</Tooltip>
// eslint-disable-next-line jsx-a11y/no-autofocus -- we don't want to autofocus the popover
<Popover autoFocus={false} isOpen={isLinkFocused}>
<Popover.Trigger>
<TextLink as="a" href={uri} rel="noopener noreferrer" className={styles.hyperlink}>
{props.children}
</TextLink>
</Popover.Trigger>
<Popover.Content className={styles.popover}>
<Flex
alignItems="center"
paddingTop="spacing2Xs"
paddingBottom="spacing2Xs"
paddingRight="spacing2Xs"
paddingLeft="spacingXs"
>
<Text
marginLeft="spacingXs"
marginRight="spacingXs"
fontColor="blue600"
fontWeight="fontWeightMedium"
>
{uri}
</Text>
<Tooltip placement="bottom" content="Edit link" usePortal>
<IconButton
className={styles.iconButton}
onClick={handleClick}
size="small"
variant="transparent"
aria-label="Edit link"
icon={<EditIcon size="tiny" />}
/>
</Tooltip>
<Tooltip placement="bottom" content="Remove link" usePortal>
<IconButton
className={styles.iconButton}
size="small"
variant="transparent"
aria-label="Remove link"
icon={<LinkIcon size="tiny" />}
/>
</Tooltip>
</Flex>
</Popover.Content>
</Popover>
);
}
6 changes: 6 additions & 0 deletions packages/rich-text/src/plugins/Hyperlink/components/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export const styles = {
fontSize: 'inherit !important',
},
}),
iconButton: css({
padding: `${tokens.spacing2Xs} ${tokens.spacingXs}`,
}),
popover: css({
zIndex: tokens.zIndexModal,
}),
hyperlink: css({
fontSize: 'inherit !important',
display: 'inline !important',
Expand Down

0 comments on commit 2af942c

Please sign in to comment.