-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathEditorBridge.swift
112 lines (90 loc) · 3.19 KB
/
EditorBridge.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// swiftformat:disable redundantRawValues
// Don't modify this file manually, it's auto generated.
import WebKit
public class EditorBridge {
weak var webView: WKWebView?
private let jsonEncoder = JSONEncoder()
private let jsonDecoder = JSONDecoder()
init(webView: WKWebView) {
self.webView = webView
}
public func toggleBold(completion: ((Result<Void, Error>) -> Void)? = nil) {
let javaScriptString = "editor.toggleBold" + "(" + ")"
print("[ts-gyb] evaluating: \(javaScriptString)")
webView?.evaluateJavaScript(javaScriptString) { evaluationResult, error in
guard let completion = completion else { return }
if let error = error {
completion(.failure(error))
return
}
completion(.success(()))
}
}
public func toggleItalic(completion: ((Result<Void, Error>) -> Void)? = nil) {
let javaScriptString = "editor.toggleItalic" + "(" + ")"
print("[ts-gyb] evaluating: \(javaScriptString)")
webView?.evaluateJavaScript(javaScriptString) { evaluationResult, error in
guard let completion = completion else { return }
if let error = error {
completion(.failure(error))
return
}
completion(.success(()))
}
}
public func toggleUnderline(completion: ((Result<Void, Error>) -> Void)? = nil) {
let javaScriptString = "editor.toggleUnderline" + "(" + ")"
print("[ts-gyb] evaluating: \(javaScriptString)")
webView?.evaluateJavaScript(javaScriptString) { evaluationResult, error in
guard let completion = completion else { return }
if let error = error {
completion(.failure(error))
return
}
completion(.success(()))
}
}
public func clear(completion: ((Result<Void, Error>) -> Void)? = nil) {
let javaScriptString = "editor.clear" + "(" + ")"
print("[ts-gyb] evaluating: \(javaScriptString)")
webView?.evaluateJavaScript(javaScriptString) { evaluationResult, error in
guard let completion = completion else { return }
if let error = error {
completion(.failure(error))
return
}
completion(.success(()))
}
}
public func insertContent(content: String, newLine: Bool?, completion: @escaping (Result<InsertContentResult, Error>) -> Void) {
struct Args: Encodable {
let content: String
let newLine: Bool?
}
let args = Args(
content: content,
newLine: newLine
)
let argsString = String(data: try! jsonEncoder.encode(args), encoding: .utf8)!
let javaScriptString = "editor.insertContent" + "(" + "\(argsString)" + ")"
print("[ts-gyb] evaluating: \(javaScriptString)")
webView?.evaluateJavaScript(javaScriptString) { [unowned self]evaluationResult, error in
if let error = error {
completion(.failure(error))
return
}
let data = try! JSONSerialization.data(withJSONObject: evaluationResult!)
let result = try! self.jsonDecoder.decode(InsertContentResult.self, from: data)
completion(.success(result))
}
}
}
public struct InsertContentResult: Codable {
public var html: String
public init(html: String) {
self.html = html
}
}