-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
Comments
@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 |
@jaywcjlove I apologize if my initial inquiry wasn't clear. It seems there's no built-in option in |
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) { But I can't add any events to the react-codemirror's editor instance, the on function is not existing at all. |
You can try using the |
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. |
If possible, could you give me an example code? |
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.movThe 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}
/> |
@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 |
@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. |
It seems that there is no way to disable copy paste in the editor : Link
The text was updated successfully, but these errors were encountered: