Skip to content

Commit

Permalink
Also announce changes to overwrite mode
Browse files Browse the repository at this point in the history
  • Loading branch information
personalizedrefrigerator committed Nov 26, 2024
1 parent 27765a9 commit 8bf51c9
Show file tree
Hide file tree
Showing 2 changed files with 19 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 @@ -3,6 +3,7 @@ import createTestEditor from '../testUtil/createTestEditor';
import overwriteModeExtension, { toggleOverwrite } from './overwriteModeExtension';
import typeText from '../testUtil/typeText';
import pressReleaseKey from '../testUtil/pressReleaseKey';
import getLastAnnouncement from '../testUtil/getLastAnnouncement';

const createEditor = async (initialText: string, defaultEnabled = false) => {
const editor = await createTestEditor(initialText, EditorSelection.cursor(0), [], [
Expand Down Expand Up @@ -54,4 +55,14 @@ describe('overwriteModeExtension', () => {

expect(editor.state.doc.toString()).toBe('Test! This is a test! test\nTest');
});

test('should announce when toggling overwrite', async () => {
const editor = await createEditor('\nTest');

pressReleaseKey(editor, { key: 'Insert', code: 'Insert' });
expect(getLastAnnouncement(editor)).toBe('Overwrite mode enabled');

pressReleaseKey(editor, { key: 'Insert', code: 'Insert' });
expect(getLastAnnouncement(editor)).toBe('Overwrite mode disabled');
});
});
9 changes: 8 additions & 1 deletion packages/editor/CodeMirror/utils/overwriteModeExtension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { keymap, EditorView } from '@codemirror/view';
import { StateField, Facet, StateEffect } from '@codemirror/state';
import { _ } from '../../localization';

const overwriteModeFacet = Facet.define({
combine: values => values[0] ?? false,
Expand Down Expand Up @@ -67,8 +68,14 @@ const overwriteModeExtension = [
keymap.of([{
key: 'Insert',
run: (view) => {
const newEnabled = !view.state.field(overwriteModeState);
view.dispatch({
effects: toggleOverwrite.of(!view.state.field(overwriteModeState)),
effects: [
toggleOverwrite.of(newEnabled),
EditorView.announce.of(
newEnabled ? _('Overwrite mode enabled') : _('Overwrite mode disabled'),
),
],
});
return false;
},
Expand Down

0 comments on commit 8bf51c9

Please sign in to comment.