diff --git a/Source/FolioReaderCenter.swift b/Source/FolioReaderCenter.swift
index 082442464..5597c7b26 100755
--- a/Source/FolioReaderCenter.swift
+++ b/Source/FolioReaderCenter.swift
@@ -18,11 +18,31 @@ var nextPageNumber: Int!
var pageScrollDirection = ScrollDirection()
var isScrolling = false
+/// Protocol which is used from `FolioReaderCenter`s.
+@objc public protocol FolioReaderCenterDelegate: class {
+
+ /**
+ Notifies that a page appeared. This is triggered is a page is chosen and displayed.
+
+ - parameter page: The appeared page
+ */
+ optional func pageDidAppear(page: FolioReaderPage)
+
+ /**
+ Passes and returns the HTML content as `String`. Implement this method if you want to modify the HTML content of a `FolioReaderPage`.
+
+ - parameter page: The `FolioReaderPage`
+ - parameter htmlContent: The current HTML content as `String`
+
+ - returns: The adjusted HTML content as `String`. This is the content which will be loaded into the given `FolioReaderPage`
+ */
+ optional func htmlContentForPage(page: FolioReaderPage, htmlContent: String) -> String
+}
public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
/// This delegate receives the events from the current `FolioReaderPage`s delegate.
- public weak var pageDelegate: FolioReaderPageDelegate?
+ public weak var delegate: FolioReaderCenterDelegate?
var collectionView: UICollectionView!
let collectionViewLayout = UICollectionViewFlowLayout()
@@ -344,38 +364,44 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
// Configure the cell
let resource = book.spine.spineReferences[indexPath.row].resource
- var html = try? String(contentsOfFile: resource.fullHref, encoding: NSUTF8StringEncoding)
- let mediaOverlayStyleColors = "\"\(readerConfig.mediaOverlayColor.hexString(false))\", \"\(readerConfig.mediaOverlayColor.highlightColor().hexString(false))\""
-
- // Inject CSS
- let jsFilePath = NSBundle.frameworkBundle().pathForResource("Bridge", ofType: "js")
- let cssFilePath = NSBundle.frameworkBundle().pathForResource("Style", ofType: "css")
- let cssTag = ""
- let jsTag = "" +
- ""
-
- let toInject = "\n\(cssTag)\n\(jsTag)\n"
- html = html?.stringByReplacingOccurrencesOfString("", withString: toInject)
-
- // Font class name
- var classes = FolioReader.currentFont.cssIdentifier
- classes += " "+FolioReader.currentMediaOverlayStyle.className()
-
- // Night mode
- if FolioReader.nightMode {
- classes += " nightMode"
- }
-
- // Font Size
- classes += " \(FolioReader.currentFontSize.cssIdentifier)"
-
- html = html?.stringByReplacingOccurrencesOfString(""
+ let jsTag = "" +
+ ""
+
+ let toInject = "\n\(cssTag)\n\(jsTag)\n"
+ html = html.stringByReplacingOccurrencesOfString("", withString: toInject)
+
+ // Font class name
+ var classes = FolioReader.currentFont.cssIdentifier
+ classes += " "+FolioReader.currentMediaOverlayStyle.className()
+
+ // Night mode
+ if FolioReader.nightMode {
+ classes += " nightMode"
+ }
+
+ // Font Size
+ classes += " \(FolioReader.currentFontSize.cssIdentifier)"
+
+ html = html.stringByReplacingOccurrencesOfString(" CGSize {
return CGSizeMake(collectionView.frame.width, collectionView.frame.height)
}
@@ -503,10 +529,8 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
currentPageNumber = page.pageNumber
} else {
let currentIndexPath = getCurrentIndexPath()
- if currentIndexPath != NSIndexPath(forRow: 0, inSection: 0) {
- currentPage = collectionView.cellForItemAtIndexPath(currentIndexPath) as? FolioReaderPage
- }
-
+ currentPage = collectionView.cellForItemAtIndexPath(currentIndexPath) as? FolioReaderPage
+
previousPageNumber = currentIndexPath.row
currentPageNumber = currentIndexPath.row+1
}
@@ -532,7 +556,11 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
}
pagesForCurrentPage(page)
}
-
+
+ if let currentPage = currentPage {
+ self.delegate?.pageDidAppear?(currentPage)
+ }
+
completion?()
}
@@ -1091,15 +1119,13 @@ extension FolioReaderCenter: FolioReaderPageDelegate {
let offsetPoint = self.currentWebViewScrollPositions[page.pageNumber - 1] {
page.webView.scrollView.setContentOffset(offsetPoint, animated: false)
}
-
- self.pageDelegate?.pageDidLoad(page)
}
}
// MARK: FolioReaderChapterListDelegate
extension FolioReaderCenter: FolioReaderChapterListDelegate {
-
+
func chapterList(chapterList: FolioReaderChapterList, didSelectRowAtIndexPath indexPath: NSIndexPath, withTocReference reference: FRTocReference) {
let item = findPageByResource(reference)
diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift
index eb566ee18..f89a13188 100755
--- a/Source/FolioReaderPage.swift
+++ b/Source/FolioReaderPage.swift
@@ -12,7 +12,7 @@ import UIMenuItem_CXAImageSupport
import JSQWebViewController
/// Protocol which is used from `FolioReaderPage`s.
-public protocol FolioReaderPageDelegate: class {
+protocol FolioReaderPageDelegate: class {
/**
Notify that page did loaded
@@ -98,35 +98,41 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture
)
}
- func loadHTMLString(string: String!, baseURL: NSURL!) {
-
- var html = (string as NSString)
-
- // Restore highlights
- let highlights = Highlight.allByBookId((kBookId as NSString).stringByDeletingPathExtension, andPage: pageNumber)
-
- if highlights.count > 0 {
- for item in highlights {
- let style = HighlightStyle.classForStyle(item.type)
- let tag = "\(item.content)"
- var locator = item.contentPre + item.content + item.contentPost
- locator = Highlight.removeSentenceSpam(locator) /// Fix for Highlights
- let range: NSRange = html.rangeOfString(locator, options: .LiteralSearch)
-
- if range.location != NSNotFound {
- let newRange = NSRange(location: range.location + item.contentPre.characters.count, length: item.content.characters.count)
- html = html.stringByReplacingCharactersInRange(newRange, withString: tag)
- }
- else {
- print("highlight range not found")
- }
- }
- }
-
+ func loadHTMLString(htmlContent: String!, baseURL: NSURL!) {
+ // Insert the stored highlights to the HTML
+ var tempHtmlContent = htmlContentWithInsertHighlights(htmlContent)
+ // Load the html into the webview
webView.alpha = 0
- webView.loadHTMLString(html as String, baseURL: baseURL)
+ webView.loadHTMLString(tempHtmlContent, baseURL: baseURL)
}
-
+
+ // MARK: - Highlights
+
+ private func htmlContentWithInsertHighlights(htmlContent: String) -> String {
+ var tempHtmlContent = htmlContent as NSString
+ // Restore highlights
+ let highlights = Highlight.allByBookId((kBookId as NSString).stringByDeletingPathExtension, andPage: pageNumber)
+
+ if highlights.count > 0 {
+ for item in highlights {
+ let style = HighlightStyle.classForStyle(item.type)
+ let tag = "\(item.content)"
+ var locator = item.contentPre + item.content + item.contentPost
+ locator = Highlight.removeSentenceSpam(locator) /// Fix for Highlights
+ let range: NSRange = tempHtmlContent.rangeOfString(locator, options: .LiteralSearch)
+
+ if range.location != NSNotFound {
+ let newRange = NSRange(location: range.location + item.contentPre.characters.count, length: item.content.characters.count)
+ tempHtmlContent = tempHtmlContent.stringByReplacingCharactersInRange(newRange, withString: tag)
+ }
+ else {
+ print("highlight range not found")
+ }
+ }
+ }
+ return tempHtmlContent as String
+ }
+
// MARK: - UIWebView Delegate
public func webViewDidFinishLoad(webView: UIWebView) {