Add front-end implementation suggestions to README.md
[OborPaste.git] / textfiles.js
blob9d4788550a11fa2802972724139fce081e92c1cc
1 /*******************************/
2 /* EVENT LISTENER MANIPULATION */
3 /*******************************/
5 /*      Adds an event listener to a button (or other clickable element), attaching 
6         it to both "click" and "keyup" events (for use with keyboard navigation).
7         Optionally also attaches the listener to the 'mousedown' event, making the 
8         element activate on mouse down instead of mouse up. */
9 Element.prototype.addActivateEvent = function(func, includeMouseDown) {
10         let ael = this.activateEventListener = (event) => { if (event.button === 0 || event.key === ' ') func(event) };
11         if (includeMouseDown) this.addEventListener("mousedown", ael);
12         this.addEventListener("click", ael);
13         this.addEventListener("keyup", ael);
16 /*      Removes event listener from a clickable element, automatically detaching it
17         from all relevant event types. */
18 Element.prototype.removeActivateEvent = function() {
19         let ael = this.activateEventListener;
20         this.removeEventListener("mousedown", ael);
21         this.removeEventListener("click", ael);
22         this.removeEventListener("keyup", ael);
25 /***********/
26 /* HELPERS */
27 /***********/
29 function selectElementContents(element) {
30         var range = document.createRange();
31         range.selectNodeContents(element);
32         var selection = window.getSelection();
33         selection.removeAllRanges();
34         selection.addRange(range);
37 function copyTextToClipboard(string) {
38         let scratchpad = document.querySelector("#scratchpad");
39         scratchpad.value = string;
40         scratchpad.select();
41         document.execCommand("copy");
44 function setMessage(string) {
45         document.querySelector("#controls .message").innerText = string;