Skip to content
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
49 changes: 48 additions & 1 deletion gravity-forms/gw-rich-text-html-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@
</style>

<script>
var formId, fieldId, gwRichTextMode;
jQuery( document ).on( 'gform_load_field_settings', function( event, field ) {
formId = field.formId;
fieldId = field.id;

// Get the `gwRichTextMode` from the field property.
gwRichTextMode = field.gwRichTextMode || 'tmce';

var id = 'field_rich_content';
wp.editor.remove( id );
jQuery( '#' + id ).val( field.content );
Expand All @@ -72,15 +79,55 @@
if ( editor.id === editorId ) {
editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link';

// Wait until the TinyMCE editor is initialized before switching mode.
function waitForEditorToBeReady(callback) {
var interval = setInterval(function () {
if (typeof tinymce !== 'undefined' && tinymce.get(editorId)) {
clearInterval(interval);
callback();
}
}, 100);
}

waitForEditorToBeReady(function () {
if (gwRichTextMode === 'html') {
window.switchEditors.go(editorId, 'html');
} else {
window.switchEditors.go(editorId, '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)
});
}
} );
</script>
</script>

<?php
} );
Loading