Skip to content

Commit

Permalink
Fix styles are added and removed on selecting text
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-divyesh-v committed Nov 29, 2024
1 parent 83c5e2a commit fa396f6
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 17 deletions.
29 changes: 29 additions & 0 deletions RichEditorDemo/RichEditorDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ struct ContentView: View {
} message: {
Text("Please enter file name")
}
.focusedValue(\.richEditorState, state)
.toolbarRole(.automatic)
.richTextFormatSheetConfig(.init(colorPickers: colorPickers))
.richTextFormatSidebarConfig(
.init(
colorPickers: colorPickers,
fontPicker: isMac
)
)
.richTextFormatToolbarConfig(.init(colorPickers: []))
}
}

Expand Down Expand Up @@ -142,3 +152,22 @@ struct ContentView: View {
}
}
}

private extension ContentView {

var isMac: Bool {
#if os(macOS)
true
#else
false
#endif
}

var colorPickers: [RichTextColor] {
[.foreground, .background]
}

var formatToolbarEdge: VerticalEdge {
isMac ? .top : .bottom
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ extension RichTextCoordinator {
textView.highlightingStyle = style
case .setStyle(let style, let newValue):
setStyle(style, to: newValue)
return
case .stepFontSize(let points):
textView.stepRichTextFontSize(points: points)
syncContextWithTextView()
return
case .stepIndent(_):
// textView.stepRichTextIndent(points: points)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ open class RichTextCoordinator: NSObject {
- Parameters:
- text: The rich text to edit.
- textView: The rich text view to keep in sync.
- richTextContext: The context to keep in sync.
- richEditorState: The context to keep in sync.
*/
public init(
text: Binding<NSAttributedString>,
Expand Down
10 changes: 2 additions & 8 deletions Sources/RichEditorSwiftUI/Format/RichTextFormat+Toolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ private extension RichTextFormat.Toolbar {
var controlsContent: some View {
HStack {
#if macOS
headerPicker(value: $context.headerType)
.onChangeBackPort(of: context.headerType) { newValue in
context.updateStyle(style: newValue.getTextSpanStyle())
}
headerPicker(context: context)
fontPicker(value: $context.fontName)
.onChangeBackPort(of: context.fontName) { newValue in
context.updateStyle(style: .font(newValue))
Expand All @@ -134,10 +131,7 @@ private extension RichTextFormat.Toolbar {
}
HStack {
#if !macOS
headerPicker(value: $context.headerType)
.onChangeBackPort(of: context.headerType) { newValue in
context.updateStyle(style: newValue.getTextSpanStyle())
}
headerPicker(context: context)
#endif
alignmentPicker(value: $context.textAlignment)
// superscriptButtons(for: context, greedy: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ extension RichTextFormatToolbarBase {

@ViewBuilder
func headerPicker(
value: Binding<HeaderType>
context: RichEditorState
) -> some View {
if !config.headers.isEmpty {
RichTextHeader.Picker(
selection: value,
context: context,
values: config.headers
)
.pickerStyle(.segmented)
}
}

Expand Down
5 changes: 2 additions & 3 deletions Sources/RichEditorSwiftUI/Headers/RichTextHeader+Picker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public extension RichTextHeader {
- selection: The selected font size.
*/
public init(
selection: Binding<HeaderType>,
context: RichEditorState,
values: [HeaderType]
) {
self._selection = selection
self._selection = context.headerBinding()
self.values = values
}

Expand All @@ -54,7 +54,6 @@ public extension RichTextHeader {
.tag($0)
}
}
.pickerStyle(.automatic)
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions Sources/RichEditorSwiftUI/UI/Context/RichEditorState+Header.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// RichEditorState+Header.swift
// RichEditorSwiftUI
//
// Created by Divyesh Vekariya on 29/11/24.
//

import SwiftUI

public extension RichEditorState {

/// Get a binding for a certain style.
func headerBinding() -> Binding<HeaderType> {
Binding(
get: { self.currentHeader() },
set: { self.setHeaderStyle($0) }
)
}

/// Check whether or not the context has a certain header style.
func currentHeader() -> HeaderType {
return headerType
}

/// Set whether or not the context has a certain header style.
func setHeaderStyle(
_ header: HeaderType
) {
guard header != headerType else { return }
updateStyle(style: header.getTextSpanStyle())
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// RichEditorStateFocusedValueKey.swift
// RichEditorSwiftUI
//
// Created by Divyesh Vekariya on 25/11/24.
//

import SwiftUI

public extension RichEditorState {

/// This key can be used to keep track of a context in a
/// multi-windowed app.
struct FocusedValueKey: SwiftUI.FocusedValueKey {

public typealias Value = RichEditorState
}
}

public extension FocusedValues {

/// This value can be used to keep track of a context in
/// a multi-windowed app.
///
/// You can bind a context to a view with `focusedValue`:
///
/// ```swift
/// RichTextEditor(...)
/// .focusedValue(\.richTextContext, richTextContext)
/// ```
///
/// You can then access the context as a `@FocusedValue`:
///
/// ```swift
/// @FocusedValue(\.richEditorState)
/// var richEditorState: RichEditorState?
/// ```
var richEditorState: RichEditorState.FocusedValueKey.Value? {
get { self[RichEditorState.FocusedValueKey.self] }
set { self[RichEditorState.FocusedValueKey.self] = newValue }
}
}

0 comments on commit fa396f6

Please sign in to comment.