Markdown toolbar: the Markdown box has a toolbar like the rich text editor's - headings, bold, italic, strikethrough, link, bulleted and numbered lists, checklist, quote, code, code block, table, line and picture - acting on the selection or the cursor's line and off again when already applied, with Ctrl+B, Ctrl+I and Ctrl+K, Enter continuing or ending a list, and changes undoable with Ctrl+Z; 26.12.3 changelog entries

This commit is contained in:
johnnyq
2026-11-29 01:17:11 -05:00
parent 6cdbe60747
commit 9ff640b60b
4 changed files with 308 additions and 8 deletions

View File

@@ -2,7 +2,10 @@
* Documents written in Markdown (agent/modals/document/document_form.php, functions/markdown.php).
*
* A document's form has both editors - the rich text one (TinyMCE, js/app.js) and a plain box for
* Markdown with Write and Preview - and shows the one it is written in. Switching converts what is
* Markdown with Write and Preview - and shows the one it is written in. The Markdown box has a
* toolbar like the rich text editor's (headings, bold, italic, struck, link, lists, checklist,
* quote, code, table, line, picture), Ctrl+B, Ctrl+I and Ctrl+K, and Enter in a list starts the
* next item. Switching converts what is
* written into the other, through agent/ajax.php, so nothing has to be typed again; switching to
* Markdown says when some of it stays as HTML, or alignment was dropped.
*
@@ -82,6 +85,7 @@
var rich = box.querySelector('[data-editor-rich]');
var markdown = box.querySelector('[data-editor-markdown]');
var note = box.querySelector('[data-format-note]');
var toolbar = box.querySelector('[data-markdown-toolbar]');
var radios = box.querySelectorAll('input[name="format"]');
if (!form || !input || !rich || !markdown) {
return;
@@ -175,12 +179,239 @@
}
});
/*
* The toolbar, and Ctrl+B, Ctrl+I and Ctrl+K - each on what is picked, or where the cursor
* is. Changes go in as typing would, so Ctrl+Z takes them back.
*/
function put(start, end, text, selectStart, selectEnd) {
input.focus();
input.setSelectionRange(start, end);
if (text === '' && start === end) {
input.setSelectionRange(selectStart, selectEnd);
return;
}
var typed = false;
try {
typed = text === '' ? document.execCommand('delete') : document.execCommand('insertText', false, text);
} catch (e) {
typed = false;
}
if (!typed || input.value.slice(start, start + text.length) !== text) {
input.setRangeText(text, start, end, 'end');
input.dispatchEvent(new Event('input', { bubbles: true }));
}
input.setSelectionRange(selectStart, selectEnd);
}
// Around what is picked: **bold** - or taken off again when it is already there
function wrap(before, after, example) {
var v = input.value;
var start = input.selectionStart;
var end = input.selectionEnd;
var picked = v.slice(start, end);
if (picked.length >= before.length + after.length && picked.indexOf(before) === 0 && picked.slice(-after.length) === after) {
var bare = picked.slice(before.length, picked.length - after.length);
put(start, end, bare, start, start + bare.length);
return;
}
if (v.slice(start - before.length, start) === before && v.slice(end, end + after.length) === after) {
put(start - before.length, end + after.length, picked, start - before.length, end - before.length);
return;
}
// Spaces picked at either end stay outside, as Markdown wants
var lead = picked.match(/^\s*/)[0];
var trail = picked.slice(lead.length).match(/\s*$/)[0];
var text = picked.slice(lead.length, picked.length - trail.length) || example;
put(start, end, lead + before + text + after + trail, start + lead.length + before.length, start + lead.length + before.length + text.length);
}
// The whole lines the cursor or what is picked is on: [start, end]
function lineRange() {
var v = input.value;
var start = v.lastIndexOf('\n', input.selectionStart - 1) + 1;
var to = input.selectionEnd > input.selectionStart && v[input.selectionEnd - 1] === '\n' ? input.selectionEnd - 1 : input.selectionEnd;
var end = v.indexOf('\n', to);
return [start, end === -1 ? v.length : end];
}
var MARKS = {
bullets: /^(\s*)[-*+] (?!\[[ xX]\] )/,
numbers: /^(\s*)\d+[.)] /,
tasks: /^(\s*)[-*+] \[[ xX]\] /,
quote: /^(\s*)> ?/
};
var ANY_LIST = /^(\s*)([-*+] \[[ xX]\] |[-*+] |\d+[.)] )/;
// Each line made a list item, a checklist item or a quote - or back to text when all of them were
function prefixLines(kind) {
var range = lineRange();
var lines = input.value.slice(range[0], range[1]).split('\n');
var filled = lines.filter(function (line) { return line.trim() !== ''; });
var all = filled.length > 0 && filled.every(function (line) { return MARKS[kind].test(line); });
var number = 0;
var out = lines.map(function (line) {
if (all) {
return line.replace(MARKS[kind], '$1');
}
if (line.trim() === '' && lines.length > 1) {
return line;
}
var indent = line.match(/^\s*/)[0];
var text = kind === 'quote' ? line.replace(MARKS.quote, '$1').slice(indent.length) : line.replace(ANY_LIST, '$1').slice(indent.length);
number += 1;
var mark = kind === 'bullets' ? '- ' : kind === 'numbers' ? number + '. ' : kind === 'tasks' ? '- [ ] ' : '> ';
return indent + mark + text;
}).join('\n');
if (lines.length === 1) {
put(range[0], range[1], out, range[0] + out.length, range[0] + out.length);
} else {
put(range[0], range[1], out, range[0], range[0] + out.length);
}
}
// The lines made a heading of that level - again, or Normal text, back to text
function heading(level) {
var range = lineRange();
var lines = input.value.slice(range[0], range[1]).split('\n');
var filled = lines.filter(function (line) { return line.trim() !== ''; });
var same = level > 0 && filled.length > 0 && filled.every(function (line) { return new RegExp('^#{' + level + '} ').test(line); });
var out = lines.map(function (line) {
var text = line.replace(/^#{1,6}\s+/, '');
if (level === 0 || same || (line.trim() === '' && lines.length > 1)) {
return text;
}
return '#'.repeat(level) + ' ' + text;
}).join('\n');
put(range[0], range[1], out, range[0] + out.length, range[0] + out.length);
}
// Something that goes on lines of its own - a blank line before and after it
function block(text, selectFrom, selectTo) {
var v = input.value;
var start = input.selectionStart;
var end = input.selectionEnd;
var before = v.slice(0, start);
var gap = before === '' || /\n\n$/.test(before) ? '' : (/\n$/.test(before) ? '\n' : '\n\n');
var after = v.slice(end);
var tail = after === '' || /^\n\n/.test(after) ? '\n' : (/^\n/.test(after) ? '\n' : '\n\n');
put(start, end, gap + text + tail, start + gap.length + selectFrom, start + gap.length + selectTo);
}
var actions = {
h1: function () { heading(1); },
h2: function () { heading(2); },
h3: function () { heading(3); },
paragraph: function () { heading(0); },
bold: function () { wrap('**', '**', 'bold text'); },
italic: function () { wrap('_', '_', 'italic text'); },
strike: function () { wrap('~~', '~~', 'struck text'); },
code: function () {
var picked = input.value.slice(input.selectionStart, input.selectionEnd);
if (picked.indexOf('\n') !== -1) {
actions.codeblock();
} else {
wrap('`', '`', 'code');
}
},
codeblock: function () {
var picked = input.value.slice(input.selectionStart, input.selectionEnd).replace(/\n$/, '');
block('```\n' + picked + '\n```', 4, 4 + picked.length);
},
link: function () {
var start = input.selectionStart;
var end = input.selectionEnd;
var picked = input.value.slice(start, end);
if (/^(https?:\/\/|mailto:)\S+$/i.test(picked)) {
put(start, end, '[](' + picked + ')', start + 1, start + 1);
} else {
var words = picked || 'link text';
var text = '[' + words + '](https://)';
// The address to type over - or the words, when there were none
var at = picked ? start + words.length + 3 : start + 1;
put(start, end, text, at, picked ? at + 8 : at + words.length);
}
},
bullets: function () { prefixLines('bullets'); },
numbers: function () { prefixLines('numbers'); },
tasks: function () { prefixLines('tasks'); },
quote: function () { prefixLines('quote'); },
table: function () {
block('| Column 1 | Column 2 |\n|---|---|\n| | |', 2, 10);
},
line: function () {
block('---', 3, 3);
},
picture: function () {
var picker = box.querySelector('[data-markdown-picture]');
if (picker) {
picker.click();
}
}
};
if (toolbar) {
toolbar.addEventListener('click', function (e) {
var button = e.target.closest('[data-md]');
if (!button || !actions[button.getAttribute('data-md')]) {
return;
}
e.preventDefault();
actions[button.getAttribute('data-md')]();
});
// Keeps what is picked in the box picked while a button is pressed
toolbar.addEventListener('mousedown', function (e) {
if (e.target.closest('.itflow-markdown-button[data-md]')) {
e.preventDefault();
}
});
var picker = box.querySelector('[data-markdown-picture]');
if (picker) {
picker.addEventListener('change', function () {
Array.prototype.forEach.call(picker.files || [], addPicture);
picker.value = '';
});
}
}
input.addEventListener('keydown', function (e) {
var key = (e.key || '').toLowerCase();
if ((e.ctrlKey || e.metaKey) && !e.altKey && !e.shiftKey && (key === 'b' || key === 'i' || key === 'k')) {
e.preventDefault();
actions[key === 'b' ? 'bold' : key === 'i' ? 'italic' : 'link']();
return;
}
// Enter in a list, checklist or quote starts the next item - and on an empty one, ends it
if (e.key !== 'Enter' || e.shiftKey || e.ctrlKey || e.metaKey || e.altKey || e.isComposing || input.selectionStart !== input.selectionEnd) {
return;
}
var v = input.value;
var at = input.selectionStart;
var lineStart = v.lastIndexOf('\n', at - 1) + 1;
var lineEnd = v.indexOf('\n', at);
var line = v.slice(lineStart, lineEnd === -1 ? v.length : lineEnd);
var m = line.match(/^(\s*)([-*+] \[[ xX]\] |[-*+] |(\d+)([.)]) |> )/);
if (!m || at < lineStart + m[0].length) {
return;
}
e.preventDefault();
// An empty item ends the list, with a blank line after it, so what comes next isn't part of it
if (line.slice(m[0].length).trim() === '') {
put(lineStart, lineStart + line.length, '\n', lineStart + 1, lineStart + 1);
return;
}
var mark = m[3] ? m[1] + (parseInt(m[3], 10) + 1) + m[4] + ' ' : m[1] + m[2].replace(/\[[xX]\]/, '[ ]');
put(at, at, '\n' + mark, at + 1 + mark.length, at + 1 + mark.length);
});
// Write and Preview
function showTab(which) {
box.querySelectorAll('[data-markdown-tab]').forEach(function (tab) {
tab.classList.toggle('active', tab.getAttribute('data-markdown-tab') === which);
});
input.hidden = which === 'preview';
if (toolbar) {
toolbar.hidden = which === 'preview';
}
preview.hidden = which !== 'preview';
if (which !== 'preview') {
input.focus();