Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichTextAction subscription #129

Merged
merged 20 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions Sources/RichTextKit/Actions/RichTextAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public enum RichTextAction: Identifiable, Equatable, RichTextLabelValue {

/// Copy the currently selected text, if any.
case copy

/// Dismiss any presented software keyboard.
case dismissKeyboard

Expand Down Expand Up @@ -48,19 +47,7 @@ public enum RichTextAction: Identifiable, Equatable, RichTextLabelValue {
case undoLatestChange

// Change background color
case setBackgroundColor(ColorRepresentable)

// Change foreground color
case setForegroundColor(ColorRepresentable)

// Change underline color
case setUnderlineColor(ColorRepresentable)

// Change strikethrough color
case setStrikethroughColor(ColorRepresentable)

// Change stroke color
case setStrokeColor(ColorRepresentable)
case setColor(ColorRepresentable, RichTextColor)

// Highlighted renge
case setHighlightedRange(NSRange?)
Expand Down Expand Up @@ -101,11 +88,7 @@ public extension RichTextAction {
case .stepSuperscript(let val): .richTextStepSuperscript(val)
case .toggleStyle(let val): val.icon
case .undoLatestChange: .richTextActionUndo
case .setBackgroundColor: .richTextColorBackground
case .setForegroundColor: .richTextColorForeground
case .setUnderlineColor: .richTextColorUnderline
case .setStrikethroughColor: .richTextColorStrikethrough
case .setStrokeColor: .richTextColorStroke
case .setColor(_, let richTextColor): richTextColor.icon ?? .richTextColorBackground
case .setHighlightedRange: .richTextAlignmentCenter
case .setHighlightingStyle: .richTextAlignmentCenter
case .pasteImage: .richTextDocuments
Expand Down Expand Up @@ -149,11 +132,15 @@ public extension RichTextAction {
case .stepSuperscript(let steps): .actionStepSuperscript(steps)
case .toggleStyle(let style): style.titleKey
case .undoLatestChange: .actionUndoLatestChange
case .setBackgroundColor: .backgroundColor
case .setForegroundColor: .foregroundColor
case .setUnderlineColor: .underlineColor
case .setStrikethroughColor: .strikethroughColor
case .setStrokeColor: .strokeColor
case .setColor(_, let richTextColor):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DominikBucher12 I thiiiiink the rich text color has a title that you can return right away.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually does not :( I wouldn't implement it in there IMHO, but the icons can stay.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix! :)

switch richTextColor {
case .foreground: .foregroundColor
case .background: .backgroundColor
case .strikethrough: .strikethroughColor
case .stroke: .strokeColor
case .underline: .underlineColor
case .undefined: .strokeColor
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct for the undefined?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to return something and this feels a lot better than exception or cascade of optionals. I would personally focus after 1.0 to get away from undefined...

}
case .setHighlightedRange: .highlightedRange
case .setHighlightingStyle: .highlightingStyle
case .pasteImage: .pasteImage
Expand Down
6 changes: 1 addition & 5 deletions Sources/RichTextKit/Context/RichTextContext+Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ public extension RichTextContext {
case .stepSuperscript: return false
case .toggleStyle: return true
case .undoLatestChange: return canUndoLatestChange
case .setBackgroundColor: return true
case .setForegroundColor: return true
case .setUnderlineColor: return true
case .setStrikethroughColor: return true
case .setStrokeColor: return true
case .setColor: return true
case .setHighlightedRange: return true
case .setHighlightingStyle: return true
case .pasteImage: return true
Expand Down
12 changes: 7 additions & 5 deletions Sources/RichTextKit/Context/RichTextContext+Colors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public extension RichTextContext {
for val: RichTextColor
) {
guard self.color(for: val) != color else { return }
userActionPublisher.send(.setColor(color, val))

switch val {
case .foreground: userActionPublisher.send(.setForegroundColor(color)); foregroundColor = color
case .background: userActionPublisher.send(.setBackgroundColor(color)); backgroundColor = color
case .strikethrough: userActionPublisher.send(.setStrikethroughColor(color)); strikethroughColor = color
case .stroke: userActionPublisher.send(.setStrokeColor(color)); strokeColor = color
case .underline: userActionPublisher.send(.setUnderlineColor(color)); underlineColor = color
case .foreground: foregroundColor = color
case .background: backgroundColor = color
case .strikethrough: strikethroughColor = color
case .stroke: strokeColor = color
case .underline: underlineColor = color
case .undefined: return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,13 @@ extension RichTextCoordinator {
if textView.hasSelectedRange {
applyRange = textView.selectedRange
}
switch coloredAttribute {
case .foreground:
setForegroundColor(color, at: applyRange)
case .background:
setBackgroundColor(color, at: applyRange)
case .strikethrough:
setStrikethroughColor(color, at: applyRange)
case .stroke:
setStrokeColor(color, at: applyRange)
case .underline:
setUnderlineColor(color, at: applyRange)
case .undefined:
break
}
}

private func setStrikethroughColor(_ color: ColorRepresentable, at range: NSRange?) {
if let range {
self.textView.setRichTextColor(.strikethrough, to: color, at: range)
} else {
self.textView.setRichTextAttribute(.strikethroughColor, to: color)
}
}

private func setStrokeColor(_ color: ColorRepresentable, at range: NSRange?) {
if let range {
self.textView.setRichTextColor(.stroke, to: color, at: range)
} else {
self.textView.setRichTextAttribute(.strokeColor, to: color)
}
}

private func setForegroundColor(_ color: ColorRepresentable, at range: NSRange?) {
if let range {
self.textView.setRichTextColor(.foreground, to: color, at: range)
} else {
self.textView.setRichTextAttribute(.foregroundColor, to: color)
}
}

private func setBackgroundColor(_ color: ColorRepresentable, at range: NSRange?) {
if let range {
self.textView.setRichTextColor(.background, to: color, at: range)
} else {
self.textView.setRichTextAttribute(.backgroundColor, to: color)
}
}

private func setUnderlineColor(_ color: ColorRepresentable, at range: NSRange?) {
if let range {
self.textView.setRichTextColor(.underline, to: color, at: range)

guard let attribute = coloredAttribute.attribute else { return }

if let applyRange {
self.textView.setRichTextColor(coloredAttribute, to: color, at: applyRange)
} else {
self.textView.setRichTextAttribute(.underlineColor, to: color)
self.textView.setRichTextAttribute(attribute, to: color)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,8 @@ private extension RichTextCoordinator {
case .undoLatestChange:
textView.undoLatestChange()
syncContextWithTextView()
case .setBackgroundColor(let color):
setColor(color, for: .background)
case .setForegroundColor(let color):
setColor(color, for: .foreground)
case .setUnderlineColor(let color):
setColor(color, for: .underline)
case .setStrikethroughColor(let color):
setColor(color, for: .strikethrough)
case .setStrokeColor(let color):
setColor(color, for: .stroke)
case .setColor(let color, let richTextColor):
setColor(color, for: richTextColor)
case .setHighlightedRange(let range):
setHighlightedRange(to: range)
case .setHighlightingStyle(let style):
Expand Down
Loading