Skip to content

Commit e014402

Browse files
authored
fix: monaco caused by the failure of window.addEventListener('compositionstart', () => {}) (#4508)
* fix: monaco caused by the failure of window.addEventListener('compositionstart', () => {}) * fix: monaco caused by the failure of window.addEventListener('compositionstart', () => {}) * fix: monaco caused by the failure of window.addEventListener('compositionstart', () => {}) * fix: monaco caused by the failure of window.addEventListener('compositionstart', () => {})
1 parent ef161de commit e014402

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

packages/core-browser/src/bootstrap/app.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,12 @@ export class ClientApp implements IClientApp, IDisposable {
487487
protected registerEventListeners(): void {
488488
window.addEventListener('beforeunload', this._handleBeforeUpload);
489489
window.addEventListener('unload', this._handleUnload);
490-
491490
window.addEventListener('resize', this._handleResize);
491+
492492
// 处理中文输入回退时可能出现多个光标问题
493493
// https://github.com/eclipse-theia/theia/pull/6673
494-
window.addEventListener('compositionstart', this._handleCompositionstart);
495-
window.addEventListener('compositionend', this._handleCompositionend);
494+
window.addEventListener('compositionstart', this.keybindingService?.handleCompositionStart);
495+
window.addEventListener('compositionend', this.keybindingService?.handleCompositionEnd);
496496
window.addEventListener('keydown', this._handleKeydown, true);
497497
window.addEventListener('keyup', this._handleKeyup, true);
498498

@@ -589,8 +589,8 @@ export class ClientApp implements IClientApp, IDisposable {
589589
window.removeEventListener('beforeunload', this._handleBeforeUpload);
590590
window.removeEventListener('unload', this._handleUnload);
591591
window.removeEventListener('resize', this._handleResize);
592-
window.removeEventListener('compositionstart', this._handleCompositionstart);
593-
window.removeEventListener('compositionend', this._handleCompositionend);
592+
window.removeEventListener('compositionstart', this.keybindingService?.handleCompositionStart);
593+
window.removeEventListener('compositionend', this.keybindingService?.handleCompositionEnd);
594594
window.removeEventListener('keydown', this._handleKeydown, true);
595595
if (isOSX) {
596596
document.body.removeEventListener('wheel', this._handleWheel);
@@ -660,7 +660,7 @@ export class ClientApp implements IClientApp, IDisposable {
660660
};
661661

662662
private _handleKeydown = (event: any) => {
663-
if (event && event.target!.name !== NO_KEYBINDING_NAME && !this._inComposition) {
663+
if (event && event.target!.name !== NO_KEYBINDING_NAME) {
664664
this.keybindingService.run(event);
665665
}
666666
};
@@ -669,14 +669,6 @@ export class ClientApp implements IClientApp, IDisposable {
669669
this.keybindingService.resolveModifierKey(event);
670670
};
671671

672-
private _handleCompositionstart = () => {
673-
this._inComposition = true;
674-
};
675-
676-
private _handleCompositionend = () => {
677-
this._inComposition = false;
678-
};
679-
680672
private _handleWheel = () => {
681673
// 屏蔽在OSX系统浏览器中由于滚动导致的前进后退事件
682674
};

packages/core-browser/src/keybinding/keybinding.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ export interface KeybindingService {
190190
* @param when
191191
*/
192192
convertMonacoWhen(when: any): string;
193+
handleCompositionStart(): void;
194+
handleCompositionEnd(): void;
193195
}
194196

195197
@Injectable()
@@ -230,6 +232,8 @@ export class KeybindingRegistryImpl implements KeybindingRegistry, KeybindingSer
230232
@Autowired(IStatusBarService)
231233
protected readonly statusBar: IStatusBarService;
232234

235+
private _inComposition = false;
236+
233237
public async initialize(): Promise<void> {
234238
await this.keyboardLayoutService.initialize();
235239
this.keyboardLayoutService.onKeyboardLayoutChanged(() => {
@@ -243,6 +247,14 @@ export class KeybindingRegistryImpl implements KeybindingRegistry, KeybindingSer
243247

244248
protected keybindingsChanged = new Emitter<{ affectsCommands: string[] }>();
245249

250+
public handleCompositionStart = () => {
251+
this._inComposition = true;
252+
};
253+
254+
public handleCompositionEnd = () => {
255+
this._inComposition = false;
256+
};
257+
246258
/**
247259
* 由于不同的键盘布局发生更改时触发的事件。
248260
*/
@@ -807,6 +819,9 @@ export class KeybindingRegistryImpl implements KeybindingRegistry, KeybindingSer
807819
* @param event 键盘事件
808820
*/
809821
public run(event: KeyboardEvent): void {
822+
if (this._inComposition) {
823+
return;
824+
}
810825
if (event.defaultPrevented) {
811826
return;
812827
}

packages/monaco/src/browser/monaco.service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Disposable,
44
ILogger,
55
KeybindingRegistry,
6+
KeybindingService,
67
MonacoOverrideServiceRegistry,
78
ServiceNames,
89
} from '@opensumi/ide-core-browser';
@@ -44,6 +45,9 @@ export default class MonacoServiceImpl extends Disposable implements MonacoServi
4445
@Autowired(KeybindingRegistry)
4546
private readonly keybindingRegistry: KeybindingRegistry;
4647

48+
@Autowired(KeybindingService)
49+
protected readonly keybindingService: KeybindingService;
50+
4751
@Autowired(ILogger)
4852
private readonly logger: ILogger;
4953

@@ -105,6 +109,19 @@ export default class MonacoServiceImpl extends Disposable implements MonacoServi
105109
);
106110
}
107111

112+
private doAddCompositionEventListener(editor: ICodeEditor) {
113+
this.addDispose(
114+
editor.onDidCompositionStart((e) => {
115+
this.keybindingService?.handleCompositionStart();
116+
}),
117+
);
118+
this.addDispose(
119+
editor.onDidCompositionEnd((e) => {
120+
this.keybindingService?.handleCompositionEnd();
121+
}),
122+
);
123+
}
124+
108125
private addClickEventListener(editor: IEditorType) {
109126
if (isDiffEditor(editor)) {
110127
const originalEditor = editor.getOriginalEditor();
@@ -114,6 +131,7 @@ export default class MonacoServiceImpl extends Disposable implements MonacoServi
114131
this.doAddClickEventListener(modifiedEditor);
115132
} else {
116133
this.doAddClickEventListener(editor as ICodeEditor);
134+
this.doAddCompositionEventListener(editor as ICodeEditor);
117135
}
118136
}
119137

0 commit comments

Comments
 (0)