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