ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Is is not possible to force Ckeditor5 to always paste plain text

Open Przemek625 opened this issue 2 years ago • 5 comments

I reviewed this code:

https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-clipboard/src/pasteplaintext.ts

It looks like the plain text is pasted to editor only if user use 'ctr + shift +v'. How to make it work for 'ctrl +v'? Thre is no easy way to configure it so I report a bug.

Anyway how to work around it?

Przemek625 avatar Jan 27 '23 00:01 Przemek625

Hi, did you see the paste as plain text plugin example? This might be a good starting point for your use case.

FilipTokarski avatar Feb 02 '23 15:02 FilipTokarski

Hi, I'm facing the same problem as Przemek625. Is there a solution for it ? Thanks.

maryahcm avatar Dec 12 '23 18:12 maryahcm

I also think that plugin should have option to force "paste as plain text' by default.

piernik avatar Mar 07 '24 12:03 piernik

As part of the create().then().catch() of initialising the plugin, I have added the below which works for me.

This is definitely an interim, rather than a plugin option.

CKEDITOR.ClassicEditor.create(
// your init code here
)
.then( editor => {
        editor.editing.view.document.on('clipboardInput', (evt, data) => {
            const plainText = data.dataTransfer.getData('text/plain');
            
            // Prevent the default behavior
            evt.stop();

            // Insert plain text into the editor
            editor.model.change(writer => {
                const insertPosition = editor.model.document.selection.getFirstPosition();
                writer.insertText(plainText, insertPosition);
            });
        });
  }).catch(error => {
            console.error(error);
    });

kb-freelance avatar Aug 26 '24 19:08 kb-freelance

I'm trying if the data contains table ,tr,td or figure html then remove this and other formatting should not get removed for example see below:

Problem statement: The limited accessibility for first and last-mile connectivity for transit stations poses a multifaceted problem with significant implications for public transport usage, environmental sustainability, public health, and social equity. Addressing this issue requires a comprehensive and context-sensitive approach that evaluates the current state of infrastructure. Objectives: To develop a context-sensitive accessibility evaluation f_ramework to enhance first- and last-mile connectivity and promote public transport usage, environmental sustainability, public health, and_ social equity.

  editor.editing.view.document.on('clipboardInput', (evt, data) => {
                          // Retrieve plain text and HTML content from the clipboard
                          const plainText = data.dataTransfer.getData('text/plain');
                          const htmlContent = data.dataTransfer.getData('text/html');
  
                          // Check if the HTML content contains table, figure, or cell-related tags
                          const containsTableOrFigure = /<(table|fig(ure|caption)|td|th)>/i.test(htmlContent);
  
                          if (containsTableOrFigure) {
                              // Prevent the default behavior
                              evt.stop();
  
                              // Insert plain text into the editor
                              editor.model.change(writer => {
                                  const insertPosition = editor.model.document.selection.getFirstPosition();
                                  writer.insertText(plainText, insertPosition);
                              });
                          }
                      });

Rohit4508 avatar Dec 13 '24 11:12 Rohit4508