Skip to content

Commit

Permalink
Added ability to override UndoManager (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep authored Jun 28, 2024
1 parent b9f81c9 commit 139b59d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
13 changes: 12 additions & 1 deletion Proton/Sources/Swift/Core/RichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class RichTextView: AutogrowingTextView {

/// Equivalent, strongly-typed alternative to `textStorage`
private let richTextStorage = PRTextStorage()
private let _undoManager: UndoManager?
static let defaultListLineFormatting = LineFormatting(indentation: 25, spacingBefore: 0)

weak var richTextViewDelegate: RichTextViewDelegate?
Expand Down Expand Up @@ -326,12 +327,18 @@ class RichTextView: AutogrowingTextView {
delegate as? RichTextViewContext
}

init(frame: CGRect = .zero, context: RichTextViewContext, allowAutogrowing: Bool = false) {
init(
frame: CGRect = .zero,
context: RichTextViewContext,
allowAutogrowing: Bool = false,
undoManager: UndoManager? = nil) {
let textContainer = TextContainer()
let layoutManager = LayoutManager()

layoutManager.addTextContainer(textContainer)
richTextStorage.addLayoutManager(layoutManager)

_undoManager = undoManager
super.init(frame: frame, textContainer: textContainer, allowAutogrowing: allowAutogrowing)
delegateOverrides = [GestureRecognizerDelegateOverride]()
layoutManager.delegate = self
Expand All @@ -347,6 +354,10 @@ class RichTextView: AutogrowingTextView {
// self.typingAttributes = defaultTypingAttributes
}

override var undoManager: UndoManager? {
_undoManager ?? super.undoManager
}

var contentLength: Int {
return textStorage.length
}
Expand Down
30 changes: 26 additions & 4 deletions Proton/Sources/Swift/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,31 @@ open class EditorView: UIView {
/// Low-tech lock mechanism to know when `attributedText` is being set
private var isSettingAttributedText = false


// Making this a convenience init fails the test `testRendersWidthRangeAttachment` as the init of a class subclassed from
// `EditorView` is returned as type `EditorView` and not the class itself, causing the test to fail.

/// Initializes the EditorView
/// - Parameters:
/// - frame: Frame to be used for `EditorView`.
/// - context: Optional context to be used. `EditorViewContext` is link between `EditorCommandExecutor` and the `EditorView`.
/// `EditorCommandExecutor` needs to have same context as the `EditorView` to execute a command on it. Unless you need to have
/// restriction around some commands to be restricted in execution on certain specific editors, the default value may be used.
public init(frame: CGRect = .zero, context: EditorViewContext = .shared, allowAutogrowing: Bool = true) {
/// - allowAutogrowing: When set to `true`, editor automatically grows based on content size and constraints applied. e.g. when used in non-fullscreen mode
/// - undoManager: Override `UndoManager`. When nil, default `UndoManager` from underlying `UITextView` is used.
public init(
frame: CGRect = .zero,
context: EditorViewContext = .shared,
allowAutogrowing: Bool = true,
undoManager: UndoManager? = nil) {
self.context = context.richTextViewContext
self.editorViewContext = context
self.richTextView = RichTextView(frame: frame, context: self.context, allowAutogrowing: allowAutogrowing)
self.richTextView = RichTextView(
frame: frame,
context: self.context,
allowAutogrowing: allowAutogrowing,
undoManager: undoManager
)

super.init(frame: frame)

Expand All @@ -255,9 +268,18 @@ open class EditorView: UIView {
setup()
}

init(frame: CGRect, richTextViewContext: RichTextViewContext, allowAutogrowing: Bool = true) {
init(
frame: CGRect,
richTextViewContext: RichTextViewContext,
allowAutogrowing: Bool = true,
undoManager: UndoManager?) {
self.context = richTextViewContext
self.richTextView = RichTextView(frame: frame, context: context, allowAutogrowing: allowAutogrowing)
self.richTextView = RichTextView(
frame: frame,
context: context,
allowAutogrowing: allowAutogrowing,
undoManager: undoManager
)
self.editorViewContext = .null
super.init(frame: frame)

Expand Down

0 comments on commit 139b59d

Please sign in to comment.