2019-10-05 02:09:52 +02:00
|
|
|
/**
|
2021-09-13 01:19:00 +02:00
|
|
|
* Markdown previews (posts, comments, messages)
|
2019-10-05 02:09:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { fetchJson } from './utils/requests';
|
2023-03-29 16:59:28 +02:00
|
|
|
import { bindImageTarget } from './image_expansion';
|
2019-10-05 02:09:52 +02:00
|
|
|
import { filterNode } from './imagesclientside';
|
2022-01-31 20:28:38 +01:00
|
|
|
import { hideEl, showEl } from './utils/dom';
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
function handleError(response) {
|
|
|
|
const errorMessage = '<div>Preview failed to load!</div>';
|
|
|
|
|
2021-09-13 22:26:01 +02:00
|
|
|
if (!response.ok) {
|
2019-10-05 02:09:52 +02:00
|
|
|
return errorMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.text();
|
|
|
|
}
|
|
|
|
|
|
|
|
function commentReply(user, url, textarea, quote) {
|
2021-10-01 16:03:42 +02:00
|
|
|
const text = `[${user}](${url})`;
|
2019-10-05 02:09:52 +02:00
|
|
|
let newval = textarea.value;
|
|
|
|
|
|
|
|
if (newval && /\n$/.test(newval)) newval += '\n';
|
|
|
|
newval += `${text}\n`;
|
|
|
|
|
2021-09-13 22:26:01 +02:00
|
|
|
if (quote) {
|
2021-10-01 16:11:28 +02:00
|
|
|
newval += `> ${quote.replace(/\n/g, '\n> ')}\n\n`;
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
function getPreview(body, anonymous, previewLoading, previewIdle, previewContent) {
|
2021-09-13 22:26:01 +02:00
|
|
|
const path = '/posts/preview';
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2021-09-13 01:19:00 +02:00
|
|
|
if (typeof body !== 'string') return;
|
|
|
|
|
|
|
|
showEl(previewLoading);
|
2021-09-29 20:00:46 +02:00
|
|
|
hideEl(previewIdle);
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
fetchJson('POST', path, { body, anonymous })
|
2019-10-05 02:09:52 +02:00
|
|
|
.then(handleError)
|
|
|
|
.then(data => {
|
2021-09-13 01:19:00 +02:00
|
|
|
previewContent.innerHTML = data;
|
|
|
|
filterNode(previewContent);
|
2023-03-29 16:59:28 +02:00
|
|
|
bindImageTarget(previewContent);
|
2021-09-29 20:00:46 +02:00
|
|
|
showEl(previewIdle);
|
2021-09-13 01:19:00 +02:00
|
|
|
hideEl(previewLoading);
|
2019-10-05 02:09:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-13 01:19:00 +02:00
|
|
|
/**
|
|
|
|
* Resizes the event target <textarea> to match the size of its contained text, between set
|
|
|
|
* minimum and maximum height values. Former comes from CSS, latter is hard coded below.
|
|
|
|
* @template {{ target: HTMLTextAreaElement }} E
|
|
|
|
* @param {E} e
|
|
|
|
*/
|
|
|
|
function resizeTextarea(e) {
|
|
|
|
const { borderTopWidth, borderBottomWidth, height } = window.getComputedStyle(e.target);
|
|
|
|
// Add scrollHeight and borders (because border-box) to get the target size that avoids scrollbars
|
|
|
|
const contentHeight = e.target.scrollHeight + parseFloat(borderTopWidth) + parseFloat(borderBottomWidth);
|
|
|
|
// Get the original default height provided by page styles
|
2021-10-01 00:43:04 +02:00
|
|
|
const currentHeight = parseFloat(height);
|
2021-09-13 01:19:00 +02:00
|
|
|
// Limit textarea's size to between the original height and 1000px
|
2021-10-01 00:43:04 +02:00
|
|
|
const newHeight = Math.max(currentHeight, Math.min(1000, contentHeight));
|
2021-09-13 01:19:00 +02:00
|
|
|
e.target.style.height = `${newHeight}px`;
|
|
|
|
}
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
function setupPreviews() {
|
|
|
|
let textarea = document.querySelector('.js-preview-input');
|
|
|
|
|
2021-09-13 22:26:01 +02:00
|
|
|
if (!textarea) {
|
2019-10-05 02:09:52 +02:00
|
|
|
textarea = document.querySelector('.js-preview-description');
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
const previewButton = document.querySelector('a[data-click-tab="preview"]');
|
|
|
|
const previewLoading = document.querySelector('.js-preview-loading');
|
|
|
|
const previewIdle = document.querySelector('.js-preview-idle');
|
|
|
|
const previewContent = document.querySelector('.js-preview-content');
|
2021-09-13 01:19:00 +02:00
|
|
|
const previewAnon = document.querySelector('.js-preview-anonymous') || false;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2021-09-13 22:26:01 +02:00
|
|
|
if (!textarea || !previewContent) {
|
2019-10-05 02:09:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
const getCacheKey = () => {
|
|
|
|
return (previewAnon && previewAnon.checked ? 'anon;' : '') + textarea.value;
|
2021-10-09 21:58:07 +02:00
|
|
|
};
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
const previewedTextAttribute = 'data-previewed-text';
|
|
|
|
const updatePreview = () => {
|
2021-10-09 21:58:07 +02:00
|
|
|
const cachedValue = getCacheKey();
|
2021-09-29 20:00:46 +02:00
|
|
|
if (previewContent.getAttribute(previewedTextAttribute) === cachedValue) return;
|
|
|
|
previewContent.setAttribute(previewedTextAttribute, cachedValue);
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
getPreview(textarea.value, previewAnon && previewAnon.checked, previewLoading, previewIdle, previewContent);
|
|
|
|
};
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
previewButton.addEventListener('click', updatePreview);
|
2021-09-13 01:19:00 +02:00
|
|
|
textarea.addEventListener('change', resizeTextarea);
|
|
|
|
textarea.addEventListener('keyup', resizeTextarea);
|
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
// Fire handler for automatic resizing if textarea contains text on page load (e.g. editing)
|
|
|
|
if (textarea.value) textarea.dispatchEvent(new Event('change'));
|
|
|
|
|
|
|
|
previewAnon && previewAnon.addEventListener('click', () => {
|
|
|
|
if (previewContent.classList.contains('hidden')) return;
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
updatePreview();
|
|
|
|
});
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
document.addEventListener('click', event => {
|
2021-09-13 22:26:01 +02:00
|
|
|
if (event.target && event.target.closest('.post-reply')) {
|
2019-10-05 02:09:52 +02:00
|
|
|
const link = event.target.closest('.post-reply');
|
|
|
|
commentReply(link.dataset.author, link.getAttribute('href'), textarea, link.dataset.post);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-13 01:19:00 +02:00
|
|
|
export { setupPreviews };
|