Skip to content

Commit

Permalink
copy resources to package bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Aim committed Jan 11, 2021
1 parent bca620f commit 5967667
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ let package = Package(
.target(
name: "TheRichTextEditor",
dependencies: [],
resources: [.process("main.js"), .process("main.html")]),
path: nil,
resources: [.copy("Resources/main.js"), .copy("Resources/main.html")]),
.testTarget(
name: "TheRichTextEditorTests",
dependencies: ["TheRichTextEditor"]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scale" : "1x"
},
{
"filename" : "indent.png",
"filename" : "left-indent.png",
"idiom" : "universal",
"scale" : "2x"
},
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
14 changes: 11 additions & 3 deletions Sources/TheRichTextEditor/TheRichTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public class TheRichTextEditor: UIView, WKScriptMessageHandler, WKNavigationDele
}

func setup() {
guard let scriptPath = Bundle.main.path(forResource: "main", ofType: "js"),
guard let scriptPath = Bundle.module.path(forResource: "main", ofType: "js"),
let scriptContent = try? String(contentsOfFile: scriptPath, encoding: String.Encoding.utf8),
let htmlPath = Bundle.main.path(forResource: "main", ofType: "html"),
let htmlPath = Bundle.module.path(forResource: "main", ofType: "html"),
let html = try? String(contentsOfFile: htmlPath, encoding: String.Encoding.utf8)
else {
fatalError("Unable to find javscript/html for text editor")
Expand Down Expand Up @@ -131,6 +131,15 @@ public class TheRichTextEditor: UIView, WKScriptMessageHandler, WKNavigationDele

webView.loadHTMLString(html, baseURL: Bundle.main.bundleURL)
}

public func setColors(background: UIColor, icons: UIColor) {
guard let customWebview = webView as? CustomWebview else {
return
}

customWebview.myTintColor = icons
customWebview.myBackgroundColor = background
}

public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
switch message.name {
Expand All @@ -141,7 +150,6 @@ public class TheRichTextEditor: UIView, WKScriptMessageHandler, WKNavigationDele
case TheRichTextEditor.heightDidChange:
guard let height = message.body as? CGFloat else { return }
if (height + 20 != self.height) {
print(self.height)
self.height = height + 20
delegate?.heightDidChange()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="jVD-uD-E95" customClass="AccessoryUICollectionViewCell" customModule="ShareExtension" customModuleProvider="target">
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="jVD-uD-E95" customClass="AccessoryUICollectionViewCell" customModule="TheRichTextEditor">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<collectionViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" id="aa0-hX-eT4">
Expand All @@ -30,7 +30,7 @@
</constraints>
</collectionViewCellContentView>
<connections>
<outlet property="iconImageView" destination="62T-xA-NgZ" id="piK-hT-QZa"/>
<outlet property="iconImageView" destination="62T-xA-NgZ" id="BiN-ge-7Gb"/>
</connections>
<point key="canvasLocation" x="-239" y="55"/>
</collectionViewCell>
Expand Down
35 changes: 23 additions & 12 deletions Sources/TheRichTextEditor/Views/CustomWebview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,31 @@ class CustomWebview: WKWebView {
var image: UIImage {
switch(self) {
case .bold:
return UIImage(named: "bold")!
return UIImage(named: "bold", in: .module, compatibleWith: nil)!
case .italic:
return UIImage(named: "italic")!
return UIImage(named: "italic", in: .module, compatibleWith: nil)!
case .textAlignLeft:
return UIImage(named: "alignLeft")!
return UIImage(named: "alignLeft", in: .module, compatibleWith: nil)!
case .textAlignCenter:
return UIImage(named: "alignCenter")!
return UIImage(named: "alignCenter", in: .module, compatibleWith: nil)!
case .textAlignRight:
return UIImage(named: "alignRight")!
return UIImage(named: "alignRight", in: .module, compatibleWith: nil)!
case .indent:
return UIImage(named: "indent")!
return UIImage(named: "indent", in: .module, compatibleWith: nil)!
case .outdent:
return UIImage(named: "outdent")!
return UIImage(named: "outdent", in: .module, compatibleWith: nil)!
case .clear:
return UIImage(named: "clear")!
return UIImage(named: "clear", in: .module, compatibleWith: nil)!
case .undo:
return UIImage(named: "undo")!
return UIImage(named: "undo", in: .module, compatibleWith: nil)!
case .redo:
return UIImage(named: "redo")!
return UIImage(named: "redo", in: .module, compatibleWith: nil)!
}
}
}
weak var toolbarDelegate: WebviewToolbarDelegate? = nil
var myBackgroundColor: UIColor? = nil
var myTintColor: UIColor? = nil
var enableAccessoryView = true
var accessoryView: UIView? = nil
var modifiers: [Modifier] = [.bold, .italic, .textAlignRight, .textAlignCenter, .textAlignLeft, .indent, .outdent, .clear, .undo, .redo]
Expand All @@ -100,14 +102,18 @@ class CustomWebview: WKWebView {
layout.scrollDirection = .horizontal

let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 100, height: 50), collectionViewLayout: layout)
//collectionView.backgroundColor = theme.background
if #available(iOS 13.0, *) {
collectionView.backgroundColor = myBackgroundColor ?? .systemBackground
} else {
collectionView.backgroundColor = myBackgroundColor ?? .lightGray
}
collectionView.dataSource = self
collectionView.delegate = self
collectionView.isScrollEnabled = true
collectionView.bounces = false
collectionView.showsHorizontalScrollIndicator = false

let accessoryNib = UINib(nibName: "AccessoryUICollectionViewCell", bundle: nil)
let accessoryNib = UINib(nibName: "AccessoryUICollectionViewCell", bundle: .module)
collectionView.register(accessoryNib, forCellWithReuseIdentifier: "accessoryCell")

accessoryView = collectionView
Expand All @@ -130,6 +136,11 @@ extension CustomWebview: UICollectionViewDataSource, UICollectionViewDelegate, U

let collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "accessoryCell", for: indexPath) as! AccessoryUICollectionViewCell
collectionCell.iconImageView.image = modifier.image
if #available(iOS 13.0, *) {
collectionCell.iconImageView.tintColor = myTintColor ?? .systemFill
} else {
collectionCell.iconImageView.tintColor = myTintColor ?? .darkText
}
return collectionCell
}

Expand Down

0 comments on commit 5967667

Please sign in to comment.