Permalink
Browse files

Initial commit

  • Loading branch information...
tj committed Aug 16, 2012
0 parents commit 7644fb3c85684de9b9c478c63e9f5a56402ef051
Showing with 230 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +11 −0 History.md
  3. +11 −0 Makefile
  4. +54 −0 Readme.md
  5. +11 −0 component.json
  6. +92 −0 index.js
  7. +48 −0 test/index.html
@@ -0,0 +1,3 @@
node_modules
build
components
@@ -0,0 +1,11 @@
0.0.3 / 2012-07-11
==================
* add `.listeners()`
* rename `.has()` to `.hasListeners()`
0.0.2 / 2012-06-28
==================
* fix `.off()` with `.once()`-registered callbacks
@@ -0,0 +1,11 @@
build: index.js components
@component build
components:
@component install
clean:
rm -fr build components
.PHONY: clean
@@ -0,0 +1,54 @@
# Clipboard
Clipboard library.
## Installation
```
$ component install component/clipboard
```
## Example
```js
var Clipboard = require('clipboard');
var clip = new Clipboard(window);
clip.on('paste', function(){
console.log('paste');
});
clip.on('cut', function(){
console.log('cut');
});
clip.on('copy', function(e){
console.log('copy');
});
clip.on('paste text', function(text){
console.log('paste "%s"', text);
});
clip.on('paste file', function(file){
file.toDataURL(function(err, url){
var img = new Image;
img.src = url;
document.body.appendChild(img);
});
});
```
### Clipboard#bind()
Bind event handlers. This is done for you in the constructor.
### Clipboard#unbind()
Unbind event handlers.
## License
MIT
@@ -0,0 +1,11 @@
{
"name": "clipboard",
"description": "Clipboard API wrapper",
"keywords": ["copy", "paste", "file", "clipboard"],
"version": "0.0.1",
"scripts": ["index.js"],
"dependencies": {
"component/file": "*",
"component/emitter": "*"
}
}
@@ -0,0 +1,92 @@
/**
* Module dependencies.
*/
var file = require('file')
, Emitter = require('emitter');
/**
* Expose `Clipboard`.
*/
module.exports = Clipboard;
/**
* Initialize a `Clipboard`.
*
* @api private
*/
function Clipboard(el) {
Emitter.call(this);
this.el = el;
this.bind();
}
/**
* Inherits from `Emitter.prototype`.
*/
Clipboard.prototype.__proto__ = Emitter.prototype;
/**
* Bind event handlers.
*
* @api public
*/
Clipboard.prototype.bind = function(){
this.el.addEventListener('paste', this._paste = this.onpaste.bind(this), false);
this.el.addEventListener('copy', this._copy = this.oncopy.bind(this), false);
this.el.addEventListener('cut', this._cut = this.oncut.bind(this), false);
};
/**
* Unbind event handlers.
*
* @api public
*/
Clipboard.prototype.unbind = function(arg){
this.el.removeEventListener('paste', this._paste);
this.el.removeEventListener('copy', this._copy);
this.el.removeEventListener('cut', this._cut);
};
/**
* Handle copy.
*/
Clipboard.prototype.oncopy = function(e){
this.emit('copy', e);
};
/**
* Handle cut.
*/
Clipboard.prototype.oncut = function(e){
this.emit('cut', e);
};
/**
* Handle paste.
*/
Clipboard.prototype.onpaste = function(e){
var self = this;
var items = e.clipboardData.items;
this.emit('paste', e);
for (var i = 0; i < items.length; ++i) {
if ('file' == items[i].kind) {
this.emit('paste file', file(items[i].getAsFile()));
continue;
}
items[i].getAsString(function(str){
self.emit('paste text', str);
});
}
};
@@ -0,0 +1,48 @@
<DOCTYPE html>
<html>
<head>
<title>Clipboard</title>
<style>
body {
padding: 50px;
}
textarea {
padding: 15px;
}
</style>
</head>
<body>
<script src="../build/build.js"></script>
<textarea>Hello world</textarea>
<script>
var Clipboard = require('clipboard');
var clip = new Clipboard(window);
clip.on('paste', function(){
console.log('paste');
});
clip.on('cut', function(){
console.log('cut');
});
clip.on('copy', function(e){
console.log('copy');
});
clip.on('paste text', function(str){
console.log('paste "%s"', str);
});
clip.on('paste file', function(file){
file.toDataURL(function(err, url){
var img = new Image;
img.src = url;
document.body.appendChild(img);
clip.unbind();
});
});
</script>
</body>
</html>

0 comments on commit 7644fb3

Please sign in to comment.