Create a highlight of the selected text when a user saves
This commit is contained in:
committed by
Hongbo Wu
parent
2e7b7c1bb5
commit
bf97194208
@ -87,7 +87,7 @@ public class ShareExtensionViewModel: ObservableObject {
|
|||||||
let hostname = URL(string: payload.url)?.host ?? ""
|
let hostname = URL(string: payload.url)?.host ?? ""
|
||||||
|
|
||||||
switch payload.contentType {
|
switch payload.contentType {
|
||||||
case let .html(html: _, title: title):
|
case let .html(html: _, title: title, _):
|
||||||
self.title = title ?? ""
|
self.title = title ?? ""
|
||||||
self.url = hostname
|
self.url = hostname
|
||||||
case .none:
|
case .none:
|
||||||
@ -143,7 +143,7 @@ public class ShareExtensionViewModel: ObservableObject {
|
|||||||
localPdfURL: localUrl,
|
localPdfURL: localUrl,
|
||||||
url: pageScrapePayload.url
|
url: pageScrapePayload.url
|
||||||
)
|
)
|
||||||
case let .html(html, title):
|
case let .html(html, title, _):
|
||||||
newRequestID = try await services.dataService.createPage(
|
newRequestID = try await services.dataService.createPage(
|
||||||
id: requestId,
|
id: requestId,
|
||||||
originalHtml: html,
|
originalHtml: html,
|
||||||
|
|||||||
@ -8,11 +8,26 @@ import UniformTypeIdentifiers
|
|||||||
|
|
||||||
let URLREGEX = #"[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"#
|
let URLREGEX = #"[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"#
|
||||||
|
|
||||||
|
public struct HighlightData {
|
||||||
|
public let highlightHTML: String
|
||||||
|
public let highlightText: String
|
||||||
|
|
||||||
|
public static func make(dict: NSDictionary?) -> HighlightData? {
|
||||||
|
if let dict = dict,
|
||||||
|
let highlightHTML = dict["highlightHTML"] as? String,
|
||||||
|
let highlightText = dict["highlightText"] as? String
|
||||||
|
{
|
||||||
|
return HighlightData(highlightHTML: highlightHTML, highlightText: highlightText)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public struct PageScrapePayload {
|
public struct PageScrapePayload {
|
||||||
public enum ContentType {
|
public enum ContentType {
|
||||||
case none
|
case none
|
||||||
case html(html: String, title: String?)
|
|
||||||
case pdf(localUrl: URL)
|
case pdf(localUrl: URL)
|
||||||
|
case html(html: String, title: String?, highlightData: HighlightData?)
|
||||||
}
|
}
|
||||||
|
|
||||||
public let url: String
|
public let url: String
|
||||||
@ -33,9 +48,9 @@ public struct PageScrapePayload {
|
|||||||
self.contentType = .pdf(localUrl: localUrl)
|
self.contentType = .pdf(localUrl: localUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
init(url: String, title: String?, html: String) {
|
init(url: String, title: String?, html: String, highlightData: HighlightData?) {
|
||||||
self.url = url
|
self.url = url
|
||||||
self.contentType = .html(html: html, title: title)
|
self.contentType = .html(html: html, title: title, highlightData: highlightData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,7 +332,10 @@ private extension PageScrapePayload {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let html = html {
|
if let html = html {
|
||||||
return PageScrapePayload(url: url, title: title, html: html)
|
return PageScrapePayload(url: url,
|
||||||
|
title: title,
|
||||||
|
html: html,
|
||||||
|
highlightData: HighlightData.make(dict: results))
|
||||||
}
|
}
|
||||||
|
|
||||||
return PageScrapePayload(url: url)
|
return PageScrapePayload(url: url)
|
||||||
|
|||||||
@ -179,7 +179,7 @@ public final class DataService: ObservableObject {
|
|||||||
linkedItem.contentReader = "PDF"
|
linkedItem.contentReader = "PDF"
|
||||||
linkedItem.tempPDFURL = localUrl
|
linkedItem.tempPDFURL = localUrl
|
||||||
linkedItem.title = PDFUtils.titleFromPdfFile(pageScrape.url)
|
linkedItem.title = PDFUtils.titleFromPdfFile(pageScrape.url)
|
||||||
case let .html(html: html, title: title):
|
case let .html(html: html, title: title, highlightData: _):
|
||||||
linkedItem.contentReader = "WEB"
|
linkedItem.contentReader = "WEB"
|
||||||
linkedItem.originalHtml = html
|
linkedItem.originalHtml = html
|
||||||
linkedItem.title = title ?? PDFUtils.titleFromPdfFile(pageScrape.url)
|
linkedItem.title = title ?? PDFUtils.titleFromPdfFile(pageScrape.url)
|
||||||
|
|||||||
@ -11,34 +11,47 @@ function iconURL() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ShareExtension.prototype = {
|
ShareExtension.prototype = {
|
||||||
getHighlightHTML: function() {
|
markHighlightSelection: () => {
|
||||||
try {
|
try {
|
||||||
var sel = window.getSelection()
|
const sel = window.getSelection();
|
||||||
return (function () {
|
if (sel.rangeCount) {
|
||||||
var html = "";
|
const range = sel.getRangeAt(0)
|
||||||
var sel = window.getSelection();
|
const endMarker = document.createElement("span")
|
||||||
if (sel.rangeCount) {
|
const startMarker = document.createElement("span")
|
||||||
var container = document.createElement("div");
|
endMarker.setAttribute("data-omnivore-highlight-end", true)
|
||||||
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
|
startMarker.setAttribute("data-omnivore-highlight-start", true)
|
||||||
container.appendChild(sel.getRangeAt(i).cloneContents());
|
|
||||||
}
|
var container = document.createElement("div");
|
||||||
html = container.innerHTML;
|
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
|
||||||
|
container.appendChild(sel.getRangeAt(i).cloneContents());
|
||||||
}
|
}
|
||||||
return html;
|
|
||||||
})()
|
const endRange = range.cloneRange()
|
||||||
} catch {
|
endRange.collapse(false)
|
||||||
|
endRange.insertNode(endMarker)
|
||||||
|
|
||||||
|
range.insertNode(startMarker)
|
||||||
|
|
||||||
|
return {
|
||||||
|
highlightHTML: container.innerHTML,
|
||||||
|
highlightText: container.innerText
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(error) {
|
||||||
|
console.log("ERROR", error)
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
},
|
},
|
||||||
run: function(arguments) {
|
run: function(arguments) {
|
||||||
|
const highlightData = this.markHighlightSelection()
|
||||||
|
|
||||||
arguments.completionFunction({
|
arguments.completionFunction({
|
||||||
'url': window.location.href,
|
'url': window.location.href,
|
||||||
'title': document.title.toString(),
|
'title': document.title.toString(),
|
||||||
'iconURL': iconURL(),
|
'iconURL': iconURL(),
|
||||||
'contentType': document.contentType,
|
'contentType': document.contentType,
|
||||||
'originalHTML': new XMLSerializer().serializeToString(document),
|
'originalHTML': new XMLSerializer().serializeToString(document),
|
||||||
'highlightHTML': this.getHighlightHTML()
|
...highlightData
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user