Monday, June 3, 2024
76
rated 0 times [  83] [ 7]  / answers: 1 / hits: 145908  / 11 Years ago, mon, july 8, 2013, 12:00:00

When you hover over a card in Trello and press Ctrl+C, the URL of this card is copied to the clipboard. How do they do this?



As far as I can tell, there is no Flash movie involved. I've got Flashblock installed, and the Firefox network tab shows no Flash movie loaded. (That's the usual method, for example, by ZeroClipboard.)



How do they achieve this magic?



(Right at this moment I think I had an epiphany: You cannot select text on the page, so I assume they have an invisible element, where they create a text selection via JavaScript code, and Ctrl+C triggers the browser's default behaviour, copying that invisible node's text value.)


More From » coffeescript

 Answers
79

Disclosure: I wrote the code that Trello uses; the code below is the actual source code Trello uses to accomplish the clipboard trick.




We don't actually "access the user's clipboard", instead we help the user out a bit by selecting something useful when they press Ctrl+C.


Sounds like you've figured it out; we take advantage of the fact that when you want to hit Ctrl+C, you have to hit the Ctrl key first. When the Ctrl key is pressed, we pop in a textarea that contains the text we want to end up on the clipboard, and select all the text in it, so the selection is all set when the C key is hit. (Then we hide the textarea when the Ctrl key comes up.)


Specifically, Trello does this:


TrelloClipboard = new class
constructor: ->
@value = ""

$(document).keydown (e) =>
# Only do this if there's something to be put on the clipboard, and it
# looks like they're starting a copy shortcut
if !@value || !(e.ctrlKey || e.metaKey)
return

if $(e.target).is("input:visible,textarea:visible")
return

# Abort if it looks like they've selected some text (maybe they're trying
# to copy out a bit of the description or something)
if window.getSelection?()?.toString()
return

if document.selection?.createRange().text
return

_.defer =>
$clipboardContainer = $("#clipboard-container")
$clipboardContainer.empty().show()
$("<textarea id='clipboard'></textarea>")
.val(@value)
.appendTo($clipboardContainer)
.focus()
.select()

$(document).keyup (e) ->
if $(e.target).is("#clipboard")
$("#clipboard-container").empty().hide()

set: (@value) ->

In the DOM we've got:


<div id="clipboard-container"><textarea id="clipboard"></textarea></div>

CSS for the clipboard stuff:


#clipboard-container {
position: fixed;
left: 0px;
top: 0px;
width: 0px;
height: 0px;
z-index: 100;
display: none;
opacity: 0;
}
#clipboard {
width: 1px;
height: 1px;
padding: 0px;
}

... and the CSS makes it so you can't actually see the textarea when it pops in ... but it's "visible" enough to copy from.


When you hover over a card, it calls


TrelloClipboard.set(cardUrl)

... so then the clipboard helper knows what to select when the Ctrl key is pressed.


[#77134] Saturday, July 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
briannar

Total Points: 354
Total Questions: 103
Total Answers: 101

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
briannar questions
Tue, Aug 31, 21, 00:00, 3 Years ago
Sat, Jun 26, 21, 00:00, 3 Years ago
Sat, Jun 20, 20, 00:00, 4 Years ago
Tue, Apr 7, 20, 00:00, 4 Years ago
;