-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DeepL implemented #79: Implementation.
- Loading branch information
Showing
9 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
ReaderTranslator/Components/ViewRepresentable/DeepLRepresenter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// DeepLRepresenter.swift | ||
// ReaderTranslator | ||
// | ||
// Created by Viktor Kushnerov on 6/3/20. | ||
// Copyright © 2020 Viktor Kushnerov. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import WebKit | ||
|
||
struct DeepLRepresenter: ViewRepresentable, WKScriptsSetup { | ||
@Binding var selectedText: TranslateAction | ||
private let defaultURL = "https://www.deepl.com/ru/translator#en/ru/" | ||
|
||
static var coorinator: Coordinator? | ||
static var pageView: WKPageView? | ||
|
||
class Coordinator: WKCoordinator { | ||
var selectedText = "" | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
makeCoordinator(coordinator: Coordinator(self)) | ||
} | ||
|
||
func makeView(context: Context) -> WKPageView { | ||
if let view = Self.pageView { return view } | ||
|
||
let view = WKPageView() | ||
view.load(urlString: defaultURL) | ||
Self.pageView = view | ||
|
||
setupScriptCoordinator(view: view, coordinator: context.coordinator) | ||
|
||
return view | ||
} | ||
|
||
func updateView(_ view: WKPageView, context _: Context) { | ||
guard case var .deepL(text) = selectedText else { return } | ||
text = text.replacingOccurrences(of: "\n", with: " ") | ||
Store.shared.translateAction.next() | ||
|
||
print("\(theClassName)_updateView_update", text) | ||
|
||
let search = text.encodeUrl | ||
let urlString = "\(defaultURL)\(search)" | ||
|
||
if view.url?.absoluteString == urlString { return } | ||
|
||
if let url = URL(string: urlString) { | ||
print("\(theClassName)_updateView_reload", urlString) | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { | ||
view.load(URLRequest(url: url)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension DeepLRepresenter.Coordinator: WKScriptMessageHandler { | ||
func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) { | ||
guard let event = getEvent(data: message.body) else { return } | ||
var text: String { event.extra?.selectedText ?? "" } | ||
|
||
switch event.name { | ||
case "selectionchange": | ||
guard let text = event.extra?.selectedText else { return } | ||
selectedText = text | ||
store.translateAction.addAll(text: text, except: .deepL) | ||
case "keydown": | ||
if event.extra?.keyCode == 18 { // Alt | ||
SpeechSynthesizer.speak(text: text, stopSpeaking: true, isVoiceEnabled: true) | ||
} | ||
default: | ||
print("webkit.messageHandlers.\(event.name).postMessage() isn't found") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// DeepLView.swift | ||
// ReaderTranslator | ||
// | ||
// Created by Viktor Kushnerov on 6/3/20. | ||
// Copyright © 2020 Viktor Kushnerov. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct DeepLView: View { | ||
@ObservedObject private var store = Store.shared | ||
@ObservedObject private var viewsStore = ViewsStore.shared | ||
@State var width: CGFloat? | ||
|
||
var body: some View { | ||
WebViewContainer { | ||
DeepLRepresenter(selectedText: self.$store.translateAction) | ||
}.frame(width: viewsStore.viewWidth[.deepL] ?? ViewsStore.defaultWidth) | ||
} | ||
} | ||
|
||
struct DeepLView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
DeepLView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters