Permalink
Browse files

add e.file, e.html, e.plain etc

  • Loading branch information...
1 parent b526c58 commit 8b427e7f9d7394ed7ca30f0edd9df9f4e83e79f9 @tj tj committed Aug 30, 2012
Showing with 61 additions and 39 deletions.
  1. +0 −13 Readme.md
  2. +43 −15 index.js
  3. +18 −11 test/index.html
View
@@ -27,19 +27,6 @@ clip.on('cut', function(){
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){
- console.log(file.name)
- file.toDataURL(function(err, url){
- var img = new Image;
- img.src = url;
- document.body.appendChild(img);
- });
-});
```
### Clipboard#bind()
View
@@ -77,23 +77,51 @@ Clipboard.prototype.oncut = function(e){
Clipboard.prototype.onpaste = function(e){
var self = this;
var items = e.clipboardData.items;
+ var pending = items.length;
- // file
- if (items[1] && 'file' == items[1].kind) {
+ // file and filename
+ if (has(items, 'file')) {
+ // XXX: assuming ordering
e.file = file(items[1].getAsFile());
- }
+ items[0].getAsString(function(str){
+ e.plain = str;
+ e.file.name = str;
+ self.emit('paste', e);
+ });
+ return;
+ }
+
+ // get string
+ function string(item) {
+ var subtype = item.type.split('/').pop();
+ item.getAsString(function(str){
+ e[subtype] = str;
+ --pending || self.emit('paste', e);
+ });
+ }
+
+ // populate by subtype
+ var len = pending;
+ for (var i = 0; i < len; ++i) {
+ if ('string' != items[i].kind) continue;
+ string(items[i]);
+ }
+};
- // text
- items[0].getAsString(function(str){
- e.text = str;
- self.emit('paste', e);
+/**
+ * Check if `items` has an item with `type`
+ * and return that item.
+ *
+ * @param {Arrayish} items
+ * @param {String} type
+ * @return {Object}
+ * @api public
+ */
- // file
- if (e.file) {
- e.file.name = str;
- self.emit('paste file', e.file, e);
- } else {
- self.emit('paste text', str, e);
+function has(items, type) {
+ for (var i = 0; i < items.length; ++i) {
+ if (type == items[i].kind) {
+ return items[i];
}
- });
-};
+ }
+}
View
@@ -19,18 +19,25 @@
var clip = new Clipboard(window);
- clip.on('paste file', function(file){
- console.log('paste file "%s"', file.name);
- file.toDataURL(function(err, url){
- var img = new Image;
- img.src = url;
- document.body.appendChild(img);
- clip.unbind();
- });
- });
+ clip.on('paste', function(e){
+ // file
+ if (e.file) {
+ console.log('paste file "%s"', e.file.name);
+ e.file.toDataURL(function(err, url){
+ var img = new Image;
+ img.src = url;
+ document.body.appendChild(img);
+ });
+ return;
+ }
+
+ if (e.html) {
+ console.log('html "%s"', e.html);
+ }
- clip.on('paste text', function(str){
- console.log('paste "%s"', str);
+ if (e.plain) {
+ console.log('plain "%s"', e.plain);
+ }
});
clip.on('cut', function(){

0 comments on commit 8b427e7

Please sign in to comment.