Skip to content

toggleEditorReadOnly

Toggles the read-only state of a single CodeMirror instance — the low-level primitive behind editor locking.

When read-only, the editor stays focusable and interactive (selection, copy, and app hotkeys such as the command palette keep working) — only document edits are prevented, via the CodeMirror readOnly state plus a change filter that drops any user-originated change (see readOnlyExtension).

This toggles ONE CodeMirror instance, not “a file”: an Obsidian Editor is reused as its leaf navigates between notes, so this primitive does not follow a note around. To lock a note across every editor that shows it (current and future, in any window), use lockResourceForPath from ./resource-lock.ts.

Import:

import { toggleEditorReadOnly } from 'obsidian-dev-utils/obsidian/editor';

Signature:

function toggleEditorReadOnly(editor: Editor, isReadOnly: boolean): void

Parameters:

ParameterTypeDescription
editorEditorThe editor whose CodeMirror instance to toggle.
isReadOnlybooleantrue to make the editor read-only, false to make it editable again.

Returns: void