-
Notifications
You must be signed in to change notification settings - Fork 92
gw-rich-text-html-fields.php: Added improvements for Rich Text HTML Fields Snippet.
#1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5e0d06d
a0c67e6
47d20f4
5916ad8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,12 +60,31 @@ | |
| </style> | ||
|
|
||
| <script> | ||
| // Immediately disable HTML click. | ||
| jQuery( '.gf-html-container' ) | ||
| .css( 'pointer-events', 'none' ) | ||
| .css( 'opacity', '0.6' ); | ||
|
|
||
| // Re-enable only after full load of page. | ||
| jQuery( window ).on( 'load', function() { | ||
| setTimeout( function() { | ||
| jQuery( '.gf-html-container' ) | ||
| .css( 'pointer-events', 'auto' ) | ||
| .css( 'opacity', '1' ); | ||
| }, 800 ); | ||
| }); | ||
|
|
||
| var gwRichTextMode; | ||
| jQuery( document ).on( 'gform_load_field_settings', function( event, field ) { | ||
| gwRichTextMode = field.gwRichTextMode || 'tmce'; | ||
|
|
||
| var id = 'field_rich_content'; | ||
| wp.editor.remove( id ); | ||
| jQuery( '#' + id ).val( field.content ); | ||
| wp.editor.initialize( id, { | ||
| tinymce: { | ||
| forced_root_block: false, | ||
| height: 250, | ||
| setup: function( editor ) { | ||
| editor.on( 'Paste Change input Undo Redo', function () { | ||
| SetFieldProperty( 'content', editor.getContent() ); | ||
|
|
@@ -75,6 +94,7 @@ | |
| quicktags: true | ||
| } ); | ||
| } ); | ||
|
|
||
| jQuery( document).on( 'tinymce-editor-setup', function ( event, editor ) { | ||
| var editorId = 'field_rich_content'; | ||
| if ( editor.id === editorId ) { | ||
|
|
@@ -108,11 +128,49 @@ | |
| } | ||
| } ); | ||
|
|
||
| // Wait until the TinyMCE editor is initialized before switching mode. | ||
| const waitForEditorToBeReady = (callback, timeout = 5000) => { | ||
| const start = Date.now(); | ||
| const interval = setInterval(() => { | ||
| const editor = typeof tinymce !== 'undefined' && tinymce.get(editorId); | ||
| if (editor) { | ||
| clearInterval(interval); | ||
| callback(); | ||
| } else if (Date.now() - start > timeout) { | ||
| clearInterval(interval); | ||
| } | ||
| }, 100); | ||
| }; | ||
|
|
||
| waitForEditorToBeReady(() => window.switchEditors.go(editorId, gwRichTextMode === 'html' ? 'html' : 'tmce')); | ||
|
|
||
| // Set the content when save. | ||
| window.SetFieldContentProperty = function () { | ||
| var mode = jQuery('#wp-' + editorId + '-wrap').hasClass('html-active') ? 'html' : 'tmce'; | ||
| var content = ''; | ||
|
|
||
| if (mode === 'html') { | ||
| content = jQuery('#' + editorId).val(); | ||
| } else if (tinymce.get(editorId)) { | ||
| content = tinymce.get(editorId).getContent(); | ||
| } | ||
|
|
||
| SetFieldProperty('content', content); | ||
| }; | ||
|
|
||
| // Update the content. | ||
| jQuery(document).on('change', `#${editorId}`, function () { | ||
| window.SetFieldContentProperty(); | ||
| }); | ||
|
Comment on lines
+161
to
+164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use The + // Namespace the event for proper cleanup
+ jQuery(document).off('input.gwRichText', `#${editorId}`);
- jQuery(document).on('change', `#${editorId}`, function () {
+ jQuery(document).on('input.gwRichText', `#${editorId}`, function () {
window.SetFieldContentProperty();
});🤖 Prompt for AI Agents |
||
|
|
||
| // Switch to visual/text mode. | ||
| jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() { | ||
| var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html'; | ||
|
|
||
| window.switchEditors.go(editorId, mode); | ||
|
|
||
| // Save the current mode to field property. | ||
| SetFieldProperty('gwRichTextMode', mode) | ||
| }); | ||
|
Comment on lines
166
to
174
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Sync content before switching modes to prevent data loss. When switching from HTML to visual mode (or vice versa), the current content should be saved first. Otherwise, unsaved changes in the active mode could be lost during the switch. jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {
var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html';
+ // Sync content before switching to preserve unsaved changes
+ window.SetFieldContentProperty();
+
window.switchEditors.go(editorId, mode);
// Save the current mode to field property.
- SetFieldProperty('gwRichTextMode', mode)
+ SetFieldProperty('gwRichTextMode', mode);
});
🤖 Prompt for AI Agents |
||
| } | ||
| } ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential content loss if TinyMCE not ready in visual mode.
When
mode === 'tmce'buttinymce.get(editorId)returns falsy,contentremains an empty string andSetFieldPropertyis still called, potentially wiping existing content. Consider falling back to the textarea value:if (mode === 'html') { content = jQuery('#' + editorId).val(); } else if (tinymce.get(editorId)) { content = tinymce.get(editorId).getContent(); + } else { + // Fallback to textarea if TinyMCE not ready + content = jQuery('#' + editorId).val(); }🤖 Prompt for AI Agents