Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream' into feat/inputview
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoolwinter committed Oct 22, 2023
2 parents af81a47 + 6a04ca7 commit 4d7dbc5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
20 changes: 12 additions & 8 deletions Sources/CodeEditInputView/Utils/CEUndoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ import TextStory
/// - Grouping pasted text
///
/// If needed, the automatic undo grouping can be overridden using the `beginGrouping()` and `endGrouping()` methods.
class CEUndoManager {
public class CEUndoManager {
/// An `UndoManager` subclass that forwards relevant actions to a `CEUndoManager`.
/// Allows for objects like `TextView` to use the `UndoManager` API
/// while CETV manages the undo/redo actions.
class DelegatedUndoManager: UndoManager {
public class DelegatedUndoManager: UndoManager {
weak var parent: CEUndoManager?

override var canUndo: Bool { parent?.canUndo ?? false }
override var canRedo: Bool { parent?.canRedo ?? false }
public override var canUndo: Bool { parent?.canUndo ?? false }
public override var canRedo: Bool { parent?.canRedo ?? false }

func registerMutation(_ mutation: TextMutation) {
public func registerMutation(_ mutation: TextMutation) {
parent?.registerMutation(mutation)
removeAllActions()
}

override func undo() {
public override func undo() {
parent?.undo()
}

override func redo() {
public override func redo() {
parent?.redo()
}

override func registerUndo(withTarget target: Any, selector: Selector, object anObject: Any?) {
public override func registerUndo(withTarget target: Any, selector: Selector, object anObject: Any?) {
// no-op, but just in case to save resources:
removeAllActions()
}
Expand Down Expand Up @@ -87,7 +87,9 @@ class CEUndoManager {
}
isUndoing = true
for mutation in item.mutations.reversed() {
NotificationCenter.default.post(name: .NSUndoManagerWillUndoChange, object: self.manager)
textView.insertText(mutation.inverse.string, replacementRange: mutation.inverse.range)
NotificationCenter.default.post(name: .NSUndoManagerDidUndoChange, object: self.manager)
}
redoStack.append(item)
isUndoing = false
Expand All @@ -100,7 +102,9 @@ class CEUndoManager {
}
isRedoing = true
for mutation in item.mutations {
NotificationCenter.default.post(name: .NSUndoManagerWillRedoChange, object: self.manager)
textView.insertText(mutation.mutation.string, replacementRange: mutation.mutation.range)
NotificationCenter.default.post(name: .NSUndoManagerDidRedoChange, object: self.manager)
}
undoStack.append(item)
isRedoing = false
Expand Down
9 changes: 7 additions & 2 deletions Sources/CodeEditTextView/CodeEditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
/// character's width between characters, etc. Defaults to `1.0`
/// - bracketPairHighlight: The type of highlight to use to highlight bracket pairs.
/// See `BracketPairHighlight` for more information. Defaults to `nil`
/// - undoManager: The undo manager for the text view. Defaults to `nil`, which will create a new CEUndoManager
public init(
_ text: Binding<String>,
language: CodeLanguage,
Expand All @@ -50,7 +51,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
contentInsets: NSEdgeInsets? = nil,
isEditable: Bool = true,
letterSpacing: Double = 1.0,
bracketPairHighlight: BracketPairHighlight? = nil
bracketPairHighlight: BracketPairHighlight? = nil,
undoManager: CEUndoManager? = nil
) {
self._text = text
self.language = language
Expand All @@ -68,6 +70,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
self.isEditable = isEditable
self.letterSpacing = letterSpacing
self.bracketPairHighlight = bracketPairHighlight
self.undoManager = undoManager ?? CEUndoManager()
}

@Binding private var text: String
Expand All @@ -86,6 +89,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
private var isEditable: Bool
private var letterSpacing: Double
private var bracketPairHighlight: BracketPairHighlight?
private var undoManager: CEUndoManager

public typealias NSViewControllerType = TextViewController

Expand All @@ -106,7 +110,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
contentInsets: contentInsets,
isEditable: isEditable,
letterSpacing: letterSpacing,
bracketPairHighlight: bracketPairHighlight
bracketPairHighlight: bracketPairHighlight,
undoManager: undoManager
)
}

Expand Down

0 comments on commit 4d7dbc5

Please sign in to comment.