Locally add prismjs
This commit is contained in:
parent
35af792c6a
commit
6aa044cf9b
701 changed files with 35787 additions and 0 deletions
160
node_modules/prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.js
generated
vendored
Normal file
160
node_modules/prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.js
generated
vendored
Normal file
|
@ -0,0 +1,160 @@
|
|||
(function () {
|
||||
|
||||
if (typeof Prism === 'undefined' || typeof document === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Prism.plugins.toolbar) {
|
||||
console.warn('Copy to Clipboard plugin loaded before Toolbar plugin.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the given elements is clicked by the user, the given text will be copied to clipboard.
|
||||
*
|
||||
* @param {HTMLElement} element
|
||||
* @param {CopyInfo} copyInfo
|
||||
*
|
||||
* @typedef CopyInfo
|
||||
* @property {() => string} getText
|
||||
* @property {() => void} success
|
||||
* @property {(reason: unknown) => void} error
|
||||
*/
|
||||
function registerClipboard(element, copyInfo) {
|
||||
element.addEventListener('click', function () {
|
||||
copyTextToClipboard(copyInfo);
|
||||
});
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/30810322/7595472
|
||||
|
||||
/** @param {CopyInfo} copyInfo */
|
||||
function fallbackCopyTextToClipboard(copyInfo) {
|
||||
var textArea = document.createElement('textarea');
|
||||
textArea.value = copyInfo.getText();
|
||||
|
||||
// Avoid scrolling to bottom
|
||||
textArea.style.top = '0';
|
||||
textArea.style.left = '0';
|
||||
textArea.style.position = 'fixed';
|
||||
|
||||
document.body.appendChild(textArea);
|
||||
textArea.focus();
|
||||
textArea.select();
|
||||
|
||||
try {
|
||||
var successful = document.execCommand('copy');
|
||||
setTimeout(function () {
|
||||
if (successful) {
|
||||
copyInfo.success();
|
||||
} else {
|
||||
copyInfo.error();
|
||||
}
|
||||
}, 1);
|
||||
} catch (err) {
|
||||
setTimeout(function () {
|
||||
copyInfo.error(err);
|
||||
}, 1);
|
||||
}
|
||||
|
||||
document.body.removeChild(textArea);
|
||||
}
|
||||
/** @param {CopyInfo} copyInfo */
|
||||
function copyTextToClipboard(copyInfo) {
|
||||
if (navigator.clipboard) {
|
||||
navigator.clipboard.writeText(copyInfo.getText()).then(copyInfo.success, function () {
|
||||
// try the fallback in case `writeText` didn't work
|
||||
fallbackCopyTextToClipboard(copyInfo);
|
||||
});
|
||||
} else {
|
||||
fallbackCopyTextToClipboard(copyInfo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the text content of the given element.
|
||||
*
|
||||
* @param {Element} element
|
||||
*/
|
||||
function selectElementText(element) {
|
||||
// https://stackoverflow.com/a/20079910/7595472
|
||||
window.getSelection().selectAllChildren(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Traverses up the DOM tree to find data attributes that override the default plugin settings.
|
||||
*
|
||||
* @param {Element} startElement An element to start from.
|
||||
* @returns {Settings} The plugin settings.
|
||||
* @typedef {Record<"copy" | "copy-error" | "copy-success" | "copy-timeout", string | number>} Settings
|
||||
*/
|
||||
function getSettings(startElement) {
|
||||
/** @type {Settings} */
|
||||
var settings = {
|
||||
'copy': 'Copy',
|
||||
'copy-error': 'Press Ctrl+C to copy',
|
||||
'copy-success': 'Copied!',
|
||||
'copy-timeout': 5000
|
||||
};
|
||||
|
||||
var prefix = 'data-prismjs-';
|
||||
for (var key in settings) {
|
||||
var attr = prefix + key;
|
||||
var element = startElement;
|
||||
while (element && !element.hasAttribute(attr)) {
|
||||
element = element.parentElement;
|
||||
}
|
||||
if (element) {
|
||||
settings[key] = element.getAttribute(attr);
|
||||
}
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
Prism.plugins.toolbar.registerButton('copy-to-clipboard', function (env) {
|
||||
var element = env.element;
|
||||
|
||||
var settings = getSettings(element);
|
||||
|
||||
var linkCopy = document.createElement('button');
|
||||
linkCopy.className = 'copy-to-clipboard-button';
|
||||
linkCopy.setAttribute('type', 'button');
|
||||
var linkSpan = document.createElement('span');
|
||||
linkCopy.appendChild(linkSpan);
|
||||
|
||||
setState('copy');
|
||||
|
||||
registerClipboard(linkCopy, {
|
||||
getText: function () {
|
||||
return element.textContent;
|
||||
},
|
||||
success: function () {
|
||||
setState('copy-success');
|
||||
|
||||
resetText();
|
||||
},
|
||||
error: function () {
|
||||
setState('copy-error');
|
||||
|
||||
setTimeout(function () {
|
||||
selectElementText(element);
|
||||
}, 1);
|
||||
|
||||
resetText();
|
||||
}
|
||||
});
|
||||
|
||||
return linkCopy;
|
||||
|
||||
function resetText() {
|
||||
setTimeout(function () { setState('copy'); }, settings['copy-timeout']);
|
||||
}
|
||||
|
||||
/** @param {"copy" | "copy-error" | "copy-success"} state */
|
||||
function setState(state) {
|
||||
linkSpan.textContent = settings[state];
|
||||
linkCopy.setAttribute('data-copy-state', state);
|
||||
}
|
||||
});
|
||||
}());
|
1
node_modules/prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js
generated
vendored
Normal file
1
node_modules/prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
!function(){function t(t){var e=document.createElement("textarea");e.value=t.getText(),e.style.top="0",e.style.left="0",e.style.position="fixed",document.body.appendChild(e),e.focus(),e.select();try{var o=document.execCommand("copy");setTimeout((function(){o?t.success():t.error()}),1)}catch(e){setTimeout((function(){t.error(e)}),1)}document.body.removeChild(e)}"undefined"!=typeof Prism&&"undefined"!=typeof document&&(Prism.plugins.toolbar?Prism.plugins.toolbar.registerButton("copy-to-clipboard",(function(e){var o=e.element,n=function(t){var e={copy:"Copy","copy-error":"Press Ctrl+C to copy","copy-success":"Copied!","copy-timeout":5e3};for(var o in e){for(var n="data-prismjs-"+o,c=t;c&&!c.hasAttribute(n);)c=c.parentElement;c&&(e[o]=c.getAttribute(n))}return e}(o),c=document.createElement("button");c.className="copy-to-clipboard-button",c.setAttribute("type","button");var r=document.createElement("span");return c.appendChild(r),u("copy"),function(e,o){e.addEventListener("click",(function(){!function(e){navigator.clipboard?navigator.clipboard.writeText(e.getText()).then(e.success,(function(){t(e)})):t(e)}(o)}))}(c,{getText:function(){return o.textContent},success:function(){u("copy-success"),i()},error:function(){u("copy-error"),setTimeout((function(){!function(t){window.getSelection().selectAllChildren(t)}(o)}),1),i()}}),c;function i(){setTimeout((function(){u("copy")}),n["copy-timeout"])}function u(t){r.textContent=n[t],c.setAttribute("data-copy-state",t)}})):console.warn("Copy to Clipboard plugin loaded before Toolbar plugin."))}();
|
Loading…
Add table
Add a link
Reference in a new issue