|
52 | 52 | </style>
|
53 | 53 |
|
54 | 54 | <script>
|
| 55 | + var formId, fieldId, editorModeKey; |
55 | 56 | jQuery( document ).on( 'gform_load_field_settings', function( event, field ) {
|
| 57 | + formId = field.formId; |
| 58 | + fieldId = field.id; |
| 59 | + |
| 60 | + // Generate a unique key for the editor mode based on formId and fieldId. |
| 61 | + editorModeKey = `gw-rich-text-html-mode-${formId}-${fieldId}`; |
| 62 | + |
56 | 63 | var id = 'field_rich_content';
|
57 | 64 | wp.editor.remove( id );
|
58 | 65 | jQuery( '#' + id ).val( field.content );
|
|
72 | 79 | if ( editor.id === editorId ) {
|
73 | 80 | editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link';
|
74 | 81 |
|
| 82 | + // Set the initial mode based on previous user selection. |
| 83 | + var savedMode = localStorage.getItem(editorModeKey); |
| 84 | + |
| 85 | + // Wait until the TinyMCE editor is initialized before switching mode. |
| 86 | + function waitForEditorToBeReady(callback) { |
| 87 | + var interval = setInterval(function () { |
| 88 | + if (typeof tinymce !== 'undefined' && tinymce.get(editorId)) { |
| 89 | + clearInterval(interval); |
| 90 | + callback(); |
| 91 | + } |
| 92 | + }, 100); |
| 93 | + } |
| 94 | + |
| 95 | + waitForEditorToBeReady(function () { |
| 96 | + if (savedMode === 'html') { |
| 97 | + window.switchEditors.go(editorId, 'html'); |
| 98 | + } else { |
| 99 | + window.switchEditors.go(editorId, 'tmce'); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + // Set the content when save. |
| 104 | + window.SetFieldContentProperty = function () { |
| 105 | + var mode = jQuery('#wp-' + editorId + '-wrap').hasClass('html-active') ? 'html' : 'tmce'; |
| 106 | + var content = ''; |
| 107 | + |
| 108 | + if (mode === 'html') { |
| 109 | + content = jQuery('#' + editorId).val(); |
| 110 | + } else if (tinymce.get(editorId)) { |
| 111 | + content = tinymce.get(editorId).getContent(); |
| 112 | + } |
| 113 | + |
| 114 | + SetFieldProperty('content', content); |
| 115 | + }; |
| 116 | + |
| 117 | + // Update the content. |
| 118 | + jQuery(document).on('change', `#${editorId}`, function () { |
| 119 | + window.SetFieldContentProperty(); |
| 120 | + }); |
| 121 | + |
75 | 122 | // Switch to visual/text mode.
|
76 | 123 | jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {
|
77 | 124 | var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html';
|
78 | 125 |
|
79 | 126 | window.switchEditors.go(editorId, mode);
|
| 127 | + |
| 128 | + // Save the current mode to localStorage. |
| 129 | + localStorage.setItem(editorModeKey, mode); |
80 | 130 | });
|
81 | 131 | }
|
82 | 132 | } );
|
83 |
| - </script> |
| 133 | + </script> |
84 | 134 |
|
85 | 135 | <?php
|
86 | 136 | } );
|
0 commit comments