clipboard
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -25,3 +25,3 @@ var path = require('path'); | ||
var builtin = platform.formats[format]; | ||
ret.push(callback(format, builtin || platform.formatName(format), !builtin)); | ||
ret.push(callback(format, platform.formatName(format), !builtin)); | ||
} | ||
@@ -69,6 +69,6 @@ platform.unref(); | ||
} | ||
platform.write(type, item); | ||
if (type === 'jsobject') { | ||
platform.write('ascii', util.inspect(item)); | ||
} | ||
platform.write(type, item); | ||
}); | ||
@@ -85,3 +85,3 @@ | ||
read: function read(format){ | ||
format = format || 'ascii'; | ||
if (!format) return read('jsobject') || read('ascii'); | ||
platform.ref(); | ||
@@ -133,2 +133,1 @@ var result = platform.read(format); | ||
}; | ||
@@ -6,3 +6,2 @@ var ffi = require('node-ffi'); | ||
Pointer: ffi.Pointer, | ||
constants: constants, | ||
errno: ffi.errno, | ||
@@ -51,17 +50,1 @@ NULL: ffi.Pointer.NULL, | ||
// convenience wrapper around sets of constants for translating back and forth | ||
function constants(name, map){ | ||
map = Object.keys(map).reduce(function(ret, key){ | ||
var val = map[key]; | ||
ret[key] = val; | ||
ret[val] = key; | ||
return ret; | ||
}, {}); | ||
name += '_'; | ||
var lookup = function(v){ | ||
return map[v] || map[name+(v+'').toUpperCase()]; | ||
}; | ||
lookup.count = map.length / 2; | ||
return lookup; | ||
} | ||
156
lib/win32.js
@@ -11,7 +11,7 @@ var ffi = require('./ffi'); | ||
var GMEM = ffi.constants('GMEM',{ | ||
GMEM_FIXED: 0x0000, | ||
GMEM_MOVEABLE: 0x0002, | ||
GMEM_ZEROINIT: 0x0040, | ||
}); | ||
var GMEM = { | ||
FIXED: 0x0000, | ||
MOVEABLE: 0x0002, | ||
ZEROINIT: 0x0040, | ||
}; | ||
@@ -37,32 +37,33 @@ | ||
var CF = ffi.constants('CF', { | ||
CF_TEXT : 1, | ||
CF_BITMAP : 2, | ||
CF_METAFILEPICT : 3, | ||
CF_SYLK : 4, | ||
CF_DIF : 5, | ||
CF_TIFF : 6, | ||
CF_OEMTEXT : 7, | ||
CF_DIB : 8, | ||
CF_PALETTE : 9, | ||
CF_PENDATA : 10, | ||
CF_RIFF : 11, | ||
CF_WAVE : 12, | ||
CF_UNICODETEXT : 13, | ||
CF_ENHMETAFILE : 14, | ||
CF_HDROP : 15, | ||
CF_LOCALE : 16, | ||
CF_DIBV5 : 17, | ||
CF_MAX : 18, | ||
CF_OWNERDISPLAY : 0x0080, | ||
CF_DSPTEXT : 0x0081, | ||
CF_DSPBITMAP : 0x0082, | ||
CF_DSPMETAFILEPICT : 0x0083, | ||
CF_DSPENHMETAFILE : 0x008E, | ||
CF_PRIVATEFIRST : 0x0200, | ||
CF_PRIVATELAST : 0x02FF, | ||
CF_GDIOBJFIRST : 0x0300, | ||
CF_GDIOBJLAST : 0x03FF, | ||
}); | ||
var CF = { | ||
TEXT : 1, | ||
BITMAP : 2, | ||
METAFILEPICT : 3, | ||
SYLK : 4, | ||
DIF : 5, | ||
TIFF : 6, | ||
OEMTEXT : 7, | ||
DIB : 8, | ||
PALETTE : 9, | ||
PENDATA : 10, | ||
RIFF : 11, | ||
WAVE : 12, | ||
UNICODETEXT : 13, | ||
ENHMETAFILE : 14, | ||
HDROP : 15, | ||
LOCALE : 16, | ||
DIBV5 : 17, | ||
MAX : 18, | ||
OWNERDISPLAY : 0x0080, | ||
DSPTEXT : 0x0081, | ||
DSPBITMAP : 0x0082, | ||
DSPMETAFILEPICT : 0x0083, | ||
DSPENHMETAFILE : 0x008E, | ||
PRIVATEFIRST : 0x0200, | ||
PRIVATELAST : 0x02FF, | ||
GDIOBJFIRST : 0x0300, | ||
GDIOBJLAST : 0x03FF, | ||
}; | ||
var CF_ = invert(CF); | ||
@@ -97,3 +98,3 @@ /** | ||
} | ||
var handle = new GlobalHandle(kernel32.GlobalAlloc(flags || GMEM('MOVEABLE'), size)); | ||
var handle = new GlobalHandle(kernel32.GlobalAlloc(flags || GMEM.MOVEABLE, size)); | ||
handle.size = size; | ||
@@ -161,2 +162,3 @@ if (input) { | ||
toObject: function toObject(){ | ||
if (!this.size) return null; | ||
var obj = kernel32.GlobalLock(this._handle).getObject(); | ||
@@ -171,33 +173,33 @@ kernel32.GlobalUnlock(this._handle); | ||
var formats = { | ||
ascii: CF('TEXT'), | ||
unicode: CF('UNICODETEXT'), | ||
bitmap: CF('BITMAP'), | ||
audio: CF('RIFF'), | ||
symlink: CF('SYLK'), | ||
dragdrop: CF('HDROP'), | ||
locale: CF('LOCALE'), | ||
jsobject: user32.RegisterClipboardFormatA('jsobject') | ||
ascii: CF.TEXT, | ||
unicode: CF.UNICODETEXT, | ||
bitmap: CF.BITMAP, | ||
audio: CF.RIFF, | ||
symlink: CF.SYLK, | ||
dragdrop: CF.HDROP, | ||
locale: CF.LOCALE, | ||
}; | ||
Object.keys(formats).forEach(function(format){ | ||
formats[formats[format]] = format; | ||
}); | ||
formats[CF('OEMTEXT')] = 'ascii'; | ||
var formats_ = invert(formats); | ||
formats_[CF.OEMTEXT] = 'ascii'; | ||
/** | ||
* Try to return the platform-neutral format name if possible | ||
* @param {String|Number} format Import format to look up which could be the platform neutral name, the | ||
* platform specific name, or the integer value of the platform constant. | ||
* @return {String} | ||
*/ | ||
function translateFormat(format){ | ||
if (isNaN(format) && format in formats) { | ||
format = formats[format]; | ||
} | ||
return format; | ||
function customFormat(name, label){ | ||
label = label || name; | ||
formats[name] = user32.RegisterClipboardFormatA(name); | ||
formats_[formats[name]] = name; | ||
return formats[name]; | ||
} | ||
customFormat('jsobject'); | ||
var refCount = 0; | ||
module.exports = { | ||
var platform = module.exports = { | ||
/** | ||
@@ -243,16 +245,24 @@ * Open the clipboard only if it's no already open, and keep track of it so we can close it when done. | ||
/** | ||
* Obtain a format's name whether it's custom or standard | ||
* Try to return the platform-neutral format name if possible | ||
* @param {String|Number} format Import format to look up which could be the platform neutral name, the | ||
* platform specific name, or the integer value of the platform constant. | ||
* @return {[type]} | ||
* @return {String} | ||
*/ | ||
formatName: function formatName(format){ | ||
if (isNaN(format)) return format; | ||
if (format in formats) return formats[format]; | ||
if (format in formats_) return formats_[format]; | ||
if (format in CF_) return CF_[format]; | ||
if (format in formats || format in CF) return format; | ||
var out = new ffi.Pointer(ffi.POINTER_SIZE); | ||
user32.GetClipboardFormatNameA(translateFormat(format), out, 512); | ||
console.log(out, format) | ||
return out.getCString(); | ||
user32.GetClipboardFormatNameA(format > 0 ? format : platform.formatHandle(format), out, 512); | ||
if (out = out.getCString()) customFormat(out); | ||
return out || null; | ||
}, | ||
formatHandle: function formatHandle(format){ | ||
if (format > 0) return platform.formatName(format) ? format : null; | ||
if (format in formats) return formats[format]; | ||
if (format in CF) return CF[format]; | ||
return null; | ||
}, | ||
/** | ||
@@ -264,3 +274,3 @@ * Read a single format from the clipboard | ||
read: function read(format){ | ||
return new GlobalHandle(user32.GetClipboardData(translateFormat(format))); | ||
return new GlobalHandle(user32.GetClipboardData(platform.formatHandle(format))); | ||
}, | ||
@@ -275,6 +285,14 @@ | ||
var handle = GlobalHandle.create(value); | ||
return user32.SetClipboardData(translateFormat(format), handle._handle); | ||
return user32.SetClipboardData(platform.formatHandle(format), handle._handle); | ||
}, | ||
formats: formats, | ||
formats: CF_, | ||
}; | ||
function invert(o){ | ||
return Object.keys(o).reduce(function(r,s){ | ||
r[o[s]] = s; | ||
return r; | ||
}, {}); | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "Easy to use utility for reading and writing to the system clipboard.", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -8,3 +8,3 @@ # node-clipboard | ||
``` | ||
npm install node-clipboard | ||
npm install clipboard | ||
``` | ||
@@ -11,0 +11,0 @@ |
15179
400