Comparing version 0.0.5 to 0.0.6
var path = require('path'); | ||
var util = require('util'); | ||
var exists = require('fs').existsSync || path.existsSync; | ||
if (!exists(path.resolve(__dirname, process.platform+'.js'))) { | ||
throw new Error("This platform isn't supported yet."); | ||
} | ||
var platform = require('./'+process.platform); | ||
var JSOBJECT = 'jsobject'+(''+Date.now()).slice(0,6); | ||
platform.addCustomFormat(JSOBJECT); | ||
platform.addCustomFormat('json'); | ||
var clipboard = module.exports = { | ||
@@ -58,15 +66,15 @@ /** | ||
item = item[1] | ||
} else if (typeof item === 'string') { | ||
type = 'ascii'; | ||
} else if (Object(item) === item) { | ||
type = JSOBJECT; | ||
} else { | ||
if (typeof item === 'string') { | ||
type = 'ascii'; | ||
} else if (Object(item) === item) { | ||
type = 'jsobject'; | ||
} else { | ||
type = 'jsobject'; | ||
item = [item]; | ||
} | ||
type = JSOBJECT; | ||
item = [item]; | ||
} | ||
platform.write(type, item); | ||
if (type === 'jsobject') { | ||
platform.write('ascii', util.inspect(item)); | ||
if (type === JSOBJECT) { | ||
try { platform.write('ascii', util.inspect(item)) } catch (e) {} | ||
try { platform.write('json', JSON.stringify(item)) } catch (e) {} | ||
} | ||
@@ -84,3 +92,4 @@ }); | ||
read: function read(format){ | ||
if (!format) return read('jsobject') || read('ascii'); | ||
//if (!format) return read(JSOBJECT) || read('ascii'); | ||
if (!format) return read('ascii'); | ||
platform.ref(); | ||
@@ -91,3 +100,5 @@ var result = platform.read(format); | ||
switch (platform.formatName(format)) { | ||
case 'jsobject': | ||
case 'json': | ||
return JSON.parse(result.toString()); | ||
case JSOBJECT: | ||
return result.toObject(); | ||
@@ -114,9 +125,11 @@ case 'ascii': | ||
readAll: function readAll(){ | ||
return clipboard.iterate(function(format, formatName, isCustom){ | ||
return { | ||
format: formatName, | ||
value: clipboard.read(format), | ||
custom: isCustom | ||
}; | ||
var formats = {}; | ||
clipboard.iterate(function(format, formatName, isCustom){ | ||
if (formatName !== JSOBJECT && formatName.slice(0,8) === 'jsobject') return; | ||
formats[formatName] = Object.defineProperties({ value: clipboard.read(format) }, { | ||
format: { value: format }, | ||
custom: { value: isCustom } | ||
}); | ||
}); | ||
return formats; | ||
}, | ||
@@ -134,1 +147,2 @@ | ||
}; | ||
@@ -85,13 +85,15 @@ var ffi = require('./ffi'); | ||
GlobalHandle.create = function create(input, flags){ | ||
var size; | ||
if (typeof input === 'string') { | ||
input += '\0'; | ||
var size = Buffer.byteLength(input, 'utf8'); | ||
size = Buffer.byteLength(input, 'utf8'); | ||
} else if (Buffer.isBuffer(input)) { | ||
var size = input.length; | ||
size = input.length; | ||
} else if (Object(input) === input) { | ||
var size = ffi.POINTER_SIZE; | ||
size = ffi.POINTER_SIZE; | ||
} else if (input > 0) { | ||
var size = input; | ||
size = input; | ||
input = null; | ||
} | ||
if (!size) return null; | ||
var handle = new GlobalHandle(kernel32.GlobalAlloc(flags || GMEM.MOVEABLE, size)); | ||
@@ -106,2 +108,3 @@ handle.size = size; | ||
GlobalHandle.prototype = { | ||
@@ -178,3 +181,2 @@ constructor: GlobalHandle, | ||
locale: CF.LOCALE, | ||
}; | ||
@@ -186,16 +188,2 @@ | ||
function customFormat(name, label){ | ||
label = label || name; | ||
formats[name] = user32.RegisterClipboardFormatA(name); | ||
formats_[formats[name]] = name; | ||
return formats[name]; | ||
} | ||
customFormat('jsobject'); | ||
var refCount = 0; | ||
@@ -243,2 +231,9 @@ | ||
addCustomFormat: function addCustomFormat(name){ | ||
formats[name] = user32.RegisterClipboardFormatA(name); | ||
formats_[formats[name]] = name; | ||
return formats[name]; | ||
}, | ||
/** | ||
@@ -256,6 +251,8 @@ * Try to return the platform-neutral format name if possible | ||
user32.GetClipboardFormatNameA(format > 0 ? format : platform.formatHandle(format), out, 512); | ||
if (out = out.getCString()) customFormat(out); | ||
if (out = out.getCString()) platform.addCustomFormat(out); | ||
return out || null; | ||
}, | ||
formatHandle: function formatHandle(format){ | ||
@@ -262,0 +259,0 @@ if (format > 0) return platform.formatName(format) ? format : null; |
@@ -5,3 +5,3 @@ { | ||
"description": "Easy to use utility for reading and writing to the system clipboard.", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
15644
407