Skip to content

Commit 891d361

Browse files
committed
fix(modal): correct vim insert mode activation in task creation modal (#1410)
The 4.2.0 implementation used an incorrect path to access the CodeMirror vim adapter. This fix: - Uses correct path (editor.cm.cm) to access the CM5 adapter - Adds fallback to activeCM for compatibility - Adds 50ms delay to ensure vim extension has initialized Thanks to @Leo310 for the feature request and testing.
1 parent 5689782 commit 891d361

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

docs/releases/unreleased.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ Example:
2424
2525
-->
2626

27+
## Fixed
28+
29+
- (#1410) Fixed vim insert mode not activating in task creation modal
30+
- The 4.2.0 implementation used incorrect path to access the CodeMirror vim adapter
31+
- Now correctly accesses the CM5 adapter and adds a small delay for vim initialization
32+
- Thanks to @Leo310 for the feature request
33+

src/editor/EmbeddableMarkdownEditor.ts

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -243,38 +243,28 @@ export class EmbeddableMarkdownEditor extends getEditorBase() {
243243
* Uses Obsidian's internal CodeMirrorAdapter.Vim API.
244244
*/
245245
private enterVimInsertMode(): void {
246-
try {
247-
// Check if vim mode is enabled in Obsidian settings
248-
// Using internal Obsidian API - vault.getConfig is not in public types
249-
const vimModeEnabled = (this.app.vault as any).getConfig("vimMode");
250-
if (!vimModeEnabled) return;
251-
252-
// Access the Vim API from Obsidian's CodeMirrorAdapter
253-
// Using internal Obsidian API - CodeMirrorAdapter is not in public types
254-
const Vim = (window as any).CodeMirrorAdapter?.Vim;
255-
if (!Vim) return;
256-
257-
// Get the CodeMirror 6 EditorView instance from the editor
258-
const cm = this.editor?.cm;
259-
if (!cm) return;
260-
261-
// The vim plugin attaches a cm5 adapter to the EditorView state
262-
// Using internal vim plugin API - vim state is not in public types
263-
const vimState = (cm.state as any)?.vim;
264-
if (!vimState) return;
265-
266-
// Get the CM5 adapter that the vim plugin uses
267-
// Using internal vim plugin API - cm property is not in public types
268-
const cm5 = vimState.cm;
269-
if (!cm5) return;
270-
271-
// Enter insert mode by simulating the 'i' key
272-
// Vim.handleKey takes (cm5adapter, key, origin) but origin is optional
273-
Vim.handleKey(cm5, "i", "api");
274-
} catch (e) {
275-
// Silently fail if vim integration isn't available
276-
console.debug("Could not enter vim insert mode:", e);
277-
}
246+
// Use a small delay to ensure vim extension has initialized
247+
setTimeout(() => {
248+
try {
249+
// Check if vim mode is enabled in Obsidian settings
250+
const vimModeEnabled = (this.app.vault as any).getConfig("vimMode");
251+
if (!vimModeEnabled) return;
252+
253+
// Access the Vim API from Obsidian's CodeMirrorAdapter
254+
const Vim = (window as any).CodeMirrorAdapter?.Vim;
255+
if (!Vim) return;
256+
257+
// Get the CM5 adapter - Obsidian nests it at editor.cm.cm
258+
// Fallback to activeCM if the standard path doesn't work
259+
const cm5 = (this.editor as any)?.cm?.cm ?? (this as any).activeCM;
260+
if (!cm5) return;
261+
262+
// Enter insert mode by simulating the 'i' key
263+
Vim.handleKey(cm5, "i", "api");
264+
} catch {
265+
// Silently fail if vim integration isn't available
266+
}
267+
}, 50);
278268
}
279269

280270
/**

0 commit comments

Comments
 (0)