Skip to content

Commit a88a71e

Browse files
authored
fix: make Keybinding work after reloading Molecule (#686)
1 parent c230885 commit a88a71e

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

src/common/__tests__/dom.test.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { findParentByClassName, getPositionByPlacement } from '../dom';
1+
import {
2+
findParentByClassName,
3+
getPositionByPlacement,
4+
getElementClientCenter,
5+
} from '../dom';
26

37
describe('Test functions in dom.ts', () => {
48
test('The getPositionByPlacement function', () => {
@@ -47,4 +51,23 @@ describe('Test functions in dom.ts', () => {
4751
findParentByClassName(1, 'test');
4852
}).toThrowError();
4953
});
54+
55+
test('The getElementClientCenter function', () => {
56+
const elem = document.createElement('div');
57+
const domRect = {
58+
x: 10,
59+
y: 10,
60+
width: 10,
61+
height: 10,
62+
top: 10,
63+
right: 10,
64+
bottom: 10,
65+
left: 10,
66+
} as DOMRect;
67+
elem.getBoundingClientRect = jest.fn(() => domRect);
68+
69+
const center = getElementClientCenter(elem);
70+
expect(center.x).toEqual(15);
71+
expect(center.y).toEqual(15);
72+
});
5073
});

src/common/__tests__/utils.test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { normalizeFlattedObject, colorLightOrDark } from '../utils';
1+
import {
2+
normalizeFlattedObject,
3+
colorLightOrDark,
4+
cloneInstance,
5+
} from '../utils';
26

37
describe('Test Utils', () => {
48
test('The normalizeFlattedObject function', () => {
@@ -40,4 +44,10 @@ describe('Test Utils', () => {
4044
expect(colorLightOrDark('#000')).toBe('dark');
4145
expect(colorLightOrDark('rgb(255,255,255)')).toBe('light');
4246
});
47+
48+
test('The cloneInstance function', () => {
49+
const obj1 = { name: 'test' };
50+
const obj2 = cloneInstance(obj1);
51+
expect(obj1.name).toEqual(obj2.name);
52+
});
4353
});

src/provider/molecule.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export class MoleculeProvider extends Component<IMoleculeProps> {
4848
}
4949

5050
componentDidMount() {
51+
// The monacoService needs to be initialized each time the MoleculeProvider is loaded to
52+
// ensure that Keybinding events can be bound to the latest dom.
53+
this.monacoService.initWorkspace(this.layoutService.container!);
54+
5155
if (!this.extensionService.isLoaded()) {
5256
this.initialize();
5357
}
@@ -59,9 +63,6 @@ export class MoleculeProvider extends Component<IMoleculeProps> {
5963
const [languages, restExts] =
6064
this.extensionService.splitLanguagesExts(extensions);
6165

62-
// First to init the monacoService
63-
this.monacoService.initWorkspace(this.layoutService.container!);
64-
6566
// Molecule should load the language extensions first to
6667
// ensure that the custom language extensions is registered in localeService
6768
this.initLocaleExts(languages);

src/services/workbench/layoutService.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ export class LayoutService
8888
}
8989

9090
public get container() {
91-
if (!this._container) {
92-
this._container = document.getElementById(ID_APP) || document.body;
93-
}
91+
// Make sure to get the latest dom element.
92+
this._container = document.getElementById(ID_APP) || document.body;
9493
return this._container;
9594
}
9695

0 commit comments

Comments
 (0)