Commit 5502e04b9c
Verified · cmc
Layout: unified · split
Sources/OrgstarMobile/EditorScreen.swift +15 −1
| @@ -76,6 +76,8 @@ struct EditorScreen: View { | |||
| 76 | case plot(TablePlot.Plot) | 76 | case plot(TablePlot.Plot) |
| 77 | case crypt(CryptRequest) | 77 | case crypt(CryptRequest) |
| 78 | case backlinks | 78 | case backlinks |
| 79 | /// The subtree at this offset, to export. | ||
| 80 | case exportSubtree(Int) | ||
| 79 | 81 | ||
| 80 | var id: String { | 82 | var id: String { |
| 81 | switch self { | 83 | switch self { |
| @@ -89,6 +91,7 @@ struct EditorScreen: View { | |||
| 89 | case .plot: "plot" | 91 | case .plot: "plot" |
| 90 | case .crypt(let request): request.id.uuidString | 92 | case .crypt(let request): request.id.uuidString |
| 91 | case .backlinks: "backlinks" | 93 | case .backlinks: "backlinks" |
| 94 | case .exportSubtree: "exportSubtree" | ||
| 92 | } | 95 | } |
| 93 | } | 96 | } |
| 94 | } | 97 | } |
| @@ -163,7 +166,9 @@ struct EditorScreen: View { | |||
| 163 | Button("Encrypt Tagged Entries…", systemImage: "lock.rectangle.stack") { crypt("app.crypt.encrypt-entries") } | 166 | Button("Encrypt Tagged Entries…", systemImage: "lock.rectangle.stack") { crypt("app.crypt.encrypt-entries") } |
| 164 | Button("Forget Encryption Passphrase", systemImage: "key.slash") { crypt("app.crypt.forget-passphrase") } | 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 | Button("Sync Conflict Copies", systemImage: "doc.on.doc") { sheet = .syncCopies } | 172 | Button("Sync Conflict Copies", systemImage: "doc.on.doc") { sheet = .syncCopies } |
| 168 | Button("Recovered Versions", systemImage: "clock.arrow.circlepath") { sheet = .recovery } | 173 | Button("Recovered Versions", systemImage: "clock.arrow.circlepath") { sheet = .recovery } |
| 169 | if onPaneCommand != nil, sizeClass == .regular { | 174 | if onPaneCommand != nil, sizeClass == .regular { |
| @@ -247,6 +252,8 @@ struct EditorScreen: View { | |||
| 247 | PlotScreen(plot: plot) | 252 | PlotScreen(plot: plot) |
| 248 | case .crypt(let request): | 253 | case .crypt(let request): |
| 249 | CryptScreen(request: request, session: session, editor: editor) { message = $0 } | 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 | case .backlinks: | 257 | case .backlinks: |
| 251 | BacklinksScreen(workspace: workspace, session: session, path: path, caret: editor?.editContext().caret ?? 0) { location in | 258 | BacklinksScreen(workspace: workspace, session: session, path: path, caret: editor?.editContext().caret ?? 0) { location in |
| 252 | if location.path == path { editor?.reveal(location.start) } else { model.openRequest = .heading(location) } | 259 | if location.path == path { editor?.reveal(location.start) } else { model.openRequest = .heading(location) } |
| @@ -521,6 +528,13 @@ extension EditorScreen { | |||
| 521 | _ = editor.perform(ImportTable(contents: contents)) | 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 | /// org-crypt's commands at the caret, through the passphrase sheet. | 538 | /// org-crypt's commands at the caret, through the passphrase sheet. |
| 525 | private func crypt(_ id: String) { | 539 | private func crypt(_ id: String) { |
| 526 | guard let editor else { return } | 540 | guard let editor else { return } |
Sources/OrgstarMobile/ExportMenu.swift +36 −3
| @@ -43,10 +43,14 @@ struct OrgExport: Transferable { | |||
| 43 | let settings: OrgSettings | 43 | let settings: OrgSettings |
| 44 | /// Made on the main actor, where the editor's highlighter is. | 44 | /// Made on the main actor, where the editor's highlighter is. |
| 45 | let codeStyle: HTMLCodeStyle | 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 | var fileName: String { | 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 | .lastPathComponent | 54 | .lastPathComponent |
| 51 | } | 55 | } |
| 52 | 56 | ||
| @@ -73,6 +77,7 @@ struct OrgExport: Transferable { | |||
| 73 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) | 77 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 74 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) | 78 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) |
| 75 | let url = folder.appendingPathComponent(fileName) | 79 | let url = folder.appendingPathComponent(fileName) |
| 80 | let text = subtree.flatMap { ExportScope.subtree(self.text, at: $0, settings: settings) } ?? self.text | ||
| 76 | switch format { | 81 | switch format { |
| 77 | case .html: | 82 | case .html: |
| 78 | try HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle, language: OrgPreferences.exportLanguage()) | 83 | try HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle, language: OrgPreferences.exportLanguage()) |
| @@ -80,7 +85,8 @@ struct OrgExport: Transferable { | |||
| 80 | case .markdown: | 85 | case .markdown: |
| 81 | try MarkdownExport.document(text, settings: settings, path: path).write(to: url, atomically: true, encoding: .utf8) | 86 | try MarkdownExport.document(text, settings: settings, path: path).write(to: url, atomically: true, encoding: .utf8) |
| 82 | case .icalendar: | 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 | case .docx: | 90 | case .docx: |
| 85 | let html = HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle, language: OrgPreferences.exportLanguage()) | 91 | let html = HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle, language: OrgPreferences.exportLanguage()) |
| 86 | try await RichTextExport.docx(html: html, directory: URL(fileURLWithPath: path).deletingLastPathComponent()).write(to: url, options: .atomic) | 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 | @Environment(MobileModel.self) private var model | 112 | @Environment(MobileModel.self) private var model |
| 107 | /// Shows what happened, as the screen's message line does. | 113 | /// Shows what happened, as the screen's message line does. |
| 108 | var report: (String) -> Void = { _ in } | 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 | var body: some View { | 118 | var body: some View { |
| 111 | Menu("Export", systemImage: "square.and.arrow.up") { | 119 | Menu("Export", systemImage: "square.and.arrow.up") { |
| @@ -114,6 +122,9 @@ struct ExportMenu: View { | |||
| 114 | codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme)) | 122 | codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme)) |
| 115 | ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) } | 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 | Divider() | 128 | Divider() |
| 118 | Button("Copy as Rich Text", systemImage: "doc.on.doc") { copy() } | 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 | #endif | 169 | #endif |