diff --git a/Sources/InfomaniakRichHTMLEditor/Resources/js/editor/selection.js b/Sources/InfomaniakRichHTMLEditor/Resources/js/editor/selection.js index 5e6dc5d..8e1abfa 100644 --- a/Sources/InfomaniakRichHTMLEditor/Resources/js/editor/selection.js +++ b/Sources/InfomaniakRichHTMLEditor/Resources/js/editor/selection.js @@ -11,7 +11,7 @@ function computeAndReportCaretPosition() { return; } - reportCursorPositionDidChange(caretRect); + reportCaretPositionDidChange(caretRect); } function computeCaretRect() { diff --git a/Sources/InfomaniakRichHTMLEditor/Resources/js/utils/javascriptBridge.js b/Sources/InfomaniakRichHTMLEditor/Resources/js/utils/javascriptBridge.js index 2d7a340..027ded7 100644 --- a/Sources/InfomaniakRichHTMLEditor/Resources/js/utils/javascriptBridge.js +++ b/Sources/InfomaniakRichHTMLEditor/Resources/js/utils/javascriptBridge.js @@ -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]); } diff --git a/Sources/InfomaniakRichHTMLEditor/RichHTMLEditorView.swift b/Sources/InfomaniakRichHTMLEditor/RichHTMLEditorView.swift index b8ae5a3..0bfc78d 100644 --- a/Sources/InfomaniakRichHTMLEditor/RichHTMLEditorView.swift +++ b/Sources/InfomaniakRichHTMLEditor/RichHTMLEditorView.swift @@ -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 { @@ -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 @@ -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 diff --git a/Sources/InfomaniakRichHTMLEditor/RichHTMLEditorViewDelegate.swift b/Sources/InfomaniakRichHTMLEditor/RichHTMLEditorViewDelegate.swift index 779965d..3645097 100644 --- a/Sources/InfomaniakRichHTMLEditor/RichHTMLEditorViewDelegate.swift +++ b/Sources/InfomaniakRichHTMLEditor/RichHTMLEditorViewDelegate.swift @@ -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. @@ -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 diff --git a/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichEditor+Modifier.swift b/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichEditor+Modifier.swift index 7d9de81..e7f3fc9 100644 --- a/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichEditor+Modifier.swift +++ b/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichEditor+Modifier.swift @@ -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. diff --git a/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditor+Environment.swift b/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditor+Environment.swift index acb4e48..f85c09e 100644 --- a/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditor+Environment.swift +++ b/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditor+Environment.swift @@ -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)? } @@ -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)? { diff --git a/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditor.swift b/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditor.swift index 64d0494..ddf3225 100644 --- a/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditor.swift +++ b/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditor.swift @@ -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 diff --git a/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditorCoordinator.swift b/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditorCoordinator.swift index 8108849..6728bc7 100644 --- a/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditorCoordinator.swift +++ b/Sources/InfomaniakRichHTMLEditor/SwiftUI/Views/RichHTMLEditorCoordinator.swift @@ -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( diff --git a/Sources/InfomaniakRichHTMLEditor/WebViewBridge/ScriptMessageHandler.swift b/Sources/InfomaniakRichHTMLEditor/WebViewBridge/ScriptMessageHandler.swift index dd71ecd..2e41ea5 100644 --- a/Sources/InfomaniakRichHTMLEditor/WebViewBridge/ScriptMessageHandler.swift +++ b/Sources/InfomaniakRichHTMLEditor/WebViewBridge/ScriptMessageHandler.swift @@ -19,7 +19,7 @@ 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 { @@ -27,7 +27,7 @@ final class ScriptMessageHandler: NSObject, WKScriptMessageHandler { case editorDidLoad case contentDidChange case contentHeightDidChange - case cursorPositionDidChange + case caretPositionDidChange case selectedTextAttributesDidChange case scriptLog } @@ -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) } @@ -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) {