优化子输入框历史记录功能

This commit is contained in:
fofolee 2022-04-20 13:00:33 +08:00
parent 22adc1549f
commit b33f4b16eb

View File

@ -60,7 +60,7 @@ export default {
subInputValue: "", subInputValue: "",
listener: null, listener: null,
history: [], history: [],
historyIdx: 1, historyIdx: null,
}; };
}, },
props: { props: {
@ -147,7 +147,7 @@ export default {
setSubInput(currentCommand) { setSubInput(currentCommand) {
this.fromUtools && utools.setExpendHeight(0); this.fromUtools && utools.setExpendHeight(0);
let matched = specialVars.subinput.match.exec(currentCommand.cmd); let matched = specialVars.subinput.match.exec(currentCommand.cmd);
let placeholder = matched[1] || "回车键执行命令,方向键上下切换历史"; let placeholder = matched[1] || "↩ 执行命令,↑↓ 切换历史";
utools.setSubInput(({ text }) => { utools.setSubInput(({ text }) => {
this.subInputValue = text; this.subInputValue = text;
}, placeholder); }, placeholder);
@ -158,6 +158,7 @@ export default {
this.subInputValue this.subInputValue
); );
this.history.push(this.subInputValue); this.history.push(this.subInputValue);
this.historyIdx = this.history.length;
utools.setSubInputValue(""); utools.setSubInputValue("");
this.fire(command); this.fire(command);
}; };
@ -173,20 +174,16 @@ export default {
break; break;
case 38: case 38:
if (!this.history.length) break; if (!this.history.length) break;
this.historyIdx = Math.min( this.historyIdx = Math.max(0, this.historyIdx - 1);
this.history.length + 1, utools.setSubInputValue(this.history[this.historyIdx] || "");
this.historyIdx + 1
);
utools.setSubInputValue(
this.history[this.history.length - this.historyIdx + 1]
);
break; break;
case 40: case 40:
if (this.historyIdx === 1) break; if (this.historyIdx === this.history.length) break;
this.historyIdx = Math.max(1, this.historyIdx - 1); this.historyIdx = Math.min(
utools.setSubInputValue( this.history.length - 1,
this.history[this.history.length - this.historyIdx] this.historyIdx + 1
); );
utools.setSubInputValue(this.history[this.historyIdx] || "");
default: default:
break; break;
} }