diff --git a/assets/js/markdown.js b/assets/js/markdown.js index b9653ce..172ff9a 100644 --- a/assets/js/markdown.js +++ b/assets/js/markdown.js @@ -506,6 +506,25 @@ function toolbarHeading(textareaId) { textarea.dispatchEvent(new Event('input', { bubbles: true })); } +function toolbarTable(textareaId) { + const textarea = document.getElementById(textareaId); + if (!textarea) return; + + const start = textarea.selectionStart; + const text = textarea.value; + + // Insert on its own line(s), matching the blank-line-before convention + // toolbarList/toolbarHeading rely on the surrounding text for — a table + // needs a full line to itself both before and after the separator row. + const needsLeadingNewline = start > 0 && text[start - 1] !== '\n'; + const template = (needsLeadingNewline ? '\n' : '') + + '| Header 1 | Header 2 |\n' + + '| --- | --- |\n' + + '| Cell 1 | Cell 2 |\n'; + + insertMarkdownText(textareaId, template); +} + function toolbarQuote(textareaId) { const textarea = document.getElementById(textareaId); if (!textarea) return; @@ -544,6 +563,7 @@ function createEditorToolbar(textareaId, containerId) { + `; @@ -563,6 +583,7 @@ function createEditorToolbar(textareaId, containerId) { case 'heading': toolbarHeading(targetId); break; case 'list': toolbarList(targetId); break; case 'quote': toolbarQuote(targetId); break; + case 'table': toolbarTable(targetId); break; case 'link': toolbarLink(targetId); break; } }); @@ -578,6 +599,7 @@ window.toolbarLink = toolbarLink; window.toolbarList = toolbarList; window.toolbarHeading = toolbarHeading; window.toolbarQuote = toolbarQuote; +window.toolbarTable = toolbarTable; window.createEditorToolbar = createEditorToolbar; window.insertMarkdownFormat = insertMarkdownFormat; window.insertMarkdownText = insertMarkdownText;