editable.ts 545 B

123456789101112131415161718192021222324
  1. import { Compartment, EditorState, TransactionSpec } from '@codemirror/state'
  2. import { EditorView } from '@codemirror/view'
  3. const readOnlyConf = new Compartment()
  4. export const editable = () => {
  5. return [
  6. readOnlyConf.of([
  7. EditorState.readOnly.of(true),
  8. EditorView.editable.of(false),
  9. ]),
  10. ]
  11. }
  12. export const setEditable = (value = true): TransactionSpec => {
  13. return {
  14. effects: [
  15. readOnlyConf.reconfigure([
  16. EditorState.readOnly.of(!value),
  17. EditorView.editable.of(value),
  18. ]),
  19. ],
  20. }
  21. }