diff --git a/resources/scripts/components/elements/CodemirrorEditor.spec.tsx b/resources/scripts/components/elements/CodemirrorEditor.spec.tsx index 70c4ecd9d..fad44b494 100644 --- a/resources/scripts/components/elements/CodemirrorEditor.spec.tsx +++ b/resources/scripts/components/elements/CodemirrorEditor.spec.tsx @@ -61,6 +61,26 @@ it('runs the latest save handler for Mod-s', () => { expect(latest).toHaveBeenCalledOnce(); }); +it('indents with Tab and outdents with Shift-Tab', () => { + const { container } = render( + + ); + const view = viewOf(container); + const content = container.querySelector('.cm-content')!; + + act(() => view.dispatch({ selection: { anchor: 0 } })); + + act(() => { + content.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', bubbles: true })); + }); + expect(view.state.doc.toString()).toBe(' alpha'); + + act(() => { + content.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', shiftKey: true, bubbles: true })); + }); + expect(view.state.doc.toString()).toBe('alpha'); +}); + it('renders the search and replace panel with theme controls', () => { const { container } = render(); diff --git a/resources/scripts/components/elements/CodemirrorEditor.tsx b/resources/scripts/components/elements/CodemirrorEditor.tsx index 28235e46b..c455ec92d 100644 --- a/resources/scripts/components/elements/CodemirrorEditor.tsx +++ b/resources/scripts/components/elements/CodemirrorEditor.tsx @@ -1,5 +1,6 @@ import React, { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react'; import { basicSetup } from 'codemirror'; +import { indentWithTab } from '@codemirror/commands'; import { Compartment, EditorState, Prec } from '@codemirror/state'; import { HighlightStyle, indentUnit, syntaxHighlighting } from '@codemirror/language'; import { EditorView, keymap, type Panel, type ViewUpdate } from '@codemirror/view'; @@ -978,6 +979,7 @@ const createEditorState = ( }, ]) ), + keymap.of([indentWithTab]), languageCompartment.of(resolveCodemirrorLanguage(mode)), EditorView.updateListener.of((update) => { if (update.docChanged) {