Skip to content

Commit

Permalink
MDL-61460 core: Create a copy to clipboard JS module
Browse files Browse the repository at this point in the history
This works by adding event listeners for elements that contain
the 'copytoclipboard' "data-action" attribute through event delegation.
These trigger elements must also define a "data-clipboard-target"
attribute which contains the query selector for the target element
where text will be copied.

On success, a toast message is shown to the user that the text has
been copied to the clipboard. This success message can be customised
by passing a message string to the "data-clipboard-success-message"
attribute in the trigger element.

When the element's value or innerText cannot be found, a toast message
indicating that the text cannot be copied to the clipboard is shown.

The copy to clipboard functionality is primarily using the Clipboard API
but in the event that this is not available, e.g. the site is not
running on HTTPS, this falls back to the document.execCommand('copy')
approach of copying the text in the target container.

Special thanks to Andrew for improving this module with the event
delegation approach and improved fallback handling.

Co-authored-by: Andrew Lyons <andrew@nicols.co.uk>
  • Loading branch information
2 people authored and abgreeve committed Jul 8, 2021
1 parent 3610f1e commit 341578b
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Expand Up @@ -2067,6 +2067,7 @@
$string['teacheronly'] = 'for the {$a} only';
$string['teacherroles'] = '{$a} roles';
$string['teachers'] = 'Teachers';
$string['textcopiedtoclipboard'] = 'Text copied to clipboard';
$string['textediting'] = 'Text editor';
$string['textediting_help'] = 'If an HTML editor such as Atto or TinyMCE is selected, text input areas will have a toolbar with buttons for easily adding content.
Expand Down Expand Up @@ -2146,6 +2147,7 @@
$string['turneditingoff'] = 'Turn editing off';
$string['turneditingon'] = 'Turn editing on';
$string['unauthorisedlogin'] = 'The user account "{$a}" is not available on this site';
$string['unabletocopytoclipboard'] = 'Unable to copy text to the clipboard';
$string['undecided'] = 'Undecided';
$string['unexistingcmid'] = 'Course module id not found';
$string['unexistingsectionid'] = 'Course section id not found';
Expand Down
2 changes: 2 additions & 0 deletions lib/amd/build/copy_to_clipboard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/amd/build/copy_to_clipboard.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 341578b

Please sign in to comment.