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

100% Statements 79/79
100% Branches 14/14
100% Functions 7/7
100% Lines 79/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 4x 4x 7x 7x 7x 7x 7x 7x 7x 7x 3x 7x 7x 7x 7x 7x 7x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 3x 3x 3x 3x 6x 6x 6x 2x 1x 1x 1x 1x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 91x 14x 14x 77x 91x 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;
		}
	}
};