PluginProbe
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code / 5.5.4
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code v5.5.4
5.5.4 5.5.3 3.1.0 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.6.0 3.7.0 3.8.0 3.8.1 3.8.2 3.8.3 4.0.0 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 All 79 releases
copy-the-code / includes / elementor / widgets / ai-prompt-generator / script.js

script.js in Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code 5.5.4, at includes/elementor/widgets/ai-prompt-generator/script.js

143 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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