forked from artisticat1/obsidian-latex-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix artisticat1#52 by force end composition
- Loading branch information
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> 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")); | ||
} |