Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/monaco/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from '@opensumi/ide-core-browser';
import { IConfigurationService } from '@opensumi/monaco-editor-core/esm/vs/platform/configuration/common/configuration';

import { MergeEditorService } from './contrib/merge-editor/merge-editor.service';
import { ConfigurationService, MonacoContextKeyService } from './monaco.context-key.service';
import { MonacoClientContribution } from './monaco.contribution';
import MonacoServiceImpl from './monaco.service';
Expand Down
41 changes: 16 additions & 25 deletions packages/monaco/src/browser/monaco.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from '@opensumi/ide-core-browser';
import { IMergeEditorEditor } from '@opensumi/ide-core-browser/lib/monaco/merge-editor-widget';
import { KeyCodeChord } from '@opensumi/monaco-editor-core/esm/vs/base/common/keybindings';
import { IDisposable } from '@opensumi/monaco-editor-core/esm/vs/base/common/lifecycle';
import { IEditorConstructionOptions } from '@opensumi/monaco-editor-core/esm/vs/editor/browser/config/editorConfiguration';
import {
IDiffEditorConstructionOptions,
Expand All @@ -18,6 +17,7 @@ import {
import { ShowLightbulbIconMode } from '@opensumi/monaco-editor-core/esm/vs/editor/common/config/editorOptions';
import { Range } from '@opensumi/monaco-editor-core/esm/vs/editor/editor.main';
import { IStandaloneEditorConstructionOptions } from '@opensumi/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor';
import { StandaloneKeybindingService } from '@opensumi/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';

import { MonacoService } from '../common';

Expand Down Expand Up @@ -146,38 +146,15 @@ export default class MonacoServiceImpl extends Disposable implements MonacoServi
}

private overrideMonacoKeybindingService(editor: IEditorType) {
this.removeMonacoKeybindingListener(editor);
this.overrideKeybindingResolver(editor);
}

/**
* 移除 Monaco 中默认的快捷键
* 防止用户修改编辑器快捷键后依然会命中默认的快捷键
* @param editor
*/
private removeMonacoKeybindingListener(editor: IEditorType) {
let keydownListener: IDisposable | undefined;
const keybindingService = editor['_standaloneKeybindingService'];
if (!keybindingService) {
return;
}
for (const listener of keybindingService._store._toDispose) {
if ('_type' in listener && listener['_type'] === 'keydown') {
keydownListener = listener;
break;
}
}
if (keydownListener) {
keydownListener.dispose();
}
}

/**
* 重载 Monaco 中 `_standaloneKeybindingService` 对应处理快捷键及快捷键事件的方法
* @param editor
*/
private overrideKeybindingResolver(editor: IEditorType) {
const keybindingService = editor['_standaloneKeybindingService'];
const keybindingService = editor['_standaloneKeybindingService'] as StandaloneKeybindingService;
if (!keybindingService) {
return;
}
Expand All @@ -194,6 +171,20 @@ export default class MonacoServiceImpl extends Disposable implements MonacoServi
).toKeybinding();
return new MonacoResolvedKeybinding(MonacoResolvedKeybinding.keySequence(keybinding), this.keybindingRegistry);
};

const oldLookup = keybindingService.lookupKeybinding;
keybindingService.lookupKeybinding = (commandId: string, context) => {
const resolvedKeybinding = oldLookup.call(keybindingService, commandId, context);
if (resolvedKeybinding) {
return resolvedKeybinding;
}

const keybindings = this.keybindingRegistry.getKeybindingsForCommand(commandId);

if (keybindings && keybindings.length) {
return new MonacoResolvedKeybinding(keybindings[0].resolved!, this.keybindingRegistry);
}
};
}

public registerOverride(serviceName: ServiceNames, service: any) {
Expand Down