Skip to content
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 7 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
12.1
-----
* Improve messages when updates to user account details fail because of server logic, for exanple email being used for another account.
* Improved text import from other apps, such as Bear or Ulysses 🥰

12.0
-----
Expand Down
62 changes: 45 additions & 17 deletions WordPress/WordPressShareExtension/ShareExtractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand All @@ -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 importedText.escapeHtmlNamedEntities()
}

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>"
}
}
}

Expand All @@ -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))
}
}
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -231,10 +241,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
}
}
Expand Down