// Integration for converting pasted spreadsheet tables to Markdown tables in EasyMDE/CodeMirror // Handles both HTML tables (from Google Sheets) and tab-separated values window.markdownTablePaste = { /** * Initializes paste handler for a MarkdownEditor instance. * Finds the textarea or CodeMirror instance created by EasyMDE and attaches paste event handler. * @param {string} editorId - The ID of the editor wrapper element, or null to find by class */ initialize: function(editorId) { let attempts = 0; const maxAttempts = 5; const tryInitialize = function() { attempts++; let textarea = null; let codeMirror = null; // Try to find the textarea element if (editorId) { const editorElement = document.getElementById(editorId); if (editorElement) { textarea = editorElement.querySelector('textarea'); } } else { // Find the most recently created EasyMDE textarea (last one in DOM) const containers = document.querySelectorAll('.EasyMDEContainer'); if (containers.length > 0) { const lastContainer = containers[containers.length - 1]; textarea = lastContainer.querySelector('textarea'); } // Fallback: try to find any textarea near an editor-toolbar if (!textarea) { const toolbars = document.querySelectorAll('.editor-toolbar'); if (toolbars.length > 0) { const lastToolbar = toolbars[toolbars.length - 1]; const nextSibling = lastToolbar.nextElementSibling; if (nextSibling && nextSibling.tagName === 'TEXTAREA') { textarea = nextSibling; } } } // Another fallback: find any textarea that's a child of a container with editor classes if (!textarea) { const allTextareas = document.querySelectorAll('textarea'); for (let ta of allTextareas) { const parent = ta.parentElement; if (parent && ( parent.classList.contains('EasyMDEContainer') || parent.classList.contains('editor') || parent.querySelector('.editor-toolbar') )) { textarea = ta; break; } } } } // If we found a textarea, try to get the CodeMirror instance from EasyMDE if (textarea) { if (window.EasyMDE) { const easyMDEInstances = window.EasyMDE.instances || []; for (let i = 0; i < easyMDEInstances.length; i++) { const instance = easyMDEInstances[i]; if (instance && instance.codemirror) { const cmTextarea = instance.codemirror.getTextArea(); if (cmTextarea === textarea) { codeMirror = instance.codemirror; break; } } } // Alternative: try to get CodeMirror from the textarea's parent if (!codeMirror && textarea.parentElement) { const parent = textarea.parentElement; if (parent._easyMDEInstance && parent._easyMDEInstance.codemirror) { codeMirror = parent._easyMDEInstance.codemirror; } } } const target = codeMirror || textarea; if (target) { // Add paste handler if (codeMirror) { codeMirror.on('paste', function(cm, event) { handleManualPaste(event, cm); }); } else if (textarea) { textarea.addEventListener('paste', function(event) { handleManualPaste(event, textarea); }, true); // Use capture phase to intercept early } return; // Success - we're done } } // If we didn't find the textarea, retry if (attempts < maxAttempts) { setTimeout(tryInitialize, attempts * 100); } }; // Start with initial delay to allow EasyMDE to initialize setTimeout(tryInitialize, 100); } }; /** * Handler for converting pasted spreadsheet tables to Markdown tables. * Handles both HTML tables (from Google Sheets) and tab-separated values. */ function handleManualPaste(event, target) { const clipboardData = event.clipboardData || window.clipboardData; if (!clipboardData) return; const html = clipboardData.getData('text/html'); const text = clipboardData.getData('text/plain'); let markdownTable = null; // Check if HTML contains a table if (html && html.includes('