Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to override UndoManager #319

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -317,12 +318,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 @@ -338,6 +345,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
Loading