|
72 | 72 | if ( editor.id === editorId ) {
|
73 | 73 | editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link';
|
74 | 74 |
|
| 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 | + |
75 | 115 | // Switch to visual/text mode.
|
76 | 116 | jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {
|
77 | 117 | var mode = jQuery(this).hasClass('switch-tmce') ? 'tmce' : 'html';
|
78 | 118 |
|
79 | 119 | window.switchEditors.go(editorId, mode);
|
| 120 | + |
| 121 | + // Save the current mode to localStorage. |
| 122 | + localStorage.setItem('editorMode', mode); |
80 | 123 | });
|
81 | 124 | }
|
82 | 125 | } );
|
83 |
| - </script> |
| 126 | + </script> |
84 | 127 |
|
85 | 128 | <?php
|
86 | 129 | } );
|
0 commit comments