Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Particular lines readonly #259

Closed
andrebnassis opened this issue Jan 30, 2022 · 8 comments
Closed

Particular lines readonly #259

andrebnassis opened this issue Jan 30, 2022 · 8 comments

Comments

@andrebnassis
Copy link

andrebnassis commented Jan 30, 2022

Is there any sample that shows how to set some lines as readonly, so it can't be edited?

I found something related to markText and beforeChange, but I couldn't solve it yet using @uiw/react-codemirror package.

https://stackoverflow.com/questions/17415100/codemirror-particular-lines-readonly

Thanks in advance!

PS: If possible, use the getting started sample as base and set only the first line as readonly:

import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';

const CodeEditor = () => {
  
  return (
    <CodeMirror
      value="console.log('hello world!');"
      height="200px"
      extensions={[javascript({ jsx: true })]}
      onChange={(value, viewUpdate) => {
        console.log('value:', value);
        console.log(viewUpdate);
      }}
      theme={'dark'}
    />
  );
}

export default CodeEditor;
@jaywcjlove
Copy link
Member

https://stackoverflow.com/a/17482204/1334703

This is the solution from the previous version.

You can try it with v3.

CodeMirror v6 used by @uiw/react-codemirror@v4

@andrebnassis
Copy link
Author

https://stackoverflow.com/a/17482204/1334703

This is the solution from the previous version.

You can try it with v3.

CodeMirror v6 used by @uiw/react-codemirror@v4

Thanks for the response, but I would like to start using the latest version.

If someone has any example of implementing this feature I would be very glad.

@raquelhortab
Copy link

Hi! I am also interested in this. I cannot seem to understand how to use options in codemirror docs for the component version, is there any tutorial about that? How would these options be applied (markText for instance), or how can we use codemirror configuration? (readOnly for instance, it's not working through props)

@simpsonphile
Copy link

simpsonphile commented Feb 15, 2022

and update how to do it in v4?

@raquelhortab
Copy link

and update how to do it in v4?

#108

it looks like the only way is to downgrade, it's weird that newer versions have fewer functionalities, maybe they'll add it in the future? I don't know but afterall, it's an opensource library so I do appreciate whatever they can offer

@jaywcjlove
Copy link
Member

@andrebnassis
Copy link
Author

andrebnassis commented Apr 6, 2022

I just released codemirror-readonly-ranges extension that easy allow to work with read-only ranges on CodeMirror6.
For anyone who is interested on the solution:

package: https://www.npmjs.com/package/codemirror-readonly-ranges

full documentation: https://andrebnassis.github.io/codemirror-readonly-ranges/?path=/story/0-introduction--page

@jaywcjlove
Copy link
Member

https://discuss.codemirror.net/t/how-to-make-certain-ranges-readonly-in-codemirror6/3400/5

function readOnlyTransactionFilter() {
  return EditorState.transactionFilter.of((tr) => {
    let readonlyRangeSet = tr.startState.field(underlineField, false)
    if (readonlyRangeSet && tr.docChanged && !tr.annotation(Transaction.remote)) {
      let block = false
      tr.changes.iterChangedRanges((chFrom, chTo) => {
        readonlyRangeSet.between(chFrom, chTo, (roFrom, roTo) => {
          if (chTo > roFrom && chFrom < roTo) block = true
        })
      })
      if (block) return []
    }
    return tr
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants