clipboard-polyfill
Advanced tools
Comparing version 4.0.0-rc3 to 4.0.0-rc5
@@ -22,29 +22,39 @@ "use strict"; | ||
// src/clipboard-polyfill/promise/constructor.ts | ||
var promiseConstructorImpl = window.Promise; | ||
// src/clipboard-polyfill/builtins/window-globalThis.ts | ||
var originalWindow = typeof window === "undefined" ? void 0 : window; | ||
var originalGlobalThis = typeof globalThis === "undefined" ? void 0 : globalThis; | ||
// src/clipboard-polyfill/builtins/promise-constructor.ts | ||
var _a, _b, _c; | ||
var promiseConstructorImpl = (_c = (_a = originalWindow) == null ? void 0 : _a.Promise) != null ? _c : (_b = originalGlobalThis) == null ? void 0 : _b.Promise; | ||
function getPromiseConstructor() { | ||
if (!promiseConstructorImpl) { | ||
throw new Error( | ||
"No `Promise` implementation available for `clipboard-polyfill`. Consider using: https://github.com/lgarron/clipboard-polyfill#flat-file-version-with-promise-included" | ||
); | ||
} | ||
return promiseConstructorImpl; | ||
} | ||
// src/clipboard-polyfill/builtin-globals.ts | ||
// src/clipboard-polyfill/builtins/builtin-globals.ts | ||
var originalNavigator = typeof navigator === "undefined" ? void 0 : navigator; | ||
var originalNavigatorClipboard = originalNavigator == null ? void 0 : originalNavigator.clipboard; | ||
var _a; | ||
var originalNavigatorClipboardRead = (_a = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.read) == null ? void 0 : _a.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a2; | ||
var originalNavigatorClipboardReadText = (_a2 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.readText) == null ? void 0 : _a2.bind( | ||
var originalNavigatorClipboardRead = (_a2 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.read) == null ? void 0 : _a2.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a3; | ||
var originalNavigatorClipboardWrite = (_a3 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.write) == null ? void 0 : _a3.bind( | ||
var originalNavigatorClipboardReadText = (_a3 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.readText) == null ? void 0 : _a3.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a4; | ||
var originalNavigatorClipboardWriteText = (_a4 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.writeText) == null ? void 0 : _a4.bind( | ||
var originalNavigatorClipboardWrite = (_a4 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.write) == null ? void 0 : _a4.bind( | ||
originalNavigatorClipboard | ||
); | ||
var originalWindow = typeof window === "undefined" ? void 0 : window; | ||
var originalWindowClipboardItem = originalWindow == null ? void 0 : originalWindow.ClipboardItem; | ||
var _a5; | ||
var originalNavigatorClipboardWriteText = (_a5 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.writeText) == null ? void 0 : _a5.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a6; | ||
var originalWindowClipboardItem = (_a6 = originalWindow) == null ? void 0 : _a6.ClipboardItem; | ||
var promiseConstructor = getPromiseConstructor(); | ||
@@ -55,19 +65,19 @@ | ||
function seemToBeInIE() { | ||
return typeof ClipboardEvent === "undefined" && typeof ieWindow.clipboardData !== "undefined" && typeof ieWindow.clipboardData.setData !== "undefined"; | ||
return typeof ClipboardEvent === "undefined" && typeof (ieWindow == null ? void 0 : ieWindow.clipboardData) !== "undefined" && typeof (ieWindow == null ? void 0 : ieWindow.clipboardData.setData) !== "undefined"; | ||
} | ||
function writeTextIE(text) { | ||
return ieWindow.clipboardData.setData("Text", text); | ||
var success = ieWindow.clipboardData.setData("Text", text); | ||
if (success) { | ||
debugLog("writeTextIE worked"); | ||
} | ||
return success; | ||
} | ||
function readTextIE() { | ||
return new promiseConstructor(function(resolve, reject) { | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
reject( | ||
new Error( | ||
"Empty clipboard or could not read plain text from clipboard" | ||
) | ||
); | ||
} | ||
resolve(text); | ||
}); | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
throw new Error( | ||
"Empty clipboard or could not read plain text from clipboard" | ||
); | ||
} | ||
return text; | ||
} | ||
@@ -187,2 +197,32 @@ | ||
// src/clipboard-polyfill/promise/promise-compat.ts | ||
function promiseRecordMap(keys, f) { | ||
var promiseList = []; | ||
for (var i in keys) { | ||
var key = keys[i]; | ||
promiseList.push(f(key)); | ||
} | ||
return promiseConstructor.all(promiseList).then(function(vList) { | ||
var dataOut = {}; | ||
for (var i2 = 0; i2 < keys.length; i2++) { | ||
dataOut[keys[i2]] = vList[i2]; | ||
} | ||
return dataOut; | ||
}); | ||
} | ||
var voidPromise = promiseConstructor.resolve(); | ||
var truePromiseFn = function() { | ||
return promiseConstructor.resolve(true); | ||
}; | ||
var falsePromise = promiseConstructor.resolve(false); | ||
function rejectThrownErrors(executor) { | ||
return new promiseConstructor(function(resolve, reject) { | ||
try { | ||
resolve(executor()); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
// src/clipboard-polyfill/implementations/text.ts | ||
@@ -195,9 +235,11 @@ function stringToStringItem(s) { | ||
function writeText(s) { | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback | ||
); | ||
} | ||
writeTextStringFallback(s); | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback | ||
); | ||
} | ||
return promiseConstructor.resolve(writeTextStringFallback(s)); | ||
}); | ||
} | ||
@@ -210,11 +252,13 @@ function writeTextStringFallback(s) { | ||
function readText() { | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
if (seemToBeInIE()) { | ||
debugLog("Reading text using IE strategy."); | ||
return readTextIE(); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
if (seemToBeInIE()) { | ||
var result = readTextIE(); | ||
return promiseConstructor.resolve(result); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
}); | ||
} | ||
@@ -235,3 +279,3 @@ | ||
function ClipboardItemPolyfillImpl(items, options) { | ||
var _a5; | ||
var _a7; | ||
var types = Object.keys(items); | ||
@@ -247,3 +291,3 @@ var _items = {}; | ||
} | ||
var presentationStyle = (_a5 = options == null ? void 0 : options.presentationStyle) != null ? _a5 : "unspecified"; | ||
var presentationStyle = (_a7 = options == null ? void 0 : options.presentationStyle) != null ? _a7 : "unspecified"; | ||
function getType(type2) { | ||
@@ -260,23 +304,2 @@ return promiseConstructor.resolve(_items[type2]); | ||
// src/clipboard-polyfill/promise/promise-compat.ts | ||
function promiseRecordMap(keys, f) { | ||
var promiseList = []; | ||
for (var i in keys) { | ||
var key = keys[i]; | ||
promiseList.push(f(key)); | ||
} | ||
return promiseConstructor.all(promiseList).then(function(vList) { | ||
var dataOut = {}; | ||
for (var i2 = 0; i2 < keys.length; i2++) { | ||
dataOut[keys[i2]] = vList[i2]; | ||
} | ||
return dataOut; | ||
}); | ||
} | ||
var voidPromise = promiseConstructor.resolve(); | ||
var truePromiseFn = function() { | ||
return promiseConstructor.resolve(true); | ||
}; | ||
var falsePromise = promiseConstructor.resolve(false); | ||
// src/clipboard-polyfill/ClipboardItem/convert.ts | ||
@@ -331,8 +354,11 @@ function stringToBlob(type, str) { | ||
function write(data) { | ||
return function() { | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardWrite && originalWindowClipboardItem) { | ||
var originalNavigatorClipboardWriteReference = originalNavigatorClipboardWrite; | ||
debugLog("Using `navigator.clipboard.write()`."); | ||
return promiseConstructor.all(data.map(clipboardItemToGlobalClipboardItem)).then( | ||
function(globalClipboardItems) { | ||
return originalNavigatorClipboardWrite(globalClipboardItems).then(truePromiseFn).catch(function(e) { | ||
return originalNavigatorClipboardWriteReference( | ||
globalClipboardItems | ||
).then(truePromiseFn).catch(function(e) { | ||
if (!hasItemWithType(data, TEXT_PLAIN) && !hasItemWithType(data, TEXT_HTML)) { | ||
@@ -347,3 +373,3 @@ throw e; | ||
return falsePromise; | ||
}().then(function(success) { | ||
}).then(function(success) { | ||
if (success) { | ||
@@ -366,8 +392,10 @@ return voidPromise; | ||
function read() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
readText().then(function(text) { | ||
return [textToClipboardItem(text)]; | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
return readText().then(function(text) { | ||
return [textToClipboardItem(text)]; | ||
}); | ||
}); | ||
@@ -374,0 +402,0 @@ } |
"use strict"; | ||
(function() { | ||
// src/clipboard-polyfill/promise/constructor.ts | ||
var promiseConstructorImpl = window.Promise; | ||
// src/clipboard-polyfill/builtins/window-globalThis.ts | ||
var originalWindow = typeof window === "undefined" ? void 0 : window; | ||
var originalGlobalThis = typeof globalThis === "undefined" ? void 0 : globalThis; | ||
// src/clipboard-polyfill/builtins/promise-constructor.ts | ||
var _a, _b, _c; | ||
var promiseConstructorImpl = (_c = (_a = originalWindow) == null ? void 0 : _a.Promise) != null ? _c : (_b = originalGlobalThis) == null ? void 0 : _b.Promise; | ||
function setPromiseConstructor(newPromiseConstructorImpl) { | ||
@@ -9,2 +14,7 @@ return promiseConstructorImpl = newPromiseConstructorImpl; | ||
function getPromiseConstructor() { | ||
if (!promiseConstructorImpl) { | ||
throw new Error( | ||
"No `Promise` implementation available for `clipboard-polyfill`. Consider using: https://github.com/lgarron/clipboard-polyfill#flat-file-version-with-promise-included" | ||
); | ||
} | ||
return promiseConstructorImpl; | ||
@@ -275,23 +285,23 @@ } | ||
// src/clipboard-polyfill/builtin-globals.ts | ||
// src/clipboard-polyfill/builtins/builtin-globals.ts | ||
var originalNavigator = typeof navigator === "undefined" ? void 0 : navigator; | ||
var originalNavigatorClipboard = originalNavigator == null ? void 0 : originalNavigator.clipboard; | ||
var _a; | ||
var originalNavigatorClipboardRead = (_a = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.read) == null ? void 0 : _a.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a2; | ||
var originalNavigatorClipboardReadText = (_a2 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.readText) == null ? void 0 : _a2.bind( | ||
var originalNavigatorClipboardRead = (_a2 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.read) == null ? void 0 : _a2.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a3; | ||
var originalNavigatorClipboardWrite = (_a3 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.write) == null ? void 0 : _a3.bind( | ||
var originalNavigatorClipboardReadText = (_a3 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.readText) == null ? void 0 : _a3.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a4; | ||
var originalNavigatorClipboardWriteText = (_a4 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.writeText) == null ? void 0 : _a4.bind( | ||
var originalNavigatorClipboardWrite = (_a4 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.write) == null ? void 0 : _a4.bind( | ||
originalNavigatorClipboard | ||
); | ||
var originalWindow = typeof window === "undefined" ? void 0 : window; | ||
var originalWindowClipboardItem = originalWindow == null ? void 0 : originalWindow.ClipboardItem; | ||
var _a5; | ||
var originalNavigatorClipboardWriteText = (_a5 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.writeText) == null ? void 0 : _a5.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a6; | ||
var originalWindowClipboardItem = (_a6 = originalWindow) == null ? void 0 : _a6.ClipboardItem; | ||
var promiseConstructor = getPromiseConstructor(); | ||
@@ -321,19 +331,19 @@ | ||
function seemToBeInIE() { | ||
return typeof ClipboardEvent === "undefined" && typeof ieWindow.clipboardData !== "undefined" && typeof ieWindow.clipboardData.setData !== "undefined"; | ||
return typeof ClipboardEvent === "undefined" && typeof (ieWindow == null ? void 0 : ieWindow.clipboardData) !== "undefined" && typeof (ieWindow == null ? void 0 : ieWindow.clipboardData.setData) !== "undefined"; | ||
} | ||
function writeTextIE(text) { | ||
return ieWindow.clipboardData.setData("Text", text); | ||
var success = ieWindow.clipboardData.setData("Text", text); | ||
if (success) { | ||
debugLog("writeTextIE worked"); | ||
} | ||
return success; | ||
} | ||
function readTextIE() { | ||
return new promiseConstructor(function(resolve2, reject2) { | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
reject2( | ||
new Error( | ||
"Empty clipboard or could not read plain text from clipboard" | ||
) | ||
); | ||
} | ||
resolve2(text); | ||
}); | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
throw new Error( | ||
"Empty clipboard or could not read plain text from clipboard" | ||
); | ||
} | ||
return text; | ||
} | ||
@@ -453,2 +463,32 @@ | ||
// src/clipboard-polyfill/promise/promise-compat.ts | ||
function promiseRecordMap(keys, f) { | ||
var promiseList = []; | ||
for (var i in keys) { | ||
var key = keys[i]; | ||
promiseList.push(f(key)); | ||
} | ||
return promiseConstructor.all(promiseList).then(function(vList) { | ||
var dataOut = {}; | ||
for (var i2 = 0; i2 < keys.length; i2++) { | ||
dataOut[keys[i2]] = vList[i2]; | ||
} | ||
return dataOut; | ||
}); | ||
} | ||
var voidPromise = promiseConstructor.resolve(); | ||
var truePromiseFn = function() { | ||
return promiseConstructor.resolve(true); | ||
}; | ||
var falsePromise = promiseConstructor.resolve(false); | ||
function rejectThrownErrors(executor) { | ||
return new promiseConstructor(function(resolve2, reject2) { | ||
try { | ||
resolve2(executor()); | ||
} catch (e) { | ||
reject2(e); | ||
} | ||
}); | ||
} | ||
// src/clipboard-polyfill/implementations/text.ts | ||
@@ -461,9 +501,11 @@ function stringToStringItem(s) { | ||
function writeText(s) { | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback | ||
); | ||
} | ||
writeTextStringFallback(s); | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback | ||
); | ||
} | ||
return promiseConstructor.resolve(writeTextStringFallback(s)); | ||
}); | ||
} | ||
@@ -476,11 +518,13 @@ function writeTextStringFallback(s) { | ||
function readText() { | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
if (seemToBeInIE()) { | ||
debugLog("Reading text using IE strategy."); | ||
return readTextIE(); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
if (seemToBeInIE()) { | ||
var result = readTextIE(); | ||
return promiseConstructor.resolve(result); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
}); | ||
} | ||
@@ -501,3 +545,3 @@ | ||
function ClipboardItemPolyfillImpl(items, options) { | ||
var _a5; | ||
var _a7; | ||
var types = Object.keys(items); | ||
@@ -513,3 +557,3 @@ var _items = {}; | ||
} | ||
var presentationStyle = (_a5 = options == null ? void 0 : options.presentationStyle) != null ? _a5 : "unspecified"; | ||
var presentationStyle = (_a7 = options == null ? void 0 : options.presentationStyle) != null ? _a7 : "unspecified"; | ||
function getType(type2) { | ||
@@ -526,23 +570,2 @@ return promiseConstructor.resolve(_items[type2]); | ||
// src/clipboard-polyfill/promise/promise-compat.ts | ||
function promiseRecordMap(keys, f) { | ||
var promiseList = []; | ||
for (var i in keys) { | ||
var key = keys[i]; | ||
promiseList.push(f(key)); | ||
} | ||
return promiseConstructor.all(promiseList).then(function(vList) { | ||
var dataOut = {}; | ||
for (var i2 = 0; i2 < keys.length; i2++) { | ||
dataOut[keys[i2]] = vList[i2]; | ||
} | ||
return dataOut; | ||
}); | ||
} | ||
var voidPromise = promiseConstructor.resolve(); | ||
var truePromiseFn = function() { | ||
return promiseConstructor.resolve(true); | ||
}; | ||
var falsePromise = promiseConstructor.resolve(false); | ||
// src/clipboard-polyfill/ClipboardItem/convert.ts | ||
@@ -597,8 +620,11 @@ function stringToBlob(type, str) { | ||
function write(data) { | ||
return function() { | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardWrite && originalWindowClipboardItem) { | ||
var originalNavigatorClipboardWriteReference = originalNavigatorClipboardWrite; | ||
debugLog("Using `navigator.clipboard.write()`."); | ||
return promiseConstructor.all(data.map(clipboardItemToGlobalClipboardItem)).then( | ||
function(globalClipboardItems) { | ||
return originalNavigatorClipboardWrite(globalClipboardItems).then(truePromiseFn).catch(function(e) { | ||
return originalNavigatorClipboardWriteReference( | ||
globalClipboardItems | ||
).then(truePromiseFn).catch(function(e) { | ||
if (!hasItemWithType(data, TEXT_PLAIN) && !hasItemWithType(data, TEXT_HTML)) { | ||
@@ -613,3 +639,3 @@ throw e; | ||
return falsePromise; | ||
}().then(function(success) { | ||
}).then(function(success) { | ||
if (success) { | ||
@@ -632,8 +658,10 @@ return voidPromise; | ||
function read() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
readText().then(function(text) { | ||
return [textToClipboardItem(text)]; | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
return readText().then(function(text) { | ||
return [textToClipboardItem(text)]; | ||
}); | ||
}); | ||
@@ -640,0 +668,0 @@ } |
"use strict"; | ||
(function() { | ||
// src/clipboard-polyfill/promise/constructor.ts | ||
var promiseConstructorImpl = window.Promise; | ||
// src/clipboard-polyfill/builtins/window-globalThis.ts | ||
var originalWindow = typeof window === "undefined" ? void 0 : window; | ||
var originalGlobalThis = typeof globalThis === "undefined" ? void 0 : globalThis; | ||
// src/clipboard-polyfill/builtins/promise-constructor.ts | ||
var _a, _b, _c; | ||
var promiseConstructorImpl = (_c = (_a = originalWindow) == null ? void 0 : _a.Promise) != null ? _c : (_b = originalGlobalThis) == null ? void 0 : _b.Promise; | ||
function getPromiseConstructor() { | ||
if (!promiseConstructorImpl) { | ||
throw new Error( | ||
"No `Promise` implementation available for `clipboard-polyfill`. Consider using: https://github.com/lgarron/clipboard-polyfill#flat-file-version-with-promise-included" | ||
); | ||
} | ||
return promiseConstructorImpl; | ||
} | ||
// src/clipboard-polyfill/builtin-globals.ts | ||
// src/clipboard-polyfill/builtins/builtin-globals.ts | ||
var originalNavigator = typeof navigator === "undefined" ? void 0 : navigator; | ||
var originalNavigatorClipboard = originalNavigator == null ? void 0 : originalNavigator.clipboard; | ||
var _a; | ||
var originalNavigatorClipboardRead = (_a = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.read) == null ? void 0 : _a.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a2; | ||
var originalNavigatorClipboardReadText = (_a2 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.readText) == null ? void 0 : _a2.bind( | ||
var originalNavigatorClipboardRead = (_a2 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.read) == null ? void 0 : _a2.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a3; | ||
var originalNavigatorClipboardWrite = (_a3 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.write) == null ? void 0 : _a3.bind( | ||
var originalNavigatorClipboardReadText = (_a3 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.readText) == null ? void 0 : _a3.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a4; | ||
var originalNavigatorClipboardWriteText = (_a4 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.writeText) == null ? void 0 : _a4.bind( | ||
var originalNavigatorClipboardWrite = (_a4 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.write) == null ? void 0 : _a4.bind( | ||
originalNavigatorClipboard | ||
); | ||
var originalWindow = typeof window === "undefined" ? void 0 : window; | ||
var originalWindowClipboardItem = originalWindow == null ? void 0 : originalWindow.ClipboardItem; | ||
var _a5; | ||
var originalNavigatorClipboardWriteText = (_a5 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.writeText) == null ? void 0 : _a5.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a6; | ||
var originalWindowClipboardItem = (_a6 = originalWindow) == null ? void 0 : _a6.ClipboardItem; | ||
var promiseConstructor = getPromiseConstructor(); | ||
@@ -56,2 +66,11 @@ | ||
var falsePromise = promiseConstructor.resolve(false); | ||
function rejectThrownErrors(executor) { | ||
return new promiseConstructor(function(resolve, reject) { | ||
try { | ||
resolve(executor()); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
@@ -107,3 +126,3 @@ // src/clipboard-polyfill/ClipboardItem/convert.ts | ||
function ClipboardItemPolyfillImpl(items, options) { | ||
var _a5; | ||
var _a7; | ||
var types = Object.keys(items); | ||
@@ -119,3 +138,3 @@ var _items = {}; | ||
} | ||
var presentationStyle = (_a5 = options == null ? void 0 : options.presentationStyle) != null ? _a5 : "unspecified"; | ||
var presentationStyle = (_a7 = options == null ? void 0 : options.presentationStyle) != null ? _a7 : "unspecified"; | ||
function getType(type2) { | ||
@@ -167,19 +186,19 @@ return promiseConstructor.resolve(_items[type2]); | ||
function seemToBeInIE() { | ||
return typeof ClipboardEvent === "undefined" && typeof ieWindow.clipboardData !== "undefined" && typeof ieWindow.clipboardData.setData !== "undefined"; | ||
return typeof ClipboardEvent === "undefined" && typeof (ieWindow == null ? void 0 : ieWindow.clipboardData) !== "undefined" && typeof (ieWindow == null ? void 0 : ieWindow.clipboardData.setData) !== "undefined"; | ||
} | ||
function writeTextIE(text) { | ||
return ieWindow.clipboardData.setData("Text", text); | ||
var success = ieWindow.clipboardData.setData("Text", text); | ||
if (success) { | ||
debugLog("writeTextIE worked"); | ||
} | ||
return success; | ||
} | ||
function readTextIE() { | ||
return new promiseConstructor(function(resolve, reject) { | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
reject( | ||
new Error( | ||
"Empty clipboard or could not read plain text from clipboard" | ||
) | ||
); | ||
} | ||
resolve(text); | ||
}); | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
throw new Error( | ||
"Empty clipboard or could not read plain text from clipboard" | ||
); | ||
} | ||
return text; | ||
} | ||
@@ -306,9 +325,11 @@ | ||
function writeText(s) { | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback | ||
); | ||
} | ||
writeTextStringFallback(s); | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback | ||
); | ||
} | ||
return promiseConstructor.resolve(writeTextStringFallback(s)); | ||
}); | ||
} | ||
@@ -321,11 +342,13 @@ function writeTextStringFallback(s) { | ||
function readText() { | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
if (seemToBeInIE()) { | ||
debugLog("Reading text using IE strategy."); | ||
return readTextIE(); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
if (seemToBeInIE()) { | ||
var result = readTextIE(); | ||
return promiseConstructor.resolve(result); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
}); | ||
} | ||
@@ -335,8 +358,11 @@ | ||
function write(data) { | ||
return function() { | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardWrite && originalWindowClipboardItem) { | ||
var originalNavigatorClipboardWriteReference = originalNavigatorClipboardWrite; | ||
debugLog("Using `navigator.clipboard.write()`."); | ||
return promiseConstructor.all(data.map(clipboardItemToGlobalClipboardItem)).then( | ||
function(globalClipboardItems) { | ||
return originalNavigatorClipboardWrite(globalClipboardItems).then(truePromiseFn).catch(function(e) { | ||
return originalNavigatorClipboardWriteReference( | ||
globalClipboardItems | ||
).then(truePromiseFn).catch(function(e) { | ||
if (!hasItemWithType(data, TEXT_PLAIN) && !hasItemWithType(data, TEXT_HTML)) { | ||
@@ -351,3 +377,3 @@ throw e; | ||
return falsePromise; | ||
}().then(function(success) { | ||
}).then(function(success) { | ||
if (success) { | ||
@@ -370,8 +396,10 @@ return voidPromise; | ||
function read() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
readText().then(function(text) { | ||
return [textToClipboardItem(text)]; | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
return readText().then(function(text) { | ||
return [textToClipboardItem(text)]; | ||
}); | ||
}); | ||
@@ -378,0 +406,0 @@ } |
"use strict"; | ||
(function() { | ||
// src/clipboard-polyfill/promise/constructor.ts | ||
var promiseConstructorImpl = window.Promise; | ||
// src/clipboard-polyfill/builtins/window-globalThis.ts | ||
var originalWindow = typeof window === "undefined" ? void 0 : window; | ||
var originalGlobalThis = typeof globalThis === "undefined" ? void 0 : globalThis; | ||
// src/clipboard-polyfill/builtins/promise-constructor.ts | ||
var _a, _b, _c; | ||
var promiseConstructorImpl = (_c = (_a = originalWindow) == null ? void 0 : _a.Promise) != null ? _c : (_b = originalGlobalThis) == null ? void 0 : _b.Promise; | ||
function setPromiseConstructor(newPromiseConstructorImpl) { | ||
@@ -9,2 +14,7 @@ return promiseConstructorImpl = newPromiseConstructorImpl; | ||
function getPromiseConstructor() { | ||
if (!promiseConstructorImpl) { | ||
throw new Error( | ||
"No `Promise` implementation available for `clipboard-polyfill`. Consider using: https://github.com/lgarron/clipboard-polyfill#flat-file-version-with-promise-included" | ||
); | ||
} | ||
return promiseConstructorImpl; | ||
@@ -275,23 +285,23 @@ } | ||
// src/clipboard-polyfill/builtin-globals.ts | ||
// src/clipboard-polyfill/builtins/builtin-globals.ts | ||
var originalNavigator = typeof navigator === "undefined" ? void 0 : navigator; | ||
var originalNavigatorClipboard = originalNavigator == null ? void 0 : originalNavigator.clipboard; | ||
var _a; | ||
var originalNavigatorClipboardRead = (_a = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.read) == null ? void 0 : _a.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a2; | ||
var originalNavigatorClipboardReadText = (_a2 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.readText) == null ? void 0 : _a2.bind( | ||
var originalNavigatorClipboardRead = (_a2 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.read) == null ? void 0 : _a2.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a3; | ||
var originalNavigatorClipboardWrite = (_a3 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.write) == null ? void 0 : _a3.bind( | ||
var originalNavigatorClipboardReadText = (_a3 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.readText) == null ? void 0 : _a3.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a4; | ||
var originalNavigatorClipboardWriteText = (_a4 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.writeText) == null ? void 0 : _a4.bind( | ||
var originalNavigatorClipboardWrite = (_a4 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.write) == null ? void 0 : _a4.bind( | ||
originalNavigatorClipboard | ||
); | ||
var originalWindow = typeof window === "undefined" ? void 0 : window; | ||
var originalWindowClipboardItem = originalWindow == null ? void 0 : originalWindow.ClipboardItem; | ||
var _a5; | ||
var originalNavigatorClipboardWriteText = (_a5 = originalNavigatorClipboard == null ? void 0 : originalNavigatorClipboard.writeText) == null ? void 0 : _a5.bind( | ||
originalNavigatorClipboard | ||
); | ||
var _a6; | ||
var originalWindowClipboardItem = (_a6 = originalWindow) == null ? void 0 : _a6.ClipboardItem; | ||
var promiseConstructor = getPromiseConstructor(); | ||
@@ -323,2 +333,11 @@ | ||
var falsePromise = promiseConstructor.resolve(false); | ||
function rejectThrownErrors(executor) { | ||
return new promiseConstructor(function(resolve2, reject2) { | ||
try { | ||
resolve2(executor()); | ||
} catch (e) { | ||
reject2(e); | ||
} | ||
}); | ||
} | ||
@@ -374,3 +393,3 @@ // src/clipboard-polyfill/ClipboardItem/convert.ts | ||
function ClipboardItemPolyfillImpl(items, options) { | ||
var _a5; | ||
var _a7; | ||
var types = Object.keys(items); | ||
@@ -386,3 +405,3 @@ var _items = {}; | ||
} | ||
var presentationStyle = (_a5 = options == null ? void 0 : options.presentationStyle) != null ? _a5 : "unspecified"; | ||
var presentationStyle = (_a7 = options == null ? void 0 : options.presentationStyle) != null ? _a7 : "unspecified"; | ||
function getType(type2) { | ||
@@ -434,19 +453,19 @@ return promiseConstructor.resolve(_items[type2]); | ||
function seemToBeInIE() { | ||
return typeof ClipboardEvent === "undefined" && typeof ieWindow.clipboardData !== "undefined" && typeof ieWindow.clipboardData.setData !== "undefined"; | ||
return typeof ClipboardEvent === "undefined" && typeof (ieWindow == null ? void 0 : ieWindow.clipboardData) !== "undefined" && typeof (ieWindow == null ? void 0 : ieWindow.clipboardData.setData) !== "undefined"; | ||
} | ||
function writeTextIE(text) { | ||
return ieWindow.clipboardData.setData("Text", text); | ||
var success = ieWindow.clipboardData.setData("Text", text); | ||
if (success) { | ||
debugLog("writeTextIE worked"); | ||
} | ||
return success; | ||
} | ||
function readTextIE() { | ||
return new promiseConstructor(function(resolve2, reject2) { | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
reject2( | ||
new Error( | ||
"Empty clipboard or could not read plain text from clipboard" | ||
) | ||
); | ||
} | ||
resolve2(text); | ||
}); | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
throw new Error( | ||
"Empty clipboard or could not read plain text from clipboard" | ||
); | ||
} | ||
return text; | ||
} | ||
@@ -573,9 +592,11 @@ | ||
function writeText(s) { | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback | ||
); | ||
} | ||
writeTextStringFallback(s); | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback | ||
); | ||
} | ||
return promiseConstructor.resolve(writeTextStringFallback(s)); | ||
}); | ||
} | ||
@@ -588,11 +609,13 @@ function writeTextStringFallback(s) { | ||
function readText() { | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
if (seemToBeInIE()) { | ||
debugLog("Reading text using IE strategy."); | ||
return readTextIE(); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
if (seemToBeInIE()) { | ||
var result = readTextIE(); | ||
return promiseConstructor.resolve(result); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
}); | ||
} | ||
@@ -602,8 +625,11 @@ | ||
function write(data) { | ||
return function() { | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardWrite && originalWindowClipboardItem) { | ||
var originalNavigatorClipboardWriteReference = originalNavigatorClipboardWrite; | ||
debugLog("Using `navigator.clipboard.write()`."); | ||
return promiseConstructor.all(data.map(clipboardItemToGlobalClipboardItem)).then( | ||
function(globalClipboardItems) { | ||
return originalNavigatorClipboardWrite(globalClipboardItems).then(truePromiseFn).catch(function(e) { | ||
return originalNavigatorClipboardWriteReference( | ||
globalClipboardItems | ||
).then(truePromiseFn).catch(function(e) { | ||
if (!hasItemWithType(data, TEXT_PLAIN) && !hasItemWithType(data, TEXT_HTML)) { | ||
@@ -618,3 +644,3 @@ throw e; | ||
return falsePromise; | ||
}().then(function(success) { | ||
}).then(function(success) { | ||
if (success) { | ||
@@ -637,8 +663,10 @@ return voidPromise; | ||
function read() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
readText().then(function(text) { | ||
return [textToClipboardItem(text)]; | ||
return rejectThrownErrors(function() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
return readText().then(function(text) { | ||
return [textToClipboardItem(text)]; | ||
}); | ||
}); | ||
@@ -645,0 +673,0 @@ } |
@@ -5,2 +5,3 @@ import { | ||
debugLog, | ||
falsePromise, | ||
originalNavigatorClipboardRead, | ||
@@ -10,9 +11,13 @@ originalNavigatorClipboardWrite, | ||
promiseConstructor, | ||
promiseRecordMap, | ||
readText, | ||
rejectThrownErrors, | ||
setDebugLog, | ||
shouldShowWarnings, | ||
suppressWarnings, | ||
truePromiseFn, | ||
voidPromise, | ||
writeFallback, | ||
writeText | ||
} from "./chunk-ABKG7VJV.js"; | ||
} from "./chunk-ZMZD3ZPV.js"; | ||
@@ -55,21 +60,2 @@ // src/clipboard-polyfill/ClipboardItem/check.ts | ||
// src/clipboard-polyfill/promise/promise-compat.ts | ||
function promiseRecordMap(keys, f) { | ||
var promiseList = []; | ||
for (var i in keys) { | ||
var key = keys[i]; | ||
promiseList.push(f(key)); | ||
} | ||
return promiseConstructor.all(promiseList).then((vList) => { | ||
var dataOut = {}; | ||
for (var i2 = 0; i2 < keys.length; i2++) { | ||
dataOut[keys[i2]] = vList[i2]; | ||
} | ||
return dataOut; | ||
}); | ||
} | ||
var voidPromise = promiseConstructor.resolve(); | ||
var truePromiseFn = () => promiseConstructor.resolve(true); | ||
var falsePromise = promiseConstructor.resolve(false); | ||
// src/clipboard-polyfill/ClipboardItem/convert.ts | ||
@@ -124,8 +110,11 @@ function stringToBlob(type, str) { | ||
function write(data) { | ||
return (() => { | ||
return rejectThrownErrors(() => { | ||
if (originalNavigatorClipboardWrite && originalWindowClipboardItem) { | ||
var originalNavigatorClipboardWriteReference = originalNavigatorClipboardWrite; | ||
debugLog("Using `navigator.clipboard.write()`."); | ||
return promiseConstructor.all(data.map(clipboardItemToGlobalClipboardItem)).then( | ||
(globalClipboardItems) => { | ||
return originalNavigatorClipboardWrite(globalClipboardItems).then(truePromiseFn).catch((e) => { | ||
return originalNavigatorClipboardWriteReference( | ||
globalClipboardItems | ||
).then(truePromiseFn).catch((e) => { | ||
if (!hasItemWithType(data, TEXT_PLAIN) && !hasItemWithType(data, TEXT_HTML)) { | ||
@@ -140,3 +129,3 @@ throw e; | ||
return falsePromise; | ||
})().then((success) => { | ||
}).then((success) => { | ||
if (success) { | ||
@@ -159,8 +148,10 @@ return voidPromise; | ||
function read() { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
readText().then((text) => { | ||
return [textToClipboardItem(text)]; | ||
return rejectThrownErrors(() => { | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
return readText().then((text) => { | ||
return [textToClipboardItem(text)]; | ||
}); | ||
}); | ||
@@ -167,0 +158,0 @@ } |
@@ -5,3 +5,3 @@ import { | ||
writeText | ||
} from "./chunk-ABKG7VJV.js"; | ||
} from "./chunk-ZMZD3ZPV.js"; | ||
export { | ||
@@ -8,0 +8,0 @@ readText, |
import "../../promise/set-promise-polyfill-if-needed"; | ||
import "../../builtin-globals"; | ||
import "../../builtins/builtin-globals"; | ||
import "./overwrite-globals"; |
@@ -5,1 +5,2 @@ export declare function promiseRecordMap<T>(keys: readonly string[], f: (key: string) => Promise<T>): Promise<Record<string, T>>; | ||
export declare var falsePromise: Promise<boolean>; | ||
export declare function rejectThrownErrors<T>(executor: () => Promise<T>): Promise<T>; |
export declare function seemToBeInIE(): boolean; | ||
export declare function writeTextIE(text: string): boolean; | ||
export declare function readTextIE(): Promise<string>; | ||
export declare function readTextIE(): string; |
{ | ||
"name": "clipboard-polyfill", | ||
"version": "4.0.0-rc3", | ||
"version": "4.0.0-rc5", | ||
"description": "A polyfill for the asynchronous clipboard API", | ||
"type": "module", | ||
"devDependencies": { | ||
"@types/node": "^18.11.18", | ||
"barely-a-dev-server": "^0.4.7", | ||
@@ -44,3 +45,5 @@ "esbuild": "^0.16.4", | ||
"build-demo": "node script/build-demo.js", | ||
"test": "npm run build && npm run build-demo && npm run lint", | ||
"test": "npm run build && npm run build-demo && npm run lint && npm run test-no-es6-browser-globals", | ||
"mock-test": "./script/mock-test.bash", | ||
"test-no-es6-browser-globals": "node dist/es6/clipboard-polyfill.es6.js && node dist/es6/clipboard-polyfill.text.es6.js", | ||
"dev": "node script/dev.js", | ||
@@ -47,0 +50,0 @@ "lint": "npx rome check ./script ./src", |
@@ -5,7 +5,13 @@ ![Logo for clipboard-polyfill: an icon of a clipboard fading into a drafting paper grid.](clipboard-polyfill-logo.svg) | ||
## ⚠️⚠️⚠️ DEPRECATED ⚠️⚠️⚠️ | ||
## ⚠️ You don't need `clipboard-polyfill` to copy text! ⚠️ | ||
I am in the process of deprecating `clipboard-polyfill`, since it has finished serving its purpose of providing a simple, safe clipboard API in lieu of browser support. | ||
| Browser | First version supporting<br>`navigator.clipboard.writeText()` | Release Date | | ||
Note: As of 2021, you can use `navigator.clipboard.writeText(...)` [in the stable versions of all major browsers](https://caniuse.com/mdn-api_clipboard_writetext). This library will only be useful to you if you want to: | ||
- target older browsers (see below for compatibility) for text copy, | ||
- copy `text/html` in Firefox, | ||
- use the `ClipboardItem` API in Firefox, or | ||
- polyfill the API shape in a non-browser environment (e.g. in [`jsdom`](https://github.com/jsdom/jsdom/issues/1568)). | ||
| Browser | First version supporting<br>`navigator.clipboard.writeText(...)` | Release Date | | ||
| ------- | ------------------------------------------------------------- | ------------ | | ||
@@ -38,8 +44,8 @@ | Chrome | 66+ | April 2018 | | ||
clipboard.writeText("hello world"); | ||
```js | ||
clipboard.writeText("hello world"); | ||
``` | ||
This library is a [ponyfill](https://github.com/sindresorhus/ponyfill)/polyfill for the modern `Promise`-based [asynchronous clipboard API](https://www.w3.org/TR/clipboard-apis/#async-clipboard-api). | ||
Note: As of 2021, you can use `navigator.clipboard` [in the stable versions of all major browsers](https://caniuse.com/mdn-api_clipboard_writetext). This library will only be useful to you if you need to target older browsers (see below for compatibility). | ||
## Usage | ||
@@ -46,0 +52,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { promiseConstructor } from "../builtin-globals"; | ||
import { promiseConstructor } from "../builtins/builtin-globals"; | ||
import { stringToBlob } from "./convert"; | ||
@@ -3,0 +3,0 @@ import { |
@@ -7,3 +7,3 @@ import { ClipboardItemPolyfill } from "./ClipboardItemPolyfill"; | ||
originalWindowClipboardItem, | ||
} from "../builtin-globals"; | ||
} from "../builtins/builtin-globals"; | ||
import { promiseRecordMap } from "../promise/promise-compat"; | ||
@@ -10,0 +10,0 @@ |
// Set the Promise polyfill before globals. | ||
import "../../promise/set-promise-polyfill-if-needed"; | ||
// Import `./globals` that the globals are cached before this runs. | ||
import "../../builtin-globals"; | ||
import "../../builtins/builtin-globals"; | ||
@@ -10,6 +10,2 @@ import { PromisePolyfillConstructor } from "../../promise/polyfill"; | ||
// declare global { | ||
// // rome-ignore lint/suspicious/noShadowRestrictedNames: This is where we export `Promise`. | ||
// var Promise: PromiseConstructor; | ||
// } | ||
(window as any).Promise = PromisePolyfillConstructor; |
@@ -5,10 +5,2 @@ import { readText, writeText } from "../../implementations/text"; | ||
// declare global { | ||
// // const ClipboardItem: ClipboardItemConstructor; | ||
// interface Window { | ||
// ClipboardItem: ClipboardItemConstructor; | ||
// } | ||
// interface Clipboard extends ClipboardInterface {} | ||
// } | ||
// Create the `navigator.clipboard` object if it doesn't exist. | ||
@@ -15,0 +7,0 @@ if (!navigator.clipboard) { |
@@ -16,5 +16,6 @@ import { hasItemWithType } from "../ClipboardItem/check"; | ||
originalWindowClipboardItem, | ||
} from "../builtin-globals"; | ||
} from "../builtins/builtin-globals"; | ||
import { | ||
falsePromise, | ||
rejectThrownErrors, | ||
truePromiseFn, | ||
@@ -29,4 +30,7 @@ voidPromise, | ||
// TODO: detect `text/html`. | ||
return ((): Promise<boolean> => { | ||
return rejectThrownErrors((): Promise<boolean> => { | ||
if (originalNavigatorClipboardWrite && originalWindowClipboardItem) { | ||
// TODO: This reference is a workaround for TypeScript inference. | ||
var originalNavigatorClipboardWriteReference = | ||
originalNavigatorClipboardWrite; | ||
debugLog("Using `navigator.clipboard.write()`."); | ||
@@ -39,3 +43,5 @@ return promiseConstructor | ||
): Promise<boolean> => { | ||
return originalNavigatorClipboardWrite(globalClipboardItems) | ||
return originalNavigatorClipboardWriteReference( | ||
globalClipboardItems, | ||
) | ||
.then(truePromiseFn) | ||
@@ -59,3 +65,3 @@ .catch((e: Error): Promise<boolean> => { | ||
return falsePromise; | ||
})().then((success: boolean) => { | ||
}).then((success: boolean) => { | ||
if (success) { | ||
@@ -84,12 +90,14 @@ return voidPromise; | ||
export function read(): Promise<ClipboardItems> { | ||
// Use the browser implementation if it exists. | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
return rejectThrownErrors(() => { | ||
// Use the browser implementation if it exists. | ||
if (originalNavigatorClipboardRead) { | ||
debugLog("Using `navigator.clipboard.read()`."); | ||
return originalNavigatorClipboardRead(); | ||
} | ||
// Fallback to reading text only. | ||
readText().then((text: string) => { | ||
return [textToClipboardItem(text)]; | ||
// Fallback to reading text only. | ||
return readText().then((text: string) => { | ||
return [textToClipboardItem(text)]; | ||
}); | ||
}); | ||
} |
@@ -7,5 +7,7 @@ import { StringItem } from "../ClipboardItem/convert"; | ||
originalNavigatorClipboardWriteText, | ||
} from "../builtin-globals"; | ||
promiseConstructor, | ||
} from "../builtins/builtin-globals"; | ||
import { readTextIE, seemToBeInIE } from "../strategies/internet-explorer"; | ||
import { writeFallback } from "./write-fallback"; | ||
import { rejectThrownErrors } from "../promise/promise-compat"; | ||
@@ -19,10 +21,12 @@ function stringToStringItem(s: string): StringItem { | ||
export function writeText(s: string): Promise<void> { | ||
// Use the browser implementation if it exists. | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback, | ||
); | ||
} | ||
writeTextStringFallback(s); | ||
return rejectThrownErrors(() => { | ||
// Use the browser implementation if it exists. | ||
if (originalNavigatorClipboardWriteText) { | ||
debugLog("Using `navigator.clipboard.writeText()`."); | ||
return originalNavigatorClipboardWriteText(s).catch( | ||
writeTextStringFallback, | ||
); | ||
} | ||
return promiseConstructor.resolve(writeTextStringFallback(s)); | ||
}); | ||
} | ||
@@ -37,15 +41,17 @@ | ||
export function readText(): Promise<string> { | ||
// Use the browser implementation if it exists. | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
return rejectThrownErrors(() => { | ||
// Use the browser implementation if it exists. | ||
if (originalNavigatorClipboardReadText) { | ||
debugLog("Using `navigator.clipboard.readText()`."); | ||
return originalNavigatorClipboardReadText(); | ||
} | ||
// Fallback for IE. | ||
if (seemToBeInIE()) { | ||
debugLog("Reading text using IE strategy."); | ||
return readTextIE(); | ||
} | ||
// Fallback for IE. | ||
if (seemToBeInIE()) { | ||
var result = readTextIE(); | ||
return promiseConstructor.resolve(result); | ||
} | ||
throw new Error("Read is not supported in your browser."); | ||
throw new Error("Read is not supported in your browser."); | ||
}); | ||
} |
@@ -332,3 +332,3 @@ import type { PromiseConstructor } from "./es6-promise"; | ||
// Set the Promise polyfill before getting globals. | ||
import { setPromiseConstructor } from "../promise/constructor"; | ||
import { setPromiseConstructor } from "../builtins/promise-constructor"; | ||
setPromiseConstructor(PromisePolyfillConstructor); |
@@ -1,2 +0,2 @@ | ||
import { promiseConstructor } from "../builtin-globals"; | ||
import { promiseConstructor } from "../builtins/builtin-globals"; | ||
@@ -27,1 +27,11 @@ export function promiseRecordMap<T>( | ||
export var falsePromise: Promise<boolean> = promiseConstructor.resolve(false); | ||
export function rejectThrownErrors<T>(executor: () => Promise<T>): Promise<T> { | ||
return new promiseConstructor((resolve, reject) => { | ||
try { | ||
resolve(executor()); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} |
@@ -1,4 +0,4 @@ | ||
import { setPromiseConstructor } from "./constructor"; | ||
import { setPromiseConstructor } from "../builtins/promise-constructor"; | ||
import { PromisePolyfillConstructor } from "./polyfill"; | ||
(window as any).Promise || setPromiseConstructor(PromisePolyfillConstructor); |
@@ -1,2 +0,3 @@ | ||
import { promiseConstructor, originalWindow } from "../builtin-globals"; | ||
import { originalWindow } from "../builtins/window-globalThis"; | ||
import { debugLog } from "../debug"; | ||
@@ -11,3 +12,3 @@ interface IEWindow extends Window { | ||
var ieWindow = originalWindow as any as IEWindow; | ||
var ieWindow = originalWindow as IEWindow | undefined; | ||
@@ -17,4 +18,4 @@ export function seemToBeInIE(): boolean { | ||
typeof ClipboardEvent === "undefined" && | ||
typeof ieWindow.clipboardData !== "undefined" && | ||
typeof ieWindow.clipboardData.setData !== "undefined" | ||
typeof ieWindow?.clipboardData !== "undefined" && | ||
typeof ieWindow?.clipboardData.setData !== "undefined" | ||
); | ||
@@ -26,18 +27,18 @@ } | ||
// TODO: Write URLs to `text/uri-list`? https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Recommended_drag_types | ||
return ieWindow.clipboardData.setData("Text", text); | ||
var success = ieWindow!.clipboardData.setData("Text", text); | ||
if (success) { | ||
debugLog("writeTextIE worked"); | ||
} | ||
return success; | ||
} | ||
// Returns "" if the read failed, e.g. because the user rejected the permission. | ||
export function readTextIE(): Promise<string> { | ||
return new promiseConstructor((resolve, reject) => { | ||
var text = ieWindow.clipboardData.getData("Text"); | ||
if (text === "") { | ||
reject( | ||
new Error( | ||
"Empty clipboard or could not read plain text from clipboard", | ||
), | ||
); | ||
} | ||
resolve(text); | ||
}); | ||
export function readTextIE(): string { | ||
var text = ieWindow!.clipboardData.getData("Text"); | ||
if (text === "") { | ||
throw new Error( | ||
"Empty clipboard or could not read plain text from clipboard", | ||
); | ||
} | ||
return text; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
154665
63
3802
246
5