editable.ts 933 B

1234567891011121314151617181920212223242526272829
  1. import { Compartment, EditorState, TransactionSpec } from '@codemirror/state'
  2. import { EditorView } from '@codemirror/view'
  3. const readOnlyConf = new Compartment()
  4. /**
  5. * A custom extension which determines whether the content is editable, by setting the value of the EditorState.readOnly and EditorView.editable facets.
  6. * Commands and extensions read the EditorState.readOnly facet to decide whether they should be applied.
  7. * EditorView.editable determines whether the DOM can be focused, by changing the value of the contenteditable attribute.
  8. */
  9. export const editable = () => {
  10. return [
  11. readOnlyConf.of([
  12. EditorState.readOnly.of(true),
  13. EditorView.editable.of(false),
  14. ]),
  15. ]
  16. }
  17. export const setEditable = (value = true): TransactionSpec => {
  18. return {
  19. effects: [
  20. readOnlyConf.reconfigure([
  21. EditorState.readOnly.of(!value),
  22. EditorView.editable.of(value),
  23. ]),
  24. ],
  25. }
  26. }