Skip to content
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

[Gutenberg] Support Multiple uploads in Gallery #13404

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class GutenbergStockPhotos {
private var mediaPickerCallback: MediaPickerDidPickMediaCallback?
private let mediaInserter: GutenbergMediaInserterHelper
private unowned var gutenberg: Gutenberg
private var multipleSelection = false

init(gutenberg: Gutenberg, mediaInserter: GutenbergMediaInserterHelper) {
self.mediaInserter = mediaInserter
Expand All @@ -19,10 +20,12 @@ class GutenbergStockPhotos {
picker.delegate = self
mediaPickerCallback = callback
picker.presentPicker(origin: origin, blog: post.blog)
self.multipleSelection = multipleSelection
}
}

extension GutenbergStockPhotos: StockPhotosPickerDelegate {

func stockPhotosPicker(_ picker: StockPhotosPicker, didFinishPicking assets: [StockPhotosMedia]) {
defer {
mediaPickerCallback = nil
Expand All @@ -33,30 +36,40 @@ extension GutenbergStockPhotos: StockPhotosPickerDelegate {
return
}

// For blocks that support multiple uploads this will upload all images.
// If multiple uploads are not supported then it will seperate them out to Image Blocks.
multipleSelection ? insertOnBlock(with: assets) : insertSingleImages(assets)
}

/// Adds the given image object to the requesting block and seperates multiple images to seperate image blocks
/// - Parameter asset: Stock Media object to add.
func insertSingleImages(_ assets: [StockPhotosMedia]) {
// Append the first item via callback given by Gutenberg.
if let firstItem = assets.first {
insertOnBlock(with: firstItem)
insertOnBlock(with: [firstItem])
}
// Append the rest of images via `.appendMedia` event.
// Ideally we would send all picked images via the given callback, but that seems to not be possible yet.
Copy link
Contributor

Choose a reason for hiding this comment

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

np: although not part of the changeset, should this comment be updated (i.e. does this PR make this possible now)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question! When I tested it out by forcing the Image Block down the multiple upload path with all of the images, it would only upload the first image and drop the rest. With the Gallery Block enabled now, it might be worth revisiting how to handle this case.

appendOnNewBlocks(assets: assets.dropFirst())
}

/// Adds the given image object to the requesting Image Block
/// - Parameter asset: Stock Media object to add.
func insertOnBlock(with asset: StockPhotosMedia) {
/// Adds the given images to the requesting block
/// - Parameter assets: Stock Media objects to add.
func insertOnBlock(with assets: [StockPhotosMedia]) {
guard let callback = mediaPickerCallback else {
return assertionFailure("Image picked without callback")
}

guard let media = self.mediaInserter.insert(exportableAsset: asset, source: .giphy) else {
callback([])
return
}
let mediaUploadID = media.gutenbergUploadID
callback([MediaInfo(id: mediaUploadID, url: asset.URL.absoluteString, type: media.mediaTypeString)])
}
let mediaInfo = assets.compactMap({ (asset) -> MediaInfo? in
guard let media = self.mediaInserter.insert(exportableAsset: asset, source: .giphy) else {
return nil
}
let mediaUploadID = media.gutenbergUploadID
return MediaInfo(id: mediaUploadID, url: asset.URL.absoluteString, type: media.mediaTypeString)
})

callback(mediaInfo)
}

/// Create a new image block for each of the image objects in the slice.
/// - Parameter assets: Stock Media objects to append.
Expand Down