55 * Stream OpenAI responses from the OpenAI Stream field to a Paragraph (or Single Line Text)
66 * field instead. Useful when wanting to provide a starting point for users while allowing them
77 * to edit the final result.
8- *
8+ *
99 * Instruction Video: https://www.loom.com/share/f793319da7e449a8b01e5a8c077e24c7
1010 *
1111 * Instructions:
1212 *
1313 * 1. Install this snippet with our free Custom JavaScript plugin.
1414 * https://gravitywiz.com/gravity-forms-custom-javascript/
15- *
15+ *
1616 * 2. Update the variables to match your own field IDs.
1717 */
1818var streamFieldId = 3 ;
@@ -21,18 +21,26 @@ var responseFieldId = 4;
2121var appendButtonFieldId = responseFieldId ;
2222
2323var $streamFieldInput = $ ( `#input_GFFORMID_${ streamFieldId } ` ) ;
24- var $streamButton = $streamFieldInput . parents ( '.gfield' ) . find ( '.gcoai-trigger' ) ;
24+ var $streamButton = $streamFieldInput . closest ( '.gfield' ) . find ( '.gcoai-trigger' ) ;
2525
2626$streamFieldInput . on ( 'change' , function ( ) {
2727 $input = $ ( `#input_GFFORMID_${ responseFieldId } ` ) ;
28- $input . val ( this . value ) ;
28+ var inputValue = this . value ;
29+
30+ // Get HTML from response field if TinyMCE is available.
2931 if ( window . tinyMCE ) {
32+ var html = $streamFieldInput . closest ( '.gfield' ) . find ( '.gcoai-output' ) . html ( ) ;
33+
34+ // Set HTML content in TinyMCE.
3035 var tiny = tinyMCE . get ( $input . attr ( 'id' ) ) ;
3136 if ( tiny ) {
32- tiny . setContent ( this . value ) ;
37+ tiny . setContent ( html ) ;
3338 }
39+ } else {
40+ // If TinyMCE is not available, use plain text.
41+ $input . val ( inputValue ) ;
3442 }
35- } ) ;
43+ } ) ;
3644
3745let $newButton = $streamButton
3846 . clone ( )
@@ -50,3 +58,12 @@ if ( $wpEditor.length ) {
5058$ ( `#input_GFFORMID_${ promptFieldId } ` ) . on ( 'blur' , function ( ) {
5159 $streamButton . trigger ( 'click' ) ;
5260} ) ;
61+
62+ $wpEditor = $newButton . parents ( '.wp-editor-container' ) ;
63+ if ( $wpEditor . length ) {
64+ $newButton . insertAfter ( $wpEditor ) ;
65+ }
66+
67+ $ ( `#input_GFFORMID_${ promptFieldId } ` ) . on ( 'blur' , function ( ) {
68+ $streamButton . trigger ( 'click' ) ;
69+ } ) ;
0 commit comments