-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Share Extension: Improve text import from other apps #11288
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b767803
Add first pass at opening a shared file
nheagy 362fda7
Add initial support for plain text file importing
nheagy 6bd0652
Give imported text unquoted appearance
nheagy 54b0ff7
Stop imported text selection from carrying HTML
nheagy b9d8bd4
Fix some weird indenting in ShareExtractor.swift
nheagy 174b306
Added release note for improved text import
nheagy 4e3cc2e
cleanup ShareExtractor.swift
nheagy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,10 @@ struct ExtractedShare { | |
var description: String | ||
var url: URL? | ||
var selectedText: String | ||
var importedText: String | ||
var images: [UIImage] | ||
|
||
var combinedContentHTML: String { | ||
var returnString: String | ||
|
||
var rawLink = "" | ||
var readOnText = "" | ||
|
||
|
@@ -25,23 +24,26 @@ struct ExtractedShare { | |
} | ||
|
||
// Build the returned string by doing the following: | ||
// * 1st check: Look for selected text, if it exists put it into a blockquote. | ||
// * 2nd check: No selected text, but we have a page description...use that. | ||
// * 3rd check: No selected text, but we have a page title...use that. | ||
// * 1: Look for imported text. | ||
// * 2: Look for selected text, if it exists put it into a blockquote. | ||
// * 3: No selected text, but we have a page description...use that. | ||
// * 4: No selected text, but we have a page title...use that. | ||
// * Finally, default to a simple link if nothing else is found | ||
if selectedText.isEmpty { | ||
if !description.isEmpty { | ||
returnString = "<p>\(description)\(readOnText)</p>" | ||
} else if !title.isEmpty { | ||
returnString = "<p>\(title)\(readOnText)</p>" | ||
} else { | ||
returnString = "<p>\(rawLink)</p>" | ||
} | ||
} else { | ||
returnString = "<blockquote><p>\(selectedText)\(readOnText)</p></blockquote>" | ||
guard importedText.isEmpty else { | ||
return "<p>\(importedText.escapeHtmlNamedEntities())</p>" | ||
} | ||
|
||
return returnString | ||
guard selectedText.isEmpty else { | ||
return "<blockquote><p>\(selectedText.escapeHtmlNamedEntities())\(readOnText)</p></blockquote>" | ||
} | ||
|
||
if !description.isEmpty { | ||
return "<p>\(description)\(readOnText)</p>" | ||
} else if !title.isEmpty { | ||
return "<p>\(title)\(readOnText)</p>" | ||
} else { | ||
return "<p>\(rawLink)</p>" | ||
} | ||
} | ||
} | ||
|
||
|
@@ -66,13 +68,15 @@ struct ShareExtractor { | |
let title = extractedTextResults?.title ?? "" | ||
let description = extractedTextResults?.description ?? "" | ||
let selectedText = extractedTextResults?.selectedText ?? "" | ||
let importedText = extractedTextResults?.importedText ?? "" | ||
let url = extractedTextResults?.url | ||
let returnedImages = images ?? [UIImage]() | ||
|
||
completion(ExtractedShare(title: title, | ||
description: description, | ||
url: url, | ||
selectedText: selectedText, | ||
importedText: importedText, | ||
images: returnedImages)) | ||
} | ||
} | ||
|
@@ -100,6 +104,10 @@ private struct ExtractedItem { | |
/// | ||
var selectedText: String? | ||
|
||
/// Text that was imported from another app | ||
/// | ||
var importedText: String? | ||
|
||
/// A description of the resource if available | ||
/// | ||
var description: String? | ||
|
@@ -151,9 +159,11 @@ private extension ShareExtractor { | |
let combinedTitle = extractedItems.compactMap({ $0.title }).joined(separator: " ") | ||
let combinedDescription = extractedItems.compactMap({ $0.description }).joined(separator: " ") | ||
let combinedSelectedText = extractedItems.compactMap({ $0.selectedText }).joined(separator: "\n\n") | ||
let combinedImportedText = extractedItems.compactMap({ $0.importedText }).joined(separator: "\n\n") | ||
let urls = extractedItems.compactMap({ $0.url }) | ||
|
||
completion(ExtractedItem(selectedText: combinedSelectedText, | ||
importedText: combinedImportedText, | ||
description: combinedDescription, | ||
url: urls.first, | ||
title: combinedTitle, | ||
|
@@ -195,6 +205,7 @@ private extension TypeBasedExtensionContentExtractor { | |
|
||
func extract(context: NSExtensionContext, completion: @escaping ([ExtractedItem]) -> Void) { | ||
let itemProviders = context.itemProviders(ofType: acceptedType) | ||
print(acceptedType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to log this out, should we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I meant to remove that entirely 😉 |
||
var results = [ExtractedItem]() | ||
guard itemProviders.count > 0 else { | ||
DispatchQueue.main.async { | ||
|
@@ -231,10 +242,28 @@ private struct URLExtractor: TypeBasedExtensionContentExtractor { | |
|
||
func convert(payload: URL) -> ExtractedItem? { | ||
guard !payload.isFileURL else { | ||
return nil | ||
return processLocalFile(url: payload) | ||
} | ||
|
||
var returnedItem = ExtractedItem() | ||
returnedItem.url = payload | ||
|
||
return returnedItem | ||
} | ||
|
||
private func processLocalFile(url: URL) -> ExtractedItem? { | ||
switch url.pathExtension { | ||
case "text", "txt": | ||
return handlePlainTextFile(url: url) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
private func handlePlainTextFile(url: URL) -> ExtractedItem? { | ||
var returnedItem = ExtractedItem() | ||
let rawText = (try? String(contentsOf: url)) ?? "" | ||
returnedItem.importedText = rawText | ||
return returnedItem | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have imported text, isn't it likely that it's going to be more than one paragraph? Do we need to wrap it in tags here?