Skip to content

Commit

Permalink
First (hacky) API attempt at using custom highlightr js lib
Browse files Browse the repository at this point in the history
  • Loading branch information
SoylentGraham committed Nov 16, 2024
1 parent 14cc191 commit 82417a4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
14 changes: 12 additions & 2 deletions Sources/CodeEditor/CodeEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public struct CodeEditor: View {
]
public static var xmlStyleAutoPairs = [ "<": ">", "\"": "\"", "'": "'" ]



/**
* Configures a CodeEditor View with the given parameters.
Expand Down Expand Up @@ -241,8 +242,12 @@ public struct CodeEditor: View {
inset : CGSize? = nil,
allowsUndo : Bool = true,
autoscroll : Bool = true,
backgroundColor: NSColor? = nil)
backgroundColor: NSColor? = nil,
highlightr : Highlightr? = nil

)
{
self.highlightr = highlightr
self.source = source
self.selection = selection
self.fontSize = fontSize
Expand Down Expand Up @@ -309,6 +314,9 @@ public struct CodeEditor: View {
backgroundColor: backgroundColor)
}

// gr: dont instantiate here, it will create a new instance every View() render
public var highlightr : Highlightr?/* = Highlightr()*/

private var source : Binding<String>
private var selection : Binding<Range<String.Index>>?
private var fontSize : Binding<CGFloat>?
Expand All @@ -334,7 +342,9 @@ public struct CodeEditor: View {
inset : inset,
allowsUndo : allowsUndo,
autoscroll : autoscroll,
backgroundColor: backgroundColor)
backgroundColor: backgroundColor,
highlightr : highlightr
)
}
}

Expand Down
7 changes: 4 additions & 3 deletions Sources/CodeEditor/UXCodeTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import Highlightr
*/
final class UXCodeTextView: UXTextView {

fileprivate let highlightr = Highlightr()

// highlightr now provided from higher level
var highlightr : Highlightr?

var customBackgroundColor: NSColor? = nil

private var hlTextStorage : CodeAttributedString? {
Expand Down Expand Up @@ -63,7 +64,7 @@ final class UXCodeTextView: UXTextView {
}
}

init() {
init(highlightr:Highlightr?) {
let textStorage = highlightr.flatMap {
CodeAttributedString(highlightr: $0)
}
Expand Down
23 changes: 17 additions & 6 deletions Sources/CodeEditor/UXCodeTextViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import SwiftUI
import Highlightr

#if os(macOS)
typealias UXViewRepresentable = NSViewRepresentable
Expand Down Expand Up @@ -56,7 +57,9 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
inset : CGSize,
allowsUndo : Bool,
autoscroll : Bool,
backgroundColor: NSColor? = nil)
backgroundColor: NSColor? = nil,
highlightr : Highlightr?
)
{
self.source = source
self.selection = selection
Expand All @@ -70,8 +73,10 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
self.allowsUndo = allowsUndo
self.autoscroll = autoscroll
self.customBackgroundColor = backgroundColor
self.highlightr = highlightr
}

private var highlightr : Highlightr? /* do not instantiate a default here, will be created on every view render */
private var source : Binding<String>
private var selection : Binding<Range<String.Index>>?
private var fontSize : Binding<CGFloat>?
Expand Down Expand Up @@ -239,7 +244,8 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {

#if os(macOS)
public func makeNSView(context: Context) -> NSScrollView {
let textView = UXCodeTextView()
// instantiate here, once, if user didn't provide a Highlightr
let textView = UXCodeTextView(highlightr:self.highlightr ?? Highlightr())
textView.customBackgroundColor = customBackgroundColor
textView.autoresizingMask = [ .width, .height ]
textView.delegate = context.coordinator
Expand Down Expand Up @@ -274,7 +280,8 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
)
}
public func makeUIView(context: Context) -> UITextView {
let textView = UXCodeTextView()
// instantiate here, once, if user didn't provide a Highlightr
let textView = UXCodeTextView(highlightr: self.highlightr ?? Highlightr())
textView.autoresizingMask = [ .flexibleWidth, .flexibleHeight ]
textView.delegate = context.coordinator
textView.textContainerInset = edgeInsets
Expand Down Expand Up @@ -329,7 +336,9 @@ struct UXCodeTextViewRepresentable_Previews: PreviewProvider {
autoPairs : [:],
inset : .init(width: 8, height: 8),
allowsUndo : true,
autoscroll : false)
autoscroll : false,
highlightr : nil
)
.frame(width: 200, height: 100)

UXCodeTextViewRepresentable(source: .constant("let a = 5"),
Expand All @@ -342,7 +351,8 @@ struct UXCodeTextViewRepresentable_Previews: PreviewProvider {
autoPairs : [:],
inset : .init(width: 8, height: 8),
allowsUndo : true,
autoscroll : false)
autoscroll : false,
highlightr: nil)
.frame(width: 200, height: 100)

UXCodeTextViewRepresentable(
Expand All @@ -361,7 +371,8 @@ struct UXCodeTextViewRepresentable_Previews: PreviewProvider {
autoPairs : [:],
inset : .init(width: 8, height: 8),
allowsUndo : true,
autoscroll : false
autoscroll : false,
highlightr : nil
)
.frame(width: 540, height: 200)
}
Expand Down

0 comments on commit 82417a4

Please sign in to comment.