Skip to content

Commit

Permalink
refactor: Simplify name between cursor and caret
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jul 17, 2024
1 parent afb5c30 commit 077b5be
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function computeAndReportCaretPosition() {
return;
}

reportCursorPositionDidChange(caretRect);
reportCaretPositionDidChange(caretRect);
}

function computeCaretRect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ function reportSelectedTextAttributesDidChange(textAttributes) {
window.webkit.messageHandlers.selectedTextAttributesDidChange.postMessage(json);
}

function reportCursorPositionDidChange(cursorRect) {
window.webkit.messageHandlers.cursorPositionDidChange.postMessage([cursorRect.x, cursorRect.y, cursorRect.width, cursorRect.height]);
function reportCaretPositionDidChange(caretRect) {
window.webkit.messageHandlers.caretPositionDidChange.postMessage([caretRect.x, caretRect.y, caretRect.width, caretRect.height]);
}
16 changes: 8 additions & 8 deletions Sources/InfomaniakRichHTMLEditor/RichHTMLEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public class RichHTMLEditorView: PlatformView {
/// A Boolean value that indicates whether the editor view use its inner scrollview.
///
/// When the Boolean is `false`, the editor will use the first parent
/// `UIScrollView` to keep the cursor always visible when the
/// cursor moves below the keyboard or off the screen.
/// `UIScrollView` to keep the caret always visible when the
/// caret moves below the keyboard or off the screen.
/// However, when the Boolean is `true`, the editor will use the
/// `UIScrollView` of the inner ``WebKit/WKWebView``
/// to keep the cursor visible.
/// to keep the caret visible.
///
/// The default value is `false`.
public var isScrollEnabled: Bool {
Expand Down Expand Up @@ -267,7 +267,7 @@ extension RichHTMLEditorView: UIScrollViewDelegate {
/// Disabling the scrollview is not enough to completely prevent
/// scrolling.
/// It is necessary to reset the scrollOffset each time it changes
/// (when the cursor is under the keyboard for example).
/// (when the caret is under the keyboard for example).
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard !isScrollEnabled else { return }
scrollView.contentOffset = .zero
Expand Down Expand Up @@ -303,15 +303,15 @@ extension RichHTMLEditorView: ScriptMessageHandlerDelegate {
}

func caretRectDidChange(_ position: CGRect) {
delegate?.richHTMLEditorView(self, cursorPositionDidChange: position)
delegate?.richHTMLEditorView(self, caretPositionDidChange: position)
}

func cursorPositionDidChange(_ cursorRect: CGRect) {
delegate?.richHTMLEditorView(self, cursorPositionDidChange: cursorRect)
func caretPositionDidChange(_ caretRect: CGRect) {
delegate?.richHTMLEditorView(self, caretPositionDidChange: caretRect)

#if canImport(UIKit)
if !isScrollEnabled, let scrollView = findClosestScrollView() {
let scrollRect = convert(cursorRect, to: scrollView)
let scrollRect = convert(caretRect, to: scrollView)
scrollView.scrollRectToVisible(scrollRect, animated: true)
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public protocol RichHTMLEditorViewDelegate: AnyObject {
/// - Parameter richHTMLEditorView: The editor which is loaded.
func richHTMLEditorViewDidChange(_ richHTMLEditorView: RichHTMLEditorView)

/// Tells the delegate when the position of the cursor or the selection range of the specified
/// Tells the delegate when the position of the carte or the selection range of the specified
/// editor moves.
///
/// Implementation of this method is optional.
///
/// - Parameters:
/// - richHTMLEditorView: The editor which is loaded.
/// - cursorPosition: The new position of the cursor or of the selection range.
func richHTMLEditorView(_ richHTMLEditorView: RichHTMLEditorView, cursorPositionDidChange cursorPosition: CGRect)
/// - caretPosition: The new position of the caret or of the selection range.
func richHTMLEditorView(_ richHTMLEditorView: RichHTMLEditorView, caretPositionDidChange caretPosition: CGRect)

/// Tells the delegate when the attributes of the selected text changes in the specified editor view.
///
/// When the cursor moves, the editor calls this method if the attributes of the selected text is
/// When the carte moves, the editor calls this method if the attributes of the selected text is
/// different from the old one.
/// For example, the method will be called if the newly selected text is bold but the previous one
/// was not.
Expand Down Expand Up @@ -87,7 +87,7 @@ public protocol RichHTMLEditorViewDelegate: AnyObject {
public extension RichHTMLEditorViewDelegate {
func richHTMLEditorViewDidLoad(_ richHTMLEditorView: RichHTMLEditorView) {}
func richHTMLEditorViewDidChange(_ richHTMLEditorView: RichHTMLEditorView) {}
func richHTMLEditorView(_ richHTMLEditorView: RichHTMLEditorView, cursorPositionDidChange cursorPosition: CGRect) {}
func richHTMLEditorView(_ richHTMLEditorView: RichHTMLEditorView, caretPositionDidChange caretPosition: CGRect) {}
func richHTMLEditorView(
_ richHTMLEditorView: RichHTMLEditorView,
selectedTextAttributesDidChange textAttributes: UITextAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public extension View {
environment(\.onEditorLoaded, action)
}

/// Performs an action when the position of the cursor in the editor changes.
/// Performs an action when the position of the caret in the editor changes.
///
/// - Parameters action: A closure to run when the cursor moves. The closure
/// - Parameters action: A closure to run when the caret moves. The closure
/// takes a `newPosition` parameter that indicates the updated position.
///
/// - Returns: A view that fires an action when the position of the cursor changes.
func onCursorPositionChange(perform action: @escaping (_ newPosition: CGRect) -> Void) -> some View {
environment(\.onCursorPositionChange, action)
/// - Returns: A view that fires an action when the position of the caret changes.
func onCaretPositionChange(perform action: @escaping (_ newPosition: CGRect) -> Void) -> some View {
environment(\.onCaretPositionChange, action)
}

/// Performs an action when a JavaScript function executed by the editor fails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct OnEditorLoadedKey: EnvironmentKey {
public static var defaultValue: (() -> Void)?
}

public struct OnCursorPositionChangeKey: EnvironmentKey {
public struct OnCaretPositionChangeKey: EnvironmentKey {
public static var defaultValue: ((CGRect) -> Void)?
}

Expand Down Expand Up @@ -70,9 +70,9 @@ public extension EnvironmentValues {
set { self[OnEditorLoadedKey.self] = newValue }
}

var onCursorPositionChange: ((CGRect) -> Void)? {
get { self[OnCursorPositionChangeKey.self] }
set { self[OnCursorPositionChangeKey.self] = newValue }
var onCaretPositionChange: ((CGRect) -> Void)? {
get { self[OnCaretPositionChangeKey.self] }
set { self[OnCaretPositionChangeKey.self] = newValue }
}

var onJavaScriptFunctionFail: ((any Error, String) -> Void)? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct RichHTMLEditor: PlateformViewRepresentable {

@Environment(\.editorCSS) var editorCSS
@Environment(\.onEditorLoaded) var onEditorLoaded
@Environment(\.onCursorPositionChange) var onCursorPositionChange
@Environment(\.onCaretPositionChange) var onCaretPositionChange
@Environment(\.onJavaScriptFunctionFail) var onJavaScriptFunctionFail
@Environment(\.introspectEditor) var introspectEditor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public final class RichHTMLEditorCoordinator: RichHTMLEditorViewDelegate {
}
}

public func richHTMLEditorView(_ richHTMLEditorView: RichHTMLEditorView, cursorPositionDidChange cursorPosition: CGRect) {
parent.onCursorPositionChange?(cursorPosition)
public func richHTMLEditorView(_ richHTMLEditorView: RichHTMLEditorView, caretPositionDidChange caretPosition: CGRect) {
parent.onCaretPositionChange?(caretPosition)
}

public func richHTMLEditorView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ protocol ScriptMessageHandlerDelegate: AnyObject {
func contentDidChange(_ text: String)
func contentHeightDidChange(_ contentHeight: CGFloat)
func selectedTextAttributesDidChange(_ selectedTextAttributes: UITextAttributes?)
func cursorPositionDidChange(_ cursorRect: CGRect)
func caretPositionDidChange(_ caretRect: CGRect)
}

final class ScriptMessageHandler: NSObject, WKScriptMessageHandler {
enum ScriptMessage: String, CaseIterable {
case editorDidLoad
case contentDidChange
case contentHeightDidChange
case cursorPositionDidChange
case caretPositionDidChange
case selectedTextAttributesDidChange
case scriptLog
}
Expand All @@ -50,8 +50,8 @@ final class ScriptMessageHandler: NSObject, WKScriptMessageHandler {
contentHeightDidChange(message)
case .selectedTextAttributesDidChange:
selectedTextAttributesDidChange(message)
case .cursorPositionDidChange:
cursorPositionDidChange(message)
case .caretPositionDidChange:
caretPositionDidChange(message)
case .scriptLog:
scriptLog(message)
}
Expand Down Expand Up @@ -91,19 +91,19 @@ final class ScriptMessageHandler: NSObject, WKScriptMessageHandler {
}
}

private func cursorPositionDidChange(_ message: WKScriptMessage) {
guard let cursorData = message.body as? [Double], cursorData.count >= 4 else {
private func caretPositionDidChange(_ message: WKScriptMessage) {
guard let caretData = message.body as? [Double], caretData.count >= 4 else {
return
}

// Sometimes, the JavaScript function returns a width and height equal to 0
let cursorPosition = CGRect(
x: cursorData[0],
y: cursorData[1],
width: max(1, cursorData[2]),
height: max(1, cursorData[3])
let caretPosition = CGRect(
x: caretData[0],
y: caretData[1],
width: max(1, caretData[2]),
height: max(1, caretData[3])
)
delegate?.cursorPositionDidChange(cursorPosition)
delegate?.caretPositionDidChange(caretPosition)
}

private func scriptLog(_ message: WKScriptMessage) {
Expand Down

0 comments on commit 077b5be

Please sign in to comment.