diff --git a/src/utils/expandMentionStylesForIndicators.ts b/src/utils/expandMentionStylesForIndicators.ts index 5ea356b7f..53583145b 100644 --- a/src/utils/expandMentionStylesForIndicators.ts +++ b/src/utils/expandMentionStylesForIndicators.ts @@ -1,5 +1,6 @@ import type { HtmlStyle, MentionStyleProperties } from '../types'; import { isMentionStyleRecord } from './isMentionStyleRecord'; +import { omitUndefined } from './omitUndefined'; export function expandMentionStylesForIndicators( mention: HtmlStyle['mention'] | undefined, @@ -10,9 +11,11 @@ export function expandMentionStylesForIndicators( for (const indicator of indicators) { out[indicator] = { ...htmlStyleToMergeWith.mention, - ...(isMentionStyleRecord(mention) - ? (mention[indicator] ?? mention.default ?? {}) - : mention), + ...omitUndefined( + isMentionStyleRecord(mention) + ? (mention[indicator] ?? mention.default) + : mention + ), }; } return out; diff --git a/src/utils/normalizeHtmlStyle.ts b/src/utils/normalizeHtmlStyle.ts index 81be317e7..b82dd2d38 100644 --- a/src/utils/normalizeHtmlStyle.ts +++ b/src/utils/normalizeHtmlStyle.ts @@ -11,6 +11,7 @@ import { DEFAULT_ENRICHED_TEXT_STYLE, } from './defaultHtmlStyle'; import { expandMentionStylesForIndicators } from './expandMentionStylesForIndicators'; +import { omitUndefined } from './omitUndefined'; const MENTION_DEFAULT_KEY = '_default'; @@ -74,12 +75,12 @@ const convertToEnrichedTextHtmlStyleInternal = ( if (typeof value === 'object' && value !== null) { mentionStyles[key] = { ...DEFAULT_ENRICHED_TEXT_STYLE.mention, - ...(value as MentionStyleProperties), + ...omitUndefined(value as MentionStyleProperties), }; } else { mentionStyles[MENTION_DEFAULT_KEY] = { ...DEFAULT_ENRICHED_TEXT_STYLE.mention, - ...(mention as MentionStyleProperties), + ...omitUndefined(mention as MentionStyleProperties), }; } } @@ -115,7 +116,7 @@ const assignDefaultValues = >( merged[key] = { ...(base[key] ?? {}), - ...(style[key as keyof typeof style] as object), + ...omitUndefined(style[key as keyof typeof style] as object), }; } diff --git a/src/utils/omitUndefined.ts b/src/utils/omitUndefined.ts new file mode 100644 index 000000000..833753dc7 --- /dev/null +++ b/src/utils/omitUndefined.ts @@ -0,0 +1,18 @@ +/** + * Returns a shallow copy of `obj` without the keys whose value is `undefined`. + */ +export function omitUndefined( + obj: T | null | undefined +): Partial { + const result: Partial = {}; + if (!obj) { + return result; + } + + for (const key of Object.keys(obj) as (keyof T)[]) { + if (obj[key] !== undefined) { + result[key] = obj[key]; + } + } + return result; +}