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

Disable a single line (or many) instead of the whole file #477

Closed
jhammond2012 opened this issue Mar 25, 2023 · 4 comments
Closed

Disable a single line (or many) instead of the whole file #477

jhammond2012 opened this issue Mar 25, 2023 · 4 comments

Comments

@jhammond2012
Copy link

Has anyone tried to disable a single line or many instead of the whole file?

I tried creating a "ViewPlugin" but I caused more issues than I started with.

const disableSingleLine = ViewPlugin.fromClass(class {
...    
});
@jaywcjlove
Copy link
Member

Yes, it is possible to disable a single line or many in CodeMirror v6 by using the rangeset API. Here's an example of how to do it:

import React from 'react';
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
import { EditorView, highlightLineRange } from "@codemirror/view";
import { RangeSet } from "@codemirror/rangeset";

const lineToDisable = 5; // Replace with the line number you want to disable

// Create a RangeSet that disables the specified line
const rangeSet = RangeSet.of([{
  from: lineToDisable,
  to: lineToDisable + 1,
  mode: "disabled" // Disable the line
}]);

function App() {
  const onChange = React.useCallback((value, viewUpdate) => {
    console.log('value:', value);
  }, []);

  const ref = useRef();

  const update = () => {
    // Alternatively, you can update the RangeSet of an existing EditorView:
    ref.current.view.dispatch({
      effects: rangeSet.addEffect(),
    });
  }
  return (
    <CodeMirror
      value="console.log('hello world!');"
      height="200px"
      ref={ref}
      extensions={[
        javascript({ jsx: true }),
        // Add other extensions here
        rangeSet
      ]}
      onChange={onChange}
    />
  );
}

@jhammond2012

@jhammond2012
Copy link
Author

@jaywcjlove wow, thanks for the quick reply. I am getting a new error:
error

This is my code:

import React from "react";
import CodeMirror from "@uiw/react-codemirror";
import { rust } from "@codemirror/lang-rust";
import { EditorView } from "codemirror";
import { RangeSet } from "@codemirror/rangeset";

const lineToDisable = 1;

const rangeSet = RangeSet.of([
  {
    from: lineToDisable,
    to: lineToDisable + 1,
    mode: "disabled", // Disable the line
  },
]);

function Learn() {
  const code = `[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"`;

  const onChange = React.useCallback((value, viewUpdate) => {
    console.log("value:", value);
  }, []);

  return (
    <div>
      <CodeMirror
        value={code}
        className="rounded-tl-lg rounded-bl-lg"
        height="100%"
        theme="dark"
        extensions={[EditorView.lineWrapping, rust(), rangeSet]}
        onChange={onChange}
      />
    </div>
  );
}

export default Learn;

Thanks so much for your help!

@jhammond2012
Copy link
Author

Maybe it has been deprecated: https://www.npmjs.com/package/@codemirror/rangeset/v/0.19.2

@jaywcjlove
Copy link
Member

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

2 participants