Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let package = Package(
name: "GutenbergKit",
platforms: [.iOS(.v15), .macOS(.v14)],
platforms: [.iOS(.v16), .macOS(.v14)],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used some iOS 16 APIs. The apps require iOS 16, so we are good.

products: [
.library(name: "GutenbergKit", targets: ["GutenbergKit"])
],
Expand Down
53 changes: 47 additions & 6 deletions ios/Demo-iOS/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,48 @@ struct ContentView: View {

let remoteEditorConfigurations: [EditorConfiguration] = [.template]

@State private var isShowingDefaultEditor = false

// Configuration settings
@AppStorage("enableNativeBlockInserter") private var enableNativeBlockInserter = true
@AppStorage("enablePlugins") private var enablePlugins = false
@AppStorage("hideTitle") private var hideTitle = false
@AppStorage("autoFocusOnLoad") private var autoFocusOnLoad = true
@AppStorage("themeStyles") private var themeStyles = true

var body: some View {
List {
Section {
NavigationLink {
EditorView(configuration: .default)
Button {
isShowingDefaultEditor = true
} label: {
Text("Bundled Editor")
}
}

Section {
Toggle("Native Block Inserter", isOn: $enableNativeBlockInserter)
Toggle("Enable Plugins", isOn: $enablePlugins)
Toggle("Hide Title", isOn: $hideTitle)
Toggle("Auto Focus on Load", isOn: $autoFocusOnLoad)
Toggle("Theme Styles", isOn: $themeStyles)
} header: {
Text("Configuration")
} footer: {
Button("Reset") {
enableNativeBlockInserter = true
enablePlugins = false
hideTitle = false
autoFocusOnLoad = true
themeStyles = true
}
.padding(.vertical, 12)
}

Section {
ForEach(remoteEditorConfigurations, id: \.siteURL) { configuration in
NavigationLink {
EditorView(configuration: configuration)
EditorView(configuration: configureWithSettings(configuration))
} label: {
Text(URL(string: configuration.siteURL)?.host ?? configuration.siteURL)
}
Expand All @@ -39,6 +67,11 @@ struct ContentView: View {
}
}
}
.fullScreenCover(isPresented: $isShowingDefaultEditor) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to show modally as in the app.

NavigationView {
EditorView(configuration: configureWithSettings(.default))
}
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
Expand All @@ -57,19 +90,27 @@ struct ContentView: View {
} label: {
Image(systemName: "arrow.clockwise")
}

}
}
}

// Helper function to apply settings to configuration
private func configureWithSettings(_ configuration: EditorConfiguration) -> EditorConfiguration {
var config = configuration
config.enableNativeBlockInserter = enableNativeBlockInserter
config.plugins = enablePlugins
config.hideTitle = hideTitle
config.autoFocusOnLoad = autoFocusOnLoad
config.themeStyles = themeStyles
return config
}
}

private extension EditorConfiguration {

static var template: Self {
var configuration = EditorConfiguration.default

#warning("1. Update the property values below")
#warning("2. Install the Jetpack plugin to the site")
configuration.siteURL = "https://modify-me.com"
configuration.authHeader = "Insert the Authorization header value here"

Expand Down
105 changes: 103 additions & 2 deletions ios/Demo-iOS/Sources/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import GutenbergKit
struct EditorView: View {
private let configuration: EditorConfiguration

@Environment(\.dismiss) private var dismiss

init(configuration: EditorConfiguration) {
self.configuration = configuration
}

var body: some View {
_EditorView(configuration: configuration)
.background(Color(.systemBackground))
.toolbar {
ToolbarItemGroup(placement: .topBarLeading) {
Button(action: {}, label: {
Button(action: { dismiss() }, label: {
Image(systemName: "xmark")
})
}
Expand All @@ -33,6 +36,7 @@ struct EditorView: View {
moreMenu
}
}
.tint(Color.primary)
}

private var moreMenu: some View {
Expand Down Expand Up @@ -79,9 +83,13 @@ private struct _EditorView: UIViewControllerRepresentable {
self.configuration = configuration
}

func makeCoordinator() -> Coordinator {
Coordinator()
}

func makeUIViewController(context: Context) -> EditorViewController {
let viewController = EditorViewController(configuration: configuration)

viewController.delegate = context.coordinator
if #available(iOS 16.4, *) {
viewController.webView.isInspectable = true
}
Expand All @@ -92,6 +100,99 @@ private struct _EditorView: UIViewControllerRepresentable {
func updateUIViewController(_ uiViewController: EditorViewController, context: Context) {
// Do nothing
}

class Coordinator: NSObject, EditorViewControllerDelegate {
func editorDidLoad(_ viewContoller: EditorViewController) {
print("Editor did load")
}

func editor(_ viewContoller: EditorViewController, didDisplayInitialContent content: String) {
print("Editor displayed initial content")
}

func editor(_ viewContoller: EditorViewController, didEncounterCriticalError error: Error) {
print("Editor critical error: \(error)")
}

func editor(_ viewController: EditorViewController, didUpdateContentWithState state: EditorState) {
// Handle content updates
}

func editor(_ viewController: EditorViewController, didUpdateHistoryState state: EditorState) {
// Handle history updates
}

func editor(_ viewController: EditorViewController, didUpdateFeaturedImage mediaID: Int) {
print("Featured image updated: \(mediaID)")
}

func editor(_ viewController: EditorViewController, didLogException error: GutenbergJSException) {
print("JS Exception: \(error.message)")
}

func editor(_ viewController: EditorViewController, didRequestMediaFromSiteMediaLibrary config: OpenMediaLibraryAction) {
print("Requested site media library")
}

func getMediaPickerController(for viewController: EditorViewController, parameters: MediaPickerParameters) -> (any MediaPickerController)? {
MockMediaPickerController()
}
}
}

private struct MockMediaPickerController: MediaPickerController {
var actions: [[MediaPickerAction]] = [[
MediaPickerAction(
id: "image-playground",
title: "Image Playground",
image: UIImage(systemName: "apple.image.playground")!
) { presentingViewController, completion in
showPickerAlert(for: "Image Playground", in: presentingViewController, completion: completion)
},
MediaPickerAction(
id: "files",
title: "Files",
image: UIImage(systemName: "folder")!
) { presentingViewController, completion in
showPickerAlert(for: "Files", in: presentingViewController, completion: completion)
}
], [
MediaPickerAction(
id: "free-photos",
title: "Free Photos Library",
image: UIImage(systemName: "photo.on.rectangle")!
) { presentingViewController, completion in
showPickerAlert(for: "Free Photos Library", in: presentingViewController, completion: completion)
},
MediaPickerAction(
id: "free-gifs",
title: "Free GIF Library",
image: UIImage(systemName: "photo.stack")!
) { presentingViewController, completion in
showPickerAlert(for: "Free GIF Library", in: presentingViewController, completion: completion)
}
]]
}

private func showPickerAlert(for pickerName: String, in viewController: UIViewController, completion: @escaping ([MediaInfo]) -> Void) {
let alert = UIAlertController(
title: "Media Picker",
message: "Picker not implemented: \(pickerName)",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in
completion([])
})
viewController.present(alert, animated: true)
}

private extension UIViewController {
var topPresentedViewController: UIViewController {
guard let presentedViewController else {
return self
}
return presentedViewController.topPresentedViewController
}
}

#Preview {
Expand Down
11 changes: 0 additions & 11 deletions ios/Sources/GutenbergKit/Sources/EditorBlock.swift

This file was deleted.

Loading