Skip to content

Commit e7f0db3

Browse files
authored
2.0 (#42)
* Renames editor source files since UIKit can run on macOS * Removes previous modifiers * Removes modifier updates * Restores onSelectionChange * Moves init args to modifiers * Removes old modifiers * Deletes wrapper class init vars * Moves init args to modifiers * Access rules * Introspect fn * Makes internal vars public * UIKit moves editor to textView sub-var to match AppKit * Removes static vars from HighlightedTextEditor * All preset * Renames class var to customTextView * Custom text view var is not recreated every change * Removes unused accessors for modifiers * Removes unused text var * Workaround for introspect bug * Workaround * Passes accessible data out of classes as struct * Updates example code to show new modifiers * Updates API for new modifiers * Cleanup * SwiftFormat config * SwiftFormat on project * Linting * xcode profile * Removes test code for removed modifiers * Team * Removes iOS tests for removed editor functionality * Removes unused AppKit tests * New editor to test modifiers * e2e tests new modifiers * New Introspect test editor * Removes the initialization of two properties that didn't need it * Introspect works on subsequent updates in UIKit editor * Introspect modifier gets updates with AppKit * Spacing * Moves internals inside HLTE struct * References right project * e2e tests introspect modifier * e2e tests introspect * Documents regex preset * UIKit tests run on iPhone 11 * Removes unused snapshots * Renames font enum option * Aliases callbacks * Renames to ScrollableTextView * Moves internal editor class inside HLTE to de-pollute global namespace * Makes internal classes final * Does not make unnecessary vars on redraw * Credit comments * Updates iOS snapshots * Resets to main branch * Removes unused system extensions * Linting stage of CI * Lint fixes * Removes Install homebrew step since GHA already has it * Removes swiftlint
1 parent 710bb6f commit e7f0db3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+877
-1122
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ env:
1313

1414
jobs:
1515

16+
linting:
17+
name: Linting
18+
runs-on: macos-latest
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v2
23+
- name: Lint project
24+
run: swiftlint
25+
1626
appkit:
1727
name: AppKit Tests
1828
runs-on: macos-latest
@@ -71,7 +81,7 @@ jobs:
7181
sed -i '' "s/kyle-n/${GITHUB_ACTOR//\//\\/}/g" $XCODEPROJ
7282
- name: Boot Simulator
7383
env:
74-
IOS_SIM_NAME: iPhone 11
84+
IOS_SIM_NAME: iPhone 12
7585
run: |
7686
IOS_SIM_UDID=`xcrun simctl list | grep -w "$IOS_SIM_NAME" | awk 'match($0, /\(([-0-9A-F]+)\)/) { print substr( $0, RSTART + 1, RLENGTH - 2 )}' | head -1`
7787
SIMULATOR_PATH='/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/Contents/MacOS/Simulator'
@@ -91,7 +101,7 @@ jobs:
91101
- name: Run UIKit tests
92102
run: |
93103
xcodebuild -resolvePackageDependencies -project "$TEST_PROJECT_PATH"
94-
xcodebuild test -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11' -scheme "$TEST_PROJECT_NAME (iOS)" -project $TEST_PROJECT_PATH
104+
xcodebuild test -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12' -scheme "$TEST_PROJECT_NAME (iOS)" -project $TEST_PROJECT_PATH
95105
killall Simulator
96106
- name: Save non-matching iOS snapshots
97107
if: failure()

.swiftformat

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--swiftversion 5
2+
--binarygrouping none
3+
--commas inline
4+
--decimalgrouping none
5+
--hexgrouping none
6+
--ifdef no-indent
7+
--nospaceoperators ..<
8+
--octalgrouping none
9+
--self init-only
10+
--semicolons never
11+
--stripunusedargs closure-only
12+
--wraparguments before-first
13+
--wrapcollections before-first
14+
--wrapparameters before-first
15+
--maxwidth 120

Package.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// swift-tools-version:5.3
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
32

43
import PackageDescription
54

@@ -12,11 +11,13 @@ public let package = Package(
1211
products: [
1312
.library(
1413
name: "HighlightedTextEditor",
15-
targets: ["HighlightedTextEditor"]),
14+
targets: ["HighlightedTextEditor"]
15+
)
1616
],
1717
targets: [
1818
.target(
1919
name: "HighlightedTextEditor",
20-
dependencies: [])
20+
dependencies: []
21+
)
2122
]
2223
)

README.md

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import HighlightedTextEditor
2929
let betweenUnderscores = try! NSRegularExpression(pattern: "_[^_]+_", options: [])
3030

3131
struct ContentView: View {
32-
@State private var text: String = "here is _bold, italicized, red text_"
32+
33+
@State private var text: String = ""
3334

3435
private let rules: [HighlightRule] = [
3536
HighlightRule(pattern: betweenUnderscores, formattingRules: [
@@ -46,9 +47,14 @@ struct ContentView: View {
4647
VStack {
4748
HighlightedTextEditor(text: $text, highlightRules: rules)
4849
// optional modifiers
49-
.autocapitalizationType(.words)
50-
.keyboardType(.numberPad)
51-
.autocorrectionType(.no)
50+
.onCommit { print("commited") }
51+
.onEditingChanged { print("editing changed") }
52+
.onTextChange { print("latest text value", $0) }
53+
.onSelectionChange { print("NSRange of current selection", $0)}
54+
.introspect { editor in
55+
// access underlying UITextView or NSTextView
56+
editor.textView.backgroundColor = .green
57+
}
5258
}
5359
}
5460
}
@@ -71,38 +77,44 @@ Example of using a preset:
7177
HighlightedTextEditor(text: $text, highlightRules: .markdown)
7278
```
7379

80+
### Regex Presets
81+
82+
I've also added a preset variable, `NSRegularExpression.all`, for easily selecting a whole string.
83+
84+
Example of using it:
85+
86+
```swift
87+
HighlightedTextEditor(text: $text, highlightRules: [
88+
HighlightRule(pattern: .all, formattingRule: TextFormattingRule(key: .underlineStyle, value: NSUnderlineStyle.single.rawValue))
89+
])
90+
```
91+
7492
## API
7593

7694
### HighlightedTextEditor
7795

78-
| Parameter | Type | Optional | Description |
79-
| --- | --- | --- | --- |
80-
| `text` | Binding&lt;String\> | No | Text content of the field |
81-
| `highlightRules` | [HighlightRule] | No | Patterns and formatting for those patterns |
82-
| `onEditingChanged` | () -> Void | Yes | Called when the user begins editing |
83-
| `onCommit` | () -> Void | Yes | Called when the user stops editing |
84-
| `onTextChange` | (String) -> Void | Yes | Called whenever `text` changes |
85-
86-
#### Modifiers (UIKit)
87-
88-
- `.autocapitalizationType(_ type: UITextAutocapitalizationType)`
89-
- `.autocorrectionType(_ type: UITextAutocorrectionType)`
90-
- `.backgroundColor(_ color: UIColor)`
91-
- `.defaultColor(_ color: UIColor)`
92-
- `.defaultFont(_ font: UIFont)`
93-
- `.keyboardType(_ type: UIKeyboardType)`
94-
- `.insertionPointColor(_ color: UIColor)`
95-
- `.multilineTextAlignment(_ alignment: TextAlignment)`
96-
97-
#### Modifiers (AppKit)
98-
99-
- `.allowsDocumentBackgroundColorChange(_ allowsChange: Bool)`
100-
- `.backgroundColor(_ color: NSColor)`
101-
- `.defaultColor(_ color: NSColor)`
102-
- `.defaultFont(_ font: NSFont)`
103-
- `.drawsBackground(_ shouldDraw: Bool)`
104-
- `.insertionPointColor(_ color: NSColor)`
105-
- `.multilineTextAlignment(_ alignment: TextAlignment)`
96+
| Parameter | Type | Description |
97+
| --- | --- | --- |
98+
| `text` | Binding&lt;String\> | Text content of the field |
99+
| `highlightRules` | [HighlightRule] | Patterns and formatting for those patterns |
100+
101+
#### Modifiers
102+
103+
- `.introspect(callback: (_ editor: HighlightedTextEditorInternals) -> Void)`: Allows you the developer to access the underlying UIKit or AppKit objects used by HighlightedTextEditor
104+
- `.onCommit(_ callback: @escaping () -> Void)`: Called when the user stops editing
105+
- `.onEditingChanged(_ callback: @escaping () -> Void)`: Called when the user begins editing
106+
- `.onTextChange(_ callback: @escaping (_ editorContent: String) -> Void)`: Called whenever `text` changes
107+
- `.onSelectionChange(_ callback: @escaping (_ selectedRange: NSRange) -> Void)`
108+
- `.onSelectionChange(_ callback: @escaping (_ selectedRanges: [NSRange]) -> Void)` (AppKit only)
109+
110+
### HighlightedTextEditorInternals
111+
112+
Passed as a parameter to `.introspect()` callbacks. Useful for customizing editor behavior in some way not supported by the HLTE API.
113+
114+
| Property | Type | Description |
115+
| --- | --- | --- |
116+
| `textView` | UITextView or NSTextView | For customizing the UIKit/AppKit text editor |
117+
| `scrollView` | NSScrollView? | For customizing the NSScrollView wrapper. Returns `nil` in UIKit |
106118

107119
### HighlightRule
108120

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
disabled_rules:
2+
- force_try
3+
- orphaned_doc_comment

0 commit comments

Comments
 (0)