Commit 5502e04b9c
Verified · cmc
Layout: unified · split
Sources/OrgstarMobile/EditorScreen.swift +15 −1
| @@ -76,6 +76,8 @@ struct EditorScreen: View { | ||
| 76 | 76 | case plot(TablePlot.Plot) |
| 77 | 77 | case crypt(CryptRequest) |
| 78 | 78 | case backlinks |
| 79 | /// The subtree at this offset, to export. | |
| 80 | case exportSubtree(Int) | |
| 79 | 81 | |
| 80 | 82 | var id: String { |
| 81 | 83 | switch self { |
| @@ -89,6 +91,7 @@ struct EditorScreen: View { | ||
| 89 | 91 | case .plot: "plot" |
| 90 | 92 | case .crypt(let request): request.id.uuidString |
| 91 | 93 | case .backlinks: "backlinks" |
| 94 | case .exportSubtree: "exportSubtree" | |
| 92 | 95 | } |
| 93 | 96 | } |
| 94 | 97 | } |
| @@ -163,7 +166,9 @@ struct EditorScreen: View { | ||
| 163 | 166 | Button("Encrypt Tagged Entries…", systemImage: "lock.rectangle.stack") { crypt("app.crypt.encrypt-entries") } |
| 164 | 167 | Button("Forget Encryption Passphrase", systemImage: "key.slash") { crypt("app.crypt.forget-passphrase") } |
| 165 | 168 | } |
| 166 | if let text = session.document(of: path)?.text { ExportMenu(path: path, text: text) { message = $0 } } | |
| 169 | if let text = session.document(of: path)?.text { | |
| 170 | ExportMenu(path: path, text: text, report: { message = $0 }, exportSubtree: exportSubtree) | |
| 171 | } | |
| 167 | 172 | Button("Sync Conflict Copies", systemImage: "doc.on.doc") { sheet = .syncCopies } |
| 168 | 173 | Button("Recovered Versions", systemImage: "clock.arrow.circlepath") { sheet = .recovery } |
| 169 | 174 | if onPaneCommand != nil, sizeClass == .regular { |
| @@ -247,6 +252,8 @@ struct EditorScreen: View { | ||
| 247 | 252 | PlotScreen(plot: plot) |
| 248 | 253 | case .crypt(let request): |
| 249 | 254 | CryptScreen(request: request, session: session, editor: editor) { message = $0 } |
| 255 | case .exportSubtree(let caret): | |
| 256 | if let text = session.document(of: path)?.text { SubtreeExportScreen(path: path, text: text, caret: caret) } | |
| 250 | 257 | case .backlinks: |
| 251 | 258 | BacklinksScreen(workspace: workspace, session: session, path: path, caret: editor?.editContext().caret ?? 0) { location in |
| 252 | 259 | if location.path == path { editor?.reveal(location.start) } else { model.openRequest = .heading(location) } |
| @@ -521,6 +528,13 @@ extension EditorScreen { | ||
| 521 | 528 | _ = editor.perform(ImportTable(contents: contents)) |
| 522 | 529 | } |
| 523 | 530 | |
| 531 | /// The export formats for the subtree at the caret, as the Mac's export sheet's scope. | |
| 532 | private func exportSubtree() { | |
| 533 | guard let context = editor?.editContext() else { return } | |
| 534 | guard ExportScope.subtree(context.text, at: context.caret) != nil else { return message = "Not in a subtree" } | |
| 535 | sheet = .exportSubtree(context.caret) | |
| 536 | } | |
| 537 | ||
| 524 | 538 | /// org-crypt's commands at the caret, through the passphrase sheet. |
| 525 | 539 | private func crypt(_ id: String) { |
| 526 | 540 | guard let editor else { return } |
Sources/OrgstarMobile/ExportMenu.swift +36 −3
| @@ -43,10 +43,14 @@ struct OrgExport: Transferable { | ||
| 43 | 43 | let settings: OrgSettings |
| 44 | 44 | /// Made on the main actor, where the editor's highlighter is. |
| 45 | 45 | let codeStyle: HTMLCodeStyle |
| 46 | /// The subtree at this offset exported alone. | |
| 47 | var subtree: Int? | |
| 46 | 48 | |
| 47 | /// `#+EXPORT_FILE_NAME`'s name, or the file's, with the format's extension. | |
| 49 | /// The subtree's `EXPORT_FILE_NAME`, `#+EXPORT_FILE_NAME`'s name, or the file's, with the | |
| 50 | /// format's extension. | |
| 48 | 51 | var fileName: String { |
| 49 | ExportScope.output(for: URL(fileURLWithPath: path), name: ExportScope.fileName(text, settings: settings), extension: format.fileExtension) | |
| 52 | ExportScope.output(for: URL(fileURLWithPath: path), name: ExportScope.fileName(text, subtreeAt: subtree, settings: settings), | |
| 53 | extension: format.fileExtension) | |
| 50 | 54 | .lastPathComponent |
| 51 | 55 | } |
| 52 | 56 | |
| @@ -73,6 +77,7 @@ struct OrgExport: Transferable { | ||
| 73 | 77 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 74 | 78 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) |
| 75 | 79 | let url = folder.appendingPathComponent(fileName) |
| 80 | let text = subtree.flatMap { ExportScope.subtree(self.text, at: $0, settings: settings) } ?? self.text | |
| 76 | 81 | switch format { |
| 77 | 82 | case .html: |
| 78 | 83 | try HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle, language: OrgPreferences.exportLanguage()) |
| @@ -80,7 +85,8 @@ struct OrgExport: Transferable { | ||
| 80 | 85 | case .markdown: |
| 81 | 86 | try MarkdownExport.document(text, settings: settings, path: path).write(to: url, atomically: true, encoding: .utf8) |
| 82 | 87 | case .icalendar: |
| 83 | try ICalendarFiles.export(text, path: path, settings: settings, options: OrgPreferences.icalendarOptions(), destination: url) | |
| 88 | try ICalendarFiles.export(self.text, path: path, settings: settings, options: OrgPreferences.icalendarOptions(), | |
| 89 | subtree: subtree, destination: url) | |
| 84 | 90 | case .docx: |
| 85 | 91 | let html = HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle, language: OrgPreferences.exportLanguage()) |
| 86 | 92 | try await RichTextExport.docx(html: html, directory: URL(fileURLWithPath: path).deletingLastPathComponent()).write(to: url, options: .atomic) |
| @@ -106,6 +112,8 @@ struct ExportMenu: View { | ||
| 106 | 112 | @Environment(MobileModel.self) private var model |
| 107 | 113 | /// Shows what happened, as the screen's message line does. |
| 108 | 114 | var report: (String) -> Void = { _ in } |
| 115 | /// Offers the subtree at the caret alone, where there is a caret. | |
| 116 | var exportSubtree: (() -> Void)? | |
| 109 | 117 | |
| 110 | 118 | var body: some View { |
| 111 | 119 | Menu("Export", systemImage: "square.and.arrow.up") { |
| @@ -114,6 +122,9 @@ struct ExportMenu: View { | ||
| 114 | 122 | codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme)) |
| 115 | 123 | ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) } |
| 116 | 124 | } |
| 125 | if let exportSubtree { | |
| 126 | Button("Export Subtree…", systemImage: "list.bullet.indent") { exportSubtree() } | |
| 127 | } | |
| 117 | 128 | Divider() |
| 118 | 129 | Button("Copy as Rich Text", systemImage: "doc.on.doc") { copy() } |
| 119 | 130 | } |
| @@ -133,4 +144,26 @@ struct ExportMenu: View { | ||
| 133 | 144 | } |
| 134 | 145 | } |
| 135 | 146 | } |
| 147 | ||
| 148 | /// The subtree at `caret` shared alone, in each of the export formats. | |
| 149 | struct SubtreeExportScreen: View { | |
| 150 | let path: String | |
| 151 | let text: String | |
| 152 | let caret: Int | |
| 153 | @Environment(MobileModel.self) private var model | |
| 154 | @Environment(\.dismiss) private var dismiss | |
| 155 | ||
| 156 | var body: some View { | |
| 157 | NavigationStack { | |
| 158 | List(OrgExport.Format.allCases, id: \.self) { format in | |
| 159 | let export = OrgExport(text: text, path: path, format: format, settings: OrgPreferences.orgSettings(), | |
| 160 | codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme), subtree: caret) | |
| 161 | ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) } | |
| 162 | } | |
| 163 | .navigationTitle("Export Subtree") | |
| 164 | .navigationBarTitleDisplayMode(.inline) | |
| 165 | .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Done") { dismiss() } } } | |
| 166 | } | |
| 167 | } | |
| 168 | } | |
| 136 | 169 | #endif |