Skip to content

Commit 1d4f6da

Browse files
authored
fix: enable the editor to save viewState (#696)
1 parent ee1edf3 commit 1d4f6da

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import 'reflect-metadata';
2+
import { container } from 'tsyringe';
3+
import { EditorService, StatusBarService, BuiltinService } from 'mo/services';
4+
import { EditorController } from '../editor';
5+
import { editor as MonacoEditor, IDisposable, Position } from 'mo/monaco';
6+
7+
const editorController = container.resolve(EditorController);
8+
const editorService = container.resolve(EditorService);
9+
const statusBarService = container.resolve(StatusBarService);
10+
const builtinService = container.resolve(BuiltinService);
11+
12+
describe('The ediotr controller', () => {
13+
test('The initEditorEvents method', () => {
14+
const editorInstance = {} as MonacoEditor.IStandaloneCodeEditor;
15+
const position = { lineNumber: 1, column: 1 } as Position;
16+
let contentListener;
17+
let focusListener;
18+
let cursorListener;
19+
let blurListener;
20+
21+
editorInstance.onDidChangeModelContent = jest.fn((listener) => {
22+
contentListener = listener;
23+
return {} as IDisposable;
24+
});
25+
editorInstance.onDidFocusEditorText = jest.fn((listener) => {
26+
focusListener = listener;
27+
return {} as IDisposable;
28+
});
29+
editorInstance.onDidChangeCursorSelection = jest.fn((listener) => {
30+
cursorListener = listener;
31+
return {} as IDisposable;
32+
});
33+
editorInstance.onDidBlurEditorText = jest.fn((listener) => {
34+
blurListener = listener;
35+
return {} as IDisposable;
36+
});
37+
editorInstance.getPosition = jest.fn(() => position);
38+
39+
const testTab = {
40+
id: 'testTab',
41+
name: 'testTab',
42+
};
43+
editorService.open(testTab);
44+
const { current } = editorService.getState();
45+
editorController.initEditorEvents(editorInstance, current?.id!);
46+
47+
// focus
48+
focusListener?.();
49+
expect(editorService.getState().current?.tab?.id).toEqual(testTab.id);
50+
51+
// change content
52+
editorInstance.getModel = jest.fn(() => {
53+
return { getValue: () => 'newValue' } as MonacoEditor.ITextModel;
54+
});
55+
contentListener?.();
56+
expect(editorService.getState().current?.tab?.data?.value).toEqual(
57+
'newValue'
58+
);
59+
60+
// change cursor
61+
const { STATUS_EDITOR_INFO } = builtinService.getModules();
62+
statusBarService.setState({ rightItems: [STATUS_EDITOR_INFO] });
63+
cursorListener?.();
64+
expect(statusBarService.getState().rightItems[0]?.data).toEqual({
65+
ln: 1,
66+
col: 1,
67+
});
68+
69+
// blur
70+
const viewState = {
71+
viewState: { scrollTop: 10 },
72+
} as MonacoEditor.ICodeEditorViewState;
73+
editorInstance.saveViewState = jest.fn(() => viewState);
74+
blurListener?.();
75+
expect(
76+
editorController.getViewState(current?.tab?.id?.toString()!)
77+
).toEqual(viewState);
78+
79+
// reset services
80+
statusBarService.reset();
81+
editorService.setState({ current: null, groups: [] });
82+
});
83+
});

src/controller/editor.tsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export interface IEditorController extends Partial<Controller> {
4646
onClickActions: (action: IEditorActionsProps) => void;
4747
onUpdateEditorIns?: (editorInstance: any, groupId: UniqueId) => void;
4848
onPaneSizeChange?: (newSize: number[]) => void;
49+
initEditorEvents?: (
50+
editorInstance: MonacoEditor.IStandaloneCodeEditor,
51+
groupId: UniqueId
52+
) => void;
53+
getViewState?: (id: UniqueId) => MonacoEditor.ICodeEditorViewState;
4954
}
5055
@singleton()
5156
export class EditorController extends Controller implements IEditorController {
@@ -239,7 +244,7 @@ export class EditorController extends Controller implements IEditorController {
239244
this.layoutService.setGroupSplitSize(newSize);
240245
};
241246

242-
private initEditorEvents(
247+
public initEditorEvents(
243248
editorInstance: MonacoEditor.IStandaloneCodeEditor,
244249
groupId: UniqueId
245250
) {
@@ -273,8 +278,21 @@ export class EditorController extends Controller implements IEditorController {
273278
editorInstance.onDidChangeCursorSelection(() => {
274279
this.updateEditorLineColumnInfo(editorInstance);
275280
});
281+
282+
editorInstance.onDidBlurEditorText(() => {
283+
const { current } = this.editorService.getState();
284+
const tab = current?.tab;
285+
if (tab?.id) {
286+
const viewState = editorInstance?.saveViewState();
287+
this.editorStates.set(tab.id?.toString(), viewState);
288+
}
289+
});
276290
}
277291

292+
public getViewState = (id: UniqueId) => {
293+
return this.editorStates.get(id);
294+
};
295+
278296
/**
279297
* Called when Editor props changed
280298
*/

0 commit comments

Comments
 (0)