| 1 |
/** |
| 2 |
* Copyright (c) 2016 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IRenderService } from 'browser/services/Services'; |
| 7 |
import { IBufferService, ICoreService, IOptionsService } from 'common/services/Services'; |
| 8 |
import { C0 } from 'common/data/EscapeSequences'; |
| 9 |
|
| 10 |
interface IPosition { |
| 11 |
start: number; |
| 12 |
end: number; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Encapsulates the logic for handling compositionstart, compositionupdate and compositionend |
| 17 |
* events, displaying the in-progress composition to the UI and forwarding the final composition |
| 18 |
* to the handler. |
| 19 |
*/ |
| 20 |
export class CompositionHelper { |
| 21 |
/** |
| 22 |
* Whether input composition is currently happening, eg. via a mobile keyboard, speech input or |
| 23 |
* IME. This variable determines whether the compositionText should be displayed on the UI. |
| 24 |
*/ |
| 25 |
private _isComposing: boolean; |
| 26 |
public get isComposing(): boolean { return this._isComposing; } |
| 27 |
|
| 28 |
/** |
| 29 |
* The position within the input textarea's value of the current composition. |
| 30 |
*/ |
| 31 |
private _compositionPosition: IPosition; |
| 32 |
|
| 33 |
/** |
| 34 |
* Whether a composition is in the process of being sent, setting this to false will cancel any |
| 35 |
* in-progress composition. |
| 36 |
*/ |
| 37 |
private _isSendingComposition: boolean; |
| 38 |
|
| 39 |
/** |
| 40 |
* Data already sent due to keydown event. |
| 41 |
*/ |
| 42 |
private _dataAlreadySent: string; |
| 43 |
|
| 44 |
constructor( |
| 45 |
private readonly _textarea: HTMLTextAreaElement, |
| 46 |
private readonly _compositionView: HTMLElement, |
| 47 |
@IBufferService private readonly _bufferService: IBufferService, |
| 48 |
@IOptionsService private readonly _optionsService: IOptionsService, |
| 49 |
@ICoreService private readonly _coreService: ICoreService, |
| 50 |
@IRenderService private readonly _renderService: IRenderService |
| 51 |
) { |
| 52 |
this._isComposing = false; |
| 53 |
this._isSendingComposition = false; |
| 54 |
this._compositionPosition = { start: 0, end: 0 }; |
| 55 |
this._dataAlreadySent = ''; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Handles the compositionstart event, activating the composition view. |
| 60 |
*/ |
| 61 |
public compositionstart(): void { |
| 62 |
this._isComposing = true; |
| 63 |
this._compositionPosition.start = this._textarea.value.length; |
| 64 |
this._compositionView.textContent = ''; |
| 65 |
this._dataAlreadySent = ''; |
| 66 |
this._compositionView.classList.add('active'); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Handles the compositionupdate event, updating the composition view. |
| 71 |
* @param ev The event. |
| 72 |
*/ |
| 73 |
public compositionupdate(ev: Pick<CompositionEvent, 'data'>): void { |
| 74 |
this._compositionView.textContent = ev.data; |
| 75 |
this.updateCompositionElements(); |
| 76 |
setTimeout(() => { |
| 77 |
this._compositionPosition.end = this._textarea.value.length; |
| 78 |
}, 0); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Handles the compositionend event, hiding the composition view and sending the composition to |
| 83 |
* the handler. |
| 84 |
*/ |
| 85 |
public compositionend(): void { |
| 86 |
this._finalizeComposition(true); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Handles the keydown event, routing any necessary events to the CompositionHelper functions. |
| 91 |
* @param ev The keydown event. |
| 92 |
* @returns Whether the Terminal should continue processing the keydown event. |
| 93 |
*/ |
| 94 |
public keydown(ev: KeyboardEvent): boolean { |
| 95 |
if (this._isComposing || this._isSendingComposition) { |
| 96 |
if (ev.keyCode === 20 || ev.keyCode === 229) { |
| 97 |
// 20 is CapsLock, 229 is Enter |
| 98 |
// Continue composing if the keyCode is the "composition character" |
| 99 |
return false; |
| 100 |
} |
| 101 |
if (ev.keyCode === 16 || ev.keyCode === 17 || ev.keyCode === 18) { |
| 102 |
// Continue composing if the keyCode is a modifier key |
| 103 |
return false; |
| 104 |
} |
| 105 |
// Finish composition immediately. This is mainly here for the case where enter is |
| 106 |
// pressed and the handler needs to be triggered before the command is executed. |
| 107 |
this._finalizeComposition(false); |
| 108 |
} |
| 109 |
|
| 110 |
if (ev.keyCode === 229) { |
| 111 |
// If the "composition character" is used but gets to this point it means a non-composition |
| 112 |
// character (eg. numbers and punctuation) was pressed when the IME was active. |
| 113 |
this._handleAnyTextareaChanges(); |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Finalizes the composition, resuming regular input actions. This is called when a composition |
| 122 |
* is ending. |
| 123 |
* @param waitForPropagation Whether to wait for events to propagate before sending |
| 124 |
* the input. This should be false if a non-composition keystroke is entered before the |
| 125 |
* compositionend event is triggered, such as enter, so that the composition is sent before |
| 126 |
* the command is executed. |
| 127 |
*/ |
| 128 |
private _finalizeComposition(waitForPropagation: boolean): void { |
| 129 |
this._compositionView.classList.remove('active'); |
| 130 |
this._isComposing = false; |
| 131 |
|
| 132 |
if (!waitForPropagation) { |
| 133 |
// Cancel any delayed composition send requests and send the input immediately. |
| 134 |
this._isSendingComposition = false; |
| 135 |
const input = this._textarea.value.substring(this._compositionPosition.start, this._compositionPosition.end); |
| 136 |
this._coreService.triggerDataEvent(input, true); |
| 137 |
} else { |
| 138 |
// Make a deep copy of the composition position here as a new compositionstart event may |
| 139 |
// fire before the setTimeout executes. |
| 140 |
const currentCompositionPosition = { |
| 141 |
start: this._compositionPosition.start, |
| 142 |
end: this._compositionPosition.end |
| 143 |
}; |
| 144 |
|
| 145 |
// Since composition* events happen before the changes take place in the textarea on most |
| 146 |
// browsers, use a setTimeout with 0ms time to allow the native compositionend event to |
| 147 |
// complete. This ensures the correct character is retrieved. |
| 148 |
// This solution was used because: |
| 149 |
// - The compositionend event's data property is unreliable, at least on Chromium |
| 150 |
// - The last compositionupdate event's data property does not always accurately describe |
| 151 |
// the character, a counter example being Korean where an ending consonsant can move to |
| 152 |
// the following character if the following input is a vowel. |
| 153 |
this._isSendingComposition = true; |
| 154 |
setTimeout(() => { |
| 155 |
// Ensure that the input has not already been sent |
| 156 |
if (this._isSendingComposition) { |
| 157 |
this._isSendingComposition = false; |
| 158 |
let input; |
| 159 |
// Add length of data already sent due to keydown event, |
| 160 |
// otherwise input characters can be duplicated. (Issue #3191) |
| 161 |
currentCompositionPosition.start += this._dataAlreadySent.length; |
| 162 |
if (this._isComposing) { |
| 163 |
// Use the start position of the new composition to get the string |
| 164 |
// if a new composition has started. |
| 165 |
input = this._textarea.value.substring(currentCompositionPosition.start, this._compositionPosition.start); |
| 166 |
} else { |
| 167 |
// Don't use the end position here in order to pick up any characters after the |
| 168 |
// composition has finished, for example when typing a non-composition character |
| 169 |
// (eg. 2) after a composition character. |
| 170 |
input = this._textarea.value.substring(currentCompositionPosition.start); |
| 171 |
} |
| 172 |
if (input.length > 0) { |
| 173 |
this._coreService.triggerDataEvent(input, true); |
| 174 |
} |
| 175 |
} |
| 176 |
}, 0); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Apply any changes made to the textarea after the current event chain is allowed to complete. |
| 182 |
* This should be called when not currently composing but a keydown event with the "composition |
| 183 |
* character" (229) is triggered, in order to allow non-composition text to be entered when an |
| 184 |
* IME is active. |
| 185 |
*/ |
| 186 |
private _handleAnyTextareaChanges(): void { |
| 187 |
const oldValue = this._textarea.value; |
| 188 |
setTimeout(() => { |
| 189 |
// Ignore if a composition has started since the timeout |
| 190 |
if (!this._isComposing) { |
| 191 |
const newValue = this._textarea.value; |
| 192 |
|
| 193 |
const diff = newValue.replace(oldValue, ''); |
| 194 |
|
| 195 |
this._dataAlreadySent = diff; |
| 196 |
|
| 197 |
if (newValue.length > oldValue.length) { |
| 198 |
this._coreService.triggerDataEvent(diff, true); |
| 199 |
} else if (newValue.length < oldValue.length) { |
| 200 |
this._coreService.triggerDataEvent(`${C0.DEL}`, true); |
| 201 |
} else if ((newValue.length === oldValue.length) && (newValue !== oldValue)) { |
| 202 |
this._coreService.triggerDataEvent(newValue, true); |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
}, 0); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Positions the composition view on top of the cursor and the textarea just below it (so the |
| 211 |
* IME helper dialog is positioned correctly). |
| 212 |
* @param dontRecurse Whether to use setTimeout to recursively trigger another update, this is |
| 213 |
* necessary as the IME events across browsers are not consistently triggered. |
| 214 |
*/ |
| 215 |
public updateCompositionElements(dontRecurse?: boolean): void { |
| 216 |
if (!this._isComposing) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
if (this._bufferService.buffer.isCursorInViewport) { |
| 221 |
const cursorX = Math.min(this._bufferService.buffer.x, this._bufferService.cols - 1); |
| 222 |
|
| 223 |
const cellHeight = this._renderService.dimensions.css.cell.height; |
| 224 |
const cursorTop = this._bufferService.buffer.y * this._renderService.dimensions.css.cell.height; |
| 225 |
const cursorLeft = cursorX * this._renderService.dimensions.css.cell.width; |
| 226 |
|
| 227 |
this._compositionView.style.left = cursorLeft + 'px'; |
| 228 |
this._compositionView.style.top = cursorTop + 'px'; |
| 229 |
this._compositionView.style.height = cellHeight + 'px'; |
| 230 |
this._compositionView.style.lineHeight = cellHeight + 'px'; |
| 231 |
this._compositionView.style.fontFamily = this._optionsService.rawOptions.fontFamily; |
| 232 |
this._compositionView.style.fontSize = this._optionsService.rawOptions.fontSize + 'px'; |
| 233 |
// Sync the textarea to the exact position of the composition view so the IME knows where the |
| 234 |
// text is. |
| 235 |
const compositionViewBounds = this._compositionView.getBoundingClientRect(); |
| 236 |
this._textarea.style.left = cursorLeft + 'px'; |
| 237 |
this._textarea.style.top = cursorTop + 'px'; |
| 238 |
// Ensure the text area is at least 1x1, otherwise certain IMEs may break |
| 239 |
this._textarea.style.width = Math.max(compositionViewBounds.width, 1) + 'px'; |
| 240 |
this._textarea.style.height = Math.max(compositionViewBounds.height, 1) + 'px'; |
| 241 |
this._textarea.style.lineHeight = compositionViewBounds.height + 'px'; |
| 242 |
} |
| 243 |
|
| 244 |
if (!dontRecurse) { |
| 245 |
setTimeout(() => this.updateCompositionElements(true), 0); |
| 246 |
} |
| 247 |
} |
| 248 |
} |