From 229d9c76c33091b21549df5888986481a18ba4df Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 4 Jan 2025 17:49:38 +0800 Subject: [PATCH] migrate item icon to composition API and typescript --- src/components/base/itemIcon.ts | 134 ++++++++++++++++++++++ src/components/desktop/ItemIcon.vue | 166 +++++----------------------- src/components/mobile/ItemIcon.vue | 146 ++++-------------------- 3 files changed, 185 insertions(+), 261 deletions(-) create mode 100644 src/components/base/itemIcon.ts diff --git a/src/components/base/itemIcon.ts b/src/components/base/itemIcon.ts new file mode 100644 index 00000000..d17437c5 --- /dev/null +++ b/src/components/base/itemIcon.ts @@ -0,0 +1,134 @@ +import { computed } from 'vue'; + +import type { ColorValue } from '@/core/color.ts'; +import { ALL_ACCOUNT_ICONS, DEFAULT_ACCOUNT_ICON, ALL_CATEGORY_ICONS, DEFAULT_CATEGORY_ICON } from '@/consts/icon.ts'; +import { DEFAULT_ICON_COLOR, DEFAULT_ACCOUNT_COLOR, DEFAULT_CATEGORY_COLOR } from '@/consts/color.ts'; +import { isNumber } from '@/lib/common.ts'; + +type IconItemStyleName = string; +type IconItemStyleValue = ColorValue | string | number | undefined; +type CommonIconItemType = 'account' | 'category' | 'fixed'; +type MobileIconItemType = 'fixed-f7'; + +export interface CommonIconProps { + iconType: CommonIconItemType | MobileIconItemType; + iconId: string | number; + color?: ColorValue; + defaultColor?: ColorValue; + additionalColorAttr?: string; + size?: string | number; +} + +export function useItemIcon(props: CommonIconProps) { + const style = computed>(() => { + let defaultColor = 'var(--default-icon-color)'; + + if (props.defaultColor) { + defaultColor = props.defaultColor; + } + + if (props.iconType === 'account') { + return getAccountIconStyle(props.color, defaultColor, props.additionalColorAttr); + } else if (props.iconType === 'category') { + return getCategoryIconStyle(props.color, defaultColor, props.additionalColorAttr); + } else { + return getDefaultIconStyle(props.color, defaultColor, props.additionalColorAttr); + } + }); + + function getAccountIcon(iconId: string | number): string { + if (isNumber(iconId)) { + iconId = iconId.toString(); + } + + if (!ALL_ACCOUNT_ICONS[iconId as string]) { + return DEFAULT_ACCOUNT_ICON.icon; + } + + return ALL_ACCOUNT_ICONS[iconId as string].icon; + } + + function getCategoryIcon(iconId: string | number): string { + if (isNumber(iconId)) { + iconId = iconId.toString(); + } + + if (!ALL_CATEGORY_ICONS[iconId as string]) { + return DEFAULT_CATEGORY_ICON.icon; + } + + return ALL_CATEGORY_ICONS[iconId as string].icon; + } + + function getAccountIconStyle(color?: ColorValue | string, defaultColor?: ColorValue | string, additionalColorAttr?: string): Record { + if (color && color !== DEFAULT_ACCOUNT_COLOR) { + color = '#' + color; + } else { + color = defaultColor; + } + + const ret: Record = { + color: color + }; + + if (additionalColorAttr) { + ret[additionalColorAttr] = color; + } + + if (props.size) { + ret['font-size'] = props.size; + } + + return ret; + } + + function getCategoryIconStyle(color?: ColorValue | string, defaultColor?: ColorValue | string, additionalColorAttr?: string): Record { + if (color && color !== DEFAULT_CATEGORY_COLOR) { + color = '#' + color; + } else { + color = defaultColor; + } + + const ret: Record = { + color: color + }; + + if (additionalColorAttr) { + ret[additionalColorAttr] = color; + } + + if (props.size) { + ret['font-size'] = props.size; + } + + return ret; + } + + function getDefaultIconStyle(color?: ColorValue | string, defaultColor?: ColorValue | string, additionalColorAttr?: string): Record { + if (color && color !== DEFAULT_ICON_COLOR) { + color = '#' + color; + } else { + color = defaultColor; + } + + const ret: Record = { + color: color + }; + + if (additionalColorAttr) { + ret[additionalColorAttr] = color; + } + + if (props.size) { + ret['font-size'] = props.size; + } + + return ret; + } + + return { + style, + getAccountIcon, + getCategoryIcon + } +} diff --git a/src/components/desktop/ItemIcon.vue b/src/components/desktop/ItemIcon.vue index 958636da..ca2ad355 100644 --- a/src/components/desktop/ItemIcon.vue +++ b/src/components/desktop/ItemIcon.vue @@ -11,151 +11,39 @@ -