Skip to content

Commit

Permalink
Merge branch 'main' into twn
Browse files Browse the repository at this point in the history
  • Loading branch information
tonisevener authored Jan 17, 2024
2 parents c42129a + e02e685 commit f5e3c80
Show file tree
Hide file tree
Showing 26 changed files with 964 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ protocol WKEditorInputViewDelegate: AnyObject {
func didTapDecreaseIndent()
func didTapHeading(type: WKEditorInputView.HeadingButtonType)
func didTapStrikethrough(isSelected: Bool)
func didTapSubscript(isSelected: Bool)
func didTapSuperscript(isSelected: Bool)
func didTapUnderline(isSelected: Bool)
func didTapLink(isSelected: Bool)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class WKEditorToolbarGroupedView: WKEditorToolbarView {
}

strikethroughButton.isSelected = selectionState.isStrikethrough
subscriptButton.isSelected = selectionState.isSubscript
superscriptButton.isSelected = selectionState.isSuperscript
underlineButton.isSelected = selectionState.isUnderline
}

// MARK: - Button Actions
Expand All @@ -106,12 +109,15 @@ class WKEditorToolbarGroupedView: WKEditorToolbarView {
}

@objc private func tappedSuperscript() {
delegate?.didTapSuperscript(isSelected: superscriptButton.isSelected)
}

@objc private func tappedSubscript() {
delegate?.didTapSubscript(isSelected: subscriptButton.isSelected)
}

@objc private func tappedUnderline() {
delegate?.didTapUnderline(isSelected: underlineButton.isSelected)
}

@objc private func tappedStrikethrough() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation
import ComponentsObjC

extension WKSourceEditorFormatterSubscript {
func toggleSubscriptFormatting(action: WKSourceEditorFormatterButtonAction, in textView: UITextView) {
toggleFormatting(startingFormattingString: "<sub>", endingFormattingString: "</sub>", action: action, in: textView)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation
import ComponentsObjC

extension WKSourceEditorFormatterSuperscript {
func toggleSuperscriptFormatting(action: WKSourceEditorFormatterButtonAction, in textView: UITextView) {
toggleFormatting(startingFormattingString: "<sup>", endingFormattingString: "</sup>", action: action, in: textView)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation
import ComponentsObjC

extension WKSourceEditorFormatterUnderline {
func toggleUnderlineFormatting(action: WKSourceEditorFormatterButtonAction, in textView: UITextView) {
toggleFormatting(startingFormattingString: "<u>", endingFormattingString: "</u>", action: action, in: textView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ fileprivate var needsTextKit2: Bool {
let isSubheading3: Bool
let isSubheading4: Bool
let isStrikethrough: Bool
let isUnderline: Bool
let isSubscript: Bool
let isSuperscript: Bool
let isSimpleLink: Bool
let isLinkWithNestedLink: Bool
init(isBold: Bool, isItalics: Bool, isHorizontalTemplate: Bool, isHorizontalReference: Bool, isBulletSingleList: Bool, isBulletMultipleList: Bool, isNumberSingleList: Bool, isNumberMultipleList: Bool, isHeading: Bool, isSubheading1: Bool, isSubheading2: Bool, isSubheading3: Bool, isSubheading4: Bool, isStrikethrough: Bool, isSimpleLink: Bool, isLinkWithNestedLink: Bool) {

init(isBold: Bool, isItalics: Bool, isHorizontalTemplate: Bool, isHorizontalReference: Bool, isBulletSingleList: Bool, isBulletMultipleList: Bool, isNumberSingleList: Bool, isNumberMultipleList: Bool, isHeading: Bool, isSubheading1: Bool, isSubheading2: Bool, isSubheading3: Bool, isSubheading4: Bool, isStrikethrough: Bool, isUnderline: Bool, isSubscript: Bool, isSuperscript: Bool, isSimpleLink: Bool, isLinkWithNestedLink: Bool) {
self.isBold = isBold
self.isItalics = isItalics
self.isHorizontalTemplate = isHorizontalTemplate
Expand All @@ -45,6 +49,9 @@ fileprivate var needsTextKit2: Bool {
self.isSubheading3 = isSubheading3
self.isSubheading4 = isSubheading4
self.isStrikethrough = isStrikethrough
self.isUnderline = isUnderline
self.isSubscript = isSubscript
self.isSuperscript = isSuperscript
self.isSimpleLink = isSimpleLink
self.isLinkWithNestedLink = isLinkWithNestedLink
}
Expand All @@ -65,8 +72,11 @@ final class WKSourceEditorTextFrameworkMediator: NSObject {
private(set) var listFormatter: WKSourceEditorFormatterList?
private(set) var headingFormatter: WKSourceEditorFormatterHeading?
private(set) var strikethroughFormatter: WKSourceEditorFormatterStrikethrough?
private(set) var underlineFormatter: WKSourceEditorFormatterUnderline?
private(set) var subscriptFormatter: WKSourceEditorFormatterSubscript?
private(set) var superscriptFormatter: WKSourceEditorFormatterSuperscript?
private(set) var linkFormatter: WKSourceEditorFormatterLink?

var isSyntaxHighlightingEnabled: Bool = true {
didSet {
updateColorsAndFonts()
Expand Down Expand Up @@ -124,7 +134,7 @@ final class WKSourceEditorTextFrameworkMediator: NSObject {
textKit1Storage?.storageDelegate = self
}
}

// MARK: Internal

func updateColorsAndFonts() {
Expand All @@ -138,20 +148,33 @@ final class WKSourceEditorTextFrameworkMediator: NSObject {
let listFormatter = WKSourceEditorFormatterList(colors: colors, fonts: fonts)
let headingFormatter = WKSourceEditorFormatterHeading(colors: colors, fonts: fonts)
let strikethroughFormatter = WKSourceEditorFormatterStrikethrough(colors: colors, fonts: fonts)
let underlineFormatter = WKSourceEditorFormatterUnderline(colors: colors, fonts: fonts)
let subscriptFormatter = WKSourceEditorFormatterSubscript(colors: colors, fonts: fonts)
let superscriptFormatter = WKSourceEditorFormatterSuperscript(colors: colors, fonts: fonts)
let linkFormatter = WKSourceEditorFormatterLink(colors: colors, fonts: fonts)


self.formatters = [WKSourceEditorFormatterBase(colors: colors, fonts: fonts, textAlignment: viewModel.textAlignment),
templateFormatter,
boldItalicsFormatter,
referenceFormatter,
listFormatter,
headingFormatter,
strikethroughFormatter, linkFormatter]
strikethroughFormatter,
superscriptFormatter,
subscriptFormatter,
underlineFormatter,
linkFormatter]

self.boldItalicsFormatter = boldItalicsFormatter
self.templateFormatter = templateFormatter
self.referenceFormatter = referenceFormatter
self.listFormatter = listFormatter
self.headingFormatter = headingFormatter
self.strikethroughFormatter = strikethroughFormatter
self.subscriptFormatter = subscriptFormatter
self.superscriptFormatter = superscriptFormatter
self.underlineFormatter = underlineFormatter
self.linkFormatter = linkFormatter

if needsTextKit2 {
Expand Down Expand Up @@ -192,7 +215,8 @@ final class WKSourceEditorTextFrameworkMediator: NSObject {

if needsTextKit2 {
guard let textKit2Data = textkit2SelectionData(selectedDocumentRange: selectedDocumentRange) else {
return WKSourceEditorSelectionState(isBold: false, isItalics: false, isHorizontalTemplate: false, isHorizontalReference: false, isBulletSingleList: false, isBulletMultipleList: false, isNumberSingleList: false, isNumberMultipleList: false, isHeading: false, isSubheading1: false, isSubheading2: false, isSubheading3: false, isSubheading4: false, isStrikethrough: false, isSimpleLink: false, isLinkWithNestedLink: false)

return WKSourceEditorSelectionState(isBold: false, isItalics: false, isHorizontalTemplate: false, isHorizontalReference: false, isBulletSingleList: false, isBulletMultipleList: false, isNumberSingleList: false, isNumberMultipleList: false, isHeading: false, isSubheading1: false, isSubheading2: false, isSubheading3: false, isSubheading4: false, isStrikethrough: false, isUnderline: false, isSubscript: false, isSuperscript: false, isSimpleLink: false, isLinkWithNestedLink: false)

}

Expand All @@ -210,13 +234,16 @@ final class WKSourceEditorTextFrameworkMediator: NSObject {
let isSubheading3 = headingFormatter?.attributedString(textKit2Data.paragraphAttributedString, isSubheading3In: textKit2Data.paragraphSelectedRange) ?? false
let isSubheading4 = headingFormatter?.attributedString(textKit2Data.paragraphAttributedString, isSubheading4In: textKit2Data.paragraphSelectedRange) ?? false
let isStrikethrough = strikethroughFormatter?.attributedString(textKit2Data.paragraphAttributedString, isStrikethroughIn: textKit2Data.paragraphSelectedRange) ?? false
let isSubscript = subscriptFormatter?.attributedString(textKit2Data.paragraphAttributedString, isSubscriptIn: textKit2Data.paragraphSelectedRange) ?? false
let isSuperscript = superscriptFormatter?.attributedString(textKit2Data.paragraphAttributedString, isSuperscriptIn: textKit2Data.paragraphSelectedRange) ?? false
let isUnderline = underlineFormatter?.attributedString(textKit2Data.paragraphAttributedString, isUnderlineIn: textKit2Data.paragraphSelectedRange) ?? false
let isSimpleLink = linkFormatter?.attributedString(textKit2Data.paragraphAttributedString, isSimpleLinkIn: textKit2Data.paragraphSelectedRange) ?? false
let isLinkWithNestedLink = linkFormatter?.attributedString(textKit2Data.paragraphAttributedString, isLinkWithNestedLinkIn: textKit2Data.paragraphSelectedRange) ?? false

return WKSourceEditorSelectionState(isBold: isBold, isItalics: isItalics, isHorizontalTemplate: isHorizontalTemplate, isHorizontalReference: isHorizontalReference, isBulletSingleList: isBulletSingleList, isBulletMultipleList: isBulletMultipleList, isNumberSingleList: isNumberSingleList, isNumberMultipleList: isNumberMultipleList, isHeading: isHeading, isSubheading1: isSubheading1, isSubheading2: isSubheading2, isSubheading3: isSubheading3, isSubheading4: isSubheading4, isStrikethrough: isStrikethrough, isSimpleLink: isSimpleLink, isLinkWithNestedLink: isLinkWithNestedLink)
return WKSourceEditorSelectionState(isBold: isBold, isItalics: isItalics, isHorizontalTemplate: isHorizontalTemplate, isHorizontalReference: isHorizontalReference, isBulletSingleList: isBulletSingleList, isBulletMultipleList: isBulletMultipleList, isNumberSingleList: isNumberSingleList, isNumberMultipleList: isNumberMultipleList, isHeading: isHeading, isSubheading1: isSubheading1, isSubheading2: isSubheading2, isSubheading3: isSubheading3, isSubheading4: isSubheading4, isStrikethrough: isStrikethrough, isUnderline: isUnderline, isSubscript: isSubscript, isSuperscript: isSuperscript, isSimpleLink: isSimpleLink, isLinkWithNestedLink: isLinkWithNestedLink)
} else {
guard let textKit1Storage else {
return WKSourceEditorSelectionState(isBold: false, isItalics: false, isHorizontalTemplate: false, isHorizontalReference: false, isBulletSingleList: false, isBulletMultipleList: false, isNumberSingleList: false, isNumberMultipleList: false, isHeading: false, isSubheading1: false, isSubheading2: false, isSubheading3: false, isSubheading4: false, isStrikethrough: false, isSimpleLink: false, isLinkWithNestedLink: false)
return WKSourceEditorSelectionState(isBold: false, isItalics: false, isHorizontalTemplate: false, isHorizontalReference: false, isBulletSingleList: false, isBulletMultipleList: false, isNumberSingleList: false, isNumberMultipleList: false, isHeading: false, isSubheading1: false, isSubheading2: false, isSubheading3: false, isSubheading4: false, isStrikethrough: false, isUnderline: false, isSubscript: false, isSuperscript: false, isSimpleLink: false, isLinkWithNestedLink: false)
}

let isBold = boldItalicsFormatter?.attributedString(textKit1Storage, isBoldIn: selectedDocumentRange) ?? false
Expand All @@ -233,10 +260,13 @@ final class WKSourceEditorTextFrameworkMediator: NSObject {
let isSubheading3 = headingFormatter?.attributedString(textKit1Storage, isSubheading3In: selectedDocumentRange) ?? false
let isSubheading4 = headingFormatter?.attributedString(textKit1Storage, isSubheading4In: selectedDocumentRange) ?? false
let isStrikethrough = strikethroughFormatter?.attributedString(textKit1Storage, isStrikethroughIn: selectedDocumentRange) ?? false
let isSubscript = subscriptFormatter?.attributedString(textKit1Storage, isSubscriptIn: selectedDocumentRange) ?? false
let isSuperscript = superscriptFormatter?.attributedString(textKit1Storage, isSuperscriptIn: selectedDocumentRange) ?? false
let isUnderline = underlineFormatter?.attributedString(textKit1Storage, isUnderlineIn: selectedDocumentRange) ?? false
let isSimpleLink = linkFormatter?.attributedString(textKit1Storage, isSimpleLinkIn: selectedDocumentRange) ?? false
let isLinkWithNestedLink = linkFormatter?.attributedString(textKit1Storage, isLinkWithNestedLinkIn: selectedDocumentRange) ?? false

return WKSourceEditorSelectionState(isBold: isBold, isItalics: isItalics, isHorizontalTemplate: isHorizontalTemplate, isHorizontalReference: isHorizontalReference, isBulletSingleList: isBulletSingleList, isBulletMultipleList: isBulletMultipleList, isNumberSingleList: isNumberSingleList, isNumberMultipleList: isNumberMultipleList, isHeading: isHeading, isSubheading1: isSubheading1, isSubheading2: isSubheading2, isSubheading3: isSubheading3, isSubheading4: isSubheading4, isStrikethrough: isStrikethrough, isSimpleLink: isSimpleLink, isLinkWithNestedLink: isLinkWithNestedLink)
return WKSourceEditorSelectionState(isBold: isBold, isItalics: isItalics, isHorizontalTemplate: isHorizontalTemplate, isHorizontalReference: isHorizontalReference, isBulletSingleList: isBulletSingleList, isBulletMultipleList: isBulletMultipleList, isNumberSingleList: isNumberSingleList, isNumberMultipleList: isNumberMultipleList, isHeading: isHeading, isSubheading1: isSubheading1, isSubheading2: isSubheading2, isSubheading3: isSubheading3, isSubheading4: isSubheading4, isStrikethrough: isStrikethrough, isUnderline: isUnderline, isSubscript: isSubscript, isSuperscript: isSuperscript, isSimpleLink: isSimpleLink, isLinkWithNestedLink: isLinkWithNestedLink)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,26 @@ extension WKSourceEditorViewController: WKEditorInputViewDelegate {
let action: WKSourceEditorFormatterButtonAction = isSelected ? .remove : .add
textFrameworkMediator.strikethroughFormatter?.toggleStrikethroughFormatting(action: action, in: textView)
}

func didTapUnderline(isSelected: Bool) {
let action: WKSourceEditorFormatterButtonAction = isSelected ? .remove : .add
textFrameworkMediator.underlineFormatter?.toggleUnderlineFormatting(action: action, in: textView)
}

func didTapSubscript(isSelected: Bool) {
let action: WKSourceEditorFormatterButtonAction = isSelected ? .remove : .add
textFrameworkMediator.subscriptFormatter?.toggleSubscriptFormatting(action: action, in: textView)
}

func didTapSuperscript(isSelected: Bool) {
let action: WKSourceEditorFormatterButtonAction = isSelected ? .remove : .add
textFrameworkMediator.superscriptFormatter?.toggleSuperscriptFormatting(action: action, in: textView)
}

func didTapLink(isSelected: Bool) {
presentLinkWizard(linkButtonIsSelected: isSelected)
}

func didTapClose() {
editorInputViewIsShowing = false
let isRangeSelected = textView.selectedRange.length > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isFormatt
isFormatted = YES;
} else {
// Edge case, check previous character if we are up against a closing bold or italic
if (attrs[WKSourceEditorCustomKeyColorOrange]) {
if (attrs[WKSourceEditorCustomKeyColorOrange] && attributedString.length > range.location - 1) {
attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil];
if (attrs[WKSourceEditorCustomKeyFontBoldItalics] != nil || attrs[formattingKey] != nil) {
isFormatted = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isContent
isContentKey = YES;
} else {
// Edge case, check previous character if we are up against closing string
if (attrs[WKSourceEditorCustomKeyColorOrange]) {
if (attrs[WKSourceEditorCustomKeyColorOrange] && attributedString.length > range.location - 1) {
attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil];
if (attrs[contentKey] != nil) {
isContentKey = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isKey:(NS
}

// Edge case, check previous character if we are up against opening markup
if (attrs[WKSourceEditorCustomKeyLink]) {
if (attrs[WKSourceEditorCustomKeyLink] && attributedString.length > range.location - 1) {
if (attributedString.length > range.location - 1) {
attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil];
if (attrs[key] == nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isContent
}

// Edge case, check previous character in case we're at the end of the line and list isn't detected
if ((attributedString.length > range.location - 1)) {
if (attributedString.length > range.location - 1) {
NSDictionary<NSAttributedStringKey,id> *attrs = [attributedString attributesAtIndex:range.location-1 effectiveRange:nil];

if (attrs[contentKey] != nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isHorizon
isContentKey = YES;
} else {
// Edge case, check previous character if we are up against closing string
if (attrs[WKSourceEditorCustomKeyColorGreen]) {
if (attrs[WKSourceEditorCustomKeyColorGreen] && attributedString.length > range.location - 1) {
attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil];
if (attrs[WKSourceEditorCustomKeyContentReference] != nil) {
isContentKey = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ - (BOOL)attributedString:(NSMutableAttributedString *)attributedString isStriket
isContentKey = YES;
} else {
// Edge case, check previous character if we are up against closing string
if (attrs[WKSourceEditorCustomKeyColorGreen]) {
if (attrs[WKSourceEditorCustomKeyColorGreen] && attributedString.length > range.location - 1) {
attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil];
if (attrs[WKSourceEditorCustomKeyContentStrikethrough] != nil) {
isContentKey = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import "WKSourceEditorFormatter.h"

NS_ASSUME_NONNULL_BEGIN

@interface WKSourceEditorFormatterSubscript: WKSourceEditorFormatter

- (BOOL) attributedString:(NSMutableAttributedString *)attributedString isSubscriptInRange:(NSRange)range;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit f5e3c80

Please sign in to comment.