-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Integrate Native Block Inserter #24708
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
173 changes: 173 additions & 0 deletions
173
WordPress/Classes/ViewRelated/Media/MediaPicker/Helpers/MediaPickerController.swift
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,173 @@ | ||
| import UIKit | ||
| import GutenbergKit | ||
| import WordPressData | ||
| import WordPressShared | ||
|
|
||
| /// A adapter for GutenbergKit that manages media picker sources the editor. | ||
| final class MediaPickerController: GutenbergKit.MediaPickerController { | ||
| private let blog: Blog | ||
|
|
||
| init(blog: Blog) { | ||
| self.blog = blog | ||
| } | ||
|
|
||
| func getActions(for parameters: MediaPickerParameters) -> [MediaPickerActionGroup] { | ||
| let menu = MediaPickerMenu( | ||
| filter: convertFilter(parameters.filter), | ||
| isMultipleSelectionEnabled: parameters.isMultipleSelectionEnabled | ||
| ) | ||
|
|
||
| // Create a temporary controller just to extract action metadata | ||
| let tempController = MediaPickerMenuController() | ||
|
|
||
| // Define media sources with their identifiers | ||
| let sources: [(source: MediaPickerSource, id: MediaPickerID)] = [ | ||
| (.siteMedia(blog: blog), .siteMedia), | ||
| (.freePhotos(blog: blog), .freePhotos), | ||
| (.freeGIFs(blog: blog), .freeGIFs) | ||
| ] | ||
|
|
||
| // Create actions from enabled sources | ||
| let actions = sources.compactMap { source, id -> MediaPickerAction? in | ||
| guard source.isEnabled else { return nil } | ||
|
|
||
| let uiAction = createUIAction(for: source, menu: menu, controller: tempController) | ||
| guard let uiAction else { return nil } | ||
|
|
||
| return MediaPickerAction( | ||
| id: id.rawValue, | ||
| title: uiAction.title, | ||
| image: uiAction.image ?? UIImage() | ||
| ) | ||
| } | ||
|
|
||
| return [MediaPickerActionGroup(id: "primary", actions: actions)] | ||
| .filter { !$0.actions.isEmpty } | ||
| } | ||
|
|
||
| func perform(_ action: MediaPickerAction, parameters: MediaPickerParameters, from presentingViewController: UIViewController) async -> [MediaInfo] { | ||
| // Find the source for this action | ||
| guard let pickerID = MediaPickerID(rawValue: action.id) else { | ||
| return [] | ||
| } | ||
|
|
||
| let source = getSource(for: pickerID) | ||
| guard source.isEnabled else { | ||
| return [] | ||
| } | ||
|
|
||
| // Create menu and controller | ||
| let menu = MediaPickerMenu( | ||
| filter: convertFilter(parameters.filter), | ||
| isMultipleSelectionEnabled: parameters.isMultipleSelectionEnabled | ||
| ) | ||
|
|
||
| let controller = MediaPickerMenuController() | ||
|
|
||
| // Use continuation to wait for the selection | ||
| return await withCheckedContinuation { continuation in | ||
| controller.onSelection = { [weak self] selection in | ||
| guard let self else { | ||
| continuation.resume(returning: []) | ||
| return | ||
| } | ||
| let mediaInfos = self.convertSelectionToMediaInfo(selection) | ||
| continuation.resume(returning: mediaInfos) | ||
| } | ||
|
|
||
| // Create and perform the UIAction | ||
| if let uiAction = createUIAction(for: source, menu: menu, controller: controller) { | ||
| MainActor.assumeIsolated { | ||
| uiAction.performWithSender(nil, target: nil) | ||
| } | ||
| } else { | ||
| continuation.resume(returning: []) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Private Methods | ||
|
|
||
| private func getSource(for id: MediaPickerID) -> MediaPickerSource { | ||
| switch id { | ||
| case .imagePlayground: .playground | ||
| case .siteMedia: .siteMedia(blog: blog) | ||
| case .applePhotos: .photos | ||
| case .freePhotos: .freePhotos(blog: blog) | ||
| case .freeGIFs: .freeGIFs(blog: blog) | ||
| default: fatalError("Unsupported: \(id)") | ||
| } | ||
| } | ||
|
|
||
| private func convertFilter(_ filter: MediaPickerParameters.MediaFilter?) -> MediaPickerMenu.MediaFilter? { | ||
| guard let filter else { return nil } | ||
| switch filter { | ||
| case .images: return .images | ||
| case .videos: return .videos | ||
| case .all: return nil | ||
| } | ||
| } | ||
|
|
||
| private func createUIAction(for source: MediaPickerSource, menu: MediaPickerMenu, controller: MediaPickerMenuController) -> UIAction? { | ||
| switch source { | ||
| case .playground: menu.makeImagePlaygroundAction(delegate: controller) | ||
| case .siteMedia: menu.makeSiteMediaAction(blog: blog, delegate: controller) | ||
| case .photos: menu.makePhotosAction(delegate: controller) | ||
| case .freePhotos: menu.makeStockPhotos(blog: blog, delegate: controller) | ||
| case .freeGIFs: menu.makeFreeGIFAction(blog: blog, delegate: controller) | ||
| default: nil | ||
| } | ||
| } | ||
|
|
||
| private func convertSelectionToMediaInfo(_ selection: MediaPickerSelection) -> [MediaInfo] { | ||
| var output: [MediaInfo] = [] | ||
|
|
||
| for item in selection.items { | ||
| switch item { | ||
| case .media(let media): | ||
| var metadata: [String: String] = [:] | ||
| if let videopressGUID = media.videopressGUID { | ||
| metadata["videopressGUID"] = videopressGUID | ||
| } | ||
| let mediaInfo = MediaInfo( | ||
| id: media.mediaID?.int32Value, | ||
| url: media.remoteURL, | ||
| type: media.mimeType, | ||
| caption: media.caption, | ||
| title: media.filename, | ||
| alt: media.alt, | ||
| metadata: metadata | ||
| ) | ||
| output.append(mediaInfo) | ||
|
|
||
| case .external(let asset): | ||
| let mediaInfo = MediaInfo( | ||
| id: nil, | ||
| url: asset.largeURL.absoluteString, | ||
| type: asset.largeURL.preferredMimeType, | ||
| caption: asset.caption, | ||
| title: asset.name, | ||
| alt: nil, | ||
| metadata: [:] | ||
| ) | ||
| output.append(mediaInfo) | ||
|
|
||
| case .image, .pickerResult: | ||
| wpAssertionFailure("unused case") | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return output | ||
| } | ||
| } | ||
|
|
||
| private extension URL { | ||
| var preferredMimeType: String { | ||
| if let mimeType = UTType(filenameExtension: pathExtension)?.preferredMIMEType { | ||
| return mimeType | ||
| } else { | ||
| return "application/octet-stream" | ||
| } | ||
| } | ||
| } | ||
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
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
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
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.
All the media providers succeeded in my testing accept for this "Free photos" provider. When selecting an image, the request always resulted in a 500 response.
I copied each request as a
curlcommand from Proxyman. When comparing the commands, I noted that the GIF provider included the.giffile ending; contrastingly, the "Free photos" provider lacked a file ending. When I manually added a.jpgfile ending, the request succeeded.Do you experience this? What might causing these 500 responses?