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

How To Disable Copy Paste in Editor? #650

Open
SakshamSahgal opened this issue Apr 27, 2024 · 13 comments
Open

How To Disable Copy Paste in Editor? #650

SakshamSahgal opened this issue Apr 27, 2024 · 13 comments

Comments

@SakshamSahgal
Copy link

SakshamSahgal commented Apr 27, 2024

It seems that there is no way to disable copy paste in the editor : Link

@SakshamSahgal SakshamSahgal changed the title How To Disable Copy Paste? How To Disable Copy Paste in Editor? Apr 27, 2024
@jaywcjlove
Copy link
Member

@SakshamSahgal I don't understand what you mean, I don't know if the following method can solve your problem.

https://uiwjs.github.io/react-codemirror/#/extensions/events

@SakshamSahgal
Copy link
Author

@jaywcjlove I apologize if my initial inquiry wasn't clear. It seems there's no built-in option in @uiw/react-codemirror to disable copy-paste while allowing typing. Maybe adding a parameter to define this behavior could be beneficial.

@jaywcjlove
Copy link
Member

The 'events' extension can achieve this, I think that's enough."

jaywcjlove added a commit that referenced this issue Jun 7, 2024
github-actions bot pushed a commit that referenced this issue Jun 7, 2024
@sailxjx
Copy link

sailxjx commented Aug 12, 2024

The 'events' extension can achieve this, I think that's enough."

The events extension is useless for this, although the paste event can be obtained, it cannot prevent the modification of the dom content.

There is a solution when using raw codemirror, I found this in the forum:

editor.on(“beforeChange”, function(_, change) {
if (change.origin == “paste”) change.cancel()
})

But I can't add any events to the react-codemirror's editor instance, the on function is not existing at all.

@jaywcjlove
Copy link
Member

@sailxjx

You can try using the @uiw/codemirror-extensions-events extension.

@sailxjx
Copy link

sailxjx commented Aug 21, 2024

@sailxjx

You can try using the @uiw/codemirror-extensions-events extension.

Boss, I tried it before saying it doesn't work 🧎, although this plugin can capture the paste event, it cannot prevent pasting content into the text box.

@sailxjx
Copy link

sailxjx commented Aug 21, 2024

@sailxjx

You can try using the @uiw/codemirror-extensions-events extension.

If possible, could you give me an example code?

@jaywcjlove
Copy link
Member

@sailxjx

import CodeMirror from '@uiw/react-codemirror';
import { langs } from '@uiw/codemirror-extensions-langs';
import * as events from '@uiw/codemirror-extensions-events';
import { Fragment } from 'react';

/**
 * https://github.com/uiwjs/react-codemirror/issues/650
 */
export function Component() {
  const extension = events.content({
    paste: (event) => {
      event.preventDefault();
    },
  });
  return (
    <Fragment>
      <CodeMirror
        value={`console.log('hello')`}
        theme="none"
        height="400px !important"
        width="100%"
        style={{ margin: '0 0 23px 0', flex: 1 }}
        extensions={[langs.markdown(), extension]}
      />
    </Fragment>
  );
}

jaywcjlove added a commit that referenced this issue Aug 21, 2024
github-actions bot pushed a commit that referenced this issue Aug 21, 2024
@sailxjx
Copy link

sailxjx commented Aug 21, 2024

@sailxjx

import CodeMirror from '@uiw/react-codemirror';
import { langs } from '@uiw/codemirror-extensions-langs';
import * as events from '@uiw/codemirror-extensions-events';
import { Fragment } from 'react';

/**
 * https://github.com/uiwjs/react-codemirror/issues/650
 */
export function Component() {
  const extension = events.content({
    paste: (event) => {
      event.preventDefault();
    },
  });
  return (
    <Fragment>
      <CodeMirror
        value={`console.log('hello')`}
        theme="none"
        height="400px !important"
        width="100%"
        style={{ margin: '0 0 23px 0', flex: 1 }}
        extensions={[langs.markdown(), extension]}
      />
    </Fragment>
  );
}

Don't work, even failed to capture the event, my codemirror version is 4.22.0

Screen.Recording.2024-08-21.at.19.47.24.mov

The code:

const extension = events.content({
      paste: (event) => {
        event.preventDefault();
      },
    });
    return (
      <div className={clsx(className, styles.editorContainer)}>
        <CodeMirror
          height={`${size.height}px`}
          width={`${size.width}px`}
          value={editorContent}
          placeholder={`# ${t('Enter the code here and click the Run button to execute.')}`}
          extensions={[langs.markdown(), extension]}
          // extensions={[...(codeMirrorExts[codeType] || []), events.content({
          //   paste: (event) => {
          //     console.log('paste', event);
          //     event.preventDefault();
          //   },
          // })]}
          onChange={onChange}
        // theme={editorThemeConfig}
        />

@jaywcjlove
Copy link
Member

@sailxjx My test example can disable pasting. https://uiwjs.github.io/react-codemirror/#/examples/650

@sailxjx
Copy link

sailxjx commented Aug 24, 2024

@sailxjx My test example can disable pasting. https://uiwjs.github.io/react-codemirror/#/examples/650

Thank you, I downloaded your sample code and ran it locally. I couldn't paste it, but the same code in my code repository still doesn't work. I am still looking for the reason. These are all my dependencies on codemirror.

@codemirror/lang-cpp 6.0.2
@codemirror/lang-html 6.4.9
@codemirror/lang-javascript 6.2.2
@codemirror/lang-python 6.1.6
@uiw/codemirror-extensions-events 4.23.0
@uiw/codemirror-extensions-langs 4.23.0
@uiw/codemirror-theme-github 4.22.0
@uiw/react-codemirror 4.23.0

@jaywcjlove
Copy link
Member

@sailxjx If you don't provide a reproducible example of the error, I won't be able to help you.

@sailxjx
Copy link

sailxjx commented Aug 25, 2024

@sailxjx If you don't provide a reproducible example of the error, I won't be able to help you.

I cannot provide you with the code because it is embedded in a large amount of production code, but I have found the location of the problem as shown in the screenshot below. If I remove the return comment inside, I can successfully block the paste event, otherwise I cannot block the paste event.

The code after return: updateUser is to update the global state, which I also included in the screenshot. I don't understand why it's like this and don't know how to modify it, because updateUser is necessary here.

Screenshot 2024-08-25 at 13 43 43 Screenshot 2024-08-25 at 13 46 37

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

3 participants