jQuery(document).ready(function($) { var editor = $('#user_bio_editor'); var hiddenTextarea = $('#user_bio'); var specializationEditor = $('#user_specialization_editor'); var specializationTextarea = $('#user_specialization'); // Rich text editor functionality for both editors function initRichEditor(editorEl, textareaEl, targetName, charCountId) { // Update character counter function updateCharCount() { var textContent = editorEl.text().length; $('#' + charCountId).text(textContent); } // Update hidden textarea with HTML content function updateHiddenTextarea() { textareaEl.val(editorEl.html()); updateCharCount(); } // Initial setup updateCharCount(); // Toolbar button functionality var buttonSelector = targetName ? '.editor-btn[data-target="' + targetName + '"]' : '.editor-btn:not([data-target])'; $(buttonSelector).on('click', function(e) { e.preventDefault(); var command = $(this).data('command'); // Focus the correct editor editorEl.focus(); if (command === 'createLink') { var url = prompt('Enter the URL:'); if (url) { document.execCommand('createLink', false, url); } } else { document.execCommand(command, false, null); } // Update button states updateToolbarButtons(); updateHiddenTextarea(); editorEl.focus(); }); // Update toolbar button states based on current selection function updateToolbarButtons() { $(buttonSelector).each(function() { var command = $(this).data('command'); var isActive = false; try { isActive = document.queryCommandState(command); } catch(e) { // Some commands might not be supported } $(this).toggleClass('active', isActive); }); } // Handle editor events editorEl.on('input keyup mouseup focus', function() { updateHiddenTextarea(); updateToolbarButtons(); }); editorEl.on('paste', function(e) { // Allow paste but clean it up setTimeout(function() { updateHiddenTextarea(); }, 10); }); // Handle keyboard shortcuts editorEl.on('keydown', function(e) { if (e.ctrlKey || e.metaKey) { switch(e.keyCode) { case 66: // Ctrl+B e.preventDefault(); document.execCommand('bold', false, null); break; case 73: // Ctrl+I e.preventDefault(); document.execCommand('italic', false, null); break; case 85: // Ctrl+U e.preventDefault(); document.execCommand('underline', false, null); break; } updateToolbarButtons(); updateHiddenTextarea(); } }); } // Initialize both editors initRichEditor(editor, hiddenTextarea, null, 'char-count'); initRichEditor(specializationEditor, specializationTextarea, 'specialization', 'specialization-char-count'); // Form submission $('#user-bio-form').on('submit', function(e) { e.preventDefault(); var $form = $(this); var $submitBtn = $form.find('.btn-update'); var $message = $('#bio-message'); // Update hidden textareas before submit $('#user_bio').val($('#user_bio_editor').html()); $('#user_specialization').val($('#user_specialization_editor').html()); // Show loading state $submitBtn.addClass('loading'); $submitBtn.find('.btn-icon').text('⏳'); $message.hide(); var formData = { action: 'update_user_bio', nonce: user_bio_ajax.nonce, first_name: $('#user_first_name').val(), last_name: $('#user_last_name').val(), website: $('#user_website').val(), bio: $('#user_bio').val(), specialization: $('#user_specialization').val() }; $.ajax({ url: user_bio_ajax.ajax_url, type: 'POST', data: formData, success: function(response) { if (response.success) { $message.removeClass('error').addClass('success').text('✅ ' + response.data.message).show(); } else { $message.removeClass('success').addClass('error').text('❌ ' + response.data.message).show(); } }, error: function() { $message.removeClass('success').addClass('error').text('❌ An error occurred. Please try again.').show(); }, complete: function() { // Remove loading state $submitBtn.removeClass('loading'); $submitBtn.find('.btn-icon').text('💾'); } }); }); // Reset form functionality $('#user-bio-form').on('reset', function() { setTimeout(function() { $('#user_bio_editor').html($('#user_bio').val()); $('#user_specialization_editor').html($('#user_specialization').val()); $('#char-count').text($('#user_bio_editor').text().length); $('#specialization-char-count').text($('#user_specialization_editor').text().length); }, 10); }); });