11import SwiftUI
22import GutenbergKit
33
4- let editorURL : URL ? = ProcessInfo . processInfo. environment [ " GUTENBERG_EDITOR_URL " ] . flatMap ( URL . init)
5-
64struct ContentView : View {
5+ private let remoteEditors : [ RemoteEditorRow ] = [
6+ . init( id: " template " , configuration: . template)
7+ ]
8+
9+ @State private var isDefaultEditorShown = false
10+ @State private var selectedRemoteEditor : RemoteEditorRow ?
711
8- let remoteEditorConfigurations : [ EditorConfiguration ] = [ . template ]
12+ @ AppStorage ( " isNativeInserterEnabled " ) private var isNativeInserterEnabled = false
913
1014 var body : some View {
1115 List {
1216 Section {
13- NavigationLink {
14- EditorView ( configuration: . default)
15- } label: {
16- Text ( " Bundled Editor " )
17+ Button ( " Show Editor " ) {
18+ isDefaultEditorShown = true
1719 }
1820 }
1921
2022 Section {
21- ForEach ( remoteEditorConfigurations, id: \. siteURL) { configuration in
22- NavigationLink {
23- EditorView ( configuration: configuration)
24- } label: {
25- Text ( URL ( string: configuration. siteURL) ? . host ?? configuration. siteURL)
23+ ForEach ( remoteEditors) { editor in
24+ Button ( editor. title) {
25+ selectedRemoteEditor = editor
2626 }
2727 }
2828
29- if remoteEditorConfigurations . isEmpty {
29+ if remoteEditors . isEmpty {
3030 Text ( " Add `EditorConfiguration` instances to the `remoteEditorConfigurations` array to launch remote editors here. " )
3131 }
3232 } header: {
@@ -38,18 +38,32 @@ struct ContentView: View {
3838 Text ( " Note: The editor is backed by the compiled web app created by `make build`. " )
3939 }
4040 }
41+
42+ Section ( " Configuration " ) {
43+ Toggle ( " Native Inserter " , isOn: $isNativeInserterEnabled)
44+ }
45+ }
46+ . fullScreenCover ( isPresented: $isDefaultEditorShown) {
47+ NavigationView {
48+ EditorView ( configuration: preconfigure ( . default) )
49+ }
50+ }
51+ . fullScreenCover ( item: $selectedRemoteEditor) { editor in
52+ NavigationView {
53+ EditorView ( configuration: preconfigure ( editor. configuration) )
54+ }
4155 }
4256 . toolbar {
4357 ToolbarItem ( placement: . primaryAction) {
4458 Button {
4559 Task {
4660 NSLog ( " Start to fetch assets " )
47- for configuration in remoteEditorConfigurations {
48- let library = EditorAssetsLibrary ( configuration: configuration)
61+ for editor in remoteEditors {
62+ let library = EditorAssetsLibrary ( configuration: editor . configuration)
4963 do {
5064 try await library. fetchAssets ( )
5165 } catch {
52- NSLog ( " Failed to fetch assets for \( configuration. siteURL) : \( error) " )
66+ NSLog ( " Failed to fetch assets for \( editor . configuration. siteURL) : \( error) " )
5367 }
5468 }
5569 NSLog ( " Done fetching assets " )
@@ -61,6 +75,22 @@ struct ContentView: View {
6175 }
6276 }
6377 }
78+
79+ private func preconfigure( _ configuration: EditorConfiguration ) -> EditorConfiguration {
80+ configuration
81+ . toBuilder ( )
82+ . setNativeInserterEnabled ( isNativeInserterEnabled)
83+ . build ( )
84+ }
85+ }
86+
87+ private struct RemoteEditorRow : Identifiable {
88+ let id : String
89+ let configuration : EditorConfiguration
90+
91+ var title : String {
92+ URL ( string: configuration. siteURL) ? . host ?? configuration. siteURL
93+ }
6494}
6595
6696private extension EditorConfiguration {
0 commit comments