Jotley / app / macOS / Views / NoteDetailPlaceholderView+Core.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | extension NoteDetailPlaceholderView { |
| 4 | func requestSummaryInlineEdit() { |
| 5 | selectedTab = DetailTab.summary.rawValue |
| 6 | summaryInlineEditRequestCounter += 1 |
| 7 | } |
| 8 | } |
| 9 | |
| 10 | /// Edits in the note's scroll view, using a separate draft so a concurrent |
| 11 | /// transcript update can never be overwritten by an older editor buffer. |
| 12 | struct LocalMacTranscriptEditor: View { |
| 13 | @ObservedObject var note: Note |
| 14 | @ObservedObject var viewModel: NotesListViewModel |
| 15 | @EnvironmentObject var store: SharedPersistenceController |
| 16 | @State private var draft: TranscriptEditDraft |
| 17 | @State private var error: String? |
| 18 | @FocusState private var editorFocused: Bool |
| 19 | var onFinish: () -> Void |
| 20 | |
| 21 | init(note: Note, viewModel: NotesListViewModel, onFinish: @escaping () -> Void = {}) { |
| 22 | self.note = note; self.viewModel = viewModel; self.onFinish = onFinish |
| 23 | _draft = State(initialValue: TranscriptEditDraft(text: note.transcription ?? "", utterances: note.storedTranscriptUtterances)) |
| 24 | } |
| 25 | private var canSave: Bool { |
| 26 | let recordingThisNote = viewModel.newNoteBeingRecorded?.objectID == note.objectID && |
| 27 | (viewModel.recorder.isRecording || viewModel.isTranscriptionPaused || viewModel.recorder.isBusy || viewModel.operationInProgress) |
| 28 | let generatingThisNote = viewModel.isGeneratingSummary && viewModel.generatingSummaryNoteID == note.objectID |
| 29 | return !note.isDeleted && !recordingThisNote && !generatingThisNote && !LocalAppModel.shared.busy |
| 30 | } |
| 31 | @discardableResult private func save() -> Bool { |
| 32 | guard draft.hasChanges else { return true } |
| 33 | do { |
| 34 | guard canSave else { throw LocalError.message("Finish transcription or processing before saving edits.") } |
| 35 | try note.saveEditedTranscript(draft, in: store) |
| 36 | draft = TranscriptEditDraft(text: note.transcription ?? "", utterances: note.storedTranscriptUtterances, |
| 37 | editorText: draft.editorText) |
| 38 | error = nil |
| 39 | return true |
| 40 | } catch { |
| 41 | self.error = error.localizedDescription |
| 42 | return false |
| 43 | } |
| 44 | } |
| 45 | private func finish() { |
| 46 | guard save() else { return } |
| 47 | onFinish() |
| 48 | } |
| 49 | var body: some View { |
| 50 | VStack(alignment: .leading, spacing: 12) { |
| 51 | HStack { |
| 52 | Spacer() |
| 53 | Button(action: finish) { |
| 54 | Label { |
| 55 | Text("Done") |
| 56 | } icon: { |
| 57 | Image(systemName: "checkmark") |
| 58 | .foregroundStyle(Color.themePrimaryAccent) |
| 59 | } |
| 60 | } |
| 61 | .jotleyDismissButtonStyle() |
| 62 | .keyboardShortcut(.return, modifiers: .command) |
| 63 | .disabled(!canSave) |
| 64 | .accessibilityIdentifier("transcriptEditorSaveButton") |
| 65 | } |
| 66 | NoteFindReplace(transcript: $draft) |
| 67 | if !draft.hasTimestamps { |
| 68 | Text("Timing is unavailable for this transcript.").font(.caption).foregroundStyle(Color.themeTextSecondary) |
| 69 | } |
| 70 | InlineEditableSummaryTextView(text: $draft.editorText, isFocused: $editorFocused, |
| 71 | accentColor: NSColor(Color.themePrimaryAccent), textColor: NSColor(Color.themeTextPrimary), |
| 72 | fontSize: 15) |
| 73 | .clipped() |
| 74 | .frame(maxWidth: .infinity, minHeight: 160, alignment: .topLeading) |
| 75 | .padding(12) |
| 76 | .modifier(JotleyEditorField(isFocused: editorFocused)) |
| 77 | .accessibilityLabel("Transcript") |
| 78 | .accessibilityIdentifier("transcriptEditorText") |
| 79 | if let error { |
| 80 | HStack(alignment: .top) { |
| 81 | Text(error).font(.callout).foregroundStyle(.red).accessibilityIdentifier("transcriptEditorError") |
| 82 | Spacer() |
| 83 | Button("Discard Changes") { |
| 84 | draft = TranscriptEditDraft(text: note.transcription ?? "", utterances: note.storedTranscriptUtterances) |
| 85 | self.error = nil |
| 86 | onFinish() |
| 87 | }.buttonStyle(JotleyGlassButtonStyle()) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | .padding(12) |
| 92 | .frame(maxWidth: .infinity, alignment: .topLeading) |
| 93 | .background(Color.themeContentBackground, in: RoundedRectangle(cornerRadius: 12)) |
| 94 | .tint(.themePrimaryAccent).observeThemeChanges() |
| 95 | .onExitCommand(perform: finish) |
| 96 | .task(id: draft.editorText) { |
| 97 | guard draft.hasChanges else { return } |
| 98 | do { try await Task.sleep(for: .seconds(2)) } catch { return } |
| 99 | guard !Task.isCancelled else { return } |
| 100 | save() |
| 101 | } |
| 102 | .onReceive(NotificationCenter.default.publisher(for: Notification.Name("JotleyForceAllEditorsSave"))) { _ in |
| 103 | if !save(), let error { viewModel.report(LocalError.message(error)) } |
| 104 | } |
| 105 | .onDisappear { |
| 106 | if !save(), let error { viewModel.report(LocalError.message(error)) } |
| 107 | } |
| 108 | } |
| 109 | } |