Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] press enter when using IME causes unwanted file open in vault search command palette #50

Closed
reorx opened this issue May 15, 2022 · 2 comments
Milestone

Comments

@reorx
Copy link

reorx commented May 15, 2022

Problem description:

When typing by input method editor (IME) in a modal input (e.g. triggered by a Capture command), if enter is hit, the text is submitted immediately.

To Reproduce

  1. Open omnisearch vault search command palette
  2. Switch to an IME (most of them are CJK language)
  3. Type something
  4. Press enter, and the first file is opened
obsidian-omnisearch.mov

Here you can see I typed test in the IME, then I pressed enter, I intended to insert test in the input and continue typing, but the input was submitted and the file is opened.

Additional context

In CJK IME, when enter is hit, it means to insert the text without converting it to the CJK character, this is a common trick to insert English words without turning off the IME.

By listening to Element: compositionstart event - Web APIs | MDN,
we can determine whether the enter key means to insert text or submit.
Here's a snippet I used in my projects:

interface CompositionState {
	lock: boolean
}

export function lockInputMethodComposition(el: HTMLInputElement): CompositionState {
	const state: CompositionState = {
		lock: false,
	}
	el.addEventListener('compositionstart', () => {
		state.lock = true
	})
	el.addEventListener('compositionend', () => {
		state.lock = false
	})
	return state
}

const nameInputState = lockInputMethodComposition(nameInputEl)
nameInputEl.addEventListener('keydown', async (e) => {
	if (e.key === 'Enter' && !nameInputState.lock) {
		e.preventDefault()
		// do things like submit the input here
	}
})

Reference: dom events - What is JavaScript's CompositionEvent? Please give examples - Stack Overflow

Your environment:

  • Omnisearch version: 1.2.1
  • Obsidian version: v0.14.6
  • Operating system: Darwin Kernel Version 21.4.0: Mon Feb 21 20:36:53 PST 2022; root:xnu-
  • Number of notes in your vault (approx.): 500
  • Other plugins that may be related to the issue:
@scambier scambier added this to the 1.3 milestone May 15, 2022
@scambier
Copy link
Owner

Thanks for that detailled issue! Would you mind trying this fix and tell me if it's working?

omnisearch composition.zip

@reorx
Copy link
Author

reorx commented May 15, 2022

Work as expected, thank you for the quick response and agile fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants