diff --git a/Sources/IBeam/MutableStringPartialSystem.swift b/Sources/IBeam/MutableStringPartialSystem.swift index c083bcf..ef49e87 100644 --- a/Sources/IBeam/MutableStringPartialSystem.swift +++ b/Sources/IBeam/MutableStringPartialSystem.swift @@ -82,30 +82,17 @@ extension MutableStringPartialSystem { content.endEditing() } -// public func applyMutation(_ range: NSRange, string: AttributedString) -> MutationOutput? { -// let nsAttrString = NSAttributedString(string) -// let length = nsAttrString.length -// -// content.replaceCharacters(in: range, with: nsAttrString) -// -// let delta = length - range.length -// let position = min(range.lowerBound + length, content.length) -// -// let newSelection = NSRange(position..(selection: newSelection, delta: delta) -// } - public func applyMutation(in range: NSRange, string: AttributedString, undoManager: UndoManager?) -> MutationOutput { let nsAttrString = NSAttributedString(string) let length = nsAttrString.length let existingString = AttributedString(content.attributedSubstring(from: range)) + let inverseRange = NSRange(location: range.location, length: length) undoManager?.registerMainActorUndo(withTarget: content, handler: { target in let existingNSAttrString = NSAttributedString(existingString) - target.replaceCharacters(in: range, with: existingNSAttrString) + target.replaceCharacters(in: inverseRange, with: existingNSAttrString) }) content.replaceCharacters(in: range, with: nsAttrString)