From 4e3383f15d925e350a33167665eb2aaf0f84c8ac Mon Sep 17 00:00:00 2001 From: Peng Lyu Date: Fri, 2 Mar 2018 16:43:09 -0800 Subject: [PATCH] Fix microsoft/monaco-editor#545. Composition data is wrong when typing numbers in Chinese. --- .../browser/controller/textAreaInput.ts | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/vs/editor/browser/controller/textAreaInput.ts b/src/vs/editor/browser/controller/textAreaInput.ts index d196d4f4e87..ea9cf23d22d 100644 --- a/src/vs/editor/browser/controller/textAreaInput.ts +++ b/src/vs/editor/browser/controller/textAreaInput.ts @@ -164,6 +164,26 @@ export class TextAreaInput extends Disposable { return [newState, typeInput]; }; + const compositionDataInValid = (locale: string): boolean => { + // https://github.com/Microsoft/monaco-editor/issues/339 + // Multi-part Japanese compositions reset cursor in Edge/IE, Chinese and Korean IME don't have this issue. + // The reason that we can't use this path for all CJK IME is IE and Edge behave differently when handling Korean IME, + // which breaks this path of code. + if (browser.isEdgeOrIE && locale === 'ja') { + return true; + } + + // https://github.com/Microsoft/monaco-editor/issues/545 + // On IE11, we can't trust composition data when typing Chinese as IE11 doesn't emit correct + // events when users type numbers in IME. + // Chinese: zh-Hans-CN, zh-Hans-SG, zh-Hant-TW, zh-Hant-HK + if (browser.isIE && locale.indexOf('zh-Han') === 0) { + return true; + } + + return false; + }; + this._register(dom.addDisposableListener(textArea.domNode, 'compositionupdate', (e: CompositionEvent) => { if (browser.isChromev56) { // See https://github.com/Microsoft/monaco-editor/issues/320 @@ -174,11 +194,7 @@ export class TextAreaInput extends Disposable { return; } - if (browser.isEdgeOrIE && e.locale === 'ja') { - // https://github.com/Microsoft/monaco-editor/issues/339 - // Multi-part Japanese compositions reset cursor in Edge/IE, Chinese and Korean IME don't have this issue. - // The reason that we can't use this path for all CJK IME is IE and Edge behave differently when handling Korean IME, - // which breaks this path of code. + if (compositionDataInValid(e.locale)) { const [newState, typeInput] = deduceInputFromTextAreaValue(/*couldBeEmojiInput*/false); this._textAreaState = newState; this._onType.fire(typeInput); @@ -193,13 +209,12 @@ export class TextAreaInput extends Disposable { })); this._register(dom.addDisposableListener(textArea.domNode, 'compositionend', (e: CompositionEvent) => { - if (browser.isEdgeOrIE && e.locale === 'ja') { + if (compositionDataInValid(e.locale)) { // https://github.com/Microsoft/monaco-editor/issues/339 const [newState, typeInput] = deduceInputFromTextAreaValue(/*couldBeEmojiInput*/false); this._textAreaState = newState; this._onType.fire(typeInput); - } - else { + } else { const [newState, typeInput] = deduceComposition(e.data); this._textAreaState = newState; this._onType.fire(typeInput); -- GitLab