Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/services/saneKeyboardEvents.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ var saneKeyboardEvents = (function () {
) {
var keydown: KeyboardEvent | null = null;
var keypress: KeyboardEvent | null = null;
var isComposing: boolean = false;

// everyTick.listen() is called after key or clipboard events to
// say "Hey, I think something was just typed" or "pasted" etc,
Expand Down Expand Up @@ -264,6 +265,7 @@ var saneKeyboardEvents = (function () {
}

function typedText() {
if (isComposing) return;
// If there is a selection, the contents of the textarea couldn't
// possibly have just been typed in.
// This happens in browsers like Firefox and Opera that fire
Expand Down Expand Up @@ -360,6 +362,24 @@ var saneKeyboardEvents = (function () {
everyTick.trigger(e);
}

function onCompositionStart(e: CompositionEvent) {
everyTick.trigger(e);
isComposing = true;
}

function onCompositionEnd(e: CompositionEvent) {
// Handle composed input events, such as those which come from dictation in Chrome.
everyTick.trigger(e);
isComposing = false;
if (!(textarea instanceof HTMLTextAreaElement) || e.target !== textarea)
return;
const text = e.data;
if (text.length === 0) return;
textarea.value = '';
controller.writeLatex(text);
controller.scrollHoriz();
}

if (controller.options && controller.options.disableCopyPaste) {
controller.addTextareaEventListeners({
keydown: onKeydown,
Expand All @@ -377,6 +397,8 @@ var saneKeyboardEvents = (function () {
e.preventDefault();
},
input: onInput,
compositionstart: onCompositionStart,
compositionend: onCompositionEnd,
});
} else {
controller.addTextareaEventListeners({
Expand All @@ -396,6 +418,8 @@ var saneKeyboardEvents = (function () {
},
paste: onPaste,
input: onInput,
compositionstart: onCompositionStart,
compositionend: onCompositionEnd,
});
}

Expand Down