Skip to content

Commit 47d20f4

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

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

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

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

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

9184
jQuery( document).on( 'tinymce-editor-setup', function ( event, editor ) {
@@ -121,11 +114,49 @@
121114
}
122115
} );
123116

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

128156
window.switchEditors.go(editorId, mode);
157+
158+
// Save the current mode to field property.
159+
SetFieldProperty('gwRichTextMode', mode)
129160
});
130161
}
131162
} );

0 commit comments

Comments
 (0)