-
Notifications
You must be signed in to change notification settings - Fork 104
refactor: remove uses of execCommand() #7246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ElectricalBoy
wants to merge
8
commits into
main
Choose a base branch
from
js-impl-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ba37765
add types-mediawiki
ElectricalBoy 6d47f5b
adjust selectall
ElectricalBoy 3f7ae28
use writeText
ElectricalBoy 8a2a79e
adjust selector
ElectricalBoy 5cb4fc9
remove deprecated fallback
ElectricalBoy 6a3368e
update stylesheet
ElectricalBoy 53ddd40
better error handling
ElectricalBoy 1cde451
use textContent
ElectricalBoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,72 @@ | ||
| /******************************************************************************* | ||
| * Template(s): Select all for pre elements | ||
| * Author(s): FO-nTTaX | ||
| ******************************************************************************/ | ||
|
|
||
| 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; | ||
ElectricalBoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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(); | ||
ElectricalBoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| selection.removeAllRanges(); | ||
| selection.addRange( range ); | ||
| } | ||
| } | ||
|
|
||
| liquipedia.selectall = { | ||
| init: function() { | ||
| document.querySelectorAll( '.selectall' ).forEach( ( selectall ) => { | ||
| const wrapper = document.createElement( 'div' ); | ||
| wrapper.classList.add( 'selectall-wrapper' ); | ||
| const buttonwrapper = document.createElement( 'div' ); | ||
| buttonwrapper.classList.add( 'selectall-buttons' ); | ||
| wrapper.appendChild( buttonwrapper ); | ||
| const relative = document.createElement( 'div' ); | ||
| relative.classList.add( 'selectall-relative' ); | ||
| wrapper.appendChild( relative ); | ||
| selectall.parentNode.replaceChild( wrapper, selectall ); | ||
| relative.appendChild( selectall ); | ||
| const selectbutton = document.createElement( 'button' ); | ||
| selectbutton.classList.add( 'btn' ); | ||
| selectbutton.classList.add( 'btn-secondary' ); | ||
| selectbutton.innerHTML = 'Select'; | ||
| selectbutton.onclick = function() { | ||
| liquipedia.selectall.selectText( this ); | ||
| }; | ||
| buttonwrapper.appendChild( selectbutton ); | ||
| buttonwrapper.appendChild( document.createTextNode( ' ' ) ); | ||
| const selectcopybutton = document.createElement( 'button' ); | ||
| selectcopybutton.classList.add( 'btn' ); | ||
| selectcopybutton.classList.add( 'btn-primary' ); | ||
| selectcopybutton.innerHTML = 'Select and copy'; | ||
| selectcopybutton.onclick = function() { | ||
| liquipedia.selectall.selectText( this ); | ||
| document.execCommand( 'copy' ); | ||
| }; | ||
| buttonwrapper.appendChild( selectcopybutton ); | ||
| document.querySelectorAll( 'pre.selectall' ).forEach( ( selectall ) => { | ||
| new SelectAllContainer( selectall ).createWrapper(); | ||
| } ); | ||
| }, | ||
| removeTextarea: function() { | ||
| this.parentNode.removeChild( this ); | ||
| }, | ||
| selectText: function( button ) { | ||
| const wrapper = button.closest( '.selectall-wrapper' ); | ||
| const selectall = wrapper.querySelector( '.selectall' ); | ||
| const textarea = document.createElement( 'textarea' ); | ||
| textarea.readOnly = true; | ||
| textarea.classList.add( 'selectall-duplicate' ); | ||
| textarea.innerHTML = selectall.innerHTML; | ||
| textarea.style.padding = window.getComputedStyle( selectall ).padding; | ||
| textarea.style.lineHeight = window.getComputedStyle( selectall ).lineHeight; | ||
| textarea.style.fontFamily = window.getComputedStyle( selectall ).fontFamily; | ||
| textarea.style.fontSize = window.getComputedStyle( selectall ).fontSize; | ||
| textarea.style.height = window.getComputedStyle( selectall ).height; | ||
| textarea.onblur = liquipedia.selectall.removeTextarea; | ||
| wrapper.querySelector( '.selectall-relative' ).appendChild( textarea ); | ||
| textarea.focus(); | ||
| textarea.select(); | ||
| } | ||
| }; | ||
|
|
||
| liquipedia.core.modules.push( 'selectall' ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,13 @@ | ||
| /******************************************************************************* | ||
| Template(s): Select all for pre elements | ||
| Author(s): Chapatiyaq | ||
| *******************************************************************************/ | ||
| div.selectall-relative { | ||
| position: relative; | ||
| .selectall-wrapper { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 0.25rem; | ||
| } | ||
|
|
||
| textarea.selectall-duplicate { | ||
| -moz-box-sizing: border-box; | ||
| box-sizing: border-box; | ||
| background: #ffffff; | ||
| border: 1px solid transparent; | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| overflow: hidden; | ||
| white-space: pre; | ||
| -moz-tab-size: 13; | ||
| tab-size: 13; | ||
| .selectall-buttons { | ||
| display: flex; | ||
| gap: 0.25rem; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.