Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable select, allow copy/paste

I'm building an app which overrides standard selecting behaviour and allow copying and pasting elements. The problem is that if I disable selecting, copy events are also gone.

I tried using

onselectstart="return false;"

and

.no-select {     
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;  
}

and it works, but it also disable copy event.

I also tried adding .no-select attribute only for these parts which contain text, but it is hard to maintain and does not work well - sometimes copy events are blocked and I cannot control it.

How can I disable select, but enable copy/paste proper way?


Edit:

  • I do not want to copy text, but my own json structures. Copying is handled in onCopy handler.
  • I need to subscribe to standard chrome copy events launched by chrome menu or system shortuts.
like image 580
remdezx Avatar asked Feb 24 '17 11:02

remdezx


1 Answers

When you disabled highlighting/selecting then what you want copy? Not selected things is still nothing

I don't want to copy text (which is standard behaviour), but my own json representation of objects

Then i have 2 solutions to your problem:

  1. Override context menu with function copying to clipboard (tutorial and library)

    if (document.addEventListener) {
            document.addEventListener('contextmenu', function(e) {
                alert("Write own menu with copy");
                e.preventDefault();
            }, false);
        } else {
            document.attachEvent('oncontextmenu', function() {
                alert("Write own menu with copy");
                window.event.returnValue = false;
            });
        }
    body {     
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;  
    }
    <body>
    Some text
    </body>
  2. Add "Copy" button with function copying to clipboard (tutorial and library)

  3. Bind key combination ctrl + c (and other like command + c) with function copying to clipboard (tutorial and library)
  4. Use Flash or other external browser addon to provide copy to clipboard function (not recommended)
like image 58
J. Doe Avatar answered Sep 30 '22 15:09

J. Doe