修正颜色映射中的一个hex值,增强localStorage数据加载的验证逻辑,新增有效和无效键的统计输出,优化调色板选择和导入功能,提升代码可读性和用户体验。

This commit is contained in:
zihanjian
2025-05-25 11:29:08 +08:00
parent 2155675ec0
commit 2519daf162
3 changed files with 64 additions and 81 deletions

View File

@@ -258,7 +258,7 @@
"#1A60C3": { "MARD": "R08", "COCO": "L08", "漫漫": "T3", "盼盼": "68", "咪小窝": "54" },
"#9A56B4": { "MARD": "R09", "COCO": "L09", "漫漫": "T2", "盼盼": "70", "咪小窝": "56" },
"#FFDB4C": { "MARD": "R10", "COCO": "L10", "漫漫": "L2", "盼盼": "156", "咪小窝": "53" },
"#FFEBFA": { "MARD": "R11", "COCO": "L11", "漫漫": "T6", "盼盼": "151", "咪小窝": "151" },
"#FFEBFB": { "MARD": "R11", "COCO": "L11", "漫漫": "T6", "盼盼": "151", "咪小窝": "151" },
"#D8D5CE": { "MARD": "R12", "COCO": "L12", "漫漫": "T7", "盼盼": "160", "咪小窝": "157" },
"#55514C": { "MARD": "R13", "COCO": "L13", "漫漫": "-", "盼盼": "152", "咪小窝": "152" },
"#9FE4DF": { "MARD": "R14", "COCO": "S1", "漫漫": "S1", "盼盼": "231", "咪小窝": "231" },

View File

@@ -251,36 +251,48 @@ export default function Home() {
// 尝试从localStorage加载
const savedSelections = loadPaletteSelections();
if (savedSelections && Object.keys(savedSelections).length > 0) {
console.log('从localStorage加载的数据键数量:', Object.keys(savedSelections).length);
// 验证加载的数据是否都是有效的hex值
const allHexValues = fullBeadPalette.map(color => color.hex.toUpperCase());
const validSelections: PaletteSelections = {};
let hasValidData = false;
let validCount = 0;
let invalidCount = 0;
Object.entries(savedSelections).forEach(([key, value]) => {
if (allHexValues.includes(key.toUpperCase())) {
// 严格验证键必须是有效的hex格式并且存在于调色板中
if (/^#[0-9A-F]{6}$/i.test(key) && allHexValues.includes(key.toUpperCase())) {
validSelections[key.toUpperCase()] = value;
hasValidData = true;
validCount++;
} else {
invalidCount++;
}
});
console.log(`验证结果: 有效键 ${validCount} 个, 无效键 ${invalidCount}`);
if (hasValidData) {
setCustomPaletteSelections(validSelections);
setIsCustomPalette(true);
} else {
// 如果本地数据无效,用当前预设初始化
console.log('所有数据无效,清除localStorage并重新初始化');
// 如果本地数据无效清除localStorage并用当前预设初始化
localStorage.removeItem('customPerlerPaletteSelections');
const currentPresetKeys = paletteOptions[selectedPaletteKeySet]?.keys || [];
const initialSelections = presetKeysToHexSelections(fullBeadPalette, currentPresetKeys);
setCustomPaletteSelections(initialSelections);
setIsCustomPalette(false);
}
} else {
console.log('没有localStorage数据使用预设初始化');
// 如果没有保存的选择,用当前预设初始化
const currentPresetKeys = paletteOptions[selectedPaletteKeySet]?.keys || [];
const initialSelections = presetKeysToHexSelections(fullBeadPalette, currentPresetKeys);
setCustomPaletteSelections(initialSelections);
setIsCustomPalette(false);
}
}, [selectedPaletteKeySet]); // Add selectedPaletteKeySet to the dependency array
}, [selectedPaletteKeySet]);
// 更新 activeBeadPalette 基于自定义选择和排除列表
useEffect(() => {
@@ -1127,19 +1139,10 @@ export default function Home() {
return;
}
// 为了兼容性也生成对应的MARD色号
const correspondingMardKeys = selectedHexValues
.map(hex => {
const colorData = fullBeadPalette.find(color => color.hex.toUpperCase() === hex.toUpperCase());
return colorData?.key;
})
.filter(key => key !== undefined) as string[];
// 导出新格式基于hex值同时保留MARD色号以便兼容
// 导出格式仅基于hex值
const exportData = {
version: "2.0", // 版本号标识
version: "3.0", // 版本号
selectedHexValues: selectedHexValues,
selectedKeys: correspondingMardKeys, // 保留旧格式以便兼容
exportDate: new Date().toISOString(),
totalColors: selectedHexValues.length
};
@@ -1166,74 +1169,40 @@ export default function Home() {
const content = e.target?.result as string;
const data = JSON.parse(content);
let validHexValues: string[] = [];
// 检查是否为新格式基于hex值
if (data.version === "2.0" && Array.isArray(data.selectedHexValues)) {
console.log("检测到新格式基于hex值的色板文件");
const importedHexValues = data.selectedHexValues as string[];
const invalidHexValues: string[] = [];
// 验证hex值
importedHexValues.forEach(hex => {
const normalizedHex = hex.toUpperCase();
const colorData = fullBeadPalette.find(color => color.hex.toUpperCase() === normalizedHex);
if (colorData) {
validHexValues.push(normalizedHex);
} else {
invalidHexValues.push(hex);
}
});
if (invalidHexValues.length > 0) {
console.warn("导入时发现无效的hex值:", invalidHexValues);
alert(`导入完成,但以下颜色无效已被忽略:\n${invalidHexValues.join(', ')}`);
}
if (validHexValues.length === 0) {
alert("导入的文件中不包含任何有效的颜色。");
return;
}
console.log(`成功验证 ${validHexValues.length} 个有效的hex值`);
}
// 检查是否为旧格式基于MARD色号
else if (Array.isArray(data.selectedKeys)) {
console.log("检测到旧格式基于MARD色号的色板文件");
const importedKeys = data.selectedKeys as string[];
const validKeys = new Set(allPaletteKeys);
const validImportedKeys = importedKeys.filter(key => validKeys.has(key));
const invalidKeys = importedKeys.filter(key => !validKeys.has(key));
if (invalidKeys.length > 0) {
console.warn("导入时发现无效的颜色key:", invalidKeys);
alert(`导入完成,但以下色号无效已被忽略:\n${invalidKeys.join(', ')}`);
}
if (validImportedKeys.length === 0) {
alert("导入的文件中不包含任何有效的色号。");
return;
}
// 将MARD色号转换为hex值
validHexValues = validImportedKeys
.map(key => {
const colorData = fullBeadPalette.find(color => color.key === key);
return colorData?.hex.toUpperCase();
})
.filter(hex => hex !== undefined) as string[];
console.log(`从旧格式转换得到 ${validHexValues.length} 个有效的hex值`);
}
// 无法识别的格式
else {
throw new Error("无效的文件格式:文件既不包含 'selectedHexValues' 数组也不包含 'selectedKeys' 数组。");
// 检查文件格式
if (!Array.isArray(data.selectedHexValues)) {
throw new Error("无效的文件格式:文件必须包含 'selectedHexValues' 数组。");
}
console.log("检测到基于hex值的色板文件");
const importedHexValues = data.selectedHexValues as string[];
const validHexValues: string[] = [];
const invalidHexValues: string[] = [];
// 验证hex值
importedHexValues.forEach(hex => {
const normalizedHex = hex.toUpperCase();
const colorData = fullBeadPalette.find(color => color.hex.toUpperCase() === normalizedHex);
if (colorData) {
validHexValues.push(normalizedHex);
} else {
invalidHexValues.push(hex);
}
});
if (invalidHexValues.length > 0) {
console.warn("导入时发现无效的hex值:", invalidHexValues);
alert(`导入完成,但以下颜色无效已被忽略:\n${invalidHexValues.join(', ')}`);
}
if (validHexValues.length === 0) {
alert("导入的文件中不包含任何有效的颜色。");
return;
}
console.log(`成功验证 ${validHexValues.length} 个有效的hex值`);
// 基于有效的hex值创建新的selections对象
const allHexValues = fullBeadPalette.map(color => color.hex.toUpperCase());
const newSelections = presetToSelections(allHexValues, validHexValues);

View File

@@ -55,11 +55,25 @@ export function presetKeysToHexSelections(
): PaletteSelections {
const presetKeySet = new Set(presetKeys);
const selections: PaletteSelections = {};
const processedHexValues = new Set<string>();
console.log(`presetKeysToHexSelections: 输入调色板大小 ${allBeadPalette.length}, 预设键数量 ${presetKeys.length}`);
allBeadPalette.forEach(color => {
const normalizedHex = color.hex.toUpperCase();
// 检查是否已经处理过这个hex值
if (processedHexValues.has(normalizedHex)) {
console.warn(`重复的hex值: ${normalizedHex}, MARD键: ${color.key}`);
return; // 跳过重复的hex值
}
processedHexValues.add(normalizedHex);
selections[normalizedHex] = presetKeySet.has(color.key);
});
const selectedCount = Object.values(selections).filter(Boolean).length;
console.log(`presetKeysToHexSelections: 生成选择对象,总数 ${Object.keys(selections).length}, 选中 ${selectedCount}`);
return selections;
}