-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathSelectall.js
More file actions
72 lines (63 loc) · 2.25 KB
/
Selectall.js
File metadata and controls
72 lines (63 loc) · 2.25 KB
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
/*******************************************************************************
* Template(s): Select all for pre elements
******************************************************************************/
class SelectAllContainer {
/**
* @param {HTMLElement} selectAllElement
*/
constructor( selectAllElement ) {
this.element = selectAllElement;
}
createWrapper() {
const wrapper = document.createElement( 'div' );
wrapper.classList.add( 'selectall-wrapper' );
const buttonWrapper = document.createElement( 'div' );
buttonWrapper.classList.add( 'selectall-buttons' );
wrapper.appendChild( buttonWrapper );
this.element.parentNode.replaceChild( wrapper, this.element );
wrapper.appendChild( this.element );
buttonWrapper.append( this.createSelectButton(), this.createSelectAllButton() );
}
createSelectButton() {
const selectButton = document.createElement( 'button' );
selectButton.classList.add( 'btn' );
selectButton.classList.add( 'btn-secondary' );
selectButton.innerHTML = 'Select';
selectButton.addEventListener( 'click', () => this.selectElementText() );
return selectButton;
}
createSelectAllButton() {
const selectCopyButton = document.createElement( 'button' );
selectCopyButton.classList.add( 'btn' );
selectCopyButton.classList.add( 'btn-primary' );
selectCopyButton.innerHTML = 'Select and copy';
selectCopyButton.addEventListener( 'click', async () => {
if ( !navigator.clipboard || !navigator.clipboard.writeText ) {
mw.notify( 'This browser does not support copying text to the clipboard.', { type: 'error' } );
return;
}
this.selectElementText();
try {
await navigator.clipboard.writeText( this.element.textContent );
} catch {
mw.notify( 'Failed to copy text to the clipboard.', { type: 'error' } );
}
} );
return selectCopyButton;
}
selectElementText() {
const range = document.createRange();
range.selectNodeContents( this.element );
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange( range );
}
}
liquipedia.selectall = {
init: function() {
document.querySelectorAll( 'pre.selectall' ).forEach( ( selectall ) => {
new SelectAllContainer( selectall ).createWrapper();
} );
}
};
liquipedia.core.modules.push( 'selectall' );