0

I tried the following but I can keep pasting onto the checkeditor?

using Classic version of ckEditor 5

$(document).ready(function () {
      var ambit = $(document);

      // Disable Cut + Copy + Paste (input)
      ambit.on('copy paste cut', function (e) {
          e.preventDefault(); //disable cut,copy,paste
          return false;
      });

      // Disable Cut + Copy + Paste and Browser Admin Tools (all document)
      ambit.keydown(function (e) {
          var forbiddenCtrlKeys = new Array('c', 'x', 'v', 'ins', 'u');
          var forbiddenShiftKeys = new Array('del', 'ins', 'f2', 'f4', 'f7');
          var forbiddenCtrlShiftKeys = new Array('k', 'i', 'm', 's', 'j');
          var keyCode = (e.keyCode) ? e.keyCode : e.which;

          var isCtrl, isShift;
          isCtrl = e.ctrlKey;
          isShift = e.ctrlShift;

          string = getKeyCodeString(keyCode);

          if (string == 'f12')
          {
              e.preventDefault();
              return false;
          }

          if (isCtrl && !isShift) {
              for (i = 0; i < forbiddenCtrlKeys.length; i++) {
                  if (forbiddenCtrlKeys[i] == string) {
                      e.preventDefault();
                      return false;
                  }
              }
          }

          if (!isCtrl && isShift) {
              for (i = 0; i < forbiddenShiftKeys.length; i++) {
                  if (forbiddenShiftKeys[i] == string) {
                      e.preventDefault();
                      return false;
                  }
              }
          }

          if (isCtrl && isShift) {
              for (i = 0; i < forbiddenCtrlShiftKeys.length; i++) {
                  if (forbiddenCtrlShiftKeys[i] == string) {
                      e.preventDefault();
                      return false;
                  }
              }
          }

          return true;
      });

      var getKeyCodeString = function(keyCode)
      {
          var string;
          switch (keyCode) {
              case 45:
                  string = 'ins'; break;
              case 46:
                  string = 'del'; break;
              case 113:
                  string = 'f2'; break;
              case 115:
                  string = 'f4'; break;
              case 118:
                  string = 'f7'; break;
              case 123:
                  string = 'f12'; break;
              default:
                  string = String.fromCharCode(keyCode);
                  break;
          }
          return string.toLowerCase();
      }

    function ignorePaste() {
      $("[data-ignorepaste]").on("cut copy paste", function (e) {
        e.preventDefault(); //prevent the default behaviour 
      });
    };

    $(".ck-editor__editable").attr("data-ignorepaste");
      ignorePaste();
  });
rob.m
  • 9,843
  • 19
  • 73
  • 162

1 Answers1

0

You should preventDefault on the paste event itself, on the window level or on a parent of that <textarea> if you don't want to disable paste globally.

Make sure you include true as the 3rd parameter of addEventListener so the event is stopped in the capture phase.

window.addEventListener('paste', e => {
  e.preventDefault()
  e.stopPropagation()
}, true)
<h4> Paste disabled: </h4>
<textarea id="textarea"></textarea>
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • Yeah if you look at the bottom of my code that is what I do. But it doesn't work, must be something with the editor which replaces the textarea by the way... – rob.m Dec 06 '18 at 17:25
  • same thing, there is this module but it is really confusing https://ckeditor.com/docs/ckeditor5/latest/api/module_clipboard_clipboard-Clipboard.html – rob.m Dec 06 '18 at 17:28
  • I've edited my answer to prevent the event on the [capture phase](https://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing). Try it out. – nicholaswmin Dec 06 '18 at 17:29
  • I can paste, as I said, the plugin replaces the textarea – rob.m Dec 06 '18 at 17:32
  • Preventing & Stopping the event should be enough. It doesn't matter if ckEditor is replacing the textarea. It could be that ckEditor is capturing the event on window. Try `window.addEventListener` instead. You also need to add `stopPropagation`. Also make sure you pass `true` as the 3rd parameter of `addEventListener`. – nicholaswmin Dec 06 '18 at 17:34
  • yes! It works, thanks a lot, last version fo your answer worked – rob.m Dec 06 '18 at 17:37