-
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
Open
crazytonyli
wants to merge
19
commits into
trunk
Choose a base branch
from
custom-post-support
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3ea75dc
Add a `PostTypeDetails`
crazytonyli bd93c51
Fetch custom post types and browse posts list
crazytonyli 2ca1b72
Update iOS simulator descriptor
crazytonyli e527d25
Include post type REST API info in `EditorConfiguration`
crazytonyli 9f16165
Add custom post types to the "Post Entities"
crazytonyli 444ee67
Use plain list style in Posts List
crazytonyli 51f48a3
Fix Swift unit tests
crazytonyli f64acb3
Fix javascript unit tests
crazytonyli 76e8411
Do not use `sequenceWithEditContext` to load posts
crazytonyli 9229bfd
Use the selected post type in "Prepare editor" button action
crazytonyli af759ee
Use "Entries" instead of "Posts"
crazytonyli fe7dcd4
Update editor configuration after toggle changes
crazytonyli c1e20d1
Move fetching post types before setting editor configuration
crazytonyli 50043d3
Run `make build`
crazytonyli a709b92
Disable the specific post API request absed on the GBKitConfig instance
crazytonyli 9428036
Run `make build`
crazytonyli 831eda8
Update wordpress-rs
crazytonyli 7a48d87
Move `WordPressAPI` instantiation out
crazytonyli 4acee98
Fix updating and using editor configuration in the demo app
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.
Show resolved
Hide 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.