overleaf/services/web/frontend/js/features/source-editor/extensions/editable.ts
Mathias Jakobsen 59b3b9b933 Merge pull request #12631 from overleaf/ae-readonly-focus
[cm6] Prevent the editor being focused when read-only

GitOrigin-RevId: 7de9f2df2421b57b3315d59d640f7c8b46caf178
2023-04-21 08:02:56 +00:00

24 lines
545 B
TypeScript

import { Compartment, EditorState, TransactionSpec } from '@codemirror/state'
import { EditorView } from '@codemirror/view'
const readOnlyConf = new Compartment()
export const editable = () => {
return [
readOnlyConf.of([
EditorState.readOnly.of(true),
EditorView.editable.of(false),
]),
]
}
export const setEditable = (value = true): TransactionSpec => {
return {
effects: [
readOnlyConf.reconfigure([
EditorState.readOnly.of(!value),
EditorView.editable.of(value),
]),
],
}
}