danielsaidi / RichTextKit

RichTextKit is a Swift SDK that helps you use rich text in Swift and SwiftUI.
MIT License
944 stars 126 forks source link

Highlighting Style Not Applied in RichTextEditor #195

Open alelordelo opened 2 months ago

alelordelo commented 2 months ago

Description

I'm encountering an issue where the RichTextHighlightingStyle is not being applied correctly in the RichTextEditor. Despite setting a custom highlighting style, the text remains unhighlighted and the foreground color is not changing as expected.

Steps to Reproduce

  1. Create a RichTextHighlightingStyle with a custom foreground color (e.g., red).
  2. Pass this style to the RichTextEditor in a SwiftUI view.
  3. Attempt to highlight text in the editor.

Expected Behavior

The highlighted text should appear with the custom foreground color (red in this case).

Actual Behavior

The foreground color does not change.

Code Sample

let customHighlightStyle = RichTextHighlightingStyle(
    backgroundColor: .yellow.opacity(0.3),
    foregroundColor: .red
)

RichTextEditor(
    text: $cell.richText,
    context: cell.text,
    config: editorConfig,
    highlightingStyle: customHighlightStyle,
    placeholder: cell.textStyle.asAttributedString(),
    font: NSFont.systemFont(ofSize: cell.textStyle.size)
) { _ in
    cell.text.setAttributedString(to: cell.richText)
}

Additional Context

I've verified that the customHighlightStyle is being created correctly and passed to the RichTextEditor. However, it seems that either the style is not being applied to the underlying NSTextView, or there might be an issue with how the highlighting is implemented.

Possible Related Code

In the RichTextView class, there's a highlightingStyle property and a setupHighlightStyle() method:

class RichTextView: NSTextView, RichTextViewComponent {
    var highlightingStyle: RichTextHighlightingStyle = .standard

    func setupHighlightStyle() {
        self.selectedTextAttributes = [
            .backgroundColor: NSColor(highlightingStyle.backgroundColor),
            .foregroundColor: NSColor(highlightingStyle.foregroundColor)
        ]
    }
    // ...
}

Question

Is there any additional setup required to ensure the highlighting style is applied correctly? Are there any known issues or limitations with custom highlighting styles in the current version of the package?

Any assistance or insights would be greatly appreciated.

danielsaidi commented 1 month ago

Hi @alelordelo

Thank you for the very detailed bug report.

I'm very busy with other projects at the moment, but am planning to revisit this project later. Until then, I'm happy to look at any PR or code that could help fix this.