Skip to content

Commit 7391c5e

Browse files
Fix attributes after paste
1 parent 38d0e2c commit 7391c5e

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

Sources/RichTextKit/Attributes/RichTextAttributeWriter+Font.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private extension RichTextAttributeWriter {
9393
/// We must adjust empty font names on some platforms.
9494
func settableFontName(for fontName: String) -> String {
9595
#if macOS
96-
fontName.isEmpty ? "Helvetica" : fontName
96+
fontName
9797
#else
9898
fontName
9999
#endif

Sources/RichTextKit/Component/RichTextViewComponent+Font.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public extension RichTextViewComponent {
1313

1414
/// Get the current font.
1515
var currentFont: FontRepresentable? {
16-
currentRichTextAttributes[.font] as? FontRepresentable
16+
currentRichTextAttributes[.font] as? FontRepresentable ?? typingAttributes[.font] as? FontRepresentable
1717
}
1818

1919
/// Get the current font size.

Sources/RichTextKit/Component/RichTextViewComponent+Pasting.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,10 @@ public extension RichTextViewComponent {
7070
let isSelectedRange = (index == selectedRange.location)
7171
if isSelectedRange { deleteCharacters(in: selectedRange) }
7272
if move { moveInputCursor(to: index) }
73-
let fontSize = currentFontSize
7473
images.reversed().forEach { performPasteImage($0, at: index) }
7574
if move { moveInputCursor(to: safeInsertRange.location + items) }
76-
if move || isSelectedRange, let fontSize {
75+
if move || isSelectedRange {
7776
DispatchQueue.main.async {
78-
self.setRichTextFontSize(fontSize)
7977
self.moveInputCursor(to: self.selectedRange.location)
8078
}
8179
}
@@ -135,10 +133,14 @@ private extension RichTextViewComponent {
135133
_ image: ImageRepresentable,
136134
at index: Int
137135
) {
136+
let newLine = NSAttributedString(string: "\n", attributes: currentRichTextAttributes)
138137
let content = NSMutableAttributedString(attributedString: richText)
139138
guard let insertString = getAttachmentString(for: image) else { return }
140-
content.insert(NSAttributedString(string: "\n"), at: index)
139+
140+
insertString.insert(newLine, at: insertString.length)
141+
insertString.addAttributes(currentRichTextAttributes, range: insertString.richTextRange)
141142
content.insert(insertString, at: index)
143+
142144
setRichText(content)
143145
}
144146
}

Sources/RichTextKit/Coordinator/RichTextCoordinator.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ extension RichTextCoordinator {
241241
richTextContext.canUndoLatestChange = canUndo
242242
}
243243

244-
let fontName = textView.currentFontName ?? ""
245-
if richTextContext.fontName != fontName {
244+
if let fontName = textView.currentFontName,
245+
!fontName.isEmpty,
246+
richTextContext.fontName != fontName {
246247
richTextContext.fontName = fontName
247248
}
248249

Sources/RichTextKit/Fonts/StandardFontSizeProvider.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ public extension StandardFontSizeProvider {
4242
*/
4343
static var standardRichTextFontSize: CGFloat {
4444
get { StandardFontSizeProviderStorage.standardRichTextFontSize }
45-
set { StandardFontSizeProviderStorage.standardRichTextFontSize = newValue }
4645
}
4746
}
4847

4948
private class StandardFontSizeProviderStorage {
5049

51-
static var standardRichTextFontSize: CGFloat = 16
50+
static let standardRichTextFontSize: CGFloat = 16
5251
}

0 commit comments

Comments
 (0)