Skip to content

Commit

Permalink
Add support for "Edit" button to PressContactsExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Jul 18, 2024
1 parent 35da523 commit b868585
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {
normalizeContactNodeAttributes,
parseSerializedElement,
} from './lib';
import type { PressContactsExtensionParameters } from './types';

export const EXTENSION_ID = 'PressContactExtension';

export const PressContactsExtension = (): Extension => ({
export const PressContactsExtension = (params: PressContactsExtensionParameters): Extension => ({
id: EXTENSION_ID,
deserialize: {
element: composeElementDeserializer({
Expand Down Expand Up @@ -43,7 +44,11 @@ export const PressContactsExtension = (): Extension => ({
renderElement: ({ attributes, children, element }) => {
if (isContactNode(element)) {
return (
<PressContactElement attributes={attributes} element={element}>
<PressContactElement
attributes={attributes}
element={element}
onEdit={params.onEdit}
>
{children}
</PressContactElement>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import { Avatar, EditorBlock } from '#components';
import { Envelope, Globe, Mobile, Phone, SocialFacebook, SocialTwitter } from '#icons';

import { getSocialHandles, getUrl, removePressContact, updatePressContact } from '../lib';
import type { PressContactsExtensionParameters } from '../types';

import styles from './PressContactElement.module.scss';
import { PressContactMenu } from './PressContactMenu';

interface Props extends RenderElementProps {
element: ContactNode;
onEdit: PressContactsExtensionParameters['onEdit'];
renderMenu?: (props: { onClose: () => void }) => ReactNode;
}

export function PressContactElement({ attributes, children, element, renderMenu }: Props) {
export function PressContactElement({ attributes, children, element, onEdit, renderMenu }: Props) {
const editor = useSlateStatic();
const { layout, show_avatar: showAvatar } = element;
const isCardLayout = layout === ContactLayout.CARD;
Expand Down Expand Up @@ -51,6 +53,7 @@ export function PressContactElement({ attributes, children, element, renderMenu
return (
<PressContactMenu
element={element}
onEdit={onEdit}
onChangeLayout={handleChangeLayout}
onToggleAvatar={handleToggleAvatar}
onRemove={handleRemove}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import React from 'react';

import type { OptionsGroupOption } from '#components';
import { Button, OptionsGroup, Toggle, Toolbox } from '#components';
import { ContactLayoutCard, ContactLayoutSignature, Delete } from '#icons';
import { ContactLayoutCard, ContactLayoutSignature, Delete, ExternalLink } from '#icons';

import type { PressContactsExtensionParameters } from '../types';

interface Props {
element: ContactNode;
onChangeLayout: (layout: ContactLayout) => void;
onEdit: PressContactsExtensionParameters['onEdit'];
onRemove: () => void;
onToggleAvatar: (visible: boolean) => void;
}
Expand All @@ -26,7 +29,19 @@ const LAYOUT_OPTIONS: OptionsGroupOption<ContactLayout>[] = [
},
];

export function PressContactMenu({ element, onChangeLayout, onRemove, onToggleAvatar }: Props) {
export function PressContactMenu({
element,
onChangeLayout,
onEdit,
onRemove,
onToggleAvatar,
}: Props) {
function handleEdit() {
if (element.reference) {
onEdit?.(element.reference);
}
}

return (
<>
<Toolbox.Header>Site contact settings</Toolbox.Header>
Expand All @@ -52,6 +67,20 @@ export function PressContactMenu({ element, onChangeLayout, onRemove, onToggleAv
/>
</Toolbox.Section>

{element.reference && onEdit && (
<Toolbox.Section noPadding>
<Button
icon={ExternalLink}
iconPosition="right"
fullWidth
onClick={handleEdit}
variant="clear"
>
Edit contact
</Button>
</Toolbox.Section>
)}

<Toolbox.Footer>
<RemoveButton onClick={onRemove} />
</Toolbox.Footer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export {
parseSerializedElement,
removePressContact,
} from './lib';
export type { PressContactsExtensionParameters } from './types';
5 changes: 5 additions & 0 deletions packages/slate-editor/src/extensions/press-contacts/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { NewsroomContact } from '@prezly/sdk';

export interface PressContactsExtensionParameters {
onEdit?: (uuid: NewsroomContact['uuid']) => void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export function* getEnabledExtensions(parameters: Parameters): Generator<Extensi
}

if (withPressContacts) {
yield PressContactsExtension();
yield PressContactsExtension(withPressContacts);
}

if (withVariables) {
Expand Down
1 change: 1 addition & 0 deletions packages/slate-editor/src/modules/editor/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function getAllExtensions() {
withPlaceholders: {},
withPressContacts: {
getSuggestions: () => [],
onEdit: () => {},
renderEmpty: () => null,
renderSuggestion: () => null,
renderSuggestionsFooter: () => null,
Expand Down
6 changes: 5 additions & 1 deletion packages/slate-editor/src/modules/editor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
PlaceholderNode,
PlaceholdersExtensionParameters,
} from '#extensions/placeholders';
import type { PressContactsExtensionParameters } from '#extensions/press-contacts';
import type { SnippetsExtensionParameters } from '#extensions/snippet';
import type { StoryBookmarkExtensionParameters } from '#extensions/story-bookmark';
import type { StoryEmbedExtensionParameters } from '#extensions/story-embed';
Expand Down Expand Up @@ -129,7 +130,10 @@ export interface EditorProps {
PlaceholdersExtensionParameters,
'format' | 'removable' | 'withMediaPlaceholders' | 'withPastedUrlsUnfurling'
>;
withPressContacts?: false | PlaceholdersExtensionParameters['withContactPlaceholders'];
withPressContacts?:
| false
| (PressContactsExtensionParameters &
PlaceholdersExtensionParameters['withContactPlaceholders']);
withRichFormattingMenu?:
| boolean
| {
Expand Down

0 comments on commit b868585

Please sign in to comment.