/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useEffect, useMemo, useState } from 'react';
import { InputNumber } from '@douyinfe/semi-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 (
{
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 (
<>
{t('自定义')}
>
);
}
const p = presets.find((x) => x.value === opt);
return (
<>
{p.label || `${p.width}×${p.height}`}
{p.label ? (
{p.width}×{p.height}
) : null}
>
);
}}
/>
{range && customActive ? (
setDraft((d) => ({ ...d, width: v }))}
onBlur={() => commit(draft)}
onEnterPress={() => commit(draft)}
min={range.min}
max={range.max}
step={range.step}
prefix={t('宽')}
suffix='px'
size='small'
aria-label={t('宽度(像素)')}
/>
×
setDraft((d) => ({ ...d, height: v }))}
onBlur={() => commit(draft)}
onEnterPress={() => commit(draft)}
min={range.min}
max={range.max}
step={range.step}
prefix={t('高')}
suffix='px'
size='small'
aria-label={t('高度(像素)')}
/>
{t('宽高范围 {{min}}–{{max}} 像素,按 {{step}} 像素取整', range)}
) : null}
);
};
export default ImageSizePicker;