Skip to content

Commit

Permalink
Implement Reverso Context #19
Browse files Browse the repository at this point in the history
Repeat selected text by [none, Ctrl, Alt]-r key in ReversoContext
  • Loading branch information
filimo committed Oct 10, 2019
1 parent d8db648 commit 7531254
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
16 changes: 4 additions & 12 deletions ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
F0039EC423447E24002F3F95 /* SharedContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0039EC223447E24002F3F95 /* SharedContainer.swift */; };
F022B6E3234E6663005CA0BB /* WKCoordinatorNavigationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F022B6E2234E6663005CA0BB /* WKCoordinatorNavigationDelegate.swift */; };
F022B6E4234E6663005CA0BB /* WKCoordinatorNavigationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F022B6E2234E6663005CA0BB /* WKCoordinatorNavigationDelegate.swift */; };
F026B1E82344830200E032B9 /* ReaderView_Safari.swift in Sources */ = {isa = PBXBuildFile; fileRef = F026B1E72344830200E032B9 /* ReaderView_Safari.swift */; };
F026B1E82344830200E032B9 /* SafariView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F026B1E72344830200E032B9 /* SafariView.swift */; };
F040D5782336A77D004567B8 /* UserDefault.swift in Sources */ = {isa = PBXBuildFile; fileRef = F040D5772336A77D004567B8 /* UserDefault.swift */; };
F040D57C2336BD97004567B8 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = F040D57B2336BD97004567B8 /* README.md */; };
F049244F2341F54800F3C728 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = F049244E2341F54800F3C728 /* Image.swift */; };
Expand Down Expand Up @@ -141,7 +141,7 @@
F00644462341E22D006BCA94 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = Library/Frameworks/Cocoa.framework; sourceTree = DEVELOPER_DIR; };
F01A50E823492398001DCC11 /* ReversoContextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReversoContextView.swift; sourceTree = "<group>"; };
F022B6E2234E6663005CA0BB /* WKCoordinatorNavigationDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKCoordinatorNavigationDelegate.swift; sourceTree = "<group>"; };
F026B1E72344830200E032B9 /* ReaderView_Safari.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderView_Safari.swift; sourceTree = "<group>"; };
F026B1E72344830200E032B9 /* SafariView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariView.swift; sourceTree = "<group>"; };
F03D7DFD234DF59500E53E3D /* files */ = {isa = PBXFileReference; lastKnownFileType = folder; path = files; sourceTree = "<group>"; };
F040D5772336A77D004567B8 /* UserDefault.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefault.swift; sourceTree = "<group>"; };
F040D57B2336BD97004567B8 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -381,7 +381,6 @@
F0AA69D9232E979B007CC07B /* Components */ = {
isa = PBXGroup;
children = (
F0C53176234B3E8B003174B5 /* ScriptViewRepresenter */,
F0EE0A1B2348A645004A5EAD /* PDFKitView */,
F0EE0A142347BEDB004A5EAD /* WebKit */,
F0A9F35A2349228A00970C97 /* ReversoContext.swift */,
Expand Down Expand Up @@ -432,18 +431,11 @@
F0AB12A4233F57B3005B9F2A /* ReaderView_Web.swift */,
F01A50E823492398001DCC11 /* ReversoContextView.swift */,
F0D31C5023491C23003CF86B /* TranslatorView.swift */,
F026B1E72344830200E032B9 /* ReaderView_Safari.swift */,
F026B1E72344830200E032B9 /* SafariView.swift */,
);
path = Modes;
sourceTree = "<group>";
};
F0C53176234B3E8B003174B5 /* ScriptViewRepresenter */ = {
isa = PBXGroup;
children = (
);
path = ScriptViewRepresenter;
sourceTree = "<group>";
};
F0EE0A142347BEDB004A5EAD /* WebKit */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -660,7 +652,7 @@
F0EE0A1A234897CC004A5EAD /* StatusBarView_Safari.swift in Sources */,
F0BB43712344843900ADBEF1 /* UserDefault.swift in Sources */,
F0BB437C2344845000ADBEF1 /* StatusBarView_PdfPage.swift in Sources */,
F026B1E82344830200E032B9 /* ReaderView_Safari.swift in Sources */,
F026B1E82344830200E032B9 /* SafariView.swift in Sources */,
F0C53175234B06D3003174B5 /* WKScriptsSetup.swift in Sources */,
F075443A234479DA00E1D88E /* AppDelegate.swift in Sources */,
F0039EC323447E24002F3F95 /* SharedContainer.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslator/Components/WebKit/WKCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension WKCoordinator: WKScriptMessageHandler {
print("onContextMenu")
case "onBodyLoaded":
print("onBodyLoaded")
case "onKeyPress":
case "onKeyDown":
if let code = message.body as? String {
if code == "MetaLeft" { SpeechSynthesizer.speak(stopSpeaking: true, isVoiceEnabled: true) }
}
Expand Down
4 changes: 2 additions & 2 deletions ReaderTranslator/Components/WebKit/WKScriptsSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private let script = """
webkit.messageHandlers.onBodyLoaded.postMessage("txt")
}
document.body.onkeydown = function(event) {
webkit.messageHandlers.onKeyPress.postMessage(event.code)
webkit.messageHandlers.onKeyDown.postMessage(event.code)
}
"""

Expand All @@ -45,7 +45,7 @@ extension WKScriptsSetup {
userContentController.add(coordinator, name: "onSelectionChange")
userContentController.add(coordinator, name: "onContextMenu")
userContentController.add(coordinator, name: "onBodyLoaded")
userContentController.add(coordinator, name: "onKeyPress")
userContentController.add(coordinator, name: "onKeyDown")

#if os(macOS)
view.allowsMagnification = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import SwiftUI
import os.log

struct ReaderView_Safari: View {
struct SafariView: View {
@ObservedObject private var store = Store.shared

var body: some View {
Expand Down Expand Up @@ -41,6 +41,9 @@ struct ReaderView_Safari: View {
self.store.translateAction = .translator(event.extra?.selectedText ?? "")
}
}
if extra.keyCode == 82 { // r
SpeechSynthesizer.speak()
}
}
case "selectionchange":
if store.canSafariSendSelectedText {
Expand All @@ -57,7 +60,7 @@ struct ReaderView_Safari: View {

struct ReaderView_Safari_Previews: PreviewProvider {
static var previews: some View {
ReaderView_Safari()
SafariView()
.frame(maxWidth: 100)
.environmentObject(Store.shared)
}
Expand Down
3 changes: 2 additions & 1 deletion ReaderTranslator/Views/ReaderView/ReaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct ReaderView: View {
ReaderView_PDF()
ReaderView_Web()
#if os(macOS)
ReaderView_Safari()
SafariView()
#endif
if store.viewMode == .safari {
ReversoContextView()
Expand All @@ -35,3 +35,4 @@ struct ReaderView_Previews: PreviewProvider {
ReaderView().environmentObject(Store.shared)
}
}

0 comments on commit 7531254

Please sign in to comment.