fix: insert heading-start newlines before hidden prefixes

This commit is contained in:
Valdemar Lorenzen
2026-10-21 15:30:16 +02:00
parent b5a0edd5bf
commit 489fa84b3c
4 changed files with 42 additions and 4 deletions
+8 -2
View File
@@ -248,11 +248,17 @@ final class NotesTextView: NSTextView, @MainActor NSLayoutManagerDelegate {
typedInput: insertString is String)
}
private func replaceSource(_ replacement: NSAttributedString, range: NSRange, semantic: Bool, typedInput: Bool = false) {
private func replaceSource(_ replacement: NSAttributedString, range originalRange: NSRange, semantic: Bool, typedInput: Bool = false) {
guard isEditable else { return }
let source = NSMutableAttributedString(attributedString: Self.serialize(attributedString(), keepingMetadata: true))
var range = originalRange
guard range.location <= source.length, NSMaxRange(range) <= source.length else { return }
let oldHeading = Self.heading(in: source.string as NSString, at: range.location)
// Return at the visible paragraph start keeps the heading prefix with its body.
if replacement.string == "\n", range.length == 0, let heading = oldHeading,
range.location == NSMaxRange(heading) {
range.location = heading.location
}
var caretPosition = range.location + replacement.length
var existingWhitespace = false
if typedInput, range.length == 0, replacement.length > 0 {
@@ -322,7 +328,7 @@ final class NotesTextView: NSTextView, @MainActor NSLayoutManagerDelegate {
}
let removed = NSRange(location: prefix, length: old.length - prefix - suffix)
let inserted = NSRange(location: prefix, length: new.length - prefix - suffix)
let structural = semantic || caretPosition != range.location + replacement.length
let structural = semantic || caretPosition != originalRange.location + replacement.length
if removed.length > 0 || inserted.length > 0 {
if structural { breakUndoCoalescing() }
super.insertText(rendered.attributedSubstring(from: inserted), replacementRange: removed)