react-json-view icon indicating copy to clipboard operation
react-json-view copied to clipboard

Custom Copy Behavior

Open danie16arrido opened this issue 7 years ago • 11 comments

Is there an example on how to implement callbacks when copying to clipboard. eg: copy a value and then capitalize it?

Thanks,

danie16arrido avatar Feb 15 '18 17:02 danie16arrido

@danie16arrido - sorry for the delayed reply. An event does not get thrown onCopy, so you would not be able to implement the example you described.

if you wanted to implement this in a fork, you could add an onCopy prop and call it when the clipboard icon is clicked.

that code would need to live in the handleCopy method here: https://github.com/mac-s-g/react-json-view/blob/master/src/js/components/CopyToClipboard.js#L27

does that answer your question?

mac-s-g avatar Feb 21 '18 17:02 mac-s-g

@mac-s-g thanks for taking time answering my query. it does answer my question and I will give it a go later in the week and report back. Changing of subject how hard will it be to change the copy behaviour in order to copy the value without quotation marks "string" : "this is a test ..." and get this is a test ... instead of "this is a test ..." Will it be the same as capitalizing? thanks,

danie16arrido avatar Feb 22 '18 22:02 danie16arrido

it's kindof a consistency thing. right now all values are copied as valid JSON values.

so you could paste the result directly into a json parser.

I don't think I'd want to change that outside of incorporating a prop to allow custom copy behaviors.

mac-s-g avatar Feb 24 '18 22:02 mac-s-g

It looks like the enableClipboard prop now supports a callback function 👍

My only request (which seems in line with this ticket) is to pass more information. I am trying to copy the JSON path of the node instead of its JSON contents.

@danie16arrido For your use case it looks like this syntax should work:

        <ReactJsonView
          name={null}
          src={{"name": "value"}}
          enableClipboard={e => console.log(e.source) }
        />

You can then intercept the event (the copied text is available at e.src) and use your own handler to modify it, then copy it again to the clipboard. You have to use your own clipboard handler. For example, here's a function from this SO post

function copyToClipboard(text){
    var dummy = document.createElement("input");
    document.body.appendChild(dummy);
    dummy.setAttribute('value', text);
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
}
copyToClipboard('Hello, World!')

It seems like accessing the JSON path of the current node might be a common need, so maybe it's worth offering that as an option? For now, I could be content with enough data to build the path myself.

Thanks for everyone's work on this excellent component! It is a big part of our product 🙌

rsweetland avatar Jul 19 '18 18:07 rsweetland

@danie16arrido I needed the same thing, I forked the repository and changed a single line, now copy works without apostrophes for strings and still doing JSON.stringify for objects.

eyalap avatar Oct 11 '18 12:10 eyalap

@danie16arrido do you want to post a PR? or do you think the change is specific to your use case?

mac-s-g avatar Oct 11 '18 17:10 mac-s-g

I would use the copy-without-quotes feature if it was available!

rasmusgo avatar Apr 19 '19 13:04 rasmusgo

It looks like the enableClipboard prop now supports a callback function

My only request (which seems in line with this ticket) is to pass more information. I am trying to copy the JSON path of the node instead of its JSON contents.

@danie16arrido For your use case it looks like this syntax should work:

        <ReactJsonView
          name={null}
          src={{"name": "value"}}
          enableClipboard={e => console.log(e.source) }
        />

You can then intercept the event (the copied text is available at e.src) and use your own handler to modify it, then copy it again to the clipboard. You have to use your own clipboard handler. For example, here's a function from this SO post

function copyToClipboard(text){
    var dummy = document.createElement("input");
    document.body.appendChild(dummy);
    dummy.setAttribute('value', text);
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
}
copyToClipboard('Hello, World!')

It seems like accessing the JSON path of the current node might be a common need, so maybe it's worth offering that as an option? For now, I could be content with enough data to build the path myself.

Thanks for everyone's work on this excellent component! It is a big part of our product

@rsweetland Is accessing the JSON path of the current node implemented? I would be interested to see how this works out. Thanks.

chaiwa-berian avatar Mar 15 '21 13:03 chaiwa-berian

copy-without-quotes can be used?

tianyan89 avatar Apr 07 '21 05:04 tianyan89

you may need to create functions to make it,As I also need this feature.WDYT?

zffocussss avatar Sep 16 '22 10:09 zffocussss

it is simple. @tianyan89

 <ReactJsonView
          name={null}
          src={{"name": "value"}}
          enableClipboard={(e) => navigator.clipboard.writeText(e.src) }
  />

zffocussss avatar Sep 16 '22 10:09 zffocussss