Skip to content

Commit

Permalink
Make coordinator properly handle indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 14, 2024
1 parent 3c0a29e commit fb1f6f9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,12 @@ import AppKit
public extension RichTextAttributeWriter {

/// Set the rich text alignment at a certain range.
///
/// > Todo: Something's currently off with alignment. It
/// spils over to other paragraphs when moving the input
/// cursor and inserting new text.
func setRichTextAlignment(
_ alignment: RichTextAlignment,
at range: NSRange
) {
let text = richText.string
let length = range.length
let location = range.location
let ulocation = UInt(location)
let uindex = text.findIndexOfCurrentParagraph(from: ulocation)
let index = Int(uindex)
let diff = location - index
let newLength = max(length + diff, 1)
let newRange = NSRange(location: index, length: newLength)
let alignment = alignment.nativeAlignment
let safeRange = safeRange(for: newRange)
#if os(macOS)
mutableRichText?.setAlignment(alignment, range: safeRange)
#else
let paragraph = richTextParagraphStyle(at: safeRange) ?? .init()
paragraph.alignment = alignment
setRichTextAttribute(
.paragraphStyle,
to: paragraph,
at: safeRange
)
#endif
let paragraph = richTextParagraphStyle(at: range) ?? .init()
paragraph.alignment = alignment.nativeAlignment
setRichTextParagraphStyle(to: paragraph, at: range)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// RichTextAttributeWriter+Paragraph.swift
// RichTextKit
//
// Created by Daniel Saidi on 2024-02-14.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

import Foundation

#if canImport(UIKit)
import UIKit
#endif

#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import AppKit
#endif

public extension RichTextAttributeWriter {

/// Set the rich text paragraph style at a certain range.
func setRichTextParagraphStyle(
to style: NSParagraphStyle,
at range: NSRange
) {
let text = richText.string
let length = range.length
let location = range.location
let ulocation = UInt(location)
let uindex = text.findIndexOfCurrentParagraph(from: ulocation)
let index = Int(uindex)
let diff = location - index
let newLength = max(length + diff, 1)
let newRange = NSRange(location: index, length: newLength)
let safeRange = safeRange(for: newRange)
setRichTextAttribute(.paragraphStyle, to: style, at: safeRange)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public extension RichTextCommand {
private var context: RichTextContext?

public var body: some View {
SwiftUI.Button(action.menuTitle) {
Button(action.menuTitle) {
context?.handle(action)
}
.disabled(!canHandle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public extension RichTextViewComponent {

/// Set the rich text alignment at current range.
///
/// This function does not use ``RichTextAttributeWriter``
/// since the text views require affecting text storage.
///
/// > Important: This function will affect the next line
/// of text if we grab `richTextParagraphStyle` and make
/// the alignment change to it, instead of creating this
Expand Down
2 changes: 1 addition & 1 deletion Sources/RichTextKit/Context/RichTextContext+Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public extension RichTextContext {
case .setAlignment(let align): textAlignment = align
case .stepFontSize(let points): fontSize += CGFloat(points)
case .toggleStyle(let style): toggle(style)
default: break
default: userActionPublisher.send(action)
}
}

Expand Down
15 changes: 10 additions & 5 deletions Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
public var body: some View {
VStack(spacing: 0) {
HStack(spacing: style.itemSpacing) {
leadingViews
Spacer()
trailingViews
content
}
.padding(10)
}
Expand Down Expand Up @@ -174,11 +172,18 @@ private extension RichTextKeyboardToolbar {
var isCompact: Bool { horizontalSizeClass == .compact }
}


private extension RichTextKeyboardToolbar {

@ViewBuilder
var content: some View {
leadingViews
Spacer()
trailingViews
}

var divider: some View {
Divider().frame(height: 25)
Divider()
.frame(height: 25)
}

@ViewBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ public extension RichTextAttributeWriter {
let increase = indent == .increase
let change: CGFloat = 30.0
let points = increase ? change : -change
return stepRichTextIndent(
points: points,
at: range
)
stepRichTextIndent(points: points, at: range)
return richTextAttributes(at: range)
}

@available(*, deprecated, renamed: "stepRichTextFontSize(points:at:)")
Expand Down

0 comments on commit fb1f6f9

Please sign in to comment.