This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
/
RichEditorView.swift
557 lines (445 loc) · 18.2 KB
/
RichEditorView.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//
// RichEditor.swift
//
// Created by Caesar Wirth on 4/1/15.
// Copyright (c) 2015 Caesar Wirth. All rights reserved.
//
import UIKit
/// RichEditorDelegate defines callbacks for the delegate of the RichEditorView
@objc public protocol RichEditorDelegate: class {
/// Called when the inner height of the text being displayed changes
/// Can be used to update the UI
@objc optional func richEditor(_ editor: RichEditorView, heightDidChange height: Int)
/// Called whenever the content inside the view changes
@objc optional func richEditor(_ editor: RichEditorView, contentDidChange content: String)
/// Called when the rich editor starts editing
@objc optional func richEditorTookFocus(_ editor: RichEditorView)
/// Called when the rich editor stops editing or loses focus
@objc optional func richEditorLostFocus(_ editor: RichEditorView)
/// Called when the RichEditorView has become ready to receive input
/// More concretely, is called when the internal UIWebView loads for the first time, and contentHTML is set
@objc optional func richEditorDidLoad(_ editor: RichEditorView)
/// Called when the internal UIWebView begins loading a URL that it does not know how to respond to
/// For example, if there is an external link, and then the user taps it
@objc optional func richEditor(_ editor: RichEditorView, shouldInteractWith url: URL) -> Bool
/// Called when custom actions are called by callbacks in the JS
/// By default, this method is not used unless called by some custom JS that you add
@objc optional func richEditor(_ editor: RichEditorView, handle action: String)
}
/// RichEditorView is a UIView that displays richly styled text, and allows it to be edited in a WYSIWYG fashion.
@objcMembers open class RichEditorView: UIView, UIScrollViewDelegate, UIWebViewDelegate, UIGestureRecognizerDelegate {
// MARK: Public Properties
/// The delegate that will receive callbacks when certain actions are completed.
open weak var delegate: RichEditorDelegate?
/// Input accessory view to display over they keyboard.
/// Defaults to nil
open override var inputAccessoryView: UIView? {
get { return webView.cjw_inputAccessoryView }
set { webView.cjw_inputAccessoryView = newValue }
}
/// The internal UIWebView that is used to display the text.
open private(set) var webView: UIWebView
/// Whether or not scroll is enabled on the view.
open var isScrollEnabled: Bool = true {
didSet {
webView.scrollView.isScrollEnabled = isScrollEnabled
}
}
/// Whether or not to allow user input in the view.
open var isEditingEnabled: Bool {
get { return isContentEditable }
set { isContentEditable = newValue }
}
/// The content HTML of the text being displayed.
/// Is continually updated as the text is being edited.
open private(set) var contentHTML: String = "" {
didSet {
delegate?.richEditor?(self, contentDidChange: contentHTML)
}
}
/// The internal height of the text being displayed.
/// Is continually being updated as the text is edited.
open private(set) var editorHeight: Int = 0 {
didSet {
delegate?.richEditor?(self, heightDidChange: editorHeight)
}
}
/// The value we hold in order to be able to set the line height before the JS completely loads.
private var innerLineHeight: Int = 28
/// The line height of the editor. Defaults to 28.
open private(set) var lineHeight: Int {
get {
if isEditorLoaded, let lineHeight = Int(runJS("RE.getLineHeight();")) {
return lineHeight
} else {
return innerLineHeight
}
}
set {
innerLineHeight = newValue
runJS("RE.setLineHeight('\(innerLineHeight)px');")
}
}
// MARK: Private Properties
/// Whether or not the editor has finished loading or not yet.
private var isEditorLoaded = false
/// Value that stores whether or not the content should be editable when the editor is loaded.
/// Is basically `isEditingEnabled` before the editor is loaded.
private var editingEnabledVar = true
/// The private internal tap gesture recognizer used to detect taps and focus the editor
private let tapRecognizer = UITapGestureRecognizer()
/// The inner height of the editor div.
/// Fetches it from JS every time, so might be slow!
private var clientHeight: Int {
let heightString = runJS("document.getElementById('editor').clientHeight;")
return Int(heightString) ?? 0
}
// MARK: Initialization
public override init(frame: CGRect) {
webView = UIWebView()
super.init(frame: frame)
setup()
}
required public init?(coder aDecoder: NSCoder) {
webView = UIWebView()
super.init(coder: aDecoder)
setup()
}
private func setup() {
backgroundColor = .red
webView.frame = bounds
webView.delegate = self
webView.keyboardDisplayRequiresUserAction = false
webView.scalesPageToFit = false
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.dataDetectorTypes = UIDataDetectorTypes()
webView.backgroundColor = .white
webView.scrollView.isScrollEnabled = isScrollEnabled
webView.scrollView.bounces = false
webView.scrollView.delegate = self
webView.scrollView.clipsToBounds = false
webView.cjw_inputAccessoryView = nil
self.addSubview(webView)
if let filePath = Bundle(for: RichEditorView.self).path(forResource: "rich_editor", ofType: "html") {
let url = URL(fileURLWithPath: filePath, isDirectory: false)
let request = URLRequest(url: url)
webView.loadRequest(request)
}
tapRecognizer.addTarget(self, action: #selector(viewWasTapped))
tapRecognizer.delegate = self
addGestureRecognizer(tapRecognizer)
}
// MARK: - Rich Text Editing
// MARK: Properties
/// The HTML that is currently loaded in the editor view, if it is loaded. If it has not been loaded yet, it is the
/// HTML that will be loaded into the editor view once it finishes initializing.
public var html: String {
get {
return runJS("RE.getHtml();")
}
set {
contentHTML = newValue
if isEditorLoaded {
runJS("RE.setHtml('\(newValue.escaped)');")
updateHeight()
}
}
}
/// Text representation of the data that has been input into the editor view, if it has been loaded.
public var text: String {
return runJS("RE.getText()")
}
/// Private variable that holds the placeholder text, so you can set the placeholder before the editor loads.
private var placeholderText: String = ""
/// The placeholder text that should be shown when there is no user input.
open var placeholder: String {
get { return placeholderText }
set {
placeholderText = newValue
runJS("RE.setPlaceholderText('\(newValue.escaped)');")
}
}
/// The href of the current selection, if the current selection's parent is an anchor tag.
/// Will be nil if there is no href, or it is an empty string.
public var selectedHref: String? {
if !hasRangeSelection { return nil }
let href = runJS("RE.getSelectedHref();")
if href == "" {
return nil
} else {
return href
}
}
/// Whether or not the selection has a type specifically of "Range".
public var hasRangeSelection: Bool {
return runJS("RE.rangeSelectionExists();") == "true" ? true : false
}
/// Whether or not the selection has a type specifically of "Range" or "Caret".
public var hasRangeOrCaretSelection: Bool {
return runJS("RE.rangeOrCaretSelectionExists();") == "true" ? true : false
}
// MARK: Methods
public func removeFormat() {
runJS("RE.removeFormat();")
}
public func setFontSize(_ size: Int) {
runJS("RE.setFontSize('\(size)px');")
}
public func setEditorBackgroundColor(_ color: UIColor) {
runJS("RE.setBackgroundColor('\(color.hex)');")
}
public func undo() {
runJS("RE.undo();")
}
public func redo() {
runJS("RE.redo();")
}
public func bold() {
runJS("RE.setBold();")
}
public func italic() {
runJS("RE.setItalic();")
}
// "superscript" is a keyword
public func subscriptText() {
runJS("RE.setSubscript();")
}
public func superscript() {
runJS("RE.setSuperscript();")
}
public func strikethrough() {
runJS("RE.setStrikeThrough();")
}
public func underline() {
runJS("RE.setUnderline();")
}
public func setTextColor(_ color: UIColor) {
runJS("RE.prepareInsert();")
runJS("RE.setTextColor('\(color.hex)');")
}
public func setEditorFontColor(_ color: UIColor) {
runJS("RE.setBaseTextColor('\(color.hex)');")
}
public func setTextBackgroundColor(_ color: UIColor) {
runJS("RE.prepareInsert();")
runJS("RE.setTextBackgroundColor('\(color.hex)');")
}
public func header(_ h: Int) {
runJS("RE.setHeading('\(h)');")
}
public func indent() {
runJS("RE.setIndent();")
}
public func outdent() {
runJS("RE.setOutdent();")
}
public func orderedList() {
runJS("RE.setOrderedList();")
}
public func unorderedList() {
runJS("RE.setUnorderedList();")
}
public func blockquote() {
runJS("RE.setBlockquote()");
}
public func alignLeft() {
runJS("RE.setJustifyLeft();")
}
public func alignCenter() {
runJS("RE.setJustifyCenter();")
}
public func alignRight() {
runJS("RE.setJustifyRight();")
}
public func insertImage(_ url: String, alt: String) {
runJS("RE.prepareInsert();")
runJS("RE.insertImage('\(url.escaped)', '\(alt.escaped)');")
}
public func insertLink(_ href: String, title: String) {
runJS("RE.prepareInsert();")
runJS("RE.insertLink('\(href.escaped)', '\(title.escaped)');")
}
public func focus() {
runJS("RE.focus();")
}
public func focus(at: CGPoint) {
runJS("RE.focusAtPoint(\(at.x), \(at.y));")
}
public func blur() {
runJS("RE.blurFocus()")
}
/// Runs some JavaScript on the UIWebView and returns the result
/// If there is no result, returns an empty string
/// - parameter js: The JavaScript string to be run
/// - returns: The result of the JavaScript that was run
@discardableResult
public func runJS(_ js: String) -> String {
let string = webView.stringByEvaluatingJavaScript(from: js) ?? ""
return string
}
// MARK: - Delegate Methods
// MARK: UIScrollViewDelegate
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
// We use this to keep the scroll view from changing its offset when the keyboard comes up
if !isScrollEnabled {
scrollView.bounds = webView.bounds
}
}
// MARK: UIWebViewDelegate
public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
// Handle pre-defined editor actions
let callbackPrefix = "re-callback://"
if request.url?.absoluteString.hasPrefix(callbackPrefix) == true {
// When we get a callback, we need to fetch the command queue to run the commands
// It comes in as a JSON array of commands that we need to parse
let commands = runJS("RE.getCommandQueue();")
if let data = commands.data(using: .utf8) {
let jsonCommands: [String]
do {
jsonCommands = try JSONSerialization.jsonObject(with: data) as? [String] ?? []
} catch {
jsonCommands = []
NSLog("RichEditorView: Failed to parse JSON Commands")
}
jsonCommands.forEach(performCommand)
}
return false
}
// User is tapping on a link, so we should react accordingly
if navigationType == .linkClicked {
if let
url = request.url,
let shouldInteract = delegate?.richEditor?(self, shouldInteractWith: url)
{
return shouldInteract
}
}
return true
}
// MARK: UIGestureRecognizerDelegate
/// Delegate method for our UITapGestureDelegate.
/// Since the internal web view also has gesture recognizers, we have to make sure that we actually receive our taps.
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
// MARK: - Private Implementation Details
private var isContentEditable: Bool {
get {
if isEditorLoaded {
let value = runJS("RE.editor.isContentEditable")
editingEnabledVar = Bool(value) ?? false
return editingEnabledVar
}
return editingEnabledVar
}
set {
editingEnabledVar = newValue
if isEditorLoaded {
let value = newValue ? "true" : "false"
runJS("RE.editor.contentEditable = \(value);")
}
}
}
/// The position of the caret relative to the currently shown content.
/// For example, if the cursor is directly at the top of what is visible, it will return 0.
/// This also means that it will be negative if it is above what is currently visible.
/// Can also return 0 if some sort of error occurs between JS and here.
private var relativeCaretYPosition: Int {
let string = runJS("RE.getRelativeCaretYPosition();")
return Int(string) ?? 0
}
private func updateHeight() {
let heightString = runJS("document.getElementById('editor').clientHeight;")
let height = Int(heightString) ?? 0
if editorHeight != height {
editorHeight = height
}
}
/// Scrolls the editor to a position where the caret is visible.
/// Called repeatedly to make sure the caret is always visible when inputting text.
/// Works only if the `lineHeight` of the editor is available.
private func scrollCaretToVisible() {
let scrollView = self.webView.scrollView
let contentHeight = clientHeight > 0 ? CGFloat(clientHeight) : scrollView.frame.height
scrollView.contentSize = CGSize(width: scrollView.frame.width, height: contentHeight)
// XXX: Maybe find a better way to get the cursor height
let lineHeight = CGFloat(self.lineHeight)
let cursorHeight = lineHeight - 4
let visiblePosition = CGFloat(relativeCaretYPosition)
var offset: CGPoint?
if visiblePosition + cursorHeight > scrollView.bounds.size.height {
// Visible caret position goes further than our bounds
offset = CGPoint(x: 0, y: (visiblePosition + lineHeight) - scrollView.bounds.height + scrollView.contentOffset.y)
} else if visiblePosition < 0 {
// Visible caret position is above what is currently visible
var amount = scrollView.contentOffset.y + visiblePosition
amount = amount < 0 ? 0 : amount
offset = CGPoint(x: scrollView.contentOffset.x, y: amount)
}
if let offset = offset {
scrollView.setContentOffset(offset, animated: true)
}
}
/// Called when actions are received from JavaScript
/// - parameter method: String with the name of the method and optional parameters that were passed in
private func performCommand(_ method: String) {
if method.hasPrefix("ready") {
// If loading for the first time, we have to set the content HTML to be displayed
if !isEditorLoaded {
isEditorLoaded = true
html = contentHTML
isContentEditable = editingEnabledVar
placeholder = placeholderText
lineHeight = innerLineHeight
delegate?.richEditorDidLoad?(self)
}
updateHeight()
}
else if method.hasPrefix("input") {
scrollCaretToVisible()
let content = runJS("RE.getHtml()")
contentHTML = content
updateHeight()
}
else if method.hasPrefix("updateHeight") {
updateHeight()
}
else if method.hasPrefix("focus") {
delegate?.richEditorTookFocus?(self)
}
else if method.hasPrefix("blur") {
delegate?.richEditorLostFocus?(self)
}
else if method.hasPrefix("action/") {
let content = runJS("RE.getHtml()")
contentHTML = content
// If there are any custom actions being called
// We need to tell the delegate about it
let actionPrefix = "action/"
let range = method.range(of: actionPrefix)!
let action = method.replacingCharacters(in: range, with: "")
delegate?.richEditor?(self, handle: action)
}
}
// MARK: - Responder Handling
/// Called by the UITapGestureRecognizer when the user taps the view.
/// If we are not already the first responder, focus the editor.
@objc private func viewWasTapped() {
if !webView.containsFirstResponder {
let point = tapRecognizer.location(in: webView)
focus(at: point)
}
}
override open func becomeFirstResponder() -> Bool {
if !webView.containsFirstResponder {
focus()
return true
} else {
return false
}
}
open override func resignFirstResponder() -> Bool {
blur()
return true
}
}