Skip to content

Commit

Permalink
fix: dispatch a beforeinput and input with `{inputType: 'insertLi…
Browse files Browse the repository at this point in the history
…neBreak'}` when the enter key on the virtual keyboard is pressed
  • Loading branch information
arnog committed Nov 17, 2022
1 parent 1f0bba2 commit 43f8531
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/editor-mathfield/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { complete } from './autocomplete';
import type { MathfieldPrivate } from './mathfield-private';
import { onTypedText } from './keyboard-input';
import { toggleKeystrokeCaption } from './keystroke-caption';
import { contentDidChange, contentWillChange } from 'editor-model/listeners';

registerCommand({
undo: (mathfield: MathfieldPrivate) => {
Expand Down Expand Up @@ -71,12 +72,21 @@ registerCommand({
return true;
},
commit: (mathfield: MathfieldPrivate) => {
mathfield.host?.dispatchEvent(
new Event('change', {
bubbles: true,
composed: true,
})
);
if (contentWillChange(mathfield.model, { inputType: 'insertLineBreak' })) {
// No matching keybinding: trigger a commit

if (mathfield.host) {
mathfield.host.dispatchEvent(
new Event('change', {
bubbles: true,
composed: true,
})
);
}

// Dispatch an 'input' event matching the behavior of `<textarea>`
contentDidChange(mathfield.model, { inputType: 'insertLineBreak' });
}
return true;
},
});
Expand Down
4 changes: 4 additions & 0 deletions src/editor/speech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ export function defaultSpeakHook(

config ??= globalMathLive().config ?? {};

// Sigh... Not really necessary, but the version of the typescript
// compiler used by `grok` will complain about this
if (!config) return;

if (!config.speechEngine || config.speechEngine === 'local') {
// On ChromeOS: chrome.accessibilityFeatures.spokenFeedback
// See also https://developer.chrome.com/apps/tts
Expand Down
8 changes: 8 additions & 0 deletions test/smoke/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ <h2>MathML</h2>
// },
});

mf.addEventListener('input', (ev) => {
console.log('commit');
});

mf.addEventListener('beforeinput', (ev) => {
if (ev.inputType === 'insertLineBreak') console.log('enter');
});

mf.addEventListener('input', (ev) => updateContent(mf));

mf.addEventListener('selection-change', (ev) => {
Expand Down

0 comments on commit 43f8531

Please sign in to comment.