Skip to content

Commit

Permalink
Merge pull request #13404 from wordpress-mobile/rnmobile/issue-1863-m…
Browse files Browse the repository at this point in the history
…ultiple-uploads

[Gutenberg] Support Multiple uploads in Gallery
  • Loading branch information
Chip authored Feb 10, 2020
2 parents 1a0b591 + 41e457d commit d1e2d69
Showing 1 changed file with 24 additions and 11 deletions.
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.
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

0 comments on commit d1e2d69

Please sign in to comment.