Files
admin/www/app/composables/useSectionContent.ts
T

52 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-07-31 19:02:00 +08:00
import type { SectionBlock, SectionConfig } from '~/types/site';
2026-08-04 19:18:35 +08:00
import { resolveAssetUrl } from '~/utils/resolveAssetUrl';
2026-07-31 19:02:00 +08:00
/**
* 区块内容多语言解析
*
* Section 的各文本字段均有 _zh / _en 两个版本,
* 该 composable 根据当前语言返回对应的值,避免各布局组件重复判断。
*/
export function useSectionContent(section: SectionBlock) {
const { locale } = useI18n();
const isEn = computed(() => locale.value === 'en');
const overline = computed(() => (isEn.value ? section.overline_en : section.overline_zh) || '');
const title = computed(() => (isEn.value ? section.title_en : section.title_zh) || '');
const subtitle = computed(() => (isEn.value ? section.subtitle_en : section.subtitle_zh) || '');
const content = computed(() => (isEn.value ? section.content_en : section.content_zh) || '');
const ctaPrimaryText = computed(() => (isEn.value ? section.cta_primary_en : section.cta_primary_zh) || '');
const ctaSecondaryText = computed(() => (isEn.value ? section.cta_secondary_en : section.cta_secondary_zh) || '');
2026-08-04 19:18:35 +08:00
// media_url_zh/_en 以语言后缀结尾,不命中数据层 resolveAssetUrls 的 *_url 匹配,需在此单独解析相对路径
const mediaUrl = computed(() =>
resolveAssetUrl((isEn.value ? section.media_url_en : section.media_url_zh) || section.media_url_zh || '')
);
2026-07-31 19:02:00 +08:00
const config = computed<SectionConfig>(() => section.config ?? {});
/** 深色主题区块 */
const isDark = computed(() => section.theme === 'dark');
2026-08-03 19:09:03 +08:00
// 前景文案自定义颜色(空 = 跟随 theme 默认配色,由布局组件的 class 控制)
// 内联 style 优先级高于 class,非空时直接覆盖默认色
const overlineStyle = computed(() => (section.overline_color ? { color: section.overline_color } : undefined));
const titleStyle = computed(() => (section.title_color ? { color: section.title_color } : undefined));
const bodyStyle = computed(() => (section.body_color ? { color: section.body_color } : undefined));
2026-07-31 19:02:00 +08:00
return {
isEn,
overline,
title,
subtitle,
content,
ctaPrimaryText,
ctaSecondaryText,
mediaUrl,
config,
2026-08-03 19:09:03 +08:00
isDark,
overlineStyle,
titleStyle,
bodyStyle
2026-07-31 19:02:00 +08:00
};
}