Skip to content

Commit

Permalink
Fixed attachments rendering with TextContainerInsets
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed Jun 14, 2023
1 parent fea4746 commit a69e124
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Proton/Sources/Swift/Attachment/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,13 @@ open class Attachment: NSTextAttachment, BoundsObserving {
// incorrect width if bounds are calculated directly on `attributedText` instead of using `textView`.
let lineFragmentPadding = containerEditorView.richTextView.textContainer.lineFragmentPadding
// Account for text leading and trailing margins within the textContainer
let horizontalTextInsets = (containerEditorView.textContainerInset.left + containerEditorView.textContainerInset.right)
let adjustedContainerSize = CGSize(
width: containerEditorView.bounds.size.width - (lineFragmentPadding * 2) - indent,
width: containerEditorView.bounds.size.width - (lineFragmentPadding * 2) - indent - horizontalTextInsets,
height: containerEditorView.bounds.size.height
)
let adjustedLineFrag = CGRect(
x: lineFrag.origin.x,
x: lineFrag.origin.x + containerEditorView.textContainerInset.left,
y: lineFrag.origin.y,
width: min(lineFrag.size.width, adjustedContainerSize.width),
height: lineFrag.height
Expand Down
1 change: 1 addition & 0 deletions Proton/Sources/Swift/Core/RichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ class RichTextView: AutogrowingTextView {
placeholderLabel.numberOfLines = 0
placeholderLabel.lineBreakMode = .byTruncatingTail

placeholderLabel.removeFromSuperview()
addSubview(placeholderLabel)
placeholderLabel.attributedText = placeholderText
NSLayoutConstraint.activate([
Expand Down
5 changes: 4 additions & 1 deletion Proton/Sources/Swift/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1197,14 +1197,17 @@ extension EditorView {
size = contentSize
}

frame = CGRect(origin: frame.origin, size: size)
var adjustedOrigin = frame.origin
adjustedOrigin.x += textContainerInset.left
frame = CGRect(origin: adjustedOrigin, size: size)

if attachment.isRendered == false {
attachment.render(in: self)
if !isSettingAttributedText, let focusable = attachment.contentView as? Focusable {
focusable.setFocus()
}
}

attachment.frame = frame
}
}
Expand Down
27 changes: 27 additions & 0 deletions Proton/Tests/Editor/EditorSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,33 @@ class EditorSnapshotTests: SnapshotTestCase {
assertSnapshot(matching: viewController.view, as: .image, record: recordMode)
}

func testRendersAttachmentWithTextContainerInset() {
let viewController = EditorTestViewController()
let editor = viewController.editor
let offsetProvider = MockAttachmentOffsetProvider()
offsetProvider.offset = CGPoint(x: 0, y: -4)

editor.font = UIFont.systemFont(ofSize: 12)
editor.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
var panel = PanelView()
panel.backgroundColor = .cyan
panel.layer.borderWidth = 1.0
panel.layer.cornerRadius = 4.0
panel.layer.borderColor = UIColor.black.cgColor

let attachment = Attachment(panel, size: .fullWidth)
panel.boundsObserver = attachment
panel.editor.font = editor.font

panel.attributedText = NSAttributedString(string: "In full-width attachment")

editor.replaceCharacters(in: .zero, with: "This text is in Editor")
editor.insertAttachment(in: editor.textEndRange, attachment: attachment)

viewController.render()
assertSnapshot(matching: viewController.view, as: .image, record: recordMode)
}

func testDeletesAttachments() {
let viewController = EditorTestViewController()
let editor = viewController.editor
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a69e124

Please sign in to comment.