Skip to content

Commit

Permalink
Right click to remove attachment in Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
livid committed Aug 31, 2023
1 parent c45638a commit 6d4de20
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 71 deletions.
166 changes: 96 additions & 70 deletions Planet/Writer/WriterView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI
import WebKit
import UniformTypeIdentifiers
import WebKit

struct WriterView: View {
@ObservedObject var draft: DraftModel
Expand All @@ -19,7 +19,7 @@ struct WriterView: View {

var body: some View {
VStack(spacing: 0) {
if let videoAttachment = draft.attachments.first(where: {$0.type == .video}) {
if let videoAttachment = draft.attachments.first(where: { $0.type == .video }) {
WriterVideoView(videoAttachment: videoAttachment)
.onAppear {
self.videoPlayerHeight = 270
Expand All @@ -28,7 +28,7 @@ struct WriterView: View {
self.videoPlayerHeight = 0
}
}
if let audioAttachment = draft.attachments.first(where: {$0.type == .audio}) {
if let audioAttachment = draft.attachments.first(where: { $0.type == .audio }) {
WriterAudioView(audioAttachment: audioAttachment)
.onAppear {
self.audioPlayerHeight = 34
Expand All @@ -38,7 +38,12 @@ struct WriterView: View {
}
}

WriterTitleView(tags: $draft.tags, date: $draft.date, title: $draft.title, focusTitle: _focusTitle)
WriterTitleView(
tags: $draft.tags,
date: $draft.date,
title: $draft.title,
focusTitle: _focusTitle
)

Divider()

Expand All @@ -53,88 +58,109 @@ struct WriterView: View {
.frame(minWidth: 640, minHeight: 300)
}

if viewModel.isMediaTrayOpen {
Divider()
ScrollView(.horizontal) {
HStack(spacing: 0) {
ForEach(
draft.attachments.filter { $0.type == .image || $0.type == .audio || $0.type == .file },
id: \.name
) { attachment in
AttachmentThumbnailView(attachment: attachment)
}
}
}
.frame(height: 80)
.frame(maxWidth: .infinity)
.background(Color.secondary.opacity(0.03))
.onDrop(of: [.fileURL], delegate: dragAndDrop)
}
mediaTray()
}
.frame(minWidth: 640, minHeight: 520 + videoPlayerHeight + audioPlayerHeight)
.onChange(of: draft.date) { _ in
try? draft.save()
.onChange(of: draft.date) { _ in
try? draft.save()
}
.onChange(of: draft.title) { _ in
try? draft.save()
}
.onChange(of: draft.content) { _ in
try? draft.save()
try? draft.renderPreview()
}
.onChange(of: draft.attachments) { _ in
if draft.attachments.contains(where: { $0.type == .image || $0.type == .file }) {
viewModel.isMediaTrayOpen = true
}
.onChange(of: draft.title) { _ in
try? draft.save()
try? draft.renderPreview()
}
.onAppear {
if draft.attachments.contains(where: { $0.type == .image || $0.type == .file }) {
viewModel.isMediaTrayOpen = true
}
.onChange(of: draft.content) { _ in
try? draft.save()
try? draft.renderPreview()
Task { @MainActor in
// workaround: wrap in a task to delay focusing the title a little
focusTitle = true
}
.onChange(of: draft.attachments) { _ in
if draft.attachments.contains(where: { $0.type == .image || $0.type == .file }) {
}
.fileImporter(
isPresented: $viewModel.isChoosingAttachment,
allowedContentTypes: viewModel.allowedContentTypes,
allowsMultipleSelection: viewModel.allowMultipleSelection
) { result in
if let urls = try? result.get() {
if viewModel.attachmentType == .image {
viewModel.isMediaTrayOpen = true
}
urls.forEach { url in
_ = try? draft.addAttachment(path: url, type: viewModel.attachmentType)
}
try? draft.renderPreview()
try? draft.save()
}
.onAppear {
if draft.attachments.contains(where: { $0.type == .image || $0.type == .file }) {
viewModel.isMediaTrayOpen = true
}
}
.confirmationDialog(
Text("Do you want to save your changes as a draft?"),
isPresented: $viewModel.isShowingDiscardConfirmation
) {
Button {
viewModel.madeDiscardChoice = true
try? draft.save()
Task { @MainActor in
// workaround: wrap in a task to delay focusing the title a little
focusTitle = true
WriterStore.shared.closeWriterWindow(byDraftID: self.draft.id)
}
} label: {
Text("Save Draft")
}
.fileImporter(
isPresented: $viewModel.isChoosingAttachment,
allowedContentTypes: viewModel.allowedContentTypes,
allowsMultipleSelection: viewModel.allowMultipleSelection
) { result in
if let urls = try? result.get() {
if viewModel.attachmentType == .image {
viewModel.isMediaTrayOpen = true
}
urls.forEach { url in
_ = try? draft.addAttachment(path: url, type: viewModel.attachmentType)
}
try? draft.renderPreview()
try? draft.save()
Button(role: .destructive) {
viewModel.madeDiscardChoice = true
try? draft.delete()
Task { @MainActor in
WriterStore.shared.closeWriterWindow(byDraftID: self.draft.id)
}
} label: {
Text("Delete Draft")
}
.confirmationDialog(
Text("Do you want to save your changes as a draft?"),
isPresented: $viewModel.isShowingDiscardConfirmation
) {
Button {
viewModel.madeDiscardChoice = true
try? draft.save()
Task { @MainActor in
WriterStore.shared.closeWriterWindow(byDraftID: self.draft.id)
}
} label: {
Text("Save Draft")
}
Button(role: .destructive) {
viewModel.madeDiscardChoice = true
try? draft.delete()
Task { @MainActor in
WriterStore.shared.closeWriterWindow(byDraftID: self.draft.id)
}
}

@ViewBuilder
private func mediaTray() -> some View {
if viewModel.isMediaTrayOpen {
Divider()
ScrollView(.horizontal) {
HStack(spacing: 0) {
ForEach(
draft.attachments.filter {
$0.type == .image || $0.type == .audio || $0.type == .file
},
id: \.name
) { attachment in
AttachmentThumbnailView(attachment: attachment)
.help(attachment.name)
.contextMenu {
Button {
if let markdown = attachment.markdown {
NotificationCenter.default.post(
name: .writerNotification(.removeText, for: attachment.draft),
object: markdown
)
}
try? attachment.draft.deleteAttachment(name: attachment.name)
} label: {
Text("Remove")
}
}
}
} label: {
Text("Delete Draft")
}
}
.frame(height: 80)
.frame(maxWidth: .infinity)
.background(Color.secondary.opacity(0.03))
.onDrop(of: [.fileURL], delegate: dragAndDrop)
}
}
}
2 changes: 1 addition & 1 deletion Planet/versioning.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CURRENT_PROJECT_VERSION = 1601
CURRENT_PROJECT_VERSION = 1602

0 comments on commit 6d4de20

Please sign in to comment.