choojs / choo-devtools

💼 - Expose a choo instance on the window
MIT License
53 stars 17 forks source link

expose .copy method #5

Closed jakeburden closed 7 years ago

jakeburden commented 7 years ago

ref: #4

allows for exposing a window.choo.copy method the lets users copy state to clipboard.

example:

window.addEventListener('keyup', function (e) {
  // press 'c' to copy current state to clipboard
  if (e.keyCode === 67) {
    window.choo.copy(window.choo.state)
  }
})

Note: it turns out document.execCommand('copy') only works with user gestured events such as click and keyup. Thats the reason why the example has an event listener. I had an idea too, it might be useful for there to be a choo-expose-ui that creates a nice button for choo devs to click and get application state in the clipboard. Would love to get your thoughts!

Cheers!

jakeburden commented 7 years ago

oh this works with plucker too!

window.addEventListener('keyup', function (e) {
  // press 'c' to copy value to clipboard
  if (e.keyCode === 67) {
    var object = { hello: { world: { lorem: 'ipsum' } } }
    window.choo.copy('hello.world.lorem', object)
  }
})

will copy 'ipsum' to the clipboard

yoshuawuyts commented 7 years ago

Nice work! Lil nit on the API tho, would be cool if this just workedâ„¢:

window.choo.copy()                 // => copied the whole state
window.choo.copy('state.form.foo') // => copied a specific path
yoshuawuyts commented 7 years ago

Also probably worth filtering out any functions b/c if they're stringified you get the weird [Object function] style blocks haha

jakeburden commented 7 years ago

hey @yoshuawuyts :wave: I just pushed up a change to this branch that lets .copy() get the whole state, it also checks if a function was passed in and if so, ignores it and just copies the whole state.

Not done yet though, I'll get this part working next:

window.choo.copy('state.form.foo') // => copied a specific path
jakeburden commented 7 years ago

just pushed up a change that lets window.choo.copy('state.form.foo') copy choo state at a specific path to clipboard :)

jakeburden commented 7 years ago

howdy @yoshuawuyts :wave:

are there any outstanding features of this api thats left to complete?

yoshuawuyts commented 7 years ago

v1.3.0 - added you as an owner too! :sparkles: