From 1a6b6df47af08b70216c9e04fb43e1395b3fec0c Mon Sep 17 00:00:00 2001 From: zihanjian Date: Fri, 27 Jun 2025 15:49:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E9=A2=9C=E8=89=B2=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E9=9D=A2=E6=9D=BF=E6=B7=BB=E5=8A=A0=E9=A2=9C=E8=89=B2=E5=88=86?= =?UTF-8?q?=E7=BB=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81=E6=8C=89?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=92=8C=E6=95=B0=E5=AD=97=E5=88=86=E7=BB=84?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=EF=BC=8C=E4=BC=98=E5=8C=96=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E4=BA=A4=E4=BA=92=E4=BD=93=E9=AA=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ColorSystemPanel.tsx | 188 +++++++++++++++++++++++----- 1 file changed, 160 insertions(+), 28 deletions(-) diff --git a/src/components/ColorSystemPanel.tsx b/src/components/ColorSystemPanel.tsx index 5c1b2f4..efc1b11 100644 --- a/src/components/ColorSystemPanel.tsx +++ b/src/components/ColorSystemPanel.tsx @@ -21,6 +21,7 @@ const ColorSystemPanel: React.FC = ({ const [searchTerm, setSearchTerm] = useState(''); const [tempCustomPalette, setTempCustomPalette] = useState>(new Set(customPalette)); const [showColorSystemDropdown, setShowColorSystemDropdown] = useState(false); + const [collapsedGroups, setCollapsedGroups] = useState>(new Set()); // 获取所有可用颜色 const allColors = getAllHexValues(); @@ -33,7 +34,23 @@ const ColorSystemPanel: React.FC = ({ }); }; - // 过滤和排序颜色 + // 分组函数 + const getGroupKey = (colorKey: string) => { + if (/^[A-Za-z]/.test(colorKey)) { + // 字母开头,按首字母分组 + return colorKey.charAt(0).toUpperCase(); + } else if (/^\d/.test(colorKey)) { + // 数字开头,按十位数分组 + const num = parseInt(colorKey); + const groupStart = Math.floor(num / 10) * 10; + return `${groupStart}-${groupStart + 9}`; + } else { + // 其他字符开头 + return '其他'; + } + }; + + // 过滤和分组颜色 const filteredColors = allColors .filter(hex => { const colorKey = getColorKeyByHex(hex, selectedColorSystem); @@ -49,6 +66,57 @@ const ColorSystemPanel: React.FC = ({ return naturalSort(keyA, keyB); }); + // 按组分类 + const groupedColors = filteredColors.reduce((groups, hex) => { + const colorKey = getColorKeyByHex(hex, selectedColorSystem); + const groupKey = getGroupKey(colorKey); + + if (!groups[groupKey]) { + groups[groupKey] = []; + } + groups[groupKey].push(hex); + + return groups; + }, {} as Record); + + // 按组键排序 + const sortedGroupKeys = Object.keys(groupedColors).sort((a, b) => { + // 字母组优先,然后是数字组,最后是其他 + const isLetterA = /^[A-Z]$/.test(a); + const isLetterB = /^[A-Z]$/.test(b); + const isNumberA = /^\d/.test(a); + const isNumberB = /^\d/.test(b); + + if (isLetterA && !isLetterB) return -1; + if (!isLetterA && isLetterB) return 1; + if (isNumberA && !isNumberB && !isLetterB) return -1; + if (!isNumberA && isNumberB && !isLetterA) return 1; + + return naturalSort(a, b); + }); + + // 初始化时默认全折叠 + React.useEffect(() => { + if (sortedGroupKeys.length > 0 && collapsedGroups.size === 0) { + setCollapsedGroups(new Set(sortedGroupKeys)); + } + }, [sortedGroupKeys.join(',')]); + + // 分组操作函数 + const handleGroupSelectAll = (groupKey: string) => { + const groupColors = groupedColors[groupKey]; + const newPalette = new Set(tempCustomPalette); + groupColors.forEach(hex => newPalette.add(hex)); + setTempCustomPalette(newPalette); + }; + + const handleGroupClear = (groupKey: string) => { + const groupColors = groupedColors[groupKey]; + const newPalette = new Set(tempCustomPalette); + groupColors.forEach(hex => newPalette.delete(hex)); + setTempCustomPalette(newPalette); + }; + // 保存自定义色板 const handleSaveCustomPalette = () => { // 从色板中剔除在当前色号系统下不存在的颜色(色号为"-") @@ -201,36 +269,100 @@ const ColorSystemPanel: React.FC = ({ - {/* 颜色网格 */} + {/* 颜色网格 - 分组显示 */}
-
- {filteredColors.map(hex => { - const colorKey = getColorKeyByHex(hex, selectedColorSystem); - const isSelected = tempCustomPalette.has(hex); - - return ( -
+ {sortedGroupKeys.map(groupKey => { + const groupColors = groupedColors[groupKey]; + const isCollapsed = collapsedGroups.has(groupKey); + const selectedInGroup = groupColors.filter(hex => tempCustomPalette.has(hex)).length; + + return ( +
+ {/* 分组标题 */} +
+ + {/* 一键操作按钮 */} +
+ + +
- ); - })} -
+ + {/* 颜色网格 */} + {!isCollapsed && ( +
+ {groupColors.map(hex => { + const colorKey = getColorKeyByHex(hex, selectedColorSystem); + const isSelected = tempCustomPalette.has(hex); + + return ( +
+
+ ); + })} +
+ )} +
+ ); + })}
{/* 底部按钮 */}