Skip to content

Commit 9331d8b

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 9331d8b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,35 @@
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+
7596
// Switch to visual/text mode.
7697
jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {
7798
var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html';
7899

79100
window.switchEditors.go(editorId, mode);
101+
102+
// Save the current mode to localStorage.
103+
localStorage.setItem('editorMode', mode);
80104
});
81105
}
82106
} );

0 commit comments

Comments
 (0)