Allow exporting highlights and notes as markdown

This commit is contained in:
Jackson Harper 2023-01-31 18:42:52 +08:00
parent 6f8bf6686b
commit c76c90e005
3 changed files with 46 additions and 2 deletions

View file

@ -7,6 +7,7 @@ struct HighlightsListCard: View {
@State var annotation = String()
@State var showAnnotationModal = false
let viewModel: HighlightsListViewModel
let highlightParams: HighlightListItemParams
@Binding var hasHighlightMutations: Bool
let onSaveAnnotation: (String) -> Void
@ -15,6 +16,7 @@ struct HighlightsListCard: View {
@State var errorAlertMessage: String?
@State var showErrorAlertMessage = false
@State var showShareView = false
var contextMenuView: some View {
Group {
@ -40,6 +42,10 @@ struct HighlightsListCard: View {
},
label: { Label(LocalText.labelsGeneric, systemImage: "tag") }
)
Button(
action: { showShareView = true },
label: { Label("Export", systemImage: "square.and.arrow.up") }
)
Button(
action: onDeleteHighlight,
label: { Label("Delete", systemImage: "trash") }
@ -166,5 +172,8 @@ struct HighlightsListCard: View {
showErrorAlertMessage: $showErrorAlertMessage
)
}
.formSheet(isPresented: $showShareView) {
ShareSheet(activityItems: [viewModel.highlightAsMarkdown(item: self.highlightParams)])
}
}
}

View file

@ -12,6 +12,7 @@ struct HighlightsListView: View {
let itemObjectID: NSManagedObjectID
@Binding var hasHighlightMutations: Bool
@State var setLabelsHighlight: Highlight?
@State var showShareView: Bool = false
var emptyView: some View {
Text(LocalText.highlightCardNoHighlightsOnPage)
@ -26,9 +27,14 @@ struct HighlightsListView: View {
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
ToolbarItem(placement: .navigationBarLeading) {
dismissButton
}
ToolbarItem(placement: .navigationBarTrailing) {
actionsMenu
}
}.formSheet(isPresented: $showShareView) {
ShareSheet(activityItems: [viewModel.highlightsAsMarkdown()])
}
#else
.toolbar {
@ -44,6 +50,7 @@ struct HighlightsListView: View {
Section {
ForEach(viewModel.highlightItems) { highlightParams in
HighlightsListCard(
viewModel: self.viewModel,
highlightParams: highlightParams,
hasHighlightMutations: $hasHighlightMutations,
onSaveAnnotation: {
@ -82,7 +89,22 @@ struct HighlightsListView: View {
var dismissButton: some View {
Button(
action: { presentationMode.wrappedValue.dismiss() },
label: { Text(LocalText.doneGeneric).foregroundColor(.appGrayTextContrast) }
label: { Text(LocalText.genericClose) }
)
}
var actionsMenu: some View {
Menu(
content: {
Button(
action: { showShareView = true },
label: { Label("Export", systemImage: "square.and.arrow.up") }
)
},
label: {
Image(systemName: "ellipsis")
.foregroundColor(.appGrayTextContrast)
}
)
}

View file

@ -58,6 +58,19 @@ struct HighlightListItemParams: Identifiable {
}
}
func highlightAsMarkdown(item: HighlightListItemParams) -> String {
var buffer = "> \(item.quote)"
if !item.annotation.isEmpty {
buffer += "\n\n\(item.annotation)"
}
buffer += "\n"
return buffer
}
func highlightsAsMarkdown() -> String {
highlightItems.map { highlightAsMarkdown(item: $0) }.lazy.joined(separator: "\n\n")
}
private func loadHighlights(item: LinkedItem) {
let unsortedHighlights = item.highlights.asArray(of: Highlight.self)