Refactor Date Calculator UI to Morandi theme and fix logic; fix Index page interaction
This commit is contained in:
@@ -6,14 +6,25 @@ Page({
|
||||
operator: null as string | null,
|
||||
firstOperand: null as number | null,
|
||||
waitingForSecondOperand: false,
|
||||
isScientific: false
|
||||
isScientific: false,
|
||||
showHistory: false,
|
||||
historyList: [] as Array<{expression: string, result: string}>
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
// 从本地存储恢复历史记录
|
||||
const saved = wx.getStorageSync('CALC_HISTORY');
|
||||
if (saved && Array.isArray(saved)) {
|
||||
this.setData({ historyList: saved });
|
||||
}
|
||||
},
|
||||
|
||||
vibrate() {
|
||||
// wx.vibrateShort({ type: 'light' });
|
||||
wx.vibrateShort({ type: 'light' }).catch(() => {});
|
||||
},
|
||||
|
||||
preventTouchMove() {
|
||||
// 阻止历史面板底层滚动
|
||||
},
|
||||
|
||||
toggleMode() {
|
||||
@@ -157,6 +168,8 @@ Page({
|
||||
history: desc,
|
||||
waitingForSecondOperand: true // 计算完后,下次输入数字是新数字
|
||||
});
|
||||
|
||||
this.addToHistory(desc, String(formatted));
|
||||
},
|
||||
|
||||
onOperator(e: any) {
|
||||
@@ -201,6 +214,7 @@ Page({
|
||||
|
||||
if (operator && firstOperand != null) {
|
||||
const result = this.performCalculation(operator, firstOperand, inputValue);
|
||||
const expression = `${firstOperand} ${operator} ${inputValue}`;
|
||||
|
||||
this.setData({
|
||||
displayValue: String(result),
|
||||
@@ -209,6 +223,8 @@ Page({
|
||||
waitingForSecondOperand: true,
|
||||
history: `${firstOperand} ${operator} ${inputValue} =`
|
||||
});
|
||||
|
||||
this.addToHistory(expression, String(result));
|
||||
}
|
||||
},
|
||||
|
||||
@@ -223,5 +239,42 @@ Page({
|
||||
}
|
||||
const precision = 10000000000;
|
||||
return Math.round(result * precision) / precision;
|
||||
},
|
||||
|
||||
// 历史记录相关方法
|
||||
addToHistory(expression: string, result: string) {
|
||||
const list = this.data.historyList.slice();
|
||||
list.unshift({ expression, result });
|
||||
// 最多保留50条
|
||||
if (list.length > 50) list.length = 50;
|
||||
this.setData({ historyList: list });
|
||||
wx.setStorageSync('CALC_HISTORY', list);
|
||||
},
|
||||
|
||||
toggleHistory() {
|
||||
this.setData({ showHistory: !this.data.showHistory });
|
||||
},
|
||||
|
||||
clearHistory() {
|
||||
this.setData({ historyList: [] });
|
||||
wx.removeStorageSync('CALC_HISTORY');
|
||||
},
|
||||
|
||||
useHistoryResult(e: any) {
|
||||
const result = e.currentTarget.dataset.result;
|
||||
this.setData({
|
||||
displayValue: String(result),
|
||||
showHistory: false,
|
||||
waitingForSecondOperand: true
|
||||
});
|
||||
},
|
||||
|
||||
copyResult() {
|
||||
wx.setClipboardData({
|
||||
data: this.data.displayValue,
|
||||
success: () => {
|
||||
wx.showToast({ title: '已复制', icon: 'success', duration: 1000 });
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user