Skip to content

Commit adf1da8

Browse files
committed
feat: 优化 iconify 的图标选择,支持选择颜色
1 parent dcf7474 commit adf1da8

File tree

2 files changed

+149
-78
lines changed

2 files changed

+149
-78
lines changed

components/modals/EditLinkModal.tsx

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
3131
}) => {
3232
const [form] = Form.useForm();
3333
const [loading, setLoading] = useState(false);
34-
const [iconType, setIconType] = useState<'1' | '2' | '3'>('1'); // 1: Favicon, 2: 自定义, 3: Iconify
34+
const [iconType, setIconType] = useState<'1' | '2' | '3'>('1'); // 1: Favicon, 2: Iconify, 3: 自定义
3535
const [previewIcon, setPreviewIcon] = useState<string>('');
3636
const [previewBgColor, setPreviewBgColor] = useState<string>(getDefaultColor());
3737
const [iconScale, setIconScale] = useState<number>(0.7);
3838
const [savedCustomIcon, setSavedCustomIcon] = useState<string>(''); // 保存自定义图标
3939
const [savedIconifyIcon, setSavedIconifyIcon] = useState<string>(''); // 保存 Iconify 图标
40+
const [iconifyColor, setIconifyColor] = useState<string>(''); // Iconify 图标颜色
4041
const categories = useAppSelector((state) => state.categories.items);
4142

4243
// 检查是否支持 EyeDropper API
@@ -86,13 +87,17 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
8687

8788
// 判断图标类型
8889
if (iconUrl && iconUrl.includes('api.iconify.design')) {
89-
setIconType('3');
90+
setIconType('2'); // Iconify 图标
91+
// 提取颜色参数
92+
const urlParams = new URLSearchParams(iconUrl.split('?')[1]);
93+
const color = urlParams.get('color') || '';
94+
setIconifyColor(color);
9095
setSavedIconifyIcon(iconUrl); // 保存 Iconify 图标
9196
} else if (iconUrl && !iconUrl.includes('favicon.im')) {
92-
setIconType('2');
97+
setIconType('3'); // 自定义图标
9398
setSavedCustomIcon(iconUrl); // 保存自定义图标
9499
} else {
95-
setIconType('1');
100+
setIconType('1'); // Favicon 图标
96101
}
97102
} else {
98103
// 添加模式:重置表单,使用第一个分类作为默认值
@@ -110,6 +115,7 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
110115
setIconType('1');
111116
setSavedCustomIcon('');
112117
setSavedIconifyIcon('');
118+
setIconifyColor('');
113119
}
114120
}
115121
}, [open, link, form, defaultCategory]);
@@ -154,13 +160,13 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
154160
setPreviewIcon(faviconUrl || '');
155161
}
156162
} else if (value === '2') {
157-
// 切换到自定义模式,恢复之前保存的自定义图标
158-
const iconToRestore = savedCustomIcon || '';
163+
// 切换到 Iconify 模式,恢复之前保存的 Iconify 图标
164+
const iconToRestore = savedIconifyIcon || '';
159165
form.setFieldsValue({ icon: iconToRestore });
160166
setPreviewIcon(iconToRestore);
161167
} else if (value === '3') {
162-
// 切换到 Iconify 模式,恢复之前保存的 Iconify 图标
163-
const iconToRestore = savedIconifyIcon || '';
168+
// 切换到自定义模式,恢复之前保存的自定义图标
169+
const iconToRestore = savedCustomIcon || '';
164170
form.setFieldsValue({ icon: iconToRestore });
165171
setPreviewIcon(iconToRestore);
166172
}
@@ -172,7 +178,7 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
172178
form.setFieldsValue({ icon: iconUrl });
173179
setPreviewIcon(iconUrl);
174180
// 实时保存自定义图标
175-
if (iconType === '2') {
181+
if (iconType === '3') {
176182
setSavedCustomIcon(iconUrl);
177183
}
178184
}, [form, iconType]);
@@ -184,6 +190,21 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
184190
setSavedIconifyIcon(iconUrl);
185191
}, [form]);
186192

193+
// 处理 Iconify 颜色变化
194+
const handleIconifyColorChange = useCallback((color: string) => {
195+
setIconifyColor(color);
196+
const currentIcon = form.getFieldValue('icon');
197+
if (currentIcon) {
198+
// 移除现有的 color 参数
199+
const baseUrl = currentIcon.split('?')[0];
200+
// 添加新的颜色参数
201+
const newUrl = color ? `${baseUrl}?color=${encodeURIComponent(color)}` : baseUrl;
202+
form.setFieldsValue({ icon: newUrl });
203+
setPreviewIcon(newUrl);
204+
setSavedIconifyIcon(newUrl);
205+
}
206+
}, [form]);
207+
187208
// 处理背景色变化
188209
const handleBgColorChange = useCallback((color: Color) => {
189210
const colorValue = color.toHexString();
@@ -257,6 +278,7 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
257278
setIconType('1');
258279
setSavedCustomIcon('');
259280
setSavedIconifyIcon('');
281+
setIconifyColor('');
260282
setLoading(false);
261283
} catch (error) {
262284
// 表单验证失败,界面已有提示
@@ -273,6 +295,7 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
273295
setIconType('1');
274296
setSavedCustomIcon('');
275297
setSavedIconifyIcon('');
298+
setIconifyColor('');
276299
onCancel();
277300
};
278301

@@ -385,14 +408,16 @@ export const EditLinkModal: React.FC<EditLinkModalProps> = ({
385408
options={iconOptions}
386409
/>
387410
{iconType === '2' ? (
388-
<Form.Item name="icon" className="flex-1">
411+
<Form.Item name="icon" className="flex-1 mb-0!">
389412
<IconifySelector
390413
value={form.getFieldValue('icon')}
391414
onChange={handleIconifyChange}
415+
onColorChange={handleIconifyColorChange}
416+
iconColor={iconifyColor}
392417
/>
393418
</Form.Item>
394419
) : (
395-
<Form.Item name="icon" className="flex-1">
420+
<Form.Item name="icon" className="flex-1 mb-0!">
396421
<Input
397422
placeholder={iconType === '1' ? '自动获取' : '输入图标 URL'}
398423
disabled={iconType === '1'}

0 commit comments

Comments
 (0)