Skip to content

Commit 52e76ce

Browse files
authored
Monaco Keyboard Navigation (#1393)
1 parent 39c451e commit 52e76ce

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

.storybook/addons/codeEditorAddon/editor.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
33

44
let debounce = (func, wait, immediate) => {
55
var timeout;
6-
return function() {
6+
return function () {
77
var context = this,
88
args = arguments;
9-
var later = function() {
9+
var later = function () {
1010
timeout = null;
1111
if (!immediate) func.apply(context, args);
1212
};
@@ -122,11 +122,18 @@ export class EditorElement extends LitElement {
122122
this.editor = monaco.editor.create(htmlElement, {
123123
model: this.currentEditorState.model,
124124
scrollBeyondLastLine: false,
125+
readOnly: true,
125126
minimap: {
126127
enabled: false
127128
}
128129
});
129130

131+
// Exit the current editor
132+
this.editor.addCommand(monaco.KeyCode.Escape, () => {
133+
this.editor.updateOptions({ readOnly: true });
134+
this.shadowRoot.getElementById(this.currentType).focus();
135+
});
136+
130137
const changeViewZones = () => {
131138
this.editor.changeViewZones(changeAccessor => {
132139
const domNode = document.createElement('div');
@@ -170,7 +177,10 @@ export class EditorElement extends LitElement {
170177
window.removeEventListener('resize', this.handleResize);
171178
}
172179

173-
showTab(type) {
180+
showTab(type, event) {
181+
this.editor.updateOptions({ readOnly: false });
182+
if (event && event.keyCode != 13) return;
183+
174184
this.currentType = type;
175185
if (this.files && typeof this.files[type] !== 'undefined') {
176186
this.currentEditorState.state = this.editor.saveViewState();
@@ -185,11 +195,16 @@ export class EditorElement extends LitElement {
185195

186196
render() {
187197
return html`
188-
<div class="root">
189-
<div class="tab-root">
198+
<div class="root" tabindex=0>
199+
<div class="tab-root" tabindex=0">
190200
${this.fileTypes.map(
191201
type => html`
192-
<div @click="${_ => this.showTab(type)}" class="tab ${type === this.currentType ? 'selected' : ''}">
202+
<div
203+
@keydown=${e => this.showTab(type, e)}
204+
tabindex=0
205+
@click="${_ => this.showTab(type)}"
206+
id="${type}"
207+
class="tab ${type === this.currentType ? 'selected' : ''}">
193208
${type}
194209
</div>
195210
`

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@
6161
<mgt-people show-presence></mgt-people>
6262
<mgt-todo></mgt-todo>
6363
</body>
64-
</html>
64+
</html>

0 commit comments

Comments
 (0)