Skip to content

Commit 0514d82

Browse files
committed
gcoai-stream-to-paragraph-field.js: Fixed an issue with stream to paragraph field not displaying rich text correctly.
1 parent 4eac0cd commit 0514d82

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Instructions:
1212
*
1313
* 1. Install this snippet with our free Custom JavaScript plugin.
14-
* https://gravitywiz.com/gravity-forms-custom-javascript/
14+
* https://gravitywiz.com/gravity-forms-custom-javascript/
1515
*
1616
* 2. Update the variables to match your own field IDs.
1717
*/
@@ -25,28 +25,43 @@ var $streamButton = $streamFieldInput.parents( '.gfield' ).find( '.gcoai-tri
2525

2626
$streamFieldInput.on( 'change', function() {
2727
$input = $( `#input_GFFORMID_${responseFieldId}` );
28-
$input.val( this.value );
28+
29+
// Get the value from the stream field
30+
var inputValue = this.value;
31+
32+
// Convert Markdown to HTML only if TinyMCE is available
2933
if (window.tinyMCE) {
34+
// Convert Markdown to HTML
35+
let formattedValue = inputValue
36+
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>') // Bold text
37+
.replace(/_(.*?)_/g, '<em>$1</em>') // Italic text with underscores
38+
.replace(/\*(.*?)\*/g, '<em>$1</em>') // Italic text with asterisks
39+
.replace(/\n/g, "<br>"); // Newlines to <br>
40+
41+
// Set HTML content in TinyMCE
3042
var tiny = tinyMCE.get( $input.attr( 'id' ) );
3143
if (tiny) {
32-
tiny.setContent( this.value );
44+
tiny.setContent(formattedValue);
3345
}
46+
} else {
47+
// If TinyMCE is not available, use plain text
48+
$input.val(inputValue);
3449
}
35-
} );
50+
});
3651

3752
let $newButton = $streamButton
3853
.clone()
39-
.attr( 'style', 'margin-top: var(--gf-label-space-primary, 8px);' )
40-
.on( 'click', function() {
41-
$streamButton.trigger( 'click' );
42-
} )
43-
.insertAfter( $( `#input_GFFORMID_${appendButtonFieldId}` ) );
44-
45-
$wpEditor = $newButton.parents( '.wp-editor-container' );
46-
if ( $wpEditor.length ) {
47-
$newButton.insertAfter( $wpEditor );
54+
.attr('style', 'margin-top: var(--gf-label-space-primary, 8px);')
55+
.on('click', function() {
56+
$streamButton.trigger('click');
57+
})
58+
.insertAfter($( `#input_GFFORMID_${appendButtonFieldId}` ));
59+
60+
$wpEditor = $newButton.parents('.wp-editor-container');
61+
if ($wpEditor.length) {
62+
$newButton.insertAfter($wpEditor);
4863
}
4964

50-
$( `#input_GFFORMID_${promptFieldId}` ).on( 'blur', function() {
51-
$streamButton.trigger( 'click' );
52-
} );
65+
$( `#input_GFFORMID_${promptFieldId}` ).on('blur', function() {
66+
$streamButton.trigger('click');
67+
});

0 commit comments

Comments
 (0)