/**
* Markdown previews (posts, comments, messages)
*/
import { fetchJson } from './utils/requests';
import { bindImageTarget } from './image_expansion';
import { filterNode } from './imagesclientside';
import { hideEl, showEl } from './utils/dom';
function handleError(response) {
const errorMessage = '
Preview failed to load!
';
if (!response.ok) {
return errorMessage;
}
return response.text();
}
function commentReply(user, url, textarea, quote) {
const text = `[${user}](${url})`;
let newval = textarea.value;
if (newval && /\n$/.test(newval)) newval += '\n';
newval += `${text}\n`;
if (quote) {
newval += `> ${quote.replace(/\n/g, '\n> ')}\n\n`;
}
textarea.value = newval;
textarea.selectionStart = textarea.selectionEnd = newval.length;
const writeTabToggle = document.querySelector('a[data-click-tab="write"]:not(.selected)');
if (writeTabToggle) writeTabToggle.click();
textarea.focus();
}
function getPreview(body, anonymous, previewLoading, previewIdle, previewContent) {
const path = '/posts/preview';
if (typeof body !== 'string') return;
showEl(previewLoading);
hideEl(previewIdle);
fetchJson('POST', path, { body, anonymous })
.then(handleError)
.then((data) => {
previewContent.innerHTML = data;
filterNode(previewContent);
bindImageTarget(previewContent);
showEl(previewIdle);
hideEl(previewLoading);
});
}
/**
* Resizes the event target