Skip to content

Commit

Permalink
Google Translate implementation #78: Small font size. Mini version fo…
Browse files Browse the repository at this point in the history
…r iOS.
  • Loading branch information
filimo committed Mar 5, 2020
1 parent fb69383 commit 2947453
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 16 deletions.
22 changes: 22 additions & 0 deletions ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
F033F41523A75B5600605325 /* SentencesView_Row.swift in Sources */ = {isa = PBXBuildFile; fileRef = F033F41423A75B5600605325 /* SentencesView_Row.swift */; };
F0369E3223A2D92C00C33139 /* StatusBarView_Sync.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0369E3123A2D92C00C33139 /* StatusBarView_Sync.swift */; };
F0369E3323A2D92C00C33139 /* StatusBarView_Sync.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0369E3123A2D92C00C33139 /* StatusBarView_Sync.swift */; };
F038B00B241112D8006B8C1F /* gtranslator.js in Resources */ = {isa = PBXBuildFile; fileRef = F038B00A241112D8006B8C1F /* gtranslator.js */; };
F038B00E2411337C006B8C1F /* gtranslator.js in Resources */ = {isa = PBXBuildFile; fileRef = F038B00C24113331006B8C1F /* gtranslator.js */; };
F038B00F24113388006B8C1F /* gtranslator.js in Resources */ = {isa = PBXBuildFile; fileRef = F038B00C24113331006B8C1F /* gtranslator.js */; };
F038B01724116964006B8C1F /* GTranslatorMiniView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F038B01624116964006B8C1F /* GTranslatorMiniView.swift */; };
F040512B23ABDBDB00A5240C /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0C36A9C2359C78A001E396C /* Array.swift */; };
F040D5782336A77D004567B8 /* UserDefault.swift in Sources */ = {isa = PBXBuildFile; fileRef = F040D5772336A77D004567B8 /* UserDefault.swift */; };
F040D57C2336BD97004567B8 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = F040D57B2336BD97004567B8 /* README.md */; };
Expand Down Expand Up @@ -604,6 +608,9 @@
F033F41123A6D3A800605325 /* DirectoryObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectoryObserver.swift; sourceTree = "<group>"; };
F033F41423A75B5600605325 /* SentencesView_Row.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentencesView_Row.swift; sourceTree = "<group>"; };
F0369E3123A2D92C00C33139 /* StatusBarView_Sync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarView_Sync.swift; sourceTree = "<group>"; };
F038B00A241112D8006B8C1F /* gtranslator.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = gtranslator.js; sourceTree = "<group>"; };
F038B00C24113331006B8C1F /* gtranslator.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = gtranslator.js; sourceTree = "<group>"; };
F038B01624116964006B8C1F /* GTranslatorMiniView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GTranslatorMiniView.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 @@ -941,6 +948,14 @@
path = SentencesView;
sourceTree = "<group>";
};
F038B00724111225006B8C1F /* Scripts */ = {
isa = PBXGroup;
children = (
F038B00A241112D8006B8C1F /* gtranslator.js */,
);
path = Scripts;
sourceTree = "<group>";
};
F04924372341F2F800F3C728 /* Recovered References */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1074,6 +1089,7 @@
isa = PBXGroup;
children = (
F09459C3240930D8001EC43D /* GTranslatorView.swift */,
F038B01624116964006B8C1F /* GTranslatorMiniView.swift */,
F09459BC24093031001EC43D /* ReaderView_Pdf.swift */,
F09459D52409320E001EC43D /* MacmillanView.swift */,
F09459DB24093230001EC43D /* SafariView.swift */,
Expand All @@ -1094,6 +1110,7 @@
F09760BB240925680030F928 /* ReaderTranslatorMobile */ = {
isa = PBXGroup;
children = (
F038B00724111225006B8C1F /* Scripts */,
F09760D5240926C40030F928 /* Delegates */,
F09459BB24093020001EC43D /* Views */,
F09760C0240925680030F928 /* ContentView.swift */,
Expand Down Expand Up @@ -1335,6 +1352,7 @@
children = (
F058C7ED2396ACC0002C84F0 /* reverso-reverso-speaker.js */,
F075445923447A2800E1D88E /* reader-translator.js */,
F038B00C24113331006B8C1F /* gtranslator.js */,
F058C7F12397A180002C84F0 /* longman.json */,
F08D45BD240E9B4D00E7A08E /* cambridge.json */,
F0E5BC6623D630DF004DACE5 /* merriam-webster.json */,
Expand Down Expand Up @@ -1650,6 +1668,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F038B00F24113388006B8C1F /* gtranslator.js in Resources */,
F058C7FD2397FBCD002C84F0 /* longman.json in Resources */,
F0120382239182DC008D0B47 /* LaunchScreen.storyboard in Resources */,
F012037F239182DC008D0B47 /* Preview Assets.xcassets in Resources */,
Expand All @@ -1662,6 +1681,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F038B00E2411337C006B8C1F /* gtranslator.js in Resources */,
F08D45BE240E9CDC00E7A08E /* cambridge.json in Resources */,
F058C7F22397A94D002C84F0 /* longman.json in Resources */,
F0C36AA7235ADF5E001E396C /* reader-translator.js in Resources */,
Expand Down Expand Up @@ -1690,6 +1710,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F038B00B241112D8006B8C1F /* gtranslator.js in Resources */,
F09760C92409256C0030F928 /* LaunchScreen.storyboard in Resources */,
F09760C62409256C0030F928 /* Preview Assets.xcassets in Resources */,
F09459A924092E0E001EC43D /* reverso-reverso-speaker.js in Resources */,
Expand Down Expand Up @@ -2098,6 +2119,7 @@
F09760C1240925680030F928 /* ContentView.swift in Sources */,
F09760E22409280E0030F928 /* String.swift in Sources */,
F094597D24092D1B001EC43D /* StackView.swift in Sources */,
F038B01724116964006B8C1F /* GTranslatorMiniView.swift in Sources */,
F09459CA240931AC001EC43D /* WikipediaView.swift in Sources */,
F09459D0240931E2001EC43D /* ReversoView.swift in Sources */,
F094599424092D7E001EC43D /* NetBrowser.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import WebKit

struct GTranslatorRepresenter: ViewRepresentable, WKScriptsSetup {
@Binding var selectedText: TranslateAction
static var isMiniMode: Bool = false {
didSet { setMiniMode() }
}

static var coorinator: Coordinator?
static var pageView: WKPageView?
Expand Down Expand Up @@ -53,6 +56,8 @@ struct GTranslatorRepresenter: ViewRepresentable, WKScriptsSetup {

setupScriptCoordinator(view: view, coordinator: context.coordinator)
setupScript(view: view, file: "gtranslator-reverso-speaker")
setupScript(view: view, file: "gtranslator")
Self.setMiniMode()

return view
}
Expand Down Expand Up @@ -93,6 +98,11 @@ struct GTranslatorRepresenter: ViewRepresentable, WKScriptsSetup {

return (slValue, tlValue)
}

static private func setMiniMode() {
let mode = isMiniMode ? "mini" : "full"
GTranslatorRepresenter.pageView?.evaluateJavaScript("readerTranslatorMode('\(mode)')")
}
}

extension GTranslatorRepresenter.Coordinator: WKScriptMessageHandler {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function() {
const css = " \
#source { font-size: 18px!important; } \
.translation { font-size: 18px!important; } \
"

var style = document.createElement('style')
style.innerHTML = css
document.head.appendChild(style)
})()
2 changes: 1 addition & 1 deletion ReaderTranslatorMac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.11.0</string>
<string>1.11.1</string>
<key>CFBundleVersion</key>
<string>1800</string>
<key>LSApplicationCategoryType</key>
Expand Down
24 changes: 21 additions & 3 deletions ReaderTranslatorMobile/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,37 @@

import SwiftUI

struct LazyView<Content: View>: View {
let build: () -> Content
init(_ build: @autoclosure @escaping () -> Content) {
self.build = build
}

var body: Content {
build()
}
}

struct ContentView: View {
@State var isShow = false

init() {
ViewsStore.shared.enabledViews = [.gTranslator, .web]
}

var body: some View {
NavigationView {
VStack {
NavigationLink(destination: GTranslatorView(), isActive: $isShow) { EmptyView() }
NavigationLink(
destination: GTranslatorView(),
isActive: $isShow
) {
EmptyView()
}
WebView()
if !isShow { GTranslatorMiniView().frame(height: 200) }
}
.edgesIgnoringSafeArea(.bottom)
.navigationBarTitle("ReaderTranslator", displayMode: .inline)
.navigationBarItems(trailing: Button("Translate") { self.isShow = true })
}
Expand Down
22 changes: 22 additions & 0 deletions ReaderTranslatorMobile/Scripts/gtranslator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(function() {
const css = " \
.reader-mini #gb, .reader-mini .tlid-input, .reader-mini .gp-footer { display: none; } \
#source { font-size: 18px!important; } \
.translation { font-size: 18px!important; } \
"

let style = document.createElement('style')
style.innerHTML = css
document.head.appendChild(style)

let mode = localStorage.getItem('readerTranslatorMode') || 'full'
readerTranslatorMode(mode)
})()

function readerTranslatorMode(mode) {
localStorage.setItem('readerTranslatorMode', mode)
let elm = document.querySelector('body')
elm.className = elm.className.replace('reader-mini', '')

if(mode=='mini') { elm.className += " reader-mini" }
}
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/BookmarksView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct BookmarksView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/CollinsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct CollinsView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
26 changes: 26 additions & 0 deletions ReaderTranslatorMobile/Views/GTranslatorMiniView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// GTranslatorView.swift
// ReaderTranslatorMobile
//
// Created by Viktor Kushnerov on 28/2/20.
// Copyright © 2020 Viktor Kushnerov. All rights reserved.
//

import SwiftUI

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

var body: some View {
GTranslatorRepresenter(selectedText: self.$store.translateAction)
.onAppear {
GTranslatorRepresenter.isMiniMode = true
}
}
}

struct GTranslatorMiniView_Previews: PreviewProvider {
static var previews: some View {
GTranslatorMiniView()
}
}
3 changes: 3 additions & 0 deletions ReaderTranslatorMobile/Views/GTranslatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ struct GTranslatorView: View {

var body: some View {
GTranslatorRepresenter(selectedText: self.$store.translateAction)
.onAppear {
GTranslatorRepresenter.isMiniMode = false
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/LongmanView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct LongmanView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/MacmillanView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct MacmillanView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/MerriamWebsterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct MerriamWebsterView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/ReaderView_Pdf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct ReaderView_Pdf: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/ReaderView_Web.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct ReaderView_Web: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/ReversoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct ReversoView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/SafariView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct SafariView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/StackExchangeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct StackExchangeView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/WikipediaView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct WikipediaView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMobile/Views/YTranslatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct YTranslatorView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
Text("Not implemented")
}
}

Expand Down
Binary file added files/Release_1.11.1_1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2947453

Please sign in to comment.