Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Commit

Permalink
Added in-app dictionary #9
Browse files Browse the repository at this point in the history
Define menu option is only available when one word is selected.
  • Loading branch information
kjantzer committed Jan 13, 2016
1 parent 722ae80 commit a961ed3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Source/FolioReaderConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class FolioReaderConfig: NSObject {
public var localizedHighlightsTitle: String
public var localizedHighlightsDateFormat: String
public var localizedHighlightMenu: String
public var localizedDefineMenu: String
public var localizedPlayMenu: String
public var localizedPauseMenu: String
public var localizedFontMenuNight: String
Expand Down Expand Up @@ -63,9 +64,10 @@ public class FolioReaderConfig: NSObject {

self.localizedHighlightsTitle = NSLocalizedString("Highlights", comment: "")
self.localizedHighlightsDateFormat = "MMM dd, YYYY | HH:mm"
self.localizedHighlightMenu = NSLocalizedString("Highlight", comment: "")
self.localizedPlayMenu = NSLocalizedString("Start Reading", comment: "")
self.localizedPauseMenu = NSLocalizedString("Stop Reading", comment: "")
self.localizedHighlightMenu = NSLocalizedString("Highlight", comment: "")
self.localizedDefineMenu = NSLocalizedString("Define", comment: "")
self.localizedFontMenuNight = NSLocalizedString("Night", comment: "")
self.localizedFontMenuDay = NSLocalizedString("Day", comment: "")
self.localizedReaderOnePageLeft = NSLocalizedString("1 page left", comment: "")
Expand Down
16 changes: 15 additions & 1 deletion Source/FolioReaderPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ extension UIWebView {

// default menu
} else {

if action == "highlight:"
|| (action == "define:" && (js("getSelectedText()"))!.componentsSeparatedByString(" ").count == 1)
|| (action == "play:" && book.hasAudio() )
|| (action == "share:" && readerConfig.allowSharing == true)
|| (action == "copy:" && readerConfig.allowSharing == true) {
Expand Down Expand Up @@ -403,6 +405,17 @@ extension UIWebView {
}
}

func define(sender: UIMenuController?) {
let selectedText = js("getSelectedText()")

setMenuVisible(false)
userInteractionEnabled = false
userInteractionEnabled = true

let vc = UIReferenceLibraryViewController(term: selectedText! )
vc.view.tintColor = readerConfig.toolBarBackgroundColor
FolioReader.sharedInstance.readerContainer.showViewController(vc, sender: nil)
}

func play(sender: UIMenuController?) {

Expand Down Expand Up @@ -468,6 +481,7 @@ extension UIWebView {

let highlightItem = UIMenuItem(title: readerConfig.localizedHighlightMenu, action: "highlight:")
let playAudioItem = UIMenuItem(title: readerConfig.localizedPlayMenu, action: "play:")
let defineItem = UIMenuItem(title: readerConfig.localizedDefineMenu, action: "define:")
let colorsItem = UIMenuItem(title: "C", image: colors!, action: "colors:")
let shareItem = UIMenuItem(title: "S", image: share!, action: "share:")
let removeItem = UIMenuItem(title: "R", image: remove!, action: "remove:")
Expand All @@ -477,7 +491,7 @@ extension UIWebView {
let pinkItem = UIMenuItem(title: "P", image: pink!, action: "setPink:")
let underlineItem = UIMenuItem(title: "U", image: underline!, action: "setUnderline:")

let menuItems = [playAudioItem, highlightItem, colorsItem, removeItem, yellowItem, greenItem, blueItem, pinkItem, underlineItem, shareItem]
let menuItems = [playAudioItem, highlightItem, defineItem, colorsItem, removeItem, yellowItem, greenItem, blueItem, pinkItem, underlineItem, shareItem]

UIMenuController.sharedMenuController().menuItems = menuItems
}
Expand Down

0 comments on commit a961ed3

Please sign in to comment.