fix: cannot add images above text (#1659)

* fix: cannot add images above text

* make sure there are always 2 newlines on either side of media

---------

Co-authored-by: k00b <k00b@stacker.news>
This commit is contained in:
soxa 2024-11-27 22:31:46 +01:00 committed by GitHub
parent e05989d371
commit 707d7bdf8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 4 deletions

View File

@ -360,12 +360,22 @@ export function MarkdownInput ({ label, topLevel, groupClassName, onChange, onKe
onUpload={file => { onUpload={file => {
const uploadMarker = `![Uploading ${file.name}…]()` const uploadMarker = `![Uploading ${file.name}…]()`
const text = innerRef.current.value const text = innerRef.current.value
const cursorPosition = innerRef.current.selectionStart || text.length const cursorPosition = innerRef.current.selectionStart
let preMarker = text.slice(0, cursorPosition) let preMarker = text.slice(0, cursorPosition)
const postMarker = text.slice(cursorPosition) let postMarker = text.slice(cursorPosition)
// when uploading multiple files at once, we want to make sure the upload markers are separated by blank lines // when uploading multiple files at once, we want to make sure the upload markers are separated by blank lines
if (preMarker && !/\n+\s*$/.test(preMarker)) { if (preMarker) {
preMarker += '\n\n' // Count existing newlines at the end of preMarker
const existingNewlines = preMarker.match(/[\n]+$/)?.[0].length || 0
// Add only the needed newlines to reach 2
preMarker += '\n'.repeat(Math.max(0, 2 - existingNewlines))
}
// if there's text after the cursor, we want to make sure the upload marker is separated by a blank line
if (postMarker) {
// Count existing newlines at the start of postMarker
const existingNewlines = postMarker.match(/^[\n]*/)?.[0].length || 0
// Add only the needed newlines to reach 2
postMarker = '\n'.repeat(Math.max(0, 2 - existingNewlines)) + postMarker
} }
const newText = preMarker + uploadMarker + postMarker const newText = preMarker + uploadMarker + postMarker
helpers.setValue(newText) helpers.setValue(newText)