Skip to content

Commit bffffa4

Browse files
authored
feat(editor-moanco): add support for switching language (#3979)
1 parent 79b777e commit bffffa4

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import lazyMonacoContribution, { isLanguageRegistered } from './language/monaco.contribution.js';
1+
import lazyMonacoContribution, {
2+
isLanguageRegistered,
3+
apidomDefaults,
4+
} from './language/monaco.contribution.js';
25

36
const makeAfterLoad =
47
({ createData = {} } = {}) =>
5-
() => {
8+
(system) => {
69
if (isLanguageRegistered()) return;
710

811
lazyMonacoContribution({ createData });
12+
system.editorActions.setLanguage(apidomDefaults.getLanguageId());
913
};
1014

1115
export default makeAfterLoad;

src/plugins/editor-monaco/actions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export const EDITOR_SET_REQUEST_JUMP_TO_EDITOR_MARKER = 'editor_set_request_jump
1010
export const EDITOR_CLEAR_REQUEST_JUMP_TO_EDITOR_MARKER =
1111
'editor_clear_request_jump_to_editor_marker';
1212

13+
export const EDITOR_SET_LANGUAGE = 'editor_set_language';
14+
1315
export const updateEditorTheme = (theme = 'my-vs-dark') => {
1416
return {
1517
payload: theme,
@@ -61,3 +63,10 @@ export const clearRequestJumpToEditorMarker = () => {
6163
type: EDITOR_CLEAR_REQUEST_JUMP_TO_EDITOR_MARKER,
6264
};
6365
};
66+
67+
export const setLanguage = (languageId) => {
68+
return {
69+
payload: languageId,
70+
type: EDITOR_SET_LANGUAGE,
71+
};
72+
};

src/plugins/editor-monaco/components/MonacoEditor/MonacoEditorContainer.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const MonacoEditorContainer = ({ editorActions, editorSelectors, isReadOnly }) =
88
const jumpToMarker = editorSelectors.selectEditorJumpToMarker();
99
const requestJumpToMarker = editorSelectors.selectEditorRequestJumpToMarker();
1010
const value = editorSelectors.selectContent();
11+
const language = editorSelectors.selectLanguage();
1112

1213
const handleEditorDidMount = useCallback(
1314
(editor) => {
@@ -55,7 +56,7 @@ const MonacoEditorContainer = ({ editorActions, editorSelectors, isReadOnly }) =
5556

5657
return (
5758
<MonacoEditor
58-
language="apidom"
59+
language={language}
5960
theme={theme}
6061
value={value}
6162
isReadOnly={isReadOnly}
@@ -84,6 +85,7 @@ MonacoEditorContainer.propTypes = {
8485
clearRequestJumpToEditorMarker: PropTypes.func.isRequired,
8586
}).isRequired,
8687
editorSelectors: PropTypes.shape({
88+
selectLanguage: PropTypes.func.isRequired,
8789
selectContent: PropTypes.func.isRequired,
8890
selectEditorTheme: PropTypes.func.isRequired,
8991
selectEditorJumpToMarker: PropTypes.func.isRequired,

src/plugins/editor-monaco/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import EditorPaneBarBottomWrapper from './wrap-components/EditorPaneBarBottomWra
88
import {
99
updateEditorTheme,
1010
setMarkers,
11+
setLanguage,
1112
appendMarkers,
1213
clearMarkers,
1314
setJumpToEditorMarker,
@@ -21,6 +22,7 @@ import {
2122
selectMarkers,
2223
selectEditorJumpToMarker,
2324
selectEditorRequestJumpToMarker,
25+
selectLanguage,
2426
} from './selectors.js';
2527
import afterLoad from './after-load.js';
2628

@@ -50,13 +52,15 @@ const EditorMonacoPlugin = () => ({
5052
clearJumpToEditorMarker,
5153
setRequestJumpToEditorMarker,
5254
clearRequestJumpToEditorMarker,
55+
setLanguage,
5356
},
5457
reducers,
5558
selectors: {
5659
selectEditorTheme,
5760
selectMarkers,
5861
selectEditorJumpToMarker,
5962
selectEditorRequestJumpToMarker,
63+
selectLanguage,
6064
},
6165
},
6266
},

src/plugins/editor-monaco/reducers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
EDITOR_CLEAR_JUMP_TO_EDITOR_MARKER,
1010
EDITOR_SET_REQUEST_JUMP_TO_EDITOR_MARKER,
1111
EDITOR_CLEAR_REQUEST_JUMP_TO_EDITOR_MARKER,
12+
EDITOR_SET_LANGUAGE,
1213
} from './actions.js';
1314

1415
const reducers = {
@@ -41,6 +42,9 @@ const reducers = {
4142
[EDITOR_CLEAR_REQUEST_JUMP_TO_EDITOR_MARKER]: (state, action) => {
4243
return state.set('editorRequestJumpToMarker', action.payload);
4344
},
45+
[EDITOR_SET_LANGUAGE]: (state, action) => {
46+
return state.set('editorLanguage', action.payload);
47+
},
4448
};
4549

4650
export default reducers;

src/plugins/editor-monaco/selectors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ export const selectEditorJumpToMarker = createSelector(
1515
}
1616
);
1717

18+
export const selectLanguage = (state) => state.get('editorLanguage', 'plaintext');
19+
1820
export const selectEditorRequestJumpToMarker = (state) => state.get('editorRequestJumpToMarker');

0 commit comments

Comments
 (0)