Skip to content

Commit

Permalink
Add override parameter to all block factories in @comet/cms-admin (
Browse files Browse the repository at this point in the history
…#3280)

Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
  • Loading branch information
Reaw and johnnyomair authored Feb 5, 2025
1 parent 711958c commit c71604e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-bees-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cms-admin": minor
---

Add an `override` argument to all block factories to follow `createCompositeBlock`'s pattern
36 changes: 21 additions & 15 deletions packages/admin/cms-admin/src/blocks/createImageLinkBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@ interface CreateImageLinkBlockOptions {
name?: string;
}

export function createImageLinkBlock({ link: LinkBlock, image = PixelImageBlock, name = "ImageLink" }: CreateImageLinkBlockOptions): BlockInterface {
return createCompositeBlock({
name,
displayName: <FormattedMessage id="comet.blocks.imageLink" defaultMessage="Image/Link" />,
blocks: {
link: {
block: LinkBlock,
title: <FormattedMessage id="comet.blocks.imageLink.link" defaultMessage="Link" />,
paper: true,
},
image: {
block: image,
title: <FormattedMessage id="comet.blocks.imageLink.image" defaultMessage="Image" />,
paper: true,
export function createImageLinkBlock(
{ link: LinkBlock, image = PixelImageBlock, name = "ImageLink" }: CreateImageLinkBlockOptions,
override?: (block: BlockInterface) => BlockInterface,
): BlockInterface {
return createCompositeBlock(
{
name,
displayName: <FormattedMessage id="comet.blocks.imageLink" defaultMessage="Image/Link" />,
blocks: {
link: {
block: LinkBlock,
title: <FormattedMessage id="comet.blocks.imageLink.link" defaultMessage="Link" />,
paper: true,
},
image: {
block: image,
title: <FormattedMessage id="comet.blocks.imageLink.image" defaultMessage="Image" />,
paper: true,
},
},
},
});
override,
);
}
34 changes: 20 additions & 14 deletions packages/admin/cms-admin/src/blocks/createLinkBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@ interface CreateLinkBlockOptions extends Omit<CreateOneOfBlockOptions<boolean>,
supportedBlocks?: Record<string, BlockInterface & LinkBlockInterface>;
}

function createLinkBlock({
name = "Link",
displayName = <FormattedMessage id="comet.blocks.link" defaultMessage="Link" />,
supportedBlocks = { internal: InternalLinkBlock, external: ExternalLinkBlock },
allowEmpty = false,
...oneOfBlockOptions
}: CreateLinkBlockOptions): BlockInterface & LinkBlockInterface {
const OneOfBlock = createOneOfBlock({
name,
displayName,
supportedBlocks,
allowEmpty,
...oneOfBlockOptions,
});
function createLinkBlock(
{
name = "Link",
displayName = <FormattedMessage id="comet.blocks.link" defaultMessage="Link" />,
supportedBlocks = { internal: InternalLinkBlock, external: ExternalLinkBlock },
allowEmpty = false,
...oneOfBlockOptions
}: CreateLinkBlockOptions,
override?: (block: BlockInterface & LinkBlockInterface) => BlockInterface & LinkBlockInterface,
): BlockInterface & LinkBlockInterface {
const OneOfBlock = createOneOfBlock(
{
name,
displayName,
supportedBlocks,
allowEmpty,
...oneOfBlockOptions,
},
override,
);

return {
...OneOfBlock,
Expand Down
8 changes: 8 additions & 0 deletions packages/admin/cms-admin/src/blocks/createRichTextBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export type RichTextBlock = BlockInterface<RichTextBlockData, RichTextBlockState

export const createRichTextBlock = (
options: RichTextBlockFactoryOptions,
override?: (
block: BlockInterface<RichTextBlockData, RichTextBlockState, RichTextBlockInput>,
) => BlockInterface<RichTextBlockData, RichTextBlockState, RichTextBlockInput>,
): BlockInterface<RichTextBlockData, RichTextBlockState, RichTextBlockInput> => {
const CmsLinkToolbarButton = createCmsLinkToolbarButton({ link: options.link });
const defaultRteOptions: IRteOptions = {
Expand Down Expand Up @@ -254,5 +257,10 @@ export const createRichTextBlock = (
return content.hasText() ? [{ type: "text", content: content.getPlainText().slice(0, MAX_CHARS) }] : [];
},
};

if (override) {
return override(RichTextBlock);
}

return RichTextBlock;
};
9 changes: 8 additions & 1 deletion packages/admin/cms-admin/src/blocks/createSeoBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ interface CreateSeoBlockOptions {
image?: BlockInterface;
}

export function createSeoBlock({ image = PixelImageBlock }: CreateSeoBlockOptions = {}): BlockInterface {
export function createSeoBlock(
{ image = PixelImageBlock }: CreateSeoBlockOptions = {},
override?: (block: BlockInterface) => BlockInterface,
): BlockInterface {
const OptionalImageBlock = createOptionalBlock(image, {
title: <FormattedMessage id="comet.sitemap.openGraphImage" defaultMessage="Open Graph Image" />,
});
Expand Down Expand Up @@ -328,6 +331,10 @@ export function createSeoBlock({ image = PixelImageBlock }: CreateSeoBlockOption
},
};

if (override) {
return override(SeoBlock);
}

return SeoBlock;
}

Expand Down
15 changes: 11 additions & 4 deletions packages/admin/cms-admin/src/blocks/createTextImageBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ export interface TextImageBlockFactoryOptions {
image?: BlockInterface;
}

const createTextImageBlock = ({
text,
image = PixelImageBlock,
}: TextImageBlockFactoryOptions): BlockInterface<TextImageBlockData, State, TextImageBlockInput> => {
const createTextImageBlock = (
{ text, image = PixelImageBlock }: TextImageBlockFactoryOptions,
override?: (
block: BlockInterface<TextImageBlockData, State, TextImageBlockInput>,
) => BlockInterface<TextImageBlockData, State, TextImageBlockInput>,
): BlockInterface<TextImageBlockData, State, TextImageBlockInput> => {
const composed = composeBlocks({ text, image });

const { api: composedApi, block: composedBlock } = composed;
Expand Down Expand Up @@ -106,6 +108,11 @@ const createTextImageBlock = ({
);
},
};

if (override) {
return override(TextImageBlock);
}

return TextImageBlock;
};

Expand Down
13 changes: 8 additions & 5 deletions packages/admin/cms-admin/src/blocks/createTextLinkBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ interface CreateTextLinkBlockOptions {
link: BlockInterface;
}

export function createTextLinkBlock({
link: LinkBlock,
name = "TextLink",
displayName = <FormattedMessage {...messages.link} />,
}: CreateTextLinkBlockOptions): BlockInterface {
export function createTextLinkBlock(
{ link: LinkBlock, name = "TextLink", displayName = <FormattedMessage {...messages.link} /> }: CreateTextLinkBlockOptions,
override?: (block: BlockInterface) => BlockInterface,
): BlockInterface {
const { api: composedApi, block: composedBlock } = composeBlocks({ link: LinkBlock });

const block = withAdditionalBlockAttributes<Pick<TextLinkBlockData, "text">>({
Expand Down Expand Up @@ -70,5 +69,9 @@ export function createTextLinkBlock({
dynamicDisplayName: (state) => LinkBlock.dynamicDisplayName?.(state.link),
};

if (override) {
return override(TextLinkBlock);
}

return TextLinkBlock;
}

0 comments on commit c71604e

Please sign in to comment.