Skip to content
New issue

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

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

Already on GitHub? # to your account

get cursor position with binding in swiftui view #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 17 additions & 2 deletions Sources/Views/FireflySyntaxView + TextDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ extension FireflySyntaxView: UITextViewDelegate {
return true
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateCursorPosition()
}

public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
delegate?.didClickLink(URL.absoluteString)
return false
Expand All @@ -169,9 +173,20 @@ extension FireflySyntaxView: UITextViewDelegate {
return newLinePrefix
}

// public func textViewDidChangeSelection(_ textView: UITextView) {
private func updateCursorPosition() {
if let cursorPositionChange = self.delegate?.cursorPositionChange {
if let pos = self.textView.cursorPosition() {
cursorPositionChange(self.textView.convert(pos, to: self.textView.superview))
} else {
cursorPositionChange(nil)
}
}
}

public func textViewDidChangeSelection(_ textView: UITextView) {
updateCursorPosition()
// textStorage.updatePlaceholders(cursorRange: textView.selectedRange)
// }
}

func updateSelectedRange(_ range: NSRange) {
if range.location + range.length <= text.utf8.count {
Expand Down
13 changes: 13 additions & 0 deletions Sources/Views/FireflyTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ public class FireflyTextView: UITextView {
}
}

/// Returns a CGRect for the cursor position in the text view's coordinates. If no cursor is present, it returns nil.
/// source: https://stackoverflow.com/a/43167060/3902590
public func cursorPosition() -> CGRect? {
if let selectedRange = self.selectedTextRange
{
// `caretRect` is in the `textView` coordinate space.
return self.caretRect(for: selectedRange.end)
} else {
// No selection and no caret in UITextView.
return nil
}
}

public func currentWord() -> String {
guard let cursorRange = self.selectedTextRange else { return "" }
func getRange(from position: UITextPosition, offset: Int) -> UITextRange? {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Views/FireflyTextViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
//

import Foundation
import CoreGraphics

public protocol FireflyDelegate: AnyObject {
var cursorPositionChange: ((_ cursorPosition: CGRect?) -> Void)? { get }

func didChangeText(_ syntaxTextView: FireflyTextView)

Expand All @@ -22,6 +24,8 @@ public protocol FireflyDelegate: AnyObject {

// Provide default empty implementations of methods that are optional.
public extension FireflyDelegate {
var cursorPositionChange: ((_ cursorPosition: CGRect?) -> Void)? { nil }

func didChangeText(_ syntaxTextView: FireflyTextView) { }

func didChangeSelectedRange(_ syntaxTextView: FireflyTextView, selectedRange: NSRange) { }
Expand Down
23 changes: 21 additions & 2 deletions Sources/Views/Swift UI/FireflySyntaxViewSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import SwiftUI
public struct FireflySyntaxEditor: UIViewRepresentable {

@Binding var text: String
let cursorPosition: Binding<CGRect?>?

var language: String
var theme: String
Expand All @@ -24,8 +25,18 @@ public struct FireflySyntaxEditor: UIViewRepresentable {
var didChangeSelectedRange: (FireflySyntaxEditor, NSRange) -> Void
var textViewDidBeginEditing: (FireflySyntaxEditor) -> Void

public init(text: Binding<String>, language: String, theme: String, fontName: String, didChangeText: @escaping (FireflySyntaxEditor) -> Void, didChangeSelectedRange: @escaping (FireflySyntaxEditor, NSRange) -> Void, textViewDidBeginEditing: @escaping (FireflySyntaxEditor) -> Void) {
public init(
text: Binding<String>,
cursorPosition: Binding<CGRect?>? = nil,
language: String,
theme: String,
fontName: String,
didChangeText: @escaping (FireflySyntaxEditor) -> Void,
didChangeSelectedRange: @escaping (FireflySyntaxEditor, NSRange) -> Void,
textViewDidBeginEditing: @escaping (FireflySyntaxEditor) -> Void
) {
self._text = text
self.cursorPosition = cursorPosition
self.didChangeText = didChangeText
self.didChangeSelectedRange = didChangeSelectedRange
self.textViewDidBeginEditing = textViewDidBeginEditing
Expand All @@ -51,15 +62,23 @@ public struct FireflySyntaxEditor: UIViewRepresentable {
public func makeCoordinator() -> Coordinator {
Coordinator(self)
}

public class Coordinator: FireflyDelegate {

public var cursorPositionChange: ((CGRect?) -> Void)?

public func didClickLink(_ link: URL) { }

let parent: FireflySyntaxEditor
var wrappedView: FireflySyntaxView!

init(_ parent: FireflySyntaxEditor) {
self.parent = parent
if let cursorPosition = parent.cursorPosition {
self.cursorPositionChange = {
cursorPosition.wrappedValue = $0
}
}
}

public func didChangeText(_ syntaxTextView: FireflyTextView) {
Expand Down