feat:优化练字贴

This commit is contained in:
R524809
2026-01-21 09:07:57 +08:00
parent 9c13b44806
commit 943250b34a
6 changed files with 490 additions and 341 deletions
+124 -34
View File
@@ -17,8 +17,14 @@ Page({
maxCol: 0 as number,
data: {
// 选中的汉字列表
// 最终的汉字列表(合并输入和选择的汉字)
words: [] as string[],
// 来自文本框输入的汉字(每次输入替换)
inputWords: [] as string[],
// 来自弹窗选择的汉字(追加)
selectedWords: [] as string[],
// 输入框的值(用于清空)
inputValue: '',
showSelectWordPopup: false,
showColorPopup: false,
currentKey: 0,
@@ -33,9 +39,12 @@ Page({
const hasShowPracticeDemo =
wx.getStorageSync('hasShowPracticeDemo') || false;
if (!hasShowPracticeDemo) {
const demoWords = ['好', '好', '学', '习', '天', '天', '向', '上'];
this.setData(
{
words: ['好', '好', '学', '习', '天', '天', '向', '上'],
words: demoWords,
inputWords: demoWords,
selectedWords: [],
},
() => {
wx.setStorageSync('hasShowPracticeDemo', true);
@@ -86,47 +95,68 @@ Page({
}
},
// 输入组件回调
// 输入组件回调(所见即所得:替换文本框输入的汉字)
onConfirmInput(e: any) {
const value: string = (e && e.detail && e.detail.value) || '';
const text = (value || '').trim();
if (!text) return;
// 如果文本框为空,清空输入汉字
if (!text) {
const { selectedWords } = this.data as any;
const updatedWords = [...selectedWords];
this.setData(
{
inputWords: [],
words: updatedWords,
},
() => {
this.renderPracticeContent().catch(console.error);
},
);
return;
}
if (!this.isChineseText(text)) {
wx.showToast({ title: '请输入汉字', icon: 'none' });
return;
}
const { words } = this.data as any;
// 1. 解析新输入的汉字
const newChars = this.splitToSingleChars(text);
if (newChars.length === 0) {
// 1. 解析输入的汉字
const newInputChars = this.splitToSingleChars(text);
if (newInputChars.length === 0) {
wx.showToast({ title: '请输入有效汉字', icon: 'none' });
return;
}
// 2. 检查是否超过限制
if (words.length + newChars.length > 11) {
const { selectedWords } = this.data as any;
// 2. 检查是否超过限制(输入汉字 + 已选择的汉字)
if (newInputChars.length + selectedWords.length > 11) {
wx.showToast({
title: `最多只能添加 11 个字,当前已 ${words.length} 个,输入了 ${newChars.length}`,
title: `最多只能添加 11 个字,当前已选择 ${selectedWords.length} 个,输入了 ${newInputChars.length}`,
icon: 'none',
duration: 3000,
});
return;
}
// 3. 直接追加新汉字
const updatedWords = [...words, ...newChars];
// 3. 替换输入汉字(不是追加),合并已选择的汉字
const updatedWords = [...newInputChars, ...selectedWords];
// 4. 更新数据并提示
this.setData({ words: updatedWords }, () => {
this.renderPracticeContent().catch(console.error);
});
this.setData(
{
inputWords: newInputChars,
words: updatedWords,
},
() => {
this.renderPracticeContent().catch(console.error);
},
);
// 5. 显示添加结果提示
wx.showToast({
title: `成功添加 ${newChars.length}汉字`,
title: `已更新输入汉字(${newInputChars.length}`,
icon: 'none',
duration: 2000,
});
@@ -140,30 +170,90 @@ Page({
this.setData({ showSelectWordPopup: false });
},
onChangeWord(e: any) {
const selectedWords = e.detail.selectedWords as string[];
const newWords = [...this.data.words, ...selectedWords];
this.setData({ words: newWords, showSelectWordPopup: false }, () => {
if (newWords && newWords.length > 0) {
this.renderPracticeContent().catch(console.error);
}
});
const newSelectedWords = e.detail.selectedWords as string[];
const { inputWords } = this.data as any;
// 检查是否超过限制
if (inputWords.length + newSelectedWords.length > 11) {
wx.showToast({
title: `最多只能添加 11 个字,当前输入 ${inputWords.length} 个,选择了 ${newSelectedWords.length}`,
icon: 'none',
duration: 3000,
});
return;
}
// 合并输入汉字和选择的汉字(求并集)
const updatedWords = [...inputWords, ...newSelectedWords];
this.setData(
{
selectedWords: newSelectedWords,
words: updatedWords,
showSelectWordPopup: false,
},
() => {
if (updatedWords && updatedWords.length > 0) {
this.renderPracticeContent().catch(console.error);
}
},
);
},
// 删除
// 删除(需要判断删除的是输入汉字还是选择的汉字)
deleteWordCard(e: any) {
const { key } = e.detail;
const { words } = this.data as any;
const next = words.filter((_: string, index: number) => index !== key);
this.setData({ words: next }, () =>
this.renderPracticeContent().catch(console.error),
);
const { inputWords, selectedWords } = this.data as any;
// 判断删除的是输入汉字还是选择的汉字
if (key < inputWords.length) {
// 删除的是输入汉字
const nextInputWords = inputWords.filter(
(_: string, index: number) => index !== key,
);
const nextWords = [...nextInputWords, ...selectedWords];
this.setData(
{
inputWords: nextInputWords,
words: nextWords,
},
() => this.renderPracticeContent().catch(console.error),
);
} else {
// 删除的是选择的汉字
const selectedIndex = key - inputWords.length;
const nextSelectedWords = selectedWords.filter(
(_: string, index: number) => index !== selectedIndex,
);
const nextWords = [...inputWords, ...nextSelectedWords];
this.setData(
{
selectedWords: nextSelectedWords,
words: nextWords,
},
() => this.renderPracticeContent().catch(console.error),
);
}
},
// 清空
// 清空(清空所有来源的汉字:文本框、预览、弹窗选择)
clearWords() {
this.setData({ words: [] }, () =>
this.renderPracticeContent().catch(console.error),
// 清空所有汉字数据
this.setData(
{
words: [],
inputWords: [],
selectedWords: [],
inputValue: '', // 清空输入框
},
() => {
this.renderPracticeContent().catch(console.error);
},
);
// 关闭弹窗(如果打开)
if (this.data.showSelectWordPopup) {
this.setData({ showSelectWordPopup: false });
}
},
// 随机生成(取基础分类,过滤为可用SVG字,最多11个)