|
60 | 60 | </style> |
61 | 61 |
|
62 | 62 | <script> |
| 63 | + var gwRichTextMode; |
63 | 64 | jQuery( document ).on( 'gform_load_field_settings', function( event, field ) { |
| 65 | + gwRichTextMode = field.gwRichTextMode || 'tmce'; |
| 66 | + |
64 | 67 | var id = 'field_rich_content'; |
65 | | - |
66 | | - // Ensure complete cleanup of existing editor |
67 | | - if ( tinymce.get( id ) ) { |
68 | | - tinymce.get( id ).remove(); |
69 | | - } |
70 | 68 | wp.editor.remove( id ); |
71 | | - |
72 | | - // Clear the textarea value and set new content |
73 | 69 | jQuery( '#' + id ).val( field.content ); |
74 | | - |
75 | | - // Small delay to ensure cleanup is complete before reinitializing |
76 | | - setTimeout( function() { |
77 | | - wp.editor.initialize( id, { |
78 | | - tinymce: { |
79 | | - forced_root_block: false, |
80 | | - setup: function( editor ) { |
81 | | - editor.on( 'Paste Change input Undo Redo', function () { |
82 | | - SetFieldProperty( 'content', editor.getContent() ); |
83 | | - } ); |
84 | | - } |
85 | | - }, |
86 | | - quicktags: true |
87 | | - } ); |
88 | | - }, 100 ); |
| 70 | + wp.editor.initialize( id, { |
| 71 | + tinymce: { |
| 72 | + forced_root_block: false, |
| 73 | + height: 250, |
| 74 | + setup: function( editor ) { |
| 75 | + editor.on( 'Paste Change input Undo Redo', function () { |
| 76 | + SetFieldProperty( 'content', editor.getContent() ); |
| 77 | + } ); |
| 78 | + } |
| 79 | + }, |
| 80 | + quicktags: true |
| 81 | + } ); |
89 | 82 | } ); |
90 | 83 |
|
91 | 84 | jQuery( document).on( 'tinymce-editor-setup', function ( event, editor ) { |
|
121 | 114 | } |
122 | 115 | } ); |
123 | 116 |
|
| 117 | + // Wait until the TinyMCE editor is initialized before switching mode. |
| 118 | + const waitForEditorToBeReady = (callback, timeout = 5000) => { |
| 119 | + const start = Date.now(); |
| 120 | + const interval = setInterval(() => { |
| 121 | + const editor = typeof tinymce !== 'undefined' && tinymce.get(editorId); |
| 122 | + if (editor) { |
| 123 | + clearInterval(interval); |
| 124 | + callback(); |
| 125 | + } else if (Date.now() - start > timeout) { |
| 126 | + clearInterval(interval); |
| 127 | + } |
| 128 | + }, 100); |
| 129 | + }; |
| 130 | + |
| 131 | + waitForEditorToBeReady(() => window.switchEditors.go(editorId, gwRichTextMode === 'html' ? 'html' : 'tmce')); |
| 132 | + |
| 133 | + // Set the content when save. |
| 134 | + window.SetFieldContentProperty = function () { |
| 135 | + var mode = jQuery('#wp-' + editorId + '-wrap').hasClass('html-active') ? 'html' : 'tmce'; |
| 136 | + var content = ''; |
| 137 | + |
| 138 | + if (mode === 'html') { |
| 139 | + content = jQuery('#' + editorId).val(); |
| 140 | + } else if (tinymce.get(editorId)) { |
| 141 | + content = tinymce.get(editorId).getContent(); |
| 142 | + } |
| 143 | + |
| 144 | + SetFieldProperty('content', content); |
| 145 | + }; |
| 146 | + |
| 147 | + // Update the content. |
| 148 | + jQuery(document).on('change', `#${editorId}`, function () { |
| 149 | + window.SetFieldContentProperty(); |
| 150 | + }); |
| 151 | + |
124 | 152 | // Switch to visual/text mode. |
125 | 153 | jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() { |
126 | 154 | var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html'; |
127 | 155 |
|
128 | 156 | window.switchEditors.go(editorId, mode); |
| 157 | + |
| 158 | + // Save the current mode to field property. |
| 159 | + SetFieldProperty('gwRichTextMode', mode) |
129 | 160 | }); |
130 | 161 | } |
131 | 162 | } ); |
|
0 commit comments