Skip to content

Commit

Permalink
Merge pull request #18 from Infomaniak/feat-swift6
Browse files Browse the repository at this point in the history
feat: Swift 6
  • Loading branch information
valentinperignon authored Jan 27, 2025
2 parents d1135b2 + f1f67d0 commit 8354881
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.9
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import Foundation

/// Describes a position where the caret can be set.
public enum CaretPosition {
public enum CaretPosition: Sendable {
/// At the beginning of the document, before any content.
case beginningOfDocument
/// At the beginning of the document, after all content.
Expand Down
2 changes: 1 addition & 1 deletion Sources/InfomaniakRichHTMLEditor/Models/EditorError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// specific language governing permissions and limitations
// under the License.

enum EditorError: Error {
enum EditorError: Error, Sendable {
case impossibleToLoadWKUserScript(filename: String)
}
2 changes: 1 addition & 1 deletion Sources/InfomaniakRichHTMLEditor/Models/ExecCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// specific language governing permissions and limitations
// under the License.

enum ExecCommand: String, CaseIterable {
enum ExecCommand: String, CaseIterable, Sendable {
// Commands that return a state
case bold
case italic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// specific language governing permissions and limitations
// under the License.

enum JavaScriptFunction {
case execCommand(command: String, argument: Any? = nil)
enum JavaScriptFunction: Sendable {
case execCommand(command: String, argument: Sendable? = nil)
case setContent(content: String)
case injectCSS(content: String)
case createLink(url: String, text: String?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// under the License.

/// Describes how the text is aligned.
public enum TextJustification: String, Codable {
public enum TextJustification: String, Codable, Sendable {
/// Fully justified.
case full
/// Aligned to the left.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import AppKit
#endif

/// This struct contains the current state of the text selected or at the insertion point.
public struct UITextAttributes: Codable {
public struct UITextAttributes: Codable, Sendable {
public var hasBold = false
public var hasItalic = false
public var hasUnderline = false
Expand Down
4 changes: 2 additions & 2 deletions Sources/InfomaniakRichHTMLEditor/Models/UserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

import WebKit

struct UserScript {
struct UserScript: Sendable {
let name: String
let injectionTime: WKUserScriptInjectionTime

func load(to webView: WKWebView) throws {
@MainActor func load(to webView: WKWebView) throws {
try webView.configuration.userContentController.addUserScript(
named: name,
injectionTime: injectionTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public extension RichHTMLEditorView {
}
}

private func execCommand(_ command: ExecCommand, argument: Any? = nil) {
private func execCommand(_ command: ExecCommand, argument: Sendable? = nil) {
javaScriptManager.execCommand(command, argument: argument)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Foundation
/// The methods for receiving editing-related messages for editor view objects.
///
/// All of the methods in this protocol are optional.
@MainActor
public protocol RichHTMLEditorViewDelegate: AnyObject {
/// Tells the delegate when the specified editor view is fully loaded and ready to be used.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import SwiftUI
///
/// The properties are read-only and are automatically updated by the editor.
/// If you want to update the style, you should use the available functions.
@MainActor
public final class TextAttributes: ObservableObject {
@Published public private(set) var hasBold = false
@Published public private(set) var hasItalic = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public extension View {
/// takes a `newValue` parameter that indicates the updated value.
///
/// - Returns: A view that fires an action when the editor is loaded.
func onEditorLoaded(perform action: @escaping () -> Void) -> some View {
func onEditorLoaded(perform action: @escaping @Sendable () -> Void) -> some View {
environment(\.onEditorLoaded, action)
}

Expand All @@ -57,7 +57,7 @@ public extension View {
/// takes a `newPosition` parameter that indicates the updated position.
///
/// - Returns: A view that fires an action when the position of the caret changes.
func onCaretPositionChange(perform action: @escaping (_ newPosition: CGRect) -> Void) -> some View {
func onCaretPositionChange(perform action: @escaping @Sendable (_ newPosition: CGRect) -> Void) -> some View {
environment(\.onCaretPositionChange, action)
}

Expand All @@ -66,7 +66,7 @@ public extension View {
/// - Parameter action: A closure to run when a JavaScript function fails.
///
/// - Returns: A view that fires an action when a JavaScript function fails.
func onJavaScriptFunctionFail(perform action: @escaping (any Error, String) -> Void) -> some View {
func onJavaScriptFunctionFail(perform action: @escaping @Sendable (any Error, String) -> Void) -> some View {
environment(\.onJavaScriptFunctionFail, action)
}

Expand All @@ -76,7 +76,7 @@ public extension View {
/// the editor.
///
/// - Returns: A view with the customizations applied to editor.
func introspectEditor(perform action: @escaping (RichHTMLEditorView) -> Void) -> some View {
func introspectEditor(perform action: @escaping @Sendable (RichHTMLEditorView) -> Void) -> some View {
environment(\.introspectEditor, action)
}

Expand All @@ -89,7 +89,7 @@ public extension View {
/// this task yourself.
///
/// - Returns: A view with the customizations applied to editor.
func handleLinkOpening(perform action: @escaping (URL) -> Bool) -> some View {
func handleLinkOpening(perform action: @escaping @Sendable (URL) -> Bool) -> some View {
environment(\.handleLinkOpening, action)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ public struct EditorCSSKey: EnvironmentKey {
}

public struct OnEditorLoadedKey: EnvironmentKey {
public static let defaultValue: (() -> Void)? = nil
public static let defaultValue: (@Sendable () -> Void)? = nil
}

public struct OnCaretPositionChangeKey: EnvironmentKey {
public static let defaultValue: ((CGRect) -> Void)? = nil
public static let defaultValue: (@Sendable (CGRect) -> Void)? = nil
}

public struct OnJavaScriptFunctionFailKey: EnvironmentKey {
public static let defaultValue: ((any Error, String) -> Void)? = nil
public static let defaultValue: (@Sendable (any Error, String) -> Void)? = nil
}

public struct IntrospectEditorKey: EnvironmentKey {
public static let defaultValue: ((RichHTMLEditorView) -> Void)? = nil
public static let defaultValue: (@Sendable (RichHTMLEditorView) -> Void)? = nil
}

public struct HandleLinkOpeningKey: EnvironmentKey {
public static let defaultValue: ((URL) -> Bool)? = nil
public static let defaultValue: (@Sendable (URL) -> Bool)? = nil
}

// MARK: - Environment Values
Expand All @@ -73,27 +73,27 @@ public extension EnvironmentValues {
set { self[EditorCSSKey.self] = newValue }
}

var onEditorLoaded: (() -> Void)? {
var onEditorLoaded: (@Sendable () -> Void)? {
get { self[OnEditorLoadedKey.self] }
set { self[OnEditorLoadedKey.self] = newValue }
}

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

var onJavaScriptFunctionFail: ((any Error, String) -> Void)? {
var onJavaScriptFunctionFail: (@Sendable (any Error, String) -> Void)? {
get { self[OnJavaScriptFunctionFailKey.self] }
set { self[OnJavaScriptFunctionFailKey.self] = newValue }
}

var introspectEditor: ((RichHTMLEditorView) -> Void)? {
var introspectEditor: (@Sendable (RichHTMLEditorView) -> Void)? {
get { self[IntrospectEditorKey.self] }
set { self[IntrospectEditorKey.self] = newValue }
}

var handleLinkOpening: ((URL) -> Bool)? {
var handleLinkOpening: (@Sendable (URL) -> Bool)? {
get { self[HandleLinkOpeningKey.self] }
set { self[HandleLinkOpeningKey.self] = newValue }
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/InfomaniakRichHTMLEditor/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// specific language governing permissions and limitations
// under the License.

enum Constants {
enum Constants: Sendable {
static let packageID = "com.infomaniak.swift-rich-html-editor"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

import WebKit

@MainActor
protocol JavaScriptManagerDelegate: AnyObject {
func javascriptFunctionDidFail(error: any Error, function: String)
}

@MainActor
final class JavaScriptManager {
var isDOMContentLoaded = false {
didSet {
Expand All @@ -43,7 +45,7 @@ final class JavaScriptManager {
evaluateWhenDOMIsReady(function: injectCSS)
}

func execCommand(_ command: ExecCommand, argument: Any? = nil) {
func execCommand(_ command: ExecCommand, argument: Sendable? = nil) {
let execCommand = JavaScriptFunction.execCommand(command: command.rawValue, argument: argument)
evaluate(function: execCommand)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import OSLog
import WebKit

@MainActor
protocol ScriptMessageHandlerDelegate: AnyObject {
func editorDidLoad()
func contentDidChange(_ text: String)
Expand Down

0 comments on commit 8354881

Please sign in to comment.