-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GutenbergStockPhotos.swift
70 lines (61 loc) · 2.73 KB
/
GutenbergStockPhotos.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import Gutenberg
class GutenbergStockPhotos {
private var stockPhotos: StockPhotosPicker?
private var mediaPickerCallback: MediaPickerDidPickMediaCallback?
private let mediaInserter: GutenbergMediaInserterHelper
private unowned var gutenberg: Gutenberg
init(gutenberg: Gutenberg, mediaInserter: GutenbergMediaInserterHelper) {
self.mediaInserter = mediaInserter
self.gutenberg = gutenberg
}
func presentPicker(origin: UIViewController, post: AbstractPost, multipleSelection: Bool, callback: @escaping MediaPickerDidPickMediaCallback) {
let picker = StockPhotosPicker()
stockPhotos = picker
// Forcing multiple selection while multipleSelection == false in JS side.
picker.allowMultipleSelection = true //multipleSelection
picker.delegate = self
mediaPickerCallback = callback
picker.presentPicker(origin: origin, blog: post.blog)
}
}
extension GutenbergStockPhotos: StockPhotosPickerDelegate {
func stockPhotosPicker(_ picker: StockPhotosPicker, didFinishPicking assets: [StockPhotosMedia]) {
defer {
mediaPickerCallback = nil
stockPhotos = nil
}
guard assets.isEmpty == false else {
mediaPickerCallback?(nil)
return
}
// Append the first item via callback given by Gutenberg.
if let firstItem = assets.first {
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) {
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)])
}
/// Create a new image block for each of the image objects in the slice.
/// - Parameter assets: Stock Media objects to append.
func appendOnNewBlocks(assets: ArraySlice<StockPhotosMedia>) {
assets.forEach {
if let media = self.mediaInserter.insert(exportableAsset: $0, source: .giphy) {
self.gutenberg.appendMedia(id: media.gutenbergUploadID, url: $0.URL, type: .image)
}
}
}
}