All files / ext.wikilambda.app/mixins clipboardUtils.js

98.73% Statements 78/79
92.3% Branches 12/13
100% Functions 7/7
98.73% Lines 78/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 807x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 30x 30x 30x 7x 7x 7x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 1x 7x 7x 7x 7x 7x 7x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 92x 13x 13x 79x 92x 7x 7x  
/**
 * WikiLambda Vue editor: Clipboard utils mixin
 * Mixin with util functions to handle clipboard actions
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
module.exports = exports = {
	data: function () {
		return {
			itemsCopied: []
		};
	},
	computed: {
		getCopiedText() {
			return this.$i18n( 'wikilambda-function-explorer-copied-text' ).text();
		}
	},
	methods: {
		/**
		 * Add the copied value to the array
		 *
		 * @param {string} value The value to copy
		 */
		copy: function ( value ) {
			this.itemsCopied.push( value );
		},
 
		/**
		 * Remove the copied value from the array
		 */
		clear: function () {
			this.itemsCopied.shift();
		},
 
		/**
		 * Copy a value to the clipboard and display a "copied" message for a short time
		 *
		 * @param {string} value The value to copy to the clipboard
		 * @param {Function} [copyFunction] Function to call when the value is copied
		 * @param {Function} [clearFunction] Function to call when the "copied" message is cleared
		 */
		copyToClipboard( value, copyFunction, clearFunction ) {
			navigator.clipboard.writeText( value );
 
			// This will allow us to display a default "copied" message
			// or call a custom function when the value is copied
			if ( copyFunction ) {
				copyFunction();
			} else {
				this.copy( value );
			}
 
			// Clear the "copied" message after a short time
			setTimeout( () => {
				if ( clearFunction ) {
					clearFunction();
				} else {
					this.clear();
				}
			}, 2000 );
		},
 
		/**
		 * Show the value or a "copied" message
		 *
		 * @param {string} value The value to display
		 * @return {string} The value or a "copied" message
		 */
		showValueOrCopiedMessage( value ) {
			if ( this.itemsCopied.includes( value ) ) {
				return this.getCopiedText;
			}
			return value;
		}
	}
};