Skip to content

Commit

Permalink
Safari plugin: Part 1 #15 Don't send empty selected text to the app.
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Oct 5, 2019
1 parent 251bc1b commit 6bf62d4
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 81 deletions.
16 changes: 15 additions & 1 deletion ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
F0EE0A172347BF09004A5EAD /* PageWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0EE0A152347BF09004A5EAD /* PageWebView.swift */; };
F0EE0A19234897CC004A5EAD /* StatusBarView_Safari.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0EE0A18234897CC004A5EAD /* StatusBarView_Safari.swift */; };
F0EE0A1A234897CC004A5EAD /* StatusBarView_Safari.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0EE0A18234897CC004A5EAD /* StatusBarView_Safari.swift */; };
F0EE0A1D2348A651004A5EAD /* PDFKitViewRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0EE0A1C2348A651004A5EAD /* PDFKitViewRepresentable.swift */; };
F0EE0A1E2348A66E004A5EAD /* PDFKitViewRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0EE0A1C2348A651004A5EAD /* PDFKitViewRepresentable.swift */; };
F0F256BD233D309F00C9D719 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0F256BC233D309F00C9D719 /* String.swift */; };
F0F256C2233E6C4C00C9D719 /* FavoriteVoiceName.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0F256C1233E6C4C00C9D719 /* FavoriteVoiceName.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -198,6 +200,7 @@
F0EE0A0F23478C86004A5EAD /* DOMEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DOMEvent.swift; sourceTree = "<group>"; };
F0EE0A152347BF09004A5EAD /* PageWebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageWebView.swift; sourceTree = "<group>"; };
F0EE0A18234897CC004A5EAD /* StatusBarView_Safari.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarView_Safari.swift; sourceTree = "<group>"; };
F0EE0A1C2348A651004A5EAD /* PDFKitViewRepresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PDFKitViewRepresentable.swift; sourceTree = "<group>"; };
F0F256BC233D309F00C9D719 /* String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
F0F256C1233E6C4C00C9D719 /* FavoriteVoiceName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteVoiceName.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -412,7 +415,7 @@
F0AA69D9232E979B007CC07B /* Components */ = {
isa = PBXGroup;
children = (
F0AA69DD232E97B0007CC07B /* PDFKitView.swift */,
F0EE0A1B2348A645004A5EAD /* PDFKitView */,
F0AA69DA232E97AF007CC07B /* SpeechSynthesizer.swift */,
F0AA69DB232E97AF007CC07B /* TranslatorView.swift */,
F0EE0A142347BEDB004A5EAD /* WebView */,
Expand Down Expand Up @@ -466,6 +469,15 @@
path = WebView;
sourceTree = "<group>";
};
F0EE0A1B2348A645004A5EAD /* PDFKitView */ = {
isa = PBXGroup;
children = (
F0AA69DD232E97B0007CC07B /* PDFKitView.swift */,
F0EE0A1C2348A651004A5EAD /* PDFKitViewRepresentable.swift */,
);
path = PDFKitView;
sourceTree = "<group>";
};
F0F256BB233D307C00C9D719 /* Extentions */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -707,6 +719,7 @@
F0BB436C2344842200ADBEF1 /* ReaderView.swift in Sources */,
F0BB437A2344845000ADBEF1 /* StatusBarView_Zoom.swift in Sources */,
F0BB43812344846200ADBEF1 /* ReaderView_PDF.swift in Sources */,
F0EE0A1E2348A66E004A5EAD /* PDFKitViewRepresentable.swift in Sources */,
F06DB117234611BB00C2DE90 /* ExtensionManager.swift in Sources */,
F0BB43722344843D00ADBEF1 /* FavoriteVoiceName.swift in Sources */,
F0EE0A1A234897CC004A5EAD /* StatusBarView_Safari.swift in Sources */,
Expand Down Expand Up @@ -739,6 +752,7 @@
F0AA69A1232E9710007CC07B /* AppDelegate.swift in Sources */,
F0F256BD233D309F00C9D719 /* String.swift in Sources */,
F0AB12A3233F5798005B9F2A /* ReaderView_PDF.swift in Sources */,
F0EE0A1D2348A651004A5EAD /* PDFKitViewRepresentable.swift in Sources */,
F06DB0FF2344856300C2DE90 /* Store.swift in Sources */,
F0AA69D5232E9762007CC07B /* StatusBarView.swift in Sources */,
F0AB12B6233F59B4005B9F2A /* StatusBarView_Voice_Volume.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import SwiftUI
import PDFKit
import Combine

struct PDFKitView: View {
@State var url: URL?
Expand Down Expand Up @@ -96,84 +95,6 @@ struct PDFKitView: View {
}
}

#if os(macOS)
private struct PDFKitViewRepresentable: NSViewRepresentable {
@Binding var url: URL?
static let pdfView = PDFView()

class Coordinator: NSObject, PDFViewDelegate {
var parent: PDFKitViewRepresentable

init(_ parent: PDFKitViewRepresentable) {
self.parent = parent
}
}

func makeCoordinator() -> PDFKitViewRepresentable.Coordinator {
Coordinator(self)
}

func makeNSView(context: Context) -> PDFView {
PDFKitViewRepresentable.pdfView
}

func updateNSView(_ uiView: PDFView, context: Context) {
if PDFKitViewRepresentable.pdfView.document != nil { return }
if let url = url {
//TODO: PDFKitViewRepresentable.pdfView.autoresizingMask = [.flexibleWidth]
PDFKitViewRepresentable.pdfView.autoScales = true
PDFKitViewRepresentable.pdfView.document = PDFDocument(url: url)
PDFKitViewRepresentable.pdfView.delegate = context.coordinator
}
}

static func getSelectedText() -> String {
guard let selections = PDFKitViewRepresentable.pdfView.currentSelection?.selectionsByLine() else { return "" }

return selections
.map { selection in selection.string! }
.joined(separator: " ")
}
}
#else
private struct PDFKitViewRepresentable: UIViewRepresentable {
@Binding var url: URL?
static let pdfView = PDFView()

class Coordinator: NSObject, PDFViewDelegate {
var parent: PDFKitViewRepresentable

init(_ parent: PDFKitViewRepresentable) {
self.parent = parent
}
}

func makeCoordinator() -> PDFKitViewRepresentable.Coordinator {
Coordinator(self)
}

func makeUIView(context: Context) -> PDFView {
PDFKitViewRepresentable.pdfView
}

func updateUIView(_ uiView: PDFView, context: Context) {
if PDFKitViewRepresentable.pdfView.document != nil { return }
if let url = url {
PDFKitViewRepresentable.pdfView.autoresizingMask = [.flexibleWidth]
PDFKitViewRepresentable.pdfView.autoScales = true
PDFKitViewRepresentable.pdfView.document = PDFDocument(url: url)
PDFKitViewRepresentable.pdfView.delegate = context.coordinator
}
}

static func getSelectedText() -> String {
guard let selections = PDFKitViewRepresentable.pdfView.currentSelection?.selectionsByLine() else { return "" }

return selections
.map { selection in selection.string! }
.joined(separator: " ")
}
}
#endif


Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// PDFKitViewRepresentable.swift
// ReaderTranslator
//
// Created by Viktor Kushnerov on 10/5/19.
// Copyright © 2019 Viktor Kushnerov. All rights reserved.
//

import SwiftUI
import PDFKit

#if os(macOS)
struct PDFKitViewRepresentable: NSViewRepresentable {
@Binding var url: URL?
static let pdfView = PDFView()

class Coordinator: NSObject, PDFViewDelegate {
var parent: PDFKitViewRepresentable

init(_ parent: PDFKitViewRepresentable) {
self.parent = parent
}
}

func makeCoordinator() -> PDFKitViewRepresentable.Coordinator {
Coordinator(self)
}

func makeNSView(context: Context) -> PDFView {
PDFKitViewRepresentable.pdfView
}

func updateNSView(_ uiView: PDFView, context: Context) {
if PDFKitViewRepresentable.pdfView.document != nil { return }
if let url = url {
//TODO: PDFKitViewRepresentable.pdfView.autoresizingMask = [.flexibleWidth]
PDFKitViewRepresentable.pdfView.autoScales = true
PDFKitViewRepresentable.pdfView.document = PDFDocument(url: url)
PDFKitViewRepresentable.pdfView.delegate = context.coordinator
}
}

static func getSelectedText() -> String {
guard let selections = PDFKitViewRepresentable.pdfView.currentSelection?.selectionsByLine() else { return "" }

return selections
.map { selection in selection.string! }
.joined(separator: " ")
}
}
#else
struct PDFKitViewRepresentable: UIViewRepresentable {
@Binding var url: URL?
static let pdfView = PDFView()

class Coordinator: NSObject, PDFViewDelegate {
var parent: PDFKitViewRepresentable

init(_ parent: PDFKitViewRepresentable) {
self.parent = parent
}
}

func makeCoordinator() -> PDFKitViewRepresentable.Coordinator {
Coordinator(self)
}

func makeUIView(context: Context) -> PDFView {
PDFKitViewRepresentable.pdfView
}

func updateUIView(_ uiView: PDFView, context: Context) {
if PDFKitViewRepresentable.pdfView.document != nil { return }
if let url = url {
PDFKitViewRepresentable.pdfView.autoresizingMask = [.flexibleWidth]
PDFKitViewRepresentable.pdfView.autoScales = true
PDFKitViewRepresentable.pdfView.document = PDFDocument(url: url)
PDFKitViewRepresentable.pdfView.delegate = context.coordinator
}
}

static func getSelectedText() -> String {
guard let selections = PDFKitViewRepresentable.pdfView.currentSelection?.selectionsByLine() else { return "" }

return selections
.map { selection in selection.string! }
.joined(separator: " ")
}
}
#endif
4 changes: 3 additions & 1 deletion ReaderTranslatorSafari/reader-translate-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

document.addEventListener('selectionchange', (event) => {
var txt = document.getSelection().toString()
send({name: 'selectionchange', source: 'document', extra: { selectedText: txt } })
if(txt.trim()) {
send({name: 'selectionchange', source: 'document', extra: { selectedText: txt } })
}
})

window.addEventListener('keydown', (e) => {
Expand Down

0 comments on commit 6bf62d4

Please sign in to comment.