Skip to content

Commit 7541990

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 7541990

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

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

+44-1
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,58 @@
7272
if ( editor.id === editorId ) {
7373
editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link';
7474

75+
// Set the initial mode based on previous user selection.
76+
var savedMode = localStorage.getItem('editorMode');
77+
78+
// Wait until the TinyMCE editor is initialized before switching mode.
79+
function waitForEditorToBeReady(callback) {
80+
var interval = setInterval(function () {
81+
if (typeof tinymce !== 'undefined' && tinymce.get(editorId)) {
82+
clearInterval(interval);
83+
callback();
84+
}
85+
}, 100);
86+
}
87+
88+
waitForEditorToBeReady(function () {
89+
if (savedMode === 'html') {
90+
window.switchEditors.go(editorId, 'html');
91+
} else {
92+
window.switchEditors.go(editorId, 'tmce');
93+
}
94+
});
95+
96+
// Set the content when save.
97+
window.SetFieldContentProperty = function () {
98+
var mode = jQuery('#wp-' + editorId + '-wrap').hasClass('html-active') ? 'html' : 'tmce';
99+
var content = '';
100+
101+
if (mode === 'html') {
102+
content = jQuery('#' + editorId).val();
103+
} else if (tinymce.get(editorId)) {
104+
content = tinymce.get(editorId).getContent();
105+
}
106+
107+
SetFieldProperty('content', content);
108+
};
109+
110+
// Update the content.
111+
jQuery(document).on('change', `#${editorId}`, function () {
112+
window.SetFieldContentProperty();
113+
});
114+
75115
// Switch to visual/text mode.
76116
jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {
77117
var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html';
78118

79119
window.switchEditors.go(editorId, mode);
120+
121+
// Save the current mode to localStorage.
122+
localStorage.setItem('editorMode', mode);
80123
});
81124
}
82125
} );
83-
</script>
126+
</script>
84127

85128
<?php
86129
} );

0 commit comments

Comments
 (0)