Skip to content

Commit

Permalink
Disable overwrite mode on <escape>
Browse files Browse the repository at this point in the history
  • Loading branch information
personalizedrefrigerator committed Nov 26, 2024
1 parent 7f11c92 commit 841b1b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/editor/CodeMirror/utils/overwriteModeExtension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ describe('overwriteModeExtension', () => {
expect(editor.state.doc.toString()).toBe('Example!');
});

test('should be disabled by pressing <escape>', async () => {
const editor = await createEditor('Test!', true);

expect(overwriteModeEnabled(editor)).toBe(true);
pressReleaseKey(editor, { key: 'Escape', code: 'Escape' });
expect(overwriteModeEnabled(editor)).toBe(false);
// Escape should not re-enable the extension
pressReleaseKey(editor, { key: 'Escape', code: 'Escape' });
expect(overwriteModeEnabled(editor)).toBe(false);
});

test('<insert> should not toggle overwrite mode if other keys are pressed', async () => {
// On Linux, the Orca screen reader's default "do screen reader action" key is
// <insert>. To avoid toggling insert mode when users perform screen reader actions,
Expand Down
16 changes: 15 additions & 1 deletion packages/editor/CodeMirror/utils/overwriteModeExtension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorView } from '@codemirror/view';
import { EditorView, keymap } from '@codemirror/view';
import { StateField, Facet, StateEffect } from '@codemirror/state';
import keyUpHandlerExtension from './keyUpHandlerExtension';

Expand Down Expand Up @@ -81,6 +81,20 @@ const setOverwriteModeEnabled = (enabled: boolean, view: EditorView) => {

const overwriteModeExtension = [
overwriteModeState,
keymap.of([
{
// The <escape> keyboard shortcut may be more easily discoverable for users
// who enter overwrite mode unintentionally.
key: 'Escape',
run: (view) => {
if (overwriteModeEnabled(view)) {
setOverwriteModeEnabled(false, view);
return true;
}
return false;
},
},
]),
keyUpHandlerExtension(
(event) => (
event.code === 'Insert' && !event.shiftKey && !event.altKey && !event.metaKey && !event.ctrlKey
Expand Down

0 comments on commit 841b1b9

Please sign in to comment.