Files
monkey-ai-web/src/pages/ExperienceCenter/components/AICreation/Image/ImageSizePicker.jsx
T
zhanjunhao cb9d836227 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

156 lines
4.8 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React, { useEffect, useMemo, useState } from 'react';
import { InputNumber } from '@/components/ui';
import { SlidersHorizontal } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import {
OptionGroup,
RatioShape,
} from '../../../../../components/creation/params';
import {
clampSizeSide,
customSizeRangeOf,
parseSize,
sizePresetsOf,
} from '../../../../../hooks/image/imageCreation';
const CUSTOM = '__custom__';
/**
* 图像尺寸(schema type = 'size'):预设按宽高比图标展示;仅当 schema 下发 custom_size 时
* 提供"自定义",宽高输入框失焦 / 回车时按 min / max / step 吸附。
*
* props: { param, value: "WxH", onChange(value) }
*/
const ImageSizePicker = ({ param, value, onChange }) => {
const { t } = useTranslation();
const presets = useMemo(() => sizePresetsOf(param), [param]);
const range = useMemo(() => customSizeRangeOf(param), [param]);
const matched = presets.find(
(p) => p.value.toLowerCase() === String(value || '').toLowerCase(),
);
// 用户点了"自定义"、或当前取值不是预设时进入自定义态
const [customActive, setCustomActive] = useState(Boolean(range && !matched));
useEffect(() => {
if (!range) setCustomActive(false);
else if (value && !matched) setCustomActive(true);
}, [range, value, matched]);
const parsed = parseSize(String(value || '')) ||
(presets[0] && { width: presets[0].width, height: presets[0].height }) || {
width: 1024,
height: 1024,
};
const [draft, setDraft] = useState(parsed);
useEffect(() => {
setDraft(parsed);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
const options = useMemo(() => {
const list = presets.map((p) => p.value);
if (range) list.push(CUSTOM);
return list;
}, [presets, range]);
const commit = (next) => {
const w = clampSizeSide(next.width, range);
const h = clampSizeSide(next.height, range);
setDraft({ width: w, height: h });
const v = `${w}x${h}`;
if (v !== value) onChange && onChange(v);
};
const onSelect = (v) => {
if (v === CUSTOM) {
setCustomActive(true);
commit(draft);
return;
}
setCustomActive(false);
onChange && onChange(v);
};
const selected = customActive ? CUSTOM : matched ? matched.value : undefined;
return (
<div className='igf-size'>
<OptionGroup
variant='ratio'
options={options}
value={selected}
onChange={onSelect}
ariaLabel={t('尺寸')}
getOptionAriaLabel={(opt) => {
if (opt === CUSTOM) return t('自定义尺寸');
const p = presets.find((x) => x.value === opt);
return p ? `${p.label || ''} ${p.width}×${p.height}`.trim() : opt;
}}
renderOption={(opt) => {
if (opt === CUSTOM) {
return (
<>
<span className='cp-ratio-shape-box' aria-hidden='true'>
<SlidersHorizontal size={14} />
</span>
<span className='cp-option-label'>{t('自定义')}</span>
</>
);
}
const p = presets.find((x) => x.value === opt);
return (
<>
<RatioShape ratio={`${p.width}:${p.height}`} />
<span className='cp-option-label tabular-nums'>
{p.label || `${p.width}×${p.height}`}
</span>
{p.label ? (
<span className='igf-size-dims tabular-nums'>
{p.width}×{p.height}
</span>
) : null}
</>
);
}}
/>
{range && customActive ? (
<div className='igf-size-custom'>
<InputNumber
value={draft.width}
onChange={(v) => setDraft((d) => ({ ...d, width: v }))}
onBlur={() => commit(draft)}
onPressEnter={() => commit(draft)}
min={range.min}
max={range.max}
step={range.step}
prefix={t('宽')}
suffix='px'
size='small'
aria-label={t('宽度(像素)')}
/>
<span className='igf-size-x' aria-hidden='true'>
×
</span>
<InputNumber
value={draft.height}
onChange={(v) => setDraft((d) => ({ ...d, height: v }))}
onBlur={() => commit(draft)}
onPressEnter={() => commit(draft)}
min={range.min}
max={range.max}
step={range.step}
prefix={t('高')}
suffix='px'
size='small'
aria-label={t('高度(像素)')}
/>
<div className='igf-hint'>
{t('宽高范围 {{min}}–{{max}} 像素,按 {{step}} 像素取整', range)}
</div>
</div>
) : null}
</div>
);
};
export default ImageSizePicker;