feat:绘制图形
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
# 汉字选择页面 (wordDemo)
|
||||
|
||||
## 功能描述
|
||||
|
||||
这是一个汉字输入和选择的页面,主要包含以下功能:
|
||||
|
||||
### 上部区域 - 汉字输入
|
||||
|
||||
- 输入框:可以输入任意汉字(最多10个字符)
|
||||
- 生成按钮:点击后将输入的汉字添加到已选择列表中
|
||||
- **汉字校验**:自动校验输入内容是否为汉字,非汉字会提示错误
|
||||
- **智能分割**:支持输入多个汉字,自动分割为单个汉字存储
|
||||
|
||||
### 中部区域 - 已选择的汉字
|
||||
|
||||
- 显示所有已选择的汉字
|
||||
- 每个汉字都有删除按钮(×)
|
||||
- 清空按钮:一键清空所有已选择的汉字
|
||||
- 显示已选择汉字的数量
|
||||
|
||||
### 下部区域 - 可选汉字
|
||||
|
||||
- 从 `demo/grade3.json` 文件中提取前30个汉字
|
||||
- 以6列网格形式展示
|
||||
- 已选择的汉字会显示为禁用状态
|
||||
- 点击未选择的汉字可以添加到列表中
|
||||
|
||||
## 核心特性
|
||||
|
||||
### 🔍 **汉字校验**
|
||||
|
||||
- 使用Unicode范围 `\u4e00-\u9fff` 校验汉字
|
||||
- 非汉字输入会显示Toast提示"请输入汉字"
|
||||
- 确保 `wordList` 中只存储纯汉字
|
||||
|
||||
### ✂️ **智能分割**
|
||||
|
||||
- 支持输入多个汉字(如:"你好世界")
|
||||
- 自动分割为单个汉字:["你", "好", "世", "界"]
|
||||
- 过滤重复汉字,避免重复添加
|
||||
- 显示实际添加的汉字数量
|
||||
|
||||
### 💾 **数据管理**
|
||||
|
||||
- `wordList` 中每个元素都是单个汉字
|
||||
- 自动去重,避免重复存储
|
||||
- 支持批量添加和单个删除
|
||||
|
||||
## 数据结构
|
||||
|
||||
- `inputWord`: 输入框中的汉字
|
||||
- `wordList`: 已选择的汉字列表(string[]类型,每个元素为单个汉字)
|
||||
- `availableWords`: 从grade3.json中提取的前30个汉字
|
||||
|
||||
## 使用方法
|
||||
|
||||
1. 在输入框中输入汉字(支持多个汉字)
|
||||
2. 点击"生成"按钮,系统自动校验和分割
|
||||
3. 点击下方网格中的汉字进行选择
|
||||
4. 已选择的汉字会显示在上方,可以单独删除或一键清空
|
||||
5. 所有选择的汉字都存储在 `this.data.wordList` 中
|
||||
|
||||
## 技术实现
|
||||
|
||||
### 汉字校验
|
||||
|
||||
```typescript
|
||||
isChineseText(text: string): boolean {
|
||||
const chineseRegex = /^[\u4e00-\u9fff]+$/;
|
||||
return chineseRegex.test(text);
|
||||
}
|
||||
```
|
||||
|
||||
### 文本分割
|
||||
|
||||
```typescript
|
||||
splitToSingleChars(text: string): string[] {
|
||||
const chineseRegex = /[\u4e00-\u9fff]/g;
|
||||
const matches = text.match(chineseRegex);
|
||||
return matches || [];
|
||||
}
|
||||
```
|
||||
|
||||
## 文件结构
|
||||
|
||||
- `index.ts`: 页面逻辑文件
|
||||
- `index.wxml`: 页面模板文件
|
||||
- `index.less`: 页面样式文件
|
||||
- `index.json`: 页面配置文件
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"navigationBarTitleText": "汉字选择",
|
||||
"navigationBarBackgroundColor": "#d2e7d8",
|
||||
"backgroundColor": "#f5f5f5",
|
||||
"enablePullDownRefresh": false
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
/**index.less**/
|
||||
page {
|
||||
height: 100vh;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
.container {
|
||||
padding: 20rpx;
|
||||
height: 100vh;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
/* 输入区域样式 */
|
||||
.input-section {
|
||||
background-color: #fff;
|
||||
border-radius: 20rpx;
|
||||
padding: 30rpx;
|
||||
margin-bottom: 20rpx;
|
||||
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.input-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 20rpx;
|
||||
}
|
||||
|
||||
.word-input {
|
||||
flex: 1;
|
||||
height: 80rpx;
|
||||
border: 2rpx solid #e0e0e0;
|
||||
border-radius: 40rpx;
|
||||
padding: 0 30rpx;
|
||||
font-size: 32rpx;
|
||||
background-color: #fafafa;
|
||||
}
|
||||
|
||||
.generate-btn {
|
||||
width: 160rpx;
|
||||
height: 80rpx;
|
||||
background-color: #07c160;
|
||||
color: #fff;
|
||||
border-radius: 40rpx;
|
||||
font-size: 28rpx;
|
||||
border: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
/* 通用区域样式 */
|
||||
.section-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
font-size: 32rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.clear-btn {
|
||||
background-color: #ff4757;
|
||||
color: #fff;
|
||||
border-radius: 20rpx;
|
||||
font-size: 24rpx;
|
||||
padding: 10rpx 20rpx;
|
||||
border: none;
|
||||
}
|
||||
|
||||
/* 已选择的汉字区域 */
|
||||
.selected-section {
|
||||
background-color: #fff;
|
||||
border-radius: 20rpx;
|
||||
padding: 30rpx;
|
||||
margin-bottom: 20rpx;
|
||||
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.word-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 20rpx;
|
||||
}
|
||||
|
||||
.word-item.selected {
|
||||
background-color: #07c160;
|
||||
color: #fff;
|
||||
border-radius: 50rpx;
|
||||
padding: 20rpx 30rpx;
|
||||
position: relative;
|
||||
min-width: 80rpx;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.word-text {
|
||||
font-size: 32rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.remove-btn {
|
||||
position: absolute;
|
||||
top: -10rpx;
|
||||
right: -10rpx;
|
||||
width: 40rpx;
|
||||
height: 40rpx;
|
||||
background-color: #ff4757;
|
||||
color: #fff;
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 24rpx;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
/* 可选汉字区域 */
|
||||
.available-section {
|
||||
background-color: #fff;
|
||||
border-radius: 20rpx;
|
||||
padding: 30rpx;
|
||||
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.word-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(6, 1fr);
|
||||
gap: 20rpx;
|
||||
}
|
||||
|
||||
.word-item.available {
|
||||
background-color: #f0f0f0;
|
||||
color: #333;
|
||||
border-radius: 50rpx;
|
||||
padding: 20rpx;
|
||||
text-align: center;
|
||||
border: 2rpx solid #e0e0e0;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.word-item.available:active {
|
||||
background-color: #e0e0e0;
|
||||
transform: scale(0.95);
|
||||
}
|
||||
|
||||
.word-item.available.disabled {
|
||||
background-color: #ccc;
|
||||
color: #999;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.word-item.available.disabled:active {
|
||||
background-color: #ccc;
|
||||
transform: none;
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
Page({
|
||||
data: {
|
||||
inputWord: '', // 输入框中的汉字
|
||||
wordList: [] as string[], // 选中的汉字列表
|
||||
availableWords: [] as string[], // 从grade3.json中提取的前30个汉字
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadGrade3Words();
|
||||
},
|
||||
|
||||
// 加载grade3.json中的前30个汉字
|
||||
loadGrade3Words() {
|
||||
const fs = wx.getFileSystemManager();
|
||||
try {
|
||||
const fileContent = fs.readFileSync('demo/grade3.json', 'utf8') as string;
|
||||
const grade3Data = JSON.parse(fileContent);
|
||||
const words = Object.keys(grade3Data).slice(0, 30);
|
||||
this.setData({
|
||||
availableWords: words
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('加载grade3.json失败:', error);
|
||||
// 如果文件读取失败,使用一些示例汉字
|
||||
const fallbackWords = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '人', '口', '手', '足', '目', '耳', '鼻', '舌', '心', '肝', '脾', '肺', '肾', '胃', '肠', '胆', '膀', '胱', '皮', '毛'];
|
||||
this.setData({
|
||||
availableWords: fallbackWords
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 输入框输入事件
|
||||
onInputChange(e: WechatMiniprogram.Input) {
|
||||
this.setData({
|
||||
inputWord: e.detail.value
|
||||
});
|
||||
},
|
||||
|
||||
// 生成按钮点击事件
|
||||
onGenerateClick() {
|
||||
const { inputWord, wordList } = this.data;
|
||||
if (inputWord && inputWord.trim()) {
|
||||
const inputText = inputWord.trim();
|
||||
|
||||
// 校验输入是否为汉字
|
||||
if (!this.isChineseText(inputText)) {
|
||||
wx.showToast({
|
||||
title: '请输入汉字',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 将输入文本分割为单个汉字
|
||||
const singleChars = this.splitToSingleChars(inputText);
|
||||
|
||||
// 过滤掉已存在的汉字
|
||||
const newChars = singleChars.filter(char => !wordList.includes(char));
|
||||
|
||||
if (newChars.length === 0) {
|
||||
wx.showToast({
|
||||
title: '所有汉字都已存在',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 添加到列表中
|
||||
const newWordList = [...wordList, ...newChars];
|
||||
this.setData({
|
||||
wordList: newWordList,
|
||||
inputWord: '' // 清空输入框
|
||||
});
|
||||
|
||||
wx.showToast({
|
||||
title: `已添加${newChars.length}个汉字`,
|
||||
icon: 'success'
|
||||
});
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: '请输入汉字',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 校验文本是否为汉字
|
||||
isChineseText(text: string): boolean {
|
||||
// 汉字Unicode范围:\u4e00-\u9fff
|
||||
const chineseRegex = /^[\u4e00-\u9fff]+$/;
|
||||
return chineseRegex.test(text);
|
||||
},
|
||||
|
||||
// 将文本分割为单个汉字
|
||||
splitToSingleChars(text: string): string[] {
|
||||
// 使用正则表达式匹配每个汉字
|
||||
const chineseRegex = /[\u4e00-\u9fff]/g;
|
||||
const matches = text.match(chineseRegex);
|
||||
return matches || [];
|
||||
},
|
||||
|
||||
// 选择汉字点击事件
|
||||
onWordSelect(e: WechatMiniprogram.TouchEvent) {
|
||||
const { word } = e.currentTarget.dataset;
|
||||
const { wordList } = this.data;
|
||||
|
||||
if (!wordList.includes(word)) {
|
||||
const newWordList = [...wordList, word];
|
||||
this.setData({
|
||||
wordList: newWordList
|
||||
});
|
||||
wx.showToast({
|
||||
title: '已选择',
|
||||
icon: 'success'
|
||||
});
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: '已选择过',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 从列表中移除汉字
|
||||
onRemoveWord(e: WechatMiniprogram.TouchEvent) {
|
||||
const { index } = e.currentTarget.dataset;
|
||||
const { wordList } = this.data;
|
||||
const newWordList = wordList.filter((_, i) => i !== index);
|
||||
this.setData({
|
||||
wordList: newWordList
|
||||
});
|
||||
wx.showToast({
|
||||
title: '已移除',
|
||||
icon: 'success'
|
||||
});
|
||||
},
|
||||
|
||||
// 清空所有选中的汉字
|
||||
onClearAll() {
|
||||
this.setData({
|
||||
wordList: []
|
||||
});
|
||||
wx.showToast({
|
||||
title: '已清空',
|
||||
icon: 'success'
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
<!--index.wxml-->
|
||||
<view class="container">
|
||||
<!-- 上部:输入区域 -->
|
||||
<view class="input-section">
|
||||
<view class="input-row">
|
||||
<input
|
||||
class="word-input"
|
||||
placeholder="请输入汉字(支持多个汉字)"
|
||||
value="{{inputWord}}"
|
||||
bindinput="onInputChange"
|
||||
maxlength="20" />
|
||||
<button class="generate-btn" bindtap="onGenerateClick">生成</button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 已选择的汉字列表 -->
|
||||
<view class="selected-section" wx:if="{{wordList.length > 0}}">
|
||||
<view class="section-header">
|
||||
<text class="section-title"
|
||||
>已选择的汉字 ({{wordList.length}})</text
|
||||
>
|
||||
<button class="clear-btn" bindtap="onClearAll" size="mini">
|
||||
清空
|
||||
</button>
|
||||
</view>
|
||||
<view class="word-list">
|
||||
<view
|
||||
class="word-item selected"
|
||||
wx:for="{{wordList}}"
|
||||
wx:key="*this"
|
||||
wx:for-item="word"
|
||||
wx:for-index="index">
|
||||
<text class="word-text">{{word}}</text>
|
||||
<text
|
||||
class="remove-btn"
|
||||
bindtap="onRemoveWord"
|
||||
data-index="{{index}}"
|
||||
>×</text
|
||||
>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 下部:可选汉字区域 -->
|
||||
<view class="available-section">
|
||||
<view class="section-header">
|
||||
<text class="section-title"
|
||||
>可选汉字 ({{availableWords.length}})</text
|
||||
>
|
||||
</view>
|
||||
<view class="word-grid">
|
||||
<view
|
||||
class="word-item available {{wordList.includes(word) ? 'disabled' : ''}}"
|
||||
wx:for="{{availableWords}}"
|
||||
wx:key="*this"
|
||||
wx:for-item="word"
|
||||
bindtap="onWordSelect"
|
||||
data-word="{{word}}">
|
||||
<text class="word-text">{{word}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
Reference in New Issue
Block a user