@ndla/util
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -0,1 +1,5 @@ | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
/** | ||
@@ -8,32 +12,51 @@ * Copyright (c) 2017-present, NDLA. | ||
*/ | ||
export function copyTextToClipboard(text) { | ||
var el = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.body; | ||
export var copyTextToClipboard = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee(copyPageUrlLink) { | ||
var permission; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!navigator) { | ||
_context.next = 8; | ||
break; | ||
} | ||
if (!window || !document) { | ||
return false; | ||
} | ||
_context.next = 3; | ||
return navigator.permissions.query({ | ||
name: 'clipboard-write' | ||
}); | ||
var textArea = document.createElement('textarea'); | ||
textArea.style.position = 'fixed'; | ||
textArea.style.top = '0'; | ||
textArea.style.left = '0'; | ||
textArea.style.padding = '0'; | ||
textArea.style.border = 'none'; | ||
textArea.style.outline = 'none'; | ||
textArea.style.boxShadow = 'none'; | ||
textArea.style.background = 'transparent'; | ||
textArea.style.width = '2em'; | ||
textArea.style.height = '2em'; | ||
textArea.value = text; | ||
el.appendChild(textArea); | ||
textArea.select(); | ||
case 3: | ||
permission = _context.sent; | ||
try { | ||
var successful = document.execCommand('copy'); | ||
el.removeChild(textArea); | ||
return successful; | ||
} catch (err) { | ||
el.removeChild(textArea); | ||
return false; | ||
} | ||
} | ||
if (!(permission && (permission.state === 'granted' || permission.state === 'prompt'))) { | ||
_context.next = 8; | ||
break; | ||
} | ||
_context.next = 7; | ||
return navigator.clipboard.writeText(copyPageUrlLink); | ||
case 7: | ||
return _context.abrupt("return", _context.sent); | ||
case 8: | ||
throw Error('Error copying to clipboard'); | ||
case 9: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return function copyTextToClipboard(_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); |
@@ -8,2 +8,2 @@ /** | ||
*/ | ||
export declare function copyTextToClipboard(text: string, el?: HTMLElement): boolean; | ||
export declare const copyTextToClipboard: (copyPageUrlLink: string) => Promise<any>; |
@@ -6,4 +6,8 @@ "use strict"; | ||
}); | ||
exports.copyTextToClipboard = copyTextToClipboard; | ||
exports.copyTextToClipboard = void 0; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
/** | ||
@@ -16,32 +20,53 @@ * Copyright (c) 2017-present, NDLA. | ||
*/ | ||
function copyTextToClipboard(text) { | ||
var el = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document.body; | ||
var copyTextToClipboard = | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee(copyPageUrlLink) { | ||
var permission; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!navigator) { | ||
_context.next = 8; | ||
break; | ||
} | ||
if (!window || !document) { | ||
return false; | ||
} | ||
_context.next = 3; | ||
return navigator.permissions.query({ | ||
name: 'clipboard-write' | ||
}); | ||
var textArea = document.createElement('textarea'); | ||
textArea.style.position = 'fixed'; | ||
textArea.style.top = '0'; | ||
textArea.style.left = '0'; | ||
textArea.style.padding = '0'; | ||
textArea.style.border = 'none'; | ||
textArea.style.outline = 'none'; | ||
textArea.style.boxShadow = 'none'; | ||
textArea.style.background = 'transparent'; | ||
textArea.style.width = '2em'; | ||
textArea.style.height = '2em'; | ||
textArea.value = text; | ||
el.appendChild(textArea); | ||
textArea.select(); | ||
case 3: | ||
permission = _context.sent; | ||
try { | ||
var successful = document.execCommand('copy'); | ||
el.removeChild(textArea); | ||
return successful; | ||
} catch (err) { | ||
el.removeChild(textArea); | ||
return false; | ||
} | ||
} | ||
if (!(permission && (permission.state === 'granted' || permission.state === 'prompt'))) { | ||
_context.next = 8; | ||
break; | ||
} | ||
_context.next = 7; | ||
return navigator.clipboard.writeText(copyPageUrlLink); | ||
case 7: | ||
return _context.abrupt("return", _context.sent); | ||
case 8: | ||
throw Error('Error copying to clipboard'); | ||
case 9: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return function copyTextToClipboard(_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
exports.copyTextToClipboard = copyTextToClipboard; |
{ | ||
"name": "@ndla/util", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Collection of util functions used by NDLA", | ||
@@ -34,3 +34,3 @@ "license": "GPL-3.0", | ||
}, | ||
"gitHead": "1d65d18513677512146cfe97749267249d8ca25a" | ||
"gitHead": "8f0f6ca808935efc1aa6810c7a2a479b69f18df4" | ||
} |
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
64711
731