Skip to content

Commit d9c1786

Browse files
committed
Add a way to configure the remote editor
1 parent f13b38a commit d9c1786

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

ios/Demo-iOS/Sources/ContentView.swift

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
import SwiftUI
22
import GutenbergKit
33

4-
let editorURL: URL? = ProcessInfo.processInfo.environment["GUTENBERG_EDITOR_URL"].flatMap(URL.init)
5-
64
struct 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

6696
private extension EditorConfiguration {

ios/Demo-iOS/Sources/EditorView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ struct EditorView: View {
1414

1515
var body: some View {
1616
_EditorView(configuration: configuration, viewModel: viewModel)
17-
.navigationBarBackButtonHidden(true)
1817
.toolbar { toolbar }
1918
}
2019

0 commit comments

Comments
 (0)