From 875315e2f589930f23e0bf9864f1881e72e243e4 Mon Sep 17 00:00:00 2001 From: ben Date: Fri, 26 Sep 2025 19:03:52 -0300 Subject: [PATCH] debug: add comprehensive TinyMCE timing and markdown testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Improved TinyMCE initialization detection with hvacTinyMCEReady flag - Added robust retry mechanism for content insertion (20 attempts with 250ms intervals) - Enhanced debugging with console logging for markdown conversion process - Added global testMarkdownConversion() function for browser console testing - Implemented proper timing coordination between WordPress editor and AI Assistant 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- assets/js/hvac-ai-assist.js | 97 +++++++++++++++++++--- includes/class-hvac-event-form-builder.php | 19 +++++ 2 files changed, 104 insertions(+), 12 deletions(-) diff --git a/assets/js/hvac-ai-assist.js b/assets/js/hvac-ai-assist.js index 8c98105c..7912e330 100644 --- a/assets/js/hvac-ai-assist.js +++ b/assets/js/hvac-ai-assist.js @@ -571,19 +571,47 @@ jQuery(document).ready(function($) { // Convert markdown to HTML for proper rich text editor formatting const htmlContent = this.markdownToHtml(data.description); - // Try TinyMCE first if available - if (typeof tinyMCE !== 'undefined' && tinyMCE.get('event_description')) { - tinyMCE.get('event_description').setContent(htmlContent); - } else { - // Update the hidden textarea with HTML content - $('#event_description, [name="event_description"]').val(htmlContent); + console.log('Original markdown:', data.description); + console.log('Converted HTML:', htmlContent); - // Also update the visible rich text editor div if it exists - const $richEditor = $('#event-description-editor'); - if ($richEditor.length && $richEditor.is('[contenteditable]')) { - $richEditor.html(htmlContent); + // Wait for TinyMCE to be fully initialized + const applyToTinyMCE = () => { + if (typeof tinyMCE !== 'undefined' && tinyMCE.get('event_description') && window.hvacTinyMCEReady) { + console.log('Setting TinyMCE content'); + tinyMCE.get('event_description').setContent(htmlContent); + return true; } - } + return false; + }; + + // Use a more robust waiting mechanism + const waitForTinyMCE = (maxAttempts = 20) => { + let attempts = 0; + const tryApply = () => { + attempts++; + if (applyToTinyMCE()) { + console.log(`TinyMCE content applied successfully on attempt ${attempts}`); + return; + } + + if (attempts < maxAttempts) { + setTimeout(tryApply, 250); + } else { + console.log('TinyMCE not available after maximum attempts, falling back to textarea'); + // Update the hidden textarea with HTML content + $('#event_description, [name="event_description"]').val(htmlContent); + + // Also update the visible rich text editor div if it exists + const $richEditor = $('#event-description-editor'); + if ($richEditor.length && $richEditor.is('[contenteditable]')) { + $richEditor.html(htmlContent); + } + } + }; + tryApply(); + }; + + waitForTinyMCE(); } // Apply start date and time (combine into datetime-local format) @@ -714,6 +742,11 @@ jQuery(document).ready(function($) { * Convert markdown to HTML for rich text editor */ markdownToHtml: function(markdown) { + // Test the function with sample input + if (window.hvacDebugMarkdown) { + console.log('Testing markdown conversion:'); + console.log('Input:', markdown); + } let html = markdown; // Convert headers (#### -> h4, ### -> h3, ## -> h2, # -> h1) @@ -818,7 +851,13 @@ jQuery(document).ready(function($) { } }); - return formattedParagraphs.join('\n'); + const result = formattedParagraphs.join('\n'); + + if (window.hvacDebugMarkdown) { + console.log('Output:', result); + } + + return result; }, /** @@ -833,4 +872,38 @@ jQuery(document).ready(function($) { // Initialize when document is ready HVACAIAssist.init(); + + // Add global test function for debugging + window.testMarkdownConversion = function(testMarkdown) { + window.hvacDebugMarkdown = true; + console.log('=== MARKDOWN CONVERSION TEST ==='); + + const testInput = testMarkdown || `## Event Overview + +This is a **bold** text and *italic* text example. + +#### Key Details +* First item in list +* Second item in list +* Third item in list + +### Additional Information +Here's a regular paragraph with more details.`; + + const result = HVACAIAssist.markdownToHtml(testInput); + console.log('=== TEST COMPLETE ==='); + + // Also test setting it to TinyMCE if available + if (typeof tinyMCE !== 'undefined' && tinyMCE.get('event_description')) { + console.log('Setting test content to TinyMCE...'); + tinyMCE.get('event_description').setContent(result); + } else { + console.log('TinyMCE not available for testing'); + } + + window.hvacDebugMarkdown = false; + return result; + }; + + console.log('HVAC AI Assist loaded. Use testMarkdownConversion() to test markdown conversion.'); }); \ No newline at end of file diff --git a/includes/class-hvac-event-form-builder.php b/includes/class-hvac-event-form-builder.php index 2da42040..c407339d 100644 --- a/includes/class-hvac-event-form-builder.php +++ b/includes/class-hvac-event-form-builder.php @@ -1673,6 +1673,25 @@ HTML; wp_editor('', 'event_description', $editor_settings); ?> Use the editor above to format your event description with headings, lists, and formatting. +