This commit is contained in:
Stefan Hardegger
2025-07-24 16:25:23 +02:00
parent a38812877a
commit 12a8f2ee27
3 changed files with 111 additions and 38 deletions

View File

@@ -33,18 +33,6 @@ export default function RichTextEditor({
});
}, []);
const handleVisualChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const plainText = e.target.value;
// Convert plain text to basic HTML paragraphs
const htmlContent = plainText
.split('\n\n')
.filter(paragraph => paragraph.trim())
.map(paragraph => `<p>${paragraph.replace(/\n/g, '<br>')}</p>`)
.join('\n');
onChange(htmlContent);
setHtmlValue(htmlContent);
};
const handleVisualContentChange = () => {
const div = visualDivRef.current;
@@ -198,16 +186,20 @@ export default function RichTextEditor({
// Split plain text into paragraphs and insert as HTML
const paragraphs = plainText.split('\n\n').filter(p => p.trim());
const fragment = document.createDocumentFragment();
paragraphs.forEach((paragraph, index) => {
if (index > 0) {
// Add some spacing between paragraphs
range.insertNode(document.createElement('br'));
fragment.appendChild(document.createElement('br'));
}
const p = document.createElement('p');
p.textContent = paragraph.replace(/\n/g, ' ');
range.insertNode(p);
fragment.appendChild(p);
});
range.insertNode(fragment);
range.collapse(false);
selection.removeAllRanges();
selection.addRange(range);