| 1 |
(function ($) { |
| 2 |
const CTCAIPromptGenerator = { |
| 3 |
|
| 4 |
/** |
| 5 |
* Init |
| 6 |
*/ |
| 7 |
init: function () { |
| 8 |
this._bind(); |
| 9 |
this._generate(); |
| 10 |
}, |
| 11 |
|
| 12 |
/** |
| 13 |
* Binds events |
| 14 |
*/ |
| 15 |
_bind: function () { |
| 16 |
$( document ).on( 'click', '.ctc-ai-generator-button', this.doCopy ); |
| 17 |
$( document ).on( 'input', '.ctc-ai-prompt-generator input', this.doChange ); |
| 18 |
}, |
| 19 |
|
| 20 |
/** |
| 21 |
* Copy text to clipboard using CTC CopyEngine. |
| 22 |
* |
| 23 |
* @param {string} text Text to copy. |
| 24 |
* @return {Promise<boolean>} Success status. |
| 25 |
*/ |
| 26 |
copyToClipboard: async function ( text ) { |
| 27 |
// Method 1: Use CTC CopyEngine. |
| 28 |
if ( window.CTC && window.CTC.CopyEngine ) { |
| 29 |
try { |
| 30 |
const copyEngine = new window.CTC.CopyEngine(); |
| 31 |
const result = await copyEngine.execute( { value: text } ); |
| 32 |
if ( result.success ) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
} catch ( err ) { |
| 36 |
// Fall through to fallbacks. |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Method 2: Clipboard API. |
| 41 |
if ( navigator.clipboard && navigator.clipboard.writeText ) { |
| 42 |
try { |
| 43 |
await navigator.clipboard.writeText( text ); |
| 44 |
return true; |
| 45 |
} catch ( err ) { |
| 46 |
// Fall through. |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Method 3: execCommand fallback. |
| 51 |
try { |
| 52 |
const textArea = document.createElement( 'textarea' ); |
| 53 |
textArea.value = text; |
| 54 |
textArea.style.cssText = 'position:fixed;opacity:0;pointer-events:none;'; |
| 55 |
document.body.appendChild( textArea ); |
| 56 |
textArea.select(); |
| 57 |
const success = document.execCommand( 'copy' ); |
| 58 |
document.body.removeChild( textArea ); |
| 59 |
return success; |
| 60 |
} catch ( err ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
}, |
| 64 |
|
| 65 |
/** |
| 66 |
* Do Change |
| 67 |
*/ |
| 68 |
doChange: function (event) { |
| 69 |
event.preventDefault(); |
| 70 |
|
| 71 |
CTCAIPromptGenerator._generate(); |
| 72 |
}, |
| 73 |
|
| 74 |
/** |
| 75 |
* Do Copy to Clipboard |
| 76 |
*/ |
| 77 |
doCopy: async function (event) { |
| 78 |
event.preventDefault(); |
| 79 |
|
| 80 |
const self = $( this ) |
| 81 |
const parent = self.parents( '.ctc-ai-prompt-generator' ) |
| 82 |
const textarea = parent.find( '.ctc-ai-prompt-generator-textarea' ) |
| 83 |
let text = textarea.val() || '' |
| 84 |
|
| 85 |
// Remove first and last new line. |
| 86 |
text = text.trim(); |
| 87 |
|
| 88 |
// Copy to clipboard using CTC CopyEngine. |
| 89 |
await CTCAIPromptGenerator.copyToClipboard( text ); |
| 90 |
|
| 91 |
// Copied! |
| 92 |
parent.addClass( 'copied' ); |
| 93 |
setTimeout( |
| 94 |
function () { |
| 95 |
parent.removeClass( 'copied' ); |
| 96 |
}, |
| 97 |
1000 |
| 98 |
); |
| 99 |
}, |
| 100 |
|
| 101 |
/** |
| 102 |
* Generate |
| 103 |
*/ |
| 104 |
_generate: function () { |
| 105 |
const blocks = $( '.ctc-ai-prompt-generator' ) |
| 106 |
|
| 107 |
blocks.each( |
| 108 |
function() { |
| 109 |
const fields = $( this ).find( '.ctc-block-field' ) |
| 110 |
const textarea = $( this ).find( '.ctc-ai-prompt-generator-textarea' ) |
| 111 |
let markup = '' |
| 112 |
|
| 113 |
fields.each( |
| 114 |
function() { |
| 115 |
const label = $( this ).find( '.ctc-block-field-label' ).text() |
| 116 |
const value = $( this ).find( 'input' ).val() || '' |
| 117 |
|
| 118 |
if ( ! value ) { |
| 119 |
return |
| 120 |
} |
| 121 |
|
| 122 |
markup += label + ': ' + value + '\n' |
| 123 |
} |
| 124 |
) |
| 125 |
|
| 126 |
textarea.val( markup ) |
| 127 |
} |
| 128 |
) |
| 129 |
} |
| 130 |
|
| 131 |
}; |
| 132 |
|
| 133 |
/** |
| 134 |
* Initialization |
| 135 |
*/ |
| 136 |
$( |
| 137 |
function () { |
| 138 |
CTCAIPromptGenerator.init(); |
| 139 |
} |
| 140 |
); |
| 141 |
|
| 142 |
})( jQuery ); |
| 143 |
|