Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions gravity-forms/gw-rich-text-html-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@
</style>

<script>
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 );
Expand All @@ -75,6 +78,7 @@
quicktags: true
} );
} );

jQuery( document).on( 'tinymce-editor-setup', function ( event, editor ) {
var editorId = 'field_rich_content';
if ( editor.id === editorId ) {
Expand Down Expand Up @@ -108,11 +112,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();
});

// 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)
});
}
} );
Expand Down
Loading