From 92cead3a1d333994f25cda3ad995b430ea36ca23 Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Wed, 24 Apr 2024 10:07:40 +0900 Subject: [PATCH 1/5] Fix #284 by disabling snippet expansion when IME is on --- package-lock.json | 4 ++-- src/latex_suite.ts | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 296294e..83b5a3f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-latex-suite", - "version": "1.9.1", + "version": "1.9.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "obsidian-latex-suite", - "version": "1.9.1", + "version": "1.9.2", "license": "MIT", "dependencies": { "set.prototype.difference": "^1.1.5", diff --git a/src/latex_suite.ts b/src/latex_suite.ts index 310871f..2a3db6b 100644 --- a/src/latex_suite.ts +++ b/src/latex_suite.ts @@ -37,6 +37,13 @@ export const handleUpdate = (update: ViewUpdate) => { } export const onKeydown = (event: KeyboardEvent, view: EditorView) => { + // Prevent IME from triggering keydown events. + // view.composing and event.isComposing are false for the first keydown event of an IME composition, + // so we need to check for event.keyCode === 229 to prevent IME from triggering keydown events. + // 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. + const isIME = view.composing || event.keyCode === 229; + if (isIME) return; + const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, view); if (success) event.preventDefault(); From a86c6cd03bf08cd9473c08afd79b855c7ce65475 Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Wed, 24 Apr 2024 10:29:37 +0900 Subject: [PATCH 2/5] Add setting --- src/latex_suite.ts | 10 ++++++---- src/settings/settings.ts | 2 ++ src/settings/settings_tab.ts | 12 ++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/latex_suite.ts b/src/latex_suite.ts index 2a3db6b..12535dc 100644 --- a/src/latex_suite.ts +++ b/src/latex_suite.ts @@ -37,19 +37,18 @@ export const handleUpdate = (update: ViewUpdate) => { } export const onKeydown = (event: KeyboardEvent, view: EditorView) => { - // Prevent IME from triggering keydown events. + // Check if the user is typing in an IME composition. // view.composing and event.isComposing are false for the first keydown event of an IME composition, // so we need to check for event.keyCode === 229 to prevent IME from triggering keydown events. // 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. const isIME = view.composing || event.keyCode === 229; - if (isIME) return; - const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, view); + const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, isIME, view); if (success) event.preventDefault(); } -export const handleKeydown = (key: string, shiftKey: boolean, ctrlKey: boolean, view: EditorView) => { +export const handleKeydown = (key: string, shiftKey: boolean, ctrlKey: boolean, isIME: boolean, view: EditorView) => { const settings = getLatexSuiteConfig(view); const ctx = Context.fromView(view); @@ -58,6 +57,9 @@ export const handleKeydown = (key: string, shiftKey: boolean, ctrlKey: boolean, if (settings.snippetsEnabled) { + // Prevent IME from triggering keydown events. + if (settings.suppressSnippetTriggerOnIME && isIME) return; + // Allows Ctrl + z for undo, instead of triggering a snippet ending with z if (!ctrlKey) { try { diff --git a/src/settings/settings.ts b/src/settings/settings.ts index 63ddbb7..a6710a7 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -6,6 +6,7 @@ import { DEFAULT_SNIPPET_VARIABLES } from "src/utils/default_snippet_variables"; interface LatexSuiteBasicSettings { snippetsEnabled: boolean; snippetsTrigger: "Tab" | " " + suppressSnippetTriggerOnIME: boolean; removeSnippetWhitespace: boolean; loadSnippetsFromFile: boolean; loadSnippetVariablesFromFile: boolean; @@ -52,6 +53,7 @@ export const DEFAULT_SETTINGS: LatexSuitePluginSettings = { // Basic settings snippetsEnabled: true, snippetsTrigger: "Tab", + suppressSnippetTriggerOnIME: true, removeSnippetWhitespace: true, loadSnippetsFromFile: false, loadSnippetVariablesFromFile: false, diff --git a/src/settings/settings_tab.ts b/src/settings/settings_tab.ts index 6d276e8..f97d318 100644 --- a/src/settings/settings_tab.ts +++ b/src/settings/settings_tab.ts @@ -127,6 +127,18 @@ export class LatexSuiteSettingTab extends PluginSettingTab { }) ); + + new Setting(containerEl) + .setName("Don't trigger snippets when IME is active") + .setDesc("Whether to suppress the snippet triggering when an IME is active.") + .addToggle((toggle) => toggle + .setValue(this.plugin.settings.suppressSnippetTriggerOnIME) + .onChange(async (value) => { + this.plugin.settings.suppressSnippetTriggerOnIME = value; + await this.plugin.saveSettings(); + }) + ); + this.addHeading(containerEl, "Conceal", "math-integral-x"); From cdf8bfc72f69d0512a54907eff5c8b246715fa3e Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Wed, 24 Apr 2024 10:38:18 +0900 Subject: [PATCH 3/5] formatting --- src/latex_suite.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/latex_suite.ts b/src/latex_suite.ts index 12535dc..baf4493 100644 --- a/src/latex_suite.ts +++ b/src/latex_suite.ts @@ -42,7 +42,7 @@ export const onKeydown = (event: KeyboardEvent, view: EditorView) => { // so we need to check for event.keyCode === 229 to prevent IME from triggering keydown events. // 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. const isIME = view.composing || event.keyCode === 229; - + const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, isIME, view); if (success) event.preventDefault(); From 2dd8a508c430524d489eb2d53a56b20e4d03d118 Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Wed, 24 Apr 2024 10:53:23 +0900 Subject: [PATCH 4/5] clean up --- src/latex_suite.ts | 9 ++------- src/utils/editor_utils.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/latex_suite.ts b/src/latex_suite.ts index baf4493..9a4680b 100644 --- a/src/latex_suite.ts +++ b/src/latex_suite.ts @@ -20,6 +20,7 @@ import { concealPlugin } from "./editor_extensions/conceal"; import { LatexSuiteCMSettings } from "./settings/settings"; import { colorPairedBracketsPluginLowestPrec, highlightCursorBracketsPlugin } from "./editor_extensions/highlight_brackets"; import { cursorTooltipBaseTheme, cursorTooltipField } from "./editor_extensions/math_tooltip"; +import { isComposing } from "./utils/editor_utils"; export const handleUpdate = (update: ViewUpdate) => { const cursorTriggeredByChange = update.state.field(cursorTriggerStateField, false); @@ -37,13 +38,7 @@ export const handleUpdate = (update: ViewUpdate) => { } export const onKeydown = (event: KeyboardEvent, view: EditorView) => { - // Check if the user is typing in an IME composition. - // view.composing and event.isComposing are false for the first keydown event of an IME composition, - // so we need to check for event.keyCode === 229 to prevent IME from triggering keydown events. - // 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. - const isIME = view.composing || event.keyCode === 229; - - const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, isIME, view); + const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, isComposing(view, event), view); if (success) event.preventDefault(); } diff --git a/src/utils/editor_utils.ts b/src/utils/editor_utils.ts index 2e3638e..1374782 100644 --- a/src/utils/editor_utils.ts +++ b/src/utils/editor_utils.ts @@ -134,3 +134,15 @@ export function escalateToToken(cursor: TreeCursor, dir: Direction, target: stri return null; } + + +/** + * Check if the user is typing in an IME composition. + * Returns true even if the given event is the first keydown event of an IME composition. + */ +export function isComposing(view: EditorView, event: KeyboardEvent): boolean { + // view.composing and event.isComposing are false for the first keydown event of an IME composition, + // so we need to check for event.keyCode === 229 to prevent IME from triggering keydown events. + // 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; +} From 4852b29506d45b2e66e86ab16638415a355e60c0 Mon Sep 17 00:00:00 2001 From: artisticat <100622574+artisticat1@users.noreply.github.com> Date: Sat, 27 Apr 2024 11:09:29 +0100 Subject: [PATCH 5/5] move setting to "advanced snippet settings" --- src/settings/settings_tab.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/settings/settings_tab.ts b/src/settings/settings_tab.ts index f97d318..d807e9e 100644 --- a/src/settings/settings_tab.ts +++ b/src/settings/settings_tab.ts @@ -127,18 +127,6 @@ export class LatexSuiteSettingTab extends PluginSettingTab { }) ); - - new Setting(containerEl) - .setName("Don't trigger snippets when IME is active") - .setDesc("Whether to suppress the snippet triggering when an IME is active.") - .addToggle((toggle) => toggle - .setValue(this.plugin.settings.suppressSnippetTriggerOnIME) - .onChange(async (value) => { - this.plugin.settings.suppressSnippetTriggerOnIME = value; - await this.plugin.saveSettings(); - }) - ); - this.addHeading(containerEl, "Conceal", "math-integral-x"); @@ -422,6 +410,17 @@ export class LatexSuiteSettingTab extends PluginSettingTab { await this.plugin.saveSettings(); })); + new Setting(containerEl) + .setName("Don't trigger snippets when IME is active") + .setDesc("Whether to suppress the snippet triggering when an IME is active.") + .addToggle((toggle) => toggle + .setValue(this.plugin.settings.suppressSnippetTriggerOnIME) + .onChange(async (value) => { + this.plugin.settings.suppressSnippetTriggerOnIME = value; + await this.plugin.saveSettings(); + }) + ); + new Setting(containerEl) .setName("Code languages to interpret as math mode") .setDesc("Codeblock languages where the whole code block should be treated like a math block, separated by commas.")