Skip to content

Commit 814c201

Browse files
committed
gw-rich-text-html-fields.php: Added functionality to remember the last selected Visual or HTML editor mode.
1 parent 3053ec0 commit 814c201

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@
5252
</style>
5353

5454
<script>
55+
var formId, fieldId, gwRichTextMode;
5556
jQuery( document ).on( 'gform_load_field_settings', function( event, field ) {
57+
formId = field.formId;
58+
fieldId = field.id;
59+
60+
// Get the `gwRichTextMode` from the field property.
61+
gwRichTextMode = field.gwRichTextMode || 'tmce';
62+
5663
var id = 'field_rich_content';
5764
wp.editor.remove( id );
5865
jQuery( '#' + id ).val( field.content );
@@ -72,15 +79,55 @@
7279
if ( editor.id === editorId ) {
7380
editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link';
7481

82+
// Wait until the TinyMCE editor is initialized before switching mode.
83+
function waitForEditorToBeReady(callback) {
84+
var interval = setInterval(function () {
85+
if (typeof tinymce !== 'undefined' && tinymce.get(editorId)) {
86+
clearInterval(interval);
87+
callback();
88+
}
89+
}, 100);
90+
}
91+
92+
waitForEditorToBeReady(function () {
93+
if (gwRichTextMode === 'html') {
94+
window.switchEditors.go(editorId, 'html');
95+
} else {
96+
window.switchEditors.go(editorId, 'tmce');
97+
}
98+
});
99+
100+
// Set the content when save.
101+
window.SetFieldContentProperty = function () {
102+
var mode = jQuery('#wp-' + editorId + '-wrap').hasClass('html-active') ? 'html' : 'tmce';
103+
var content = '';
104+
105+
if (mode === 'html') {
106+
content = jQuery('#' + editorId).val();
107+
} else if (tinymce.get(editorId)) {
108+
content = tinymce.get(editorId).getContent();
109+
}
110+
111+
SetFieldProperty('content', content);
112+
};
113+
114+
// Update the content.
115+
jQuery(document).on('change', `#${editorId}`, function () {
116+
window.SetFieldContentProperty();
117+
});
118+
75119
// Switch to visual/text mode.
76120
jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {
77121
var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html';
78122

79123
window.switchEditors.go(editorId, mode);
124+
125+
// Save the current mode to field property.
126+
SetFieldProperty('gwRichTextMode', mode)
80127
});
81128
}
82129
} );
83-
</script>
130+
</script>
84131

85132
<?php
86133
} );

0 commit comments

Comments
 (0)