Skip to content

Commit 0407cfc

Browse files
committed
pref: cli support chinese ime
1 parent 10fcc87 commit 0407cfc

File tree

1 file changed

+77
-34
lines changed

1 file changed

+77
-34
lines changed

frontend/src/components/content_value/ContentCli.vue

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ let termInst = null
2727
*/
2828
let fitAddonInst = null
2929
30+
const nonPrintableKeys = [
31+
'F1',
32+
'F2',
33+
'F3',
34+
'F4',
35+
'F5',
36+
'F6',
37+
'F7',
38+
'F8',
39+
'F9',
40+
'F10',
41+
'F11',
42+
'F12',
43+
'Shift',
44+
'CapsLock',
45+
'Tab',
46+
'Escape',
47+
'ScrollLock',
48+
'Pause',
49+
'Insert',
50+
'NumLock',
51+
'ContextMenu',
52+
]
53+
3054
/**
3155
*
3256
* @return {{fitAddon: xterm-addon-fit.FitAddon, term: Terminal}}
@@ -137,6 +161,9 @@ let inputCursor = 0
137161
const inputHistory = []
138162
let historyIndex = 0
139163
let waitForOutput = false
164+
let isComposingBefore = false
165+
let ignore229 = false
166+
140167
const onTermData = (data) => {
141168
if (termInst == null) {
142169
return
@@ -145,10 +172,6 @@ const onTermData = (data) => {
145172
if (data) {
146173
const cc = data.charCodeAt(0)
147174
switch (cc) {
148-
case 127: // backspace
149-
deleteInput(true)
150-
return
151-
152175
case 13: // enter
153176
// try to process local command first
154177
switch (getCurrentInput()) {
@@ -163,33 +186,9 @@ const onTermData = (data) => {
163186
flushTermInput()
164187
return
165188
}
166-
167-
case 27:
168-
switch (data.substring(1)) {
169-
case '[A': // arrow up
170-
changeHistory(true)
171-
return
172-
case '[B': // arrow down
173-
changeHistory(false)
174-
return
175-
case '[C': // arrow right ->
176-
moveInputCursor(1)
177-
return
178-
case '[D': // arrow left <-
179-
moveInputCursor(-1)
180-
return
181-
case '[3~': // del
182-
deleteInput(false)
183-
return
184-
}
185-
186-
case 9: // tab
187-
return
188189
}
190+
updateInput(data)
189191
}
190-
191-
updateInput(data)
192-
// term.write(data)
193192
}
194193
195194
/**
@@ -198,6 +197,19 @@ const onTermData = (data) => {
198197
* @return {boolean}
199198
*/
200199
const onTermKey = (e) => {
200+
// ignore first input when leave composing
201+
if (!e.isComposing) {
202+
if (isComposingBefore) {
203+
ignore229 = true
204+
isComposingBefore = false
205+
} else {
206+
ignore229 = false
207+
}
208+
} else if (e.isComposing) {
209+
ignore229 = true
210+
isComposingBefore = true
211+
}
212+
201213
if (e.type === 'keydown') {
202214
if (e.ctrlKey) {
203215
switch (e.key) {
@@ -250,6 +262,12 @@ const onTermKey = (e) => {
250262
replaceTermInput()
251263
newInputLine()
252264
return false
265+
266+
case 'c': // interrupt and new line
267+
termInst.writeln('')
268+
replaceTermInput()
269+
newInputLine()
270+
return false
253271
}
254272
// block all ctrl key combinations input
255273
return false
@@ -258,13 +276,40 @@ const onTermKey = (e) => {
258276
case 'Home': // move to head of line
259277
moveInputCursorTo(0)
260278
return false
279+
261280
case 'End': // move to tail of line
262281
moveInputCursorTo(Number.MAX_SAFE_INTEGER)
263282
return false
264-
case ' ': // replace space
265-
e.preventDefault()
266-
updateInput(' ')
283+
284+
case 'Backspace':
285+
deleteInput(true)
267286
return false
287+
288+
case 'ArrowUp': // arrow up
289+
case 'PageUp':
290+
changeHistory(true)
291+
return false
292+
case 'ArrowDown': // arrow down
293+
case 'PageDown':
294+
changeHistory(false)
295+
return false
296+
case 'ArrowRight': // arrow right ->
297+
moveInputCursor(1)
298+
return false
299+
case 'ArrowLeft': // arrow left <-
300+
moveInputCursor(-1)
301+
return false
302+
case 'Delete': // del
303+
deleteInput(false)
304+
return false
305+
306+
default:
307+
if (e.altKey || e.altGraphKey || e.ctrlKey || e.metaKey || nonPrintableKeys.includes(e.key)) {
308+
return false
309+
} else if (e.keyCode === 229 && !ignore229) {
310+
updateInput(e.key)
311+
return false
312+
}
268313
}
269314
}
270315
}
@@ -326,8 +371,6 @@ const updateInput = (data) => {
326371
if (data == null || data.length <= 0) {
327372
return
328373
}
329-
// replace &nbsp;(Non-Breaking Space) with normal blank space
330-
data = data.replace(/\u00A0/g, ' ')
331374
332375
if (termInst == null) {
333376
return

0 commit comments

Comments
 (0)