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

[Angular 18] [ngx-quill: 26.0.10] How to disable copy paste images from Desktop in Quill Editor? #4568

Open
so-amuk opened this issue Jan 15, 2025 · 0 comments

Comments

@so-amuk
Copy link

so-amuk commented Jan 15, 2025

We are attempting to disable the copy-paste functionality from the desktop in the Quill Editor. However, the Quill Editor should still support image uploads. The solution should not completely block image functionality within the editor; it should only restrict copy-pasting. I have tried various solutions to prevent copy-pasting from tools like Snipping Tool or directly from the desktop, but none have been successful.

These are the following solutions that we implemented but none of them worked:

this.quillEditor.on('editor-change', () => {
  // Add paste event listener to the editor's container
  const quillContainer = this.quillEditor.container;

  if (quillContainer) {
    quillContainer.addEventListener('paste', (event: ClipboardEvent) => {
      console.log('onPaste triggered');
      const clipboardData = event.clipboardData;
      if (clipboardData && clipboardData.files.length > 0) {
        for (let i = 0; i < clipboardData.files.length; i++) {
          const file = clipboardData.files[i];
          if (file.type.startsWith('image/')) {
            // Prevent the default paste action for images
            event.preventDefault();
            console.log('Pasting image blocked');
            return;
          }
        }
      }
    });
  }
});
onPaste: (event: ClipboardEvent) => {
              console.log('onPaste');
              const clipboardData = event.clipboardData; // Use clipboardData from the event
              if (clipboardData && clipboardData.files.length > 0) {
                for (let i = 0; i < clipboardData.files.length; i++) {
                  const file = clipboardData.files[i];
                  if (file.type.startsWith('image/')) {
                    // Prevent the default paste action for clipboard images
                    event.preventDefault();
                    return;
                  }
                }
              }
   }
this.quill= {
  ...this.quill, // Keep the existing configuration
  clipboard: {
    matchers: [
      [
        Node.ELEMENT_NODE,
        (node: any, delta: any) => {
          // Allow all elements except the pasted images from the desktop
          return delta;
        },
      ],
    ],
    // Add a custom paste handler to block images from the clipboard
    onPaste: (event: ClipboardEvent) => {
      const clipboardData = event.clipboardData; // Use clipboardData from the event
      if (clipboardData && clipboardData.files.length > 0) {
        for (let i = 0; i < clipboardData.files.length; i++) {
          const file = clipboardData.files[i];
          if (file.type.startsWith('image/')) {
            // Prevent the default paste action for clipboard images
            event.preventDefault();
            return;
          }
        }
      }
    },
  },
};
@so-amuk so-amuk changed the title How to disable copy paste images from Desktop in Quill Editor? [Angular] How to disable copy paste images from Desktop in Quill Editor? Jan 15, 2025
@so-amuk so-amuk changed the title [Angular] How to disable copy paste images from Desktop in Quill Editor? [Angular 18] [ngx-quill: 26.0.10] How to disable copy paste images from Desktop in Quill Editor? Jan 15, 2025
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

1 participant