-
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.
Safari plugin: Part 1 #15 Don't send empty selected text to the app.
- Loading branch information
Showing
4 changed files
with
108 additions
and
81 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
90 changes: 90 additions & 0 deletions
90
ReaderTranslator/Components/PDFKitView/PDFKitViewRepresentable.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,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 |
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