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

Editable prop not working #661

Open
prem-banker opened this issue Jun 11, 2024 · 7 comments
Open

Editable prop not working #661

prem-banker opened this issue Jun 11, 2024 · 7 comments

Comments

@prem-banker
Copy link

prem-banker commented Jun 11, 2024

Hi,

I am using React Codemirror in my next app with tailwind. For some reason, the editable prop (and read only) are not working for me. I am still able to edit the code in the editor. I checked CSS attributes and the contenteditable flag is also changing to false (which is how it seems to be working on the demo home page). Can you help me understand why this might be ?

using new version BTW
"@uiw/react-codemirror": "^4.22.2",

@ianizaguirre
Copy link

I have the same issue.

@ianizaguirre
Copy link

ianizaguirre commented Jun 13, 2024

@prem-banker

The only way i was able to make it work was to do something like this:

helper.ts


import { keymap } from '@codemirror/view';

/**
 * Prevents typing in the editor.
 */
function preventTyping() {
  return true; // Returning true in a command function prevents the action
}

/**
 *  Generates key bindings for alphabetic and numeric characters.
 */
function generateAlphaKeyBindings() {
  const bindings = [];
  // Loop through lowercase and uppercase letters
  for (let i = 0; i < 26; i++) {
    const lowerCase = String.fromCharCode(97 + i); // 'a' starts at ASCII code 97
    const upperCase = String.fromCharCode(65 + i); // 'A' starts at ASCII code 65
    bindings.push({ key: lowerCase, run: preventTyping, preventDefault: true });
    bindings.push({ key: upperCase, run: preventTyping, preventDefault: true });
  }

  // Loop through numeric keys
  for (let i = 0; i < 10; i++) {
    const number = i.toString();
    bindings.push({ key: number, run: preventTyping, preventDefault: true });
  }

  return bindings;
}

/**
 * Custom key handler that prevents all modifications to the editor.
 */
const customKeymap = keymap.of([
  ...generateAlphaKeyBindings(),

  { key: 'Space', run: preventTyping, preventDefault: true },
  { key: 'Enter', run: preventTyping, preventDefault: true },
  { key: 'Backspace', run: preventTyping, preventDefault: true },
  { key: 'Delete', run: preventTyping, preventDefault: true },
  // Add other specific keys or functionalities here
]);

export default customKeymap;

and then I import that object into my file and in the extensions array I add it:

  // Sets up the CodeMirror editor.
  const { setContainer } = useCodeMirror({
    value: jsonData,
    extensions: [
      json(),
      search({ top: true }),
      EditorView.lineWrapping,
      EditorState.readOnly.of(true),
      Prec.highest(EditorView.editable.of(false)),
      customKeymap,                  // <--------------------------- Here
    ],
    theme: 'light',
    height: '100%',
    readOnly: true,
    editable: false,
  });

  return (
    <div className={styles.wrapper}>
      <div
        ref={setContainer}
        style={{
          border: '1px solid #ccc',
          maxHeight: 'calc(100vh - 610px)',
        }}
      />
    </div>
  );

@prem-banker
Copy link
Author

prem-banker commented Jun 14, 2024

@ianizaguirre

Very Creative Workaround !! :) Another work around that was able to make to implement the Editable = false functionality was to do add a cusotm CSS class to the whole div surrounding the code editor.

.no-interaction { pointer-events: none !important; /* Prevent all interactions */ user-select: none !important; /* Prevent text selection */ }

This prevented me from making changes but ruined the UX a bit as user is not able to select/copy the code on their own and also the pointer does not change when you hover over it

@ianizaguirre
Copy link

ianizaguirre commented Jun 14, 2024

@ianizaguirre

Very Creative Workaround !! :) Another work around that was able to make to implement the Editable = false functionality was to do add a custom CSS class to the whole div surrounding the code editor.

.no-interaction { pointer-events: none !important; /* Prevent all interactions */ user-select: none !important; /* Prevent text selection */ }

This prevented me from making changes but ruined the UX a bit as user is not able to select/copy the code on their own and also the pointer does not change when you hover over it

Nice idea. The keymap way above still allows me to select/copy etc. so maybe that will solve your current problem instead of using the .no-interaction css class.

@jaywcjlove
Copy link
Member

@ianizaguirre @prem-banker I can't reproduce the error.

if (editable === false) {
getExtensions.push(EditorView.editable.of(false));
}
if (readOnly) {
getExtensions.push(EditorState.readOnly.of(true));
}

The tests are valid and there are no issues.

image

@nicolasmuller
Copy link

I have the same issue (using v4.23.5 version) :(

@jaywcjlove
Copy link
Member

@nicolasmuller I didn’t encounter any issues during my testing. Could you provide a reproducible example of the error? I’ll try to help troubleshoot the problem.

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