Skip to content

Commit dffb81b

Browse files
authored
gcoai-stream-to-paragraph-field.js: Added support for multiple OpenAI streams.
1 parent 55e3e9a commit dffb81b

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

gc-openai/gcoai-stream-to-paragraph-field.js

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,55 @@
1515
*
1616
* 2. Update the variables to match your own field IDs.
1717
*/
18-
var streamFieldId = 3;
19-
var promptFieldId = 1;
20-
var responseFieldId = 4;
21-
var appendButtonFieldId = responseFieldId;
18+
// Add one or more mappings for your Stream Field, Prompt Field, and Response Field.
19+
const streamFieldMappings = [
20+
{
21+
streamFieldId: 3, // Stream Field.
22+
promptFieldId: 1, // Prompt Field.
23+
responseFieldId: 4 // Paragraph/Text Field where response content should go.
24+
},
25+
{
26+
streamFieldId: 6,
27+
promptFieldId: 5,
28+
responseFieldId: 7
29+
},
30+
// Add more mappings here (as needed).
31+
];
2232

23-
var $streamFieldInput = $( `#input_GFFORMID_${streamFieldId}` );
24-
var $streamButton = $streamFieldInput.closest( '.gfield' ).find( '.gcoai-trigger' );
33+
streamFieldMappings.forEach(({ streamFieldId, promptFieldId, responseFieldId }) => {
34+
const $streamFieldInput = $(`#input_GFFORMID_${streamFieldId}`);
35+
const $streamButton = $streamFieldInput.closest('.gfield').find('.gcoai-trigger');
2536

26-
$streamFieldInput.on( 'change', function() {
27-
$input = $( `#input_GFFORMID_${responseFieldId}` );
28-
var inputValue = this.value;
37+
// When the stream field changes (OpenAI output comes in)
38+
$streamFieldInput.on('change', function () {
39+
const $responseInput = $(`#input_GFFORMID_${responseFieldId}`);
40+
const value = this.value;
41+
const tiny = window.tinyMCE && tinyMCE.get($responseInput.attr('id'));
2942

30-
// Check if the response field has TinyMCE enabled.
31-
var tiny = window.tinyMCE && tinyMCE.get( $input.attr( 'id' ) );
43+
if (tiny) {
44+
const html = $streamFieldInput.closest('.gfield').find('.gcoai-output').html();
45+
tiny.setContent(html);
46+
} else {
47+
$responseInput.val(value);
48+
}
49+
});
3250

33-
// Get HTML for the response field if TinyMCE is available.
34-
if (tiny) {
35-
var html = $streamFieldInput.closest( '.gfield' ).find('.gcoai-output').html();
36-
37-
// Set HTML content in TinyMCE.
38-
tiny.setContent( html );
39-
} else {
40-
// If TinyMCE is not available, use plain text.
41-
$input.val( inputValue );
42-
}
43-
});
51+
// Add a secondary button that re-triggers OpenAI generation
52+
const $newButton = $streamButton
53+
.clone()
54+
.attr('style', 'margin-top: var(--gf-label-space-primary, 8px);')
55+
.on('click', function () {
56+
$streamButton.trigger('click');
57+
})
58+
.insertAfter($(`#input_GFFORMID_${responseFieldId}`));
4459

45-
let $newButton = $streamButton
46-
.clone()
47-
.attr( 'style', 'margin-top: var(--gf-label-space-primary, 8px);' )
48-
.on( 'click', function() {
49-
$streamButton.trigger( 'click' );
50-
} )
51-
.insertAfter( $( `#input_GFFORMID_${appendButtonFieldId}` ) );
52-
53-
$wpEditor = $newButton.parents( '.wp-editor-container' );
54-
if ( $wpEditor.length ) {
55-
$newButton.insertAfter( $wpEditor );
56-
}
60+
const $wpEditor = $newButton.parents('.wp-editor-container');
61+
if ($wpEditor.length) {
62+
$newButton.insertAfter($wpEditor);
63+
}
5764

58-
$( `#input_GFFORMID_${promptFieldId}` ).on( 'blur', function() {
59-
$streamButton.trigger( 'click' );
65+
// Optional: auto-trigger generation on prompt field blur
66+
$(`#input_GFFORMID_${promptFieldId}`).on('blur', function () {
67+
$streamButton.trigger('click');
68+
});
6069
});

0 commit comments

Comments
 (0)