codemirror / dev

Development repository for the CodeMirror editor project
https://codemirror.net/
Other
5.94k stars 377 forks source link

Can't make editor read-only and not editable #1412

Closed d3lm closed 4 months ago

d3lm commented 4 months ago

Describe the issue

I am trying to make the editor read-only and non-editable. For that I am using EditorState.readOnly as well as EditorView.editable to configure it but it doesn't work and I was wondering if I am doing something wrong.

Here's my code:

import {minimalSetup, EditorView} from "codemirror"
import {EditorState} from "@codemirror/state"

new EditorView({
  doc: "You should not be able to edit this",
  extensions: [
    EditorView.editable.of([false]),
    EditorState.readOnly.of([true]),
  ],
  parent: document.body
});

Update: Seems to work in Firefox.

Browser and platform

Chrome Version 126.0.6478.127

Reproduction link

https://codemirror.net/try/?c=aW1wb3J0IHttaW5pbWFsU2V0dXAsIEVkaXRvclZpZXd9IGZyb20gImNvZGVtaXJyb3IiCmltcG9ydCB7RWRpdG9yU3RhdGV9IGZyb20gIkBjb2RlbWlycm9yL3N0YXRlIgoKbmV3IEVkaXRvclZpZXcoewogIGRvYzogIllvdSBzaG91bGQgbm90IGJlIGFibGUgdG8gZWRpdCB0aGlzIiwKICBleHRlbnNpb25zOiBbCiAgICBFZGl0b3JWaWV3LmVkaXRhYmxlLm9mKFtmYWxzZV0pLAogICAgRWRpdG9yU3RhdGUucmVhZE9ubHkub2YoW3RydWVdKSwKICBdLAogIHBhcmVudDogZG9jdW1lbnQuYm9keQp9KQo=

errmayank commented 4 months ago

Remove braces around the value

  extensions: [
    EditorView.editable.of(false),
    EditorState.readOnly.of(true),
  ]

https://codemirror.net/try/?c=aW1wb3J0IHttaW5pbWFsU2V0dXAsIEVkaXRvclZpZXd9IGZyb20gImNvZGVtaXJyb3IiCmltcG9ydCB7RWRpdG9yU3RhdGV9IGZyb20gIkBjb2RlbWlycm9yL3N0YXRlIgoKbmV3IEVkaXRvclZpZXcoewogIGRvYzogIllvdSBzaG91bGQgbm90IGJlIGFibGUgdG8gZWRpdCB0aGlzIiwKICBleHRlbnNpb25zOiBbCiAgICBFZGl0b3JWaWV3LmVkaXRhYmxlLm9mKGZhbHNlKSwKICAgIEVkaXRvclN0YXRlLnJlYWRPbmx5Lm9mKHRydWUpLAogIF0sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5Cn0pCg==

d3lm commented 4 months ago

Uh, sigh 🤦‍♂️. Thanks. It does work now!