Skip to content

Commit

Permalink
feat(QuoteBlock): assets do not unregister on switch off (#975)
Browse files Browse the repository at this point in the history
* feat(QuoteBlock): assets do not unregister on switch off

* fix review
  • Loading branch information
fulopdaniel authored Dec 5, 2024
1 parent 538cdba commit 609001d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
6 changes: 2 additions & 4 deletions packages/quote-block/src/CustomIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* (c) Copyright Frontify Ltd., all rights reserved. */

import { useBlockAssets } from '@frontify/app-bridge';
import { type CustomIconProps } from './types';

export const CustomIcon = ({ style, customIconId, appBridge }: CustomIconProps) => {
const { blockAssets } = useBlockAssets(appBridge);
const customIconUrl = blockAssets?.[customIconId]?.[0]?.genericUrl;
export const CustomIcon = ({ style, customIcon }: CustomIconProps) => {
const customIconUrl = customIcon?.genericUrl;

return customIconUrl ? <img style={style} alt="Quote Icon" src={customIconUrl} /> : null;
};
33 changes: 27 additions & 6 deletions packages/quote-block/src/QuoteBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* (c) Copyright Frontify Ltd., all rights reserved. */

import { useBlockSettings, useEditorState } from '@frontify/app-bridge';
import { useBlockAssets, useBlockSettings, useEditorState } from '@frontify/app-bridge';
import {
AutoformatPlugin,
BoldPlugin,
Expand All @@ -21,7 +21,7 @@ import {
convertToRteValue,
toRgbaString,
} from '@frontify/guideline-blocks-settings';
import { FC } from 'react';
import { FC, useEffect } from 'react';
import '@frontify/guideline-blocks-settings/styles';
import '@frontify/fondue/style';
import 'tailwindcss/tailwind.css';
Expand All @@ -45,6 +45,29 @@ export const QuoteBlock: FC<BlockProps> = ({ appBridge }) => {
const [blockSettings, setBlockSettings] = useBlockSettings<Settings>(appBridge);
const isEditing = useEditorState(appBridge);

const { blockAssets, deleteAssetIdsFromKey } = useBlockAssets(appBridge);
const customLeftIcon = blockAssets?.[CUSTOM_QUOTE_STYLE_LEFT_ID]?.[0];
const customRightIcon = blockAssets?.[CUSTOM_QUOTE_STYLE_RIGHT_ID]?.[0];

useEffect(() => {
if (!isEditing) {
return;
}
if (!blockSettings.isCustomQuoteStyleRight && customRightIcon) {
deleteAssetIdsFromKey(CUSTOM_QUOTE_STYLE_RIGHT_ID, [customRightIcon.id]);
}
if (!blockSettings.isCustomQuoteStyleLeft && customLeftIcon) {
deleteAssetIdsFromKey(CUSTOM_QUOTE_STYLE_LEFT_ID, [customLeftIcon.id]);
}
}, [
blockSettings.isCustomQuoteStyleLeft,
blockSettings.isCustomQuoteStyleRight,
customRightIcon,
customLeftIcon,
deleteAssetIdsFromKey,
isEditing,
]);

const isQuotationMarkType = blockSettings.type !== QuoteType.Indentation;
const isFullWidth =
blockSettings.quotationMarksAnchoring !== QuotationMarksAnchoring.HugText && isQuotationMarkType;
Expand Down Expand Up @@ -89,12 +112,11 @@ export const QuoteBlock: FC<BlockProps> = ({ appBridge }) => {
<div className={getWrapperClasses()}>
{isQuotationMarkType && (
<QuoteBlockIcon
appBridge={appBridge}
color={iconColor}
isCustomSize={blockSettings.isCustomSize}
sizeValue={sizeValue}
sizeChoice={blockSettings.sizeChoice}
customIconId={CUSTOM_QUOTE_STYLE_LEFT_ID}
customIcon={customLeftIcon}
quoteStyle={
blockSettings.isCustomQuoteStyleLeft
? QuoteStyle.Custom
Expand Down Expand Up @@ -128,12 +150,11 @@ export const QuoteBlock: FC<BlockProps> = ({ appBridge }) => {
</div>
{isQuotationMarkType && (
<QuoteBlockIcon
appBridge={appBridge}
color={iconColor}
isCustomSize={blockSettings.isCustomSize}
sizeValue={sizeValue}
sizeChoice={blockSettings.sizeChoice}
customIconId={CUSTOM_QUOTE_STYLE_RIGHT_ID}
customIcon={customRightIcon}
quoteStyle={
blockSettings.isCustomQuoteStyleRight
? QuoteStyle.Custom
Expand Down
10 changes: 4 additions & 6 deletions packages/quote-block/src/QuoteBlockIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import 'tailwindcss/tailwind.css';
import '@frontify/guideline-blocks-settings/styles';
import { QuoteSize, type QuoteStyle, quoteSizeMap } from './types';
import { quoteIconMap } from './utilities';
import { type AppBridgeBlock } from '@frontify/app-bridge';
import { Asset } from '@frontify/app-bridge';

export type QuoteBlockIconProps = {
customIconId: string;
appBridge: AppBridgeBlock;
customIcon: Asset | undefined;
quoteStyle: QuoteStyle;
color: string;
isCustomSize?: boolean;
Expand All @@ -18,14 +17,13 @@ export type QuoteBlockIconProps = {
};

export const QuoteBlockIcon: FC<QuoteBlockIconProps> = ({
customIconId,
customIcon,
quoteStyle,
color,
isCustomSize,
sizeValue,
sizeChoice,
appBridge,
}) => {
const size = isCustomSize ? (sizeValue ?? '') : quoteSizeMap[sizeChoice ?? QuoteSize.LargeSize];
return quoteIconMap(size, color, customIconId, appBridge)[quoteStyle];
return quoteIconMap(size, color, customIcon)[quoteStyle];
};
5 changes: 2 additions & 3 deletions packages/quote-block/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* (c) Copyright Frontify Ltd., all rights reserved. */

import { type AppBridgeBlock } from '@frontify/app-bridge';
import { type Asset } from '@frontify/app-bridge';
import { type Color } from '@frontify/fondue';
import { type CSSProperties } from 'react';

Expand Down Expand Up @@ -82,6 +82,5 @@ export enum QuotationMarksAnchoring {

export type CustomIconProps = {
style: CSSProperties;
customIconId: string;
appBridge: AppBridgeBlock;
customIcon: Asset | undefined;
};
7 changes: 3 additions & 4 deletions packages/quote-block/src/utilities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import { IconSingleQuoteDown } from './foundation/IconSingleQuoteDown';
import { IconSingleQuoteUp } from './foundation/IconSingleQuoteUp';
import { type QuoteStyle } from './types';
import { CustomIcon } from './CustomIcon';
import { type AppBridgeBlock } from '@frontify/app-bridge';
import { Asset } from '@frontify/app-bridge';

export const ICON_CLASS_NAME = 'tw-flex tw-items-center tw-justify-center tw-fill-current';

export const quoteIconMap = (
size: string,
color: string,
customIconId: string,
appBridge: AppBridgeBlock
customIcon: Asset | undefined
): Record<QuoteStyle, ReactElement | null> => {
const style: CSSProperties = {
color,
Expand All @@ -43,7 +42,7 @@ export const quoteIconMap = (
HookBracketLeft: <IconHookBracketLeft style={style} />,
HookBracketRight: <IconHookBracketRight style={style} />,
None: null,
Custom: <CustomIcon customIconId={customIconId} style={style} appBridge={appBridge} />,
Custom: <CustomIcon customIcon={customIcon} style={style} />,
};
};

Expand Down

0 comments on commit 609001d

Please sign in to comment.