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
3752let $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