Question

I am trying to select all the content from a CKEditor instance and copy it to the clipboard when the user clicks a button on a different part of the screen. When the page initially loads, and I click the "Copy to Clipboard" button, the selectAll command works, but the copy command does not work. However, if I click the button a second time, everything works correctly.

Does anyone know how to resolve this issue? I am new to CKEditor, but this seems like a focus or timing issue. I'd rather not have to code phantom button clicks to make this work. Any help is greatly appreciated.

Here is my code. As you can see, I also tried wrapping the copy command in the "afterCommandExec" callback from the "selectAll" command. That didn't work either.

<html>
<head>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript">
var g_editorObj = new Object;

window.onload = function () {
    g_editorObj = CKEDITOR.replace('editor1');       
}

function doCopyClipBoard() {
    try {
        g_editorObj.focus();
        g_editorObj.execCommand("selectAll");
        g_editorObj.execCommand("copy");
        //g_editorObj.on('afterCommandExec', handleCopy);
    } catch (ex) {
        alert(ex.Message);
    }
}

function handleCopy() {
    g_editorObj.execCommand("copy");
}    
</script>
</head>
<body>
<input type="button" onclick="doCopyClipBoard()" value="Copy to Clipboard" /><br />
<textarea id="editor1" name="editor1" rows="10" cols="80">Testing this thing</textarea>
</body>
</html>
Était-ce utile?

La solution

Please check if this order of things will work for you:

editor.focus();
editor.once( 'selectionChange', function() {
    editor.execCommand( 'copy' );
} );
editor.execCommand( 'selectAll' );

or alternatively:

editor.execCommand( 'selectAll' );
setTimeout( function() {
    editor.execCommand( 'copy' );
}, 0 );

Note that access to the clipboard is only possible in IEs. It's a matter browser limitations and there's no way to bypass that thing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top