Skip to content

Commit 31ec99d

Browse files
committed
[vue3] opt 优化多行输入时光标定位 tzfun#110
1 parent 58db9e2 commit 31ec99d

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/Terminal.vue

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,6 @@ const _printHelp = (regExp: RegExp, srcStr: string) => {
952952
}
953953
954954
const _inputEnter = (e: KeyboardEvent) => {
955-
e.preventDefault()
956955
if (e.ctrlKey) {
957956
if (command.value.length > 0) {
958957
let cursorIdx = cursorConf.idx
@@ -967,18 +966,6 @@ const _inputEnter = (e: KeyboardEvent) => {
967966
})
968967
}
969968
} else {
970-
// 因无法阻止回车输入,这里手动删掉前后一个回车
971-
let cursorIdx = terminalCmdInputRef.value.selectionStart
972-
let enterIdx = -1
973-
if (command.value[cursorIdx] == '\n') {
974-
enterIdx = cursorIdx
975-
} else if (command.value[cursorIdx - 1] == '\n') {
976-
enterIdx = cursorIdx - 1
977-
}
978-
979-
if (enterIdx >= 0) {
980-
command.value = command.value.substring(0, enterIdx) + command.value.substring(enterIdx + 1)
981-
}
982969
_execute()
983970
}
984971
}
@@ -1290,7 +1277,6 @@ const _calculateCursorPos = (cmdStr?: string) => {
12901277
_calculateByteLen()
12911278
12921279
if (idx < 0 || idx >= cmd.length) {
1293-
console.debug(`reset cursor, idx: ${idx}, cmd.length: ${cmd.length}`)
12941280
_resetCursorPos()
12951281
return
12961282
}
@@ -1485,11 +1471,17 @@ const _onInputKeydown = (e: KeyboardEvent) => {
14851471
} else if (key === 'arrowright') {
14861472
_checkInputCursor()
14871473
_cursorGoRight()
1474+
} else if (key === 'enter') {
1475+
e.preventDefault()
14881476
}
14891477
}
14901478
14911479
const _onInputKeyup = (e: KeyboardEvent) => {
14921480
let key = e.key.toLowerCase()
1481+
if (key === 'enter') {
1482+
e.preventDefault()
1483+
return
1484+
}
14931485
let code = e.code.toLowerCase()
14941486
if (key === 'home' || key === 'end' || code === 'altleft' || code === 'metaleft' || code === 'controlleft'
14951487
|| ((e.ctrlKey || e.metaKey || e.altKey) && (key === 'arrowright' || key === 'arrowleft'))) {

test/App.vue

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ const terminals = ref<Array<any>>([
6060
}
6161
])
6262
63-
const onExecCmd = (key: string, command: Command, success: SuccessFunc, failed: FailedFunc, name: string) => {
63+
const onExecCmd = (key: string, command: string, success: SuccessFunc, failed: FailedFunc, name: string) => {
64+
console.log(`Executed command is '${command}'`)
6465
if (key === 'list') {
6566
success("hello")
6667
TerminalApi.pushMessage(name, {
@@ -311,13 +312,25 @@ const onClick = (key: string, name: string) => {
311312
}
312313
}
313314
315+
const textAreaKeyDown = (e: KeyboardEvent) => {
316+
console.log(e)
317+
if (e.key === 'Enter') {
318+
e.preventDefault()
319+
}
320+
}
321+
322+
const textAreaKeyEnter = () => {
323+
console.log("input enter")
324+
}
314325
</script>
315326
<template>
316327
<div id="app">
317328

318329
<button @click="getCommand">get command</button>
319330
<button @click="setCommand">set command</button>
320-
<textarea v-model="testInputValue"/>
331+
<textarea v-model="testInputValue"
332+
@keydown="textAreaKeyDown"
333+
@keyup.enter="textAreaKeyEnter"/>
321334

322335
<!-- <div style="width: 700px;height: 400px;margin-left: 150px;margin-top: 300px">-->
323336
<!-- <terminal-->

0 commit comments

Comments
 (0)