Skip to content

Commit

Permalink
Restore missing undo support
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Dec 16, 2024
1 parent 2838522 commit aab054d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Sources/IBeam/MockTextSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ public final class MockTextSystem : TextSystem {
}

public func applyMutation(_ range: TextRange, string: AttributedString) -> MutationOutput<TextRange>? {
partialSystem.applyMutation(range, string: string)
partialSystem.applyMutation(in: range, string: string, undoManager: nil)
}
}
24 changes: 23 additions & 1 deletion Sources/IBeam/MutableStringPartialSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,32 @@ extension MutableStringPartialSystem {
content.endEditing()
}

public func applyMutation(_ range: NSRange, string: AttributedString) -> MutationOutput<NSRange>? {
// public func applyMutation(_ range: NSRange, string: AttributedString) -> MutationOutput<NSRange>? {
// 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..<position)
//
// return MutationOutput<NSRange>(selection: newSelection, delta: delta)
// }

public func applyMutation(in range: NSRange, string: AttributedString, undoManager: UndoManager?) -> MutationOutput<NSRange> {
let nsAttrString = NSAttributedString(string)
let length = nsAttrString.length

let existingString = AttributedString(content.attributedSubstring(from: range))

undoManager?.registerMainActorUndo(withTarget: content, handler: { target in
let existingNSAttrString = NSAttributedString(existingString)

target.replaceCharacters(in: range, with: existingNSAttrString)
})

content.replaceCharacters(in: range, with: nsAttrString)

let delta = length - range.length
Expand Down
136 changes: 0 additions & 136 deletions Sources/IBeam/TextStorage.swift

This file was deleted.

0 comments on commit aab054d

Please sign in to comment.