-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
prism-copy-to-clipboard.js
157 lines (133 loc) · 3.73 KB
/
prism-copy-to-clipboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(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, copyInfo.error);
} 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);
}
});
})();