-
Notifications
You must be signed in to change notification settings - Fork 3
Support editing posts of custom post types #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
942f293
Add a `PostTypeDetails`
crazytonyli 1fad10f
Fetch custom post types and browse posts list
crazytonyli ee8467c
Update iOS simulator descriptor
crazytonyli d3513a7
Include post type REST API info in `EditorConfiguration`
crazytonyli 83a8d85
Add custom post types to the "Post Entities"
crazytonyli fa3d71a
Use plain list style in Posts List
crazytonyli 212a381
Fix Swift unit tests
crazytonyli cfc076a
Fix javascript unit tests
crazytonyli 7441ce7
Do not use `sequenceWithEditContext` to load posts
crazytonyli ff6993c
Use the selected post type in "Prepare editor" button action
crazytonyli e8ba2b7
Use "Entries" instead of "Posts"
crazytonyli 3a9d957
Update editor configuration after toggle changes
crazytonyli 7905162
Move fetching post types before setting editor configuration
crazytonyli 8a4f637
Disable the specific post API request absed on the GBKitConfig instance
crazytonyli 228c7c5
Update wordpress-rs
crazytonyli 3df294f
Move `WordPressAPI` instantiation out
crazytonyli e8360f9
Fix updating and using editor configuration in the demo app
crazytonyli c60333e
Run `make build`
crazytonyli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 22 additions & 4 deletions
26
ios/Demo-iOS/Gutenberg.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import SwiftUI | ||
| import WordPressAPI | ||
| import GutenbergKit | ||
|
|
||
| struct PostsListView: View { | ||
|
|
||
| @Environment(\.navigation) | ||
| private var navigation | ||
|
|
||
| @State | ||
| private var viewModel: PostsListViewModel | ||
|
|
||
| init(client: WordPressAPI, postTypeDetails: PostTypeDetails, editorConfiguration: EditorConfiguration, editorDependencies: EditorDependencies?) { | ||
| self.viewModel = PostsListViewModel( | ||
| client: client, | ||
| postTypeDetails: postTypeDetails, | ||
| editorConfiguration: editorConfiguration, | ||
| editorDependencies: editorDependencies | ||
| ) | ||
| } | ||
|
|
||
| var body: some View { | ||
| Group { | ||
| if viewModel.isLoading && viewModel.posts.isEmpty { | ||
| ProgressView("Loading entries...") | ||
| } else if let error = viewModel.error { | ||
| ContentUnavailableView { | ||
| Label("Error Loading Entries", systemImage: "exclamationmark.triangle") | ||
| } description: { | ||
| Text(error.localizedDescription) | ||
| } actions: { | ||
| Button("Retry") { | ||
| Task { | ||
| await viewModel.loadPosts() | ||
| } | ||
| } | ||
| } | ||
| } else if viewModel.posts.isEmpty { | ||
| ContentUnavailableView { | ||
| Label("No Entires", systemImage: "doc.text") | ||
| } description: { | ||
| Text("No entries found for this post type.") | ||
| } | ||
| } else { | ||
| List(viewModel.posts, id: \.id) { post in | ||
| Button { | ||
| openPost(post) | ||
| } label: { | ||
| VStack(alignment: .leading, spacing: 4) { | ||
| Text(post.title?.rendered ?? "") | ||
| .font(.headline) | ||
| if let excerpt = post.excerpt?.rendered, !excerpt.isEmpty { | ||
| Text(excerpt.strippingHTML()) | ||
| .font(.caption) | ||
| .foregroundStyle(.secondary) | ||
| .lineLimit(2) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| .listStyle(.plain) | ||
| } | ||
| } | ||
| .navigationTitle(viewModel.postTypeDetails.restBase.capitalized) | ||
| .task { | ||
| await viewModel.loadPosts() | ||
| } | ||
| } | ||
|
|
||
| private func openPost(_ post: AnyPostWithEditContext) { | ||
| let configuration = viewModel.editorConfiguration.toBuilder() | ||
| .setPostType(viewModel.postTypeDetails) | ||
| .setPostID(Int(post.id)) | ||
| .setTitle(post.title?.raw ?? "") | ||
| .setContent(post.content.raw ?? "") | ||
| .build() | ||
|
|
||
| let editor = RunnableEditor( | ||
| configuration: configuration, | ||
| dependencies: viewModel.editorDependencies | ||
| ) | ||
|
|
||
| navigation.present(editor) | ||
| } | ||
| } | ||
|
|
||
| @Observable | ||
| class PostsListViewModel { | ||
| var posts: [AnyPostWithEditContext] = [] | ||
| var isLoading = false | ||
| var error: Error? | ||
|
|
||
| let client: WordPressAPI | ||
| let postTypeDetails: PostTypeDetails | ||
| let editorConfiguration: EditorConfiguration | ||
| let editorDependencies: EditorDependencies? | ||
|
|
||
| init(client: WordPressAPI, postTypeDetails: PostTypeDetails, editorConfiguration: EditorConfiguration, editorDependencies: EditorDependencies?) { | ||
| self.client = client | ||
| self.postTypeDetails = postTypeDetails | ||
| self.editorConfiguration = editorConfiguration | ||
| self.editorDependencies = editorDependencies | ||
| } | ||
|
|
||
| @MainActor | ||
| func loadPosts() async { | ||
| guard !isLoading else { return } | ||
|
|
||
| isLoading = true | ||
| error = nil | ||
| posts = [] | ||
|
|
||
| defer { isLoading = false } | ||
|
|
||
| do { | ||
| let endpointType: PostEndpointType | ||
| if postTypeDetails.postType == "post" { | ||
| endpointType = .posts | ||
| } else if postTypeDetails.postType == "page" { | ||
| endpointType = .pages | ||
| } else { | ||
| endpointType = .custom(postTypeDetails.restBase) | ||
| } | ||
|
|
||
| var currentPage: UInt32 = 1 | ||
| let perPage: UInt32 = 20 | ||
| while true { | ||
| let params = PostListParams(page: currentPage, perPage: perPage, status: [.custom("any")]) | ||
| let fetched = try await client.posts.listWithEditContext(type: endpointType, params: params).data | ||
| self.posts.append(contentsOf: fetched) | ||
|
|
||
| if fetched.count < perPage { | ||
| break | ||
| } | ||
| currentPage += 1 | ||
| } | ||
| } catch { | ||
| self.error = error | ||
|
dcalhoun marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| private extension String { | ||
| func strippingHTML() -> String { | ||
| self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't take into consideration the toggles controlling the native inserter and network logging. The result is the editor opening with the default
falsevalue for both.This isn't a regression with the GBK library, but it's an oddity for the demo app. So, not a blocker, but we ideally address this oversight.