Skip to content

Commit a0c67e6

Browse files
committed
gw-rich-text-html-fields.php: Added improvements for Rich Text HTML Fields Snippet.
1 parent 5e0d06d commit a0c67e6

File tree

1 file changed

+22
-51
lines changed

1 file changed

+22
-51
lines changed

gravity-forms/gw-rich-text-html-fields.php

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,32 @@
6060
</style>
6161

6262
<script>
63-
var gwRichTextMode;
6463
jQuery( document ).on( 'gform_load_field_settings', function( event, field ) {
65-
gwRichTextMode = field.gwRichTextMode || 'tmce';
66-
6764
var id = 'field_rich_content';
65+
66+
// Ensure complete cleanup of existing editor
67+
if ( tinymce.get( id ) ) {
68+
tinymce.get( id ).remove();
69+
}
6870
wp.editor.remove( id );
71+
72+
// Clear the textarea value and set new content
6973
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 );
8089
} );
8190

8291
jQuery( document).on( 'tinymce-editor-setup', function ( event, editor ) {
@@ -112,49 +121,11 @@
112121
}
113122
} );
114123

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-
150124
// Switch to visual/text mode.
151125
jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {
152126
var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html';
153127

154128
window.switchEditors.go(editorId, mode);
155-
156-
// Save the current mode to field property.
157-
SetFieldProperty('gwRichTextMode', mode)
158129
});
159130
}
160131
} );

0 commit comments

Comments
 (0)