Skip to content

Commit

Permalink
Fix artisticat1#52 by force end composition
Browse files Browse the repository at this point in the history
  • Loading branch information
oldkingOK committed Dec 10, 2024
1 parent 8b570b0 commit a1c8d79
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/latex_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { clearSnippetQueue } from "./snippets/codemirror/snippet_queue_state_fie
import { handleUndoRedo } from "./snippets/codemirror/history";

import { handleMathTooltip } from "./editor_extensions/math_tooltip";
import { isComposing } from "./utils/editor_utils";
import { isComposing, forceEndComposition } from "./utils/editor_utils";

export const handleUpdate = (update: ViewUpdate) => {
const settings = getLatexSuiteConfig(update.state);
Expand All @@ -28,7 +28,38 @@ export const handleUpdate = (update: ViewUpdate) => {
handleUndoRedo(update);
}

let lastKeyboardEvent: KeyboardEvent | null = null;
let useNextTextInput = false;

export const onInput = (view: EditorView, from: number, to: number, text: string) => {
if (text === "\0\0") {
return true;
}
if (text.length == 1 && useNextTextInput) {
const success = handleKeydown(
text,
lastKeyboardEvent.shiftKey,
lastKeyboardEvent.ctrlKey || lastKeyboardEvent.metaKey,
isComposing(view, lastKeyboardEvent),
view
);
if (success) {
forceEndComposition(view);
return true;
}
}
}

export const onKeydown = (event: KeyboardEvent, view: EditorView) => {
// the input event handler `onInput` will try to handle the unknown key.
if (event.key == "Unidentified" || event.key == "Process" || event.key == "Dead") {
useNextTextInput = true;
lastKeyboardEvent = event;
return;
} else {
useNextTextInput = false;
}

const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, isComposing(view, event), view);

if (success) event.preventDefault();
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ICONS } from "./settings/ui/icons";
import { getEditorCommands } from "./features/editor_commands";
import { getLatexSuiteConfigExtension } from "./snippets/codemirror/config";
import { SnippetVariables, parseSnippetVariables, parseSnippets } from "./snippets/parse";
import { handleUpdate, onKeydown } from "./latex_suite";
import { handleUpdate, onKeydown, onInput } from "./latex_suite";
import { EditorView, tooltips } from "@codemirror/view";
import { snippetExtensions } from "./snippets/codemirror/extensions";
import { mkConcealPlugin } from "./editor_extensions/conceal";
Expand Down Expand Up @@ -163,6 +163,7 @@ export default class LatexSuitePlugin extends Plugin {
this.editorExtensions.push([
getLatexSuiteConfigExtension(this.CMSettings),
Prec.highest(EditorView.domEventHandlers({ "keydown": onKeydown })),
Prec.highest(EditorView.inputHandler.of(onInput)),
EditorView.updateListener.of(handleUpdate),
snippetExtensions,
]);
Expand Down
35 changes: 35 additions & 0 deletions src/utils/editor_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,38 @@ export function isComposing(view: EditorView, event: KeyboardEvent): boolean {
// Note that keyCode is deprecated - it is used here because it is apparently the only way to detect the first keydown event of an IME composition.
return view.composing || event.keyCode === 229;
}

/**
* Force end an IME composition.
*
* MIT License
* Copyright (C) 2018-2021 by Marijn Haverbeke <marijnh@gmail.com> and others
*/
export function forceEndComposition(view: EditorView) {
let parent = view.scrollDOM.parentElement;
if (!parent) return;

let sibling = view.scrollDOM.nextSibling;
let selection = window.getSelection();
let savedSelection = selection && {
anchorNode: selection.anchorNode,
anchorOffset: selection.anchorOffset,
focusNode: selection.focusNode,
focusOffset: selection.focusOffset
};

view.scrollDOM.remove();
parent.insertBefore(view.scrollDOM, sibling);
try {
if (savedSelection && selection) {
selection.setPosition(savedSelection.anchorNode, savedSelection.anchorOffset);
if (savedSelection.focusNode) {
selection.extend(savedSelection.focusNode, savedSelection.focusOffset);
}
}
} catch(e) {
console.error(e);
}
view.focus();
view.contentDOM.dispatchEvent(new CustomEvent("compositionend"));
}

0 comments on commit a1c8d79

Please sign in to comment.