Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
- cleanup code
- resolve js to swift bool conversion
  • Loading branch information
cbess committed Nov 1, 2020
1 parent f0ca930 commit 1c058f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
8 changes: 6 additions & 2 deletions RichEditorView/Classes/RichEditorOptionItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ public struct RichEditorOptionItem: RichEditorOption {
/// The action to be performed when tapped
public var handler: ((RichEditorToolbar) -> Void)

public init(image: UIImage?, title: String, action: @escaping ((RichEditorToolbar) -> Void)) {
public init(image: UIImage? = nil, title: String, action: @escaping ((RichEditorToolbar) -> Void)) {
self.image = image
self.title = title
self.handler = action
}

public init(title: String, action: @escaping ((RichEditorToolbar) -> Void)) {
self.init(image: nil, title: title, action: action)
}

// MARK: RichEditorOption

public func action(_ toolbar: RichEditorToolbar) {
Expand Down Expand Up @@ -114,7 +118,7 @@ public enum RichEditorDefaultOption: RichEditorOption {
}

let bundle = Bundle(for: RichEditorToolbar.self)
return UIImage(named: name, in: bundle, compatibleWith: nil)
return UIImage(named: name, in: bundle, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate)
}

public var title: String {
Expand Down
8 changes: 4 additions & 4 deletions RichEditorView/Classes/RichEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ private let DefaultInnerLineHeight: Int = 21

/// Whether or not the selection has a type specifically of "Range".
public func hasRangeSelection(handler: @escaping (Bool) -> Void) {
runJS("RE.rangeSelectionExists()") { r in
handler(r == "true" ? true : false)
runJS("RE.rangeSelectionExists()") { val in
handler((val as NSString).boolValue)
}
}

/// Whether or not the selection has a type specifically of "Range" or "Caret".
public func hasRangeOrCaretSelection(handler: @escaping (Bool) -> Void) {
runJS("RE.rangeOrCaretSelectionExists()") { r in
handler(r == "true" ? true : false)
runJS("RE.rangeOrCaretSelectionExists()") { val in
handler((val as NSString).boolValue)
}
}

Expand Down
29 changes: 19 additions & 10 deletions RichEditorViewSample/RichEditorViewSample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class ViewController: UIViewController {

lazy var toolbar: RichEditorToolbar = {
let toolbar = RichEditorToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 44))
// let options: [RichEditorDefaultOption] = [
// .bold, .italic, .underline,
// .unorderedList, .orderedList,
// .indent, .outdent,
// .textColor, .textBackgroundColor,
// .undo, .redo,
// ]
toolbar.options = RichEditorDefaultOption.all
return toolbar
}()
Expand All @@ -32,13 +39,13 @@ class ViewController: UIViewController {
toolbar.editor = editorView

// This will create a custom action that clears all the input text when it is pressed
// let item = RichEditorOptionItem(image: nil, title: "Clear") { toolbar in
// toolbar?.editor?.html = ""
// }
//
// var options = toolbar.options
// options.append(item)
// toolbar.options = options
let item = RichEditorOptionItem(title: "Clear") { toolbar in
toolbar.editor?.html = ""
}

var options = toolbar.options
options.append(item)
toolbar.options = options
}

}
Expand Down Expand Up @@ -113,9 +120,11 @@ extension ViewController: RichEditorToolbarDelegate, UIColorPickerViewController

func richEditorToolbarInsertLink(_ toolbar: RichEditorToolbar) {
// Can only add links to selected text, so make sure there is a range selection first
// if let hasSelection = toolbar.editor?.rangeSelectionExists(), hasSelection {
// toolbar.editor?.insertLink("https://github.com/cbess/RichEditorView", title: "GitHub Link")
// }
toolbar.editor?.hasRangeSelection(handler: { (hasSelection) in
if hasSelection {
self.toolbar.editor?.insertLink("https://github.com/cbess/RichEditorView", title: "GitHub Link")
}
})
}

func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
Expand Down

0 comments on commit 1c058f5

Please sign in to comment.