-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ImageExtract.ts
39 lines (34 loc) · 1.27 KB
/
ImageExtract.ts
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
import { Blob, URL } from '@ephox/dom-globals';
import { Future, Futures, Id } from '@ephox/katamari';
import * as BlobConversions from '../api/BlobConversions';
import * as ResultConversions from '../api/ResultConversions';
import ImageAsset from './ImageAsset';
import { ImageAssetAdt } from './ImageAssetTypes';
/**
* Converts a blob into a Future<ImageAsset>.
*/
const single = (img: Blob) => {
const objurl = URL.createObjectURL(img);
return singleWithUrl(img, objurl);
};
const singleWithUrl = (img: Blob, objurl: string) => {
return Future.nu((callback: (data: ImageAssetAdt) => void) => {
BlobConversions.blobToDataUri(img).then((datauri) => {
const ir = ResultConversions.fromBlobAndUrlSync(img, datauri);
const id = Id.generate('image');
const asset = ImageAsset.blob(id, ir, objurl);
callback(asset);
});
});
};
/**
* Converts a list of files into a list of ImageAssets. This is
* asynchronous. The assets are passed to the callback
* @param imgs the list of files
* @param callback the callback function for the {BlobImageAsset[]}
*/
const multiple = (imgs: Blob[]): Future<ImageAssetAdt[]> => {
// edge case: where a drop of a non-file takes place
return Futures.traverse(imgs, single);
};
export { multiple, single, singleWithUrl };