iOS: export the subtree at the caret !321

merged merged by cmc on 2026-10-12 04:44 UTC · krz/orgstar:ios-subtree-export into main

2 files changed, +51 −4

Layout: unified · split

Sources/OrgstarMobile/EditorScreen.swift +15 −1
@@ -76,6 +76,8 @@ struct EditorScreen: View {
7676 case plot(TablePlot.Plot)
7777 case crypt(CryptRequest)
7878 case backlinks
79 /// The subtree at this offset, to export.
80 case exportSubtree(Int)
7981
8082 var id: String {
8183 switch self {
@@ -89,6 +91,7 @@ struct EditorScreen: View {
8991 case .plot: "plot"
9092 case .crypt(let request): request.id.uuidString
9193 case .backlinks: "backlinks"
94 case .exportSubtree: "exportSubtree"
9295 }
9396 }
9497 }
@@ -163,7 +166,9 @@ struct EditorScreen: View {
163166 Button("Encrypt Tagged Entries…", systemImage: "lock.rectangle.stack") { crypt("app.crypt.encrypt-entries") }
164167 Button("Forget Encryption Passphrase", systemImage: "key.slash") { crypt("app.crypt.forget-passphrase") }
165168 }
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 }
167172 Button("Sync Conflict Copies", systemImage: "doc.on.doc") { sheet = .syncCopies }
168173 Button("Recovered Versions", systemImage: "clock.arrow.circlepath") { sheet = .recovery }
169174 if onPaneCommand != nil, sizeClass == .regular {
@@ -247,6 +252,8 @@ struct EditorScreen: View {
247252 PlotScreen(plot: plot)
248253 case .crypt(let request):
249254 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) }
250257 case .backlinks:
251258 BacklinksScreen(workspace: workspace, session: session, path: path, caret: editor?.editContext().caret ?? 0) { location in
252259 if location.path == path { editor?.reveal(location.start) } else { model.openRequest = .heading(location) }
@@ -521,6 +528,13 @@ extension EditorScreen {
521528 _ = editor.perform(ImportTable(contents: contents))
522529 }
523530
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
524538 /// org-crypt's commands at the caret, through the passphrase sheet.
525539 private func crypt(_ id: String) {
526540 guard let editor else { return }
Sources/OrgstarMobile/ExportMenu.swift +36 −3
@@ -43,10 +43,14 @@ struct OrgExport: Transferable {
4343 let settings: OrgSettings
4444 /// Made on the main actor, where the editor's highlighter is.
4545 let codeStyle: HTMLCodeStyle
46 /// The subtree at this offset exported alone.
47 var subtree: Int?
4648
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.
4851 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)
5054 .lastPathComponent
5155 }
5256
@@ -73,6 +77,7 @@ struct OrgExport: Transferable {
7377 let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
7478 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
7579 let url = folder.appendingPathComponent(fileName)
80 let text = subtree.flatMap { ExportScope.subtree(self.text, at: $0, settings: settings) } ?? self.text
7681 switch format {
7782 case .html:
7883 try HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle, language: OrgPreferences.exportLanguage())
@@ -80,7 +85,8 @@ struct OrgExport: Transferable {
8085 case .markdown:
8186 try MarkdownExport.document(text, settings: settings, path: path).write(to: url, atomically: true, encoding: .utf8)
8287 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)
8490 case .docx:
8591 let html = HTMLExport.page(text, fallbackTitle: base, settings: settings, path: path, codeStyle: codeStyle, language: OrgPreferences.exportLanguage())
8692 try await RichTextExport.docx(html: html, directory: URL(fileURLWithPath: path).deletingLastPathComponent()).write(to: url, options: .atomic)
@@ -106,6 +112,8 @@ struct ExportMenu: View {
106112 @Environment(MobileModel.self) private var model
107113 /// Shows what happened, as the screen's message line does.
108114 var report: (String) -> Void = { _ in }
115 /// Offers the subtree at the caret alone, where there is a caret.
116 var exportSubtree: (() -> Void)?
109117
110118 var body: some View {
111119 Menu("Export", systemImage: "square.and.arrow.up") {
@@ -114,6 +122,9 @@ struct ExportMenu: View {
114122 codeStyle: HTMLCodeStyle(highlighter: MobileEditor.highlighter, theme: model.theme))
115123 ShareLink(item: export, preview: SharePreview(export.fileName)) { Text(format.name) }
116124 }
125 if let exportSubtree {
126 Button("Export Subtree…", systemImage: "list.bullet.indent") { exportSubtree() }
127 }
117128 Divider()
118129 Button("Copy as Rich Text", systemImage: "doc.on.doc") { copy() }
119130 }
@@ -133,4 +144,26 @@ struct ExportMenu: View {
133144 }
134145 }
135146}
147
148/// The subtree at `caret` shared alone, in each of the export formats.
149struct 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}
136169#endif