|
1 | 1 | import imageCompression from './index' |
2 | | -// import compress from './image-compression' |
3 | | -// import { getNewCanvasAndCtx } from './utils' |
| 2 | +import compress from './image-compression' |
| 3 | +import { getNewCanvasAndCtx } from './utils' |
4 | 4 |
|
5 | 5 | let cnt = 0 |
6 | 6 | let imageCompressionLibUrl |
@@ -29,48 +29,47 @@ const worker = createWorker(() => { |
29 | 29 | }) |
30 | 30 | }) |
31 | 31 |
|
32 | | -// function createSourceObject (str) { |
33 | | -// return URL.createObjectURL(new Blob([str], { type: 'application/javascript' })) |
34 | | -// } |
| 32 | +function createSourceObject (str) { |
| 33 | + return URL.createObjectURL(new Blob([str], { type: 'application/javascript' })) |
| 34 | +} |
35 | 35 |
|
36 | 36 | export function compressOnWebWorker (file, options) { |
37 | 37 | return new Promise(async (resolve, reject) => { |
38 | 38 | if (!imageCompressionLibUrl) { |
39 | | - if (typeof document !== 'undefined' && 'currentScript' in document) { |
40 | | - imageCompressionLibUrl = document.currentScript.src |
41 | | - } else { |
42 | | - return reject(new Error('Cannot obtain library url to be loaded in web worker')) |
43 | | - } |
| 39 | + imageCompressionLibUrl = createSourceObject(` |
| 40 | + function imageCompression (){return (${imageCompression}).apply(null, arguments)} |
| 41 | +
|
| 42 | + imageCompression.getDataUrlFromFile = ${imageCompression.getDataUrlFromFile} |
| 43 | + imageCompression.getFilefromDataUrl = ${imageCompression.getFilefromDataUrl} |
| 44 | + imageCompression.loadImage = ${imageCompression.loadImage} |
| 45 | + imageCompression.drawImageInCanvas = ${imageCompression.drawImageInCanvas} |
| 46 | + imageCompression.drawFileInCanvas = ${imageCompression.drawFileInCanvas} |
| 47 | + imageCompression.canvasToFile = ${imageCompression.canvasToFile} |
| 48 | + imageCompression.getExifOrientation = ${imageCompression.getExifOrientation} |
| 49 | + imageCompression.handleMaxWidthOrHeight = ${imageCompression.handleMaxWidthOrHeight} |
| 50 | + imageCompression.followExifOrientation = ${imageCompression.followExifOrientation} |
| 51 | +
|
| 52 | + getDataUrlFromFile = imageCompression.getDataUrlFromFile |
| 53 | + getFilefromDataUrl = imageCompression.getFilefromDataUrl |
| 54 | + loadImage = imageCompression.loadImage |
| 55 | + drawImageInCanvas = imageCompression.drawImageInCanvas |
| 56 | + drawFileInCanvas = imageCompression.drawFileInCanvas |
| 57 | + canvasToFile = imageCompression.canvasToFile |
| 58 | + getExifOrientation = imageCompression.getExifOrientation |
| 59 | + handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight |
| 60 | + followExifOrientation = imageCompression.followExifOrientation |
| 61 | +
|
| 62 | + getNewCanvasAndCtx = ${getNewCanvasAndCtx} |
| 63 | + |
| 64 | + CustomFileReader = FileReader |
| 65 | + |
| 66 | + CustomFile = File |
| 67 | + |
| 68 | + function _slicedToArray(arr, n) { return arr } |
| 69 | +
|
| 70 | + function compress (){return (${compress}).apply(null, arguments)} |
| 71 | + `) |
44 | 72 | } |
45 | | - // if (!imageCompressionLibUrl) { |
46 | | - // imageCompressionLibUrl = createSourceObject(` |
47 | | - // function imageCompression (){return (${imageCompression}).apply(null, arguments)} |
48 | | - // |
49 | | - // imageCompression.getDataUrlFromFile = ${imageCompression.getDataUrlFromFile} |
50 | | - // imageCompression.getFilefromDataUrl = ${imageCompression.getFilefromDataUrl} |
51 | | - // imageCompression.loadImage = ${imageCompression.loadImage} |
52 | | - // imageCompression.drawImageInCanvas = ${imageCompression.drawImageInCanvas} |
53 | | - // imageCompression.drawFileInCanvas = ${imageCompression.drawFileInCanvas} |
54 | | - // imageCompression.canvasToFile = ${imageCompression.canvasToFile} |
55 | | - // imageCompression.getExifOrientation = ${imageCompression.getExifOrientation} |
56 | | - // imageCompression.handleMaxWidthOrHeight = ${imageCompression.handleMaxWidthOrHeight} |
57 | | - // imageCompression.followExifOrientation = ${imageCompression.followExifOrientation} |
58 | | - // |
59 | | - // getDataUrlFromFile = imageCompression.getDataUrlFromFile |
60 | | - // getFilefromDataUrl = imageCompression.getFilefromDataUrl |
61 | | - // loadImage = imageCompression.loadImage |
62 | | - // drawImageInCanvas = imageCompression.drawImageInCanvas |
63 | | - // drawFileInCanvas = imageCompression.drawFileInCanvas |
64 | | - // canvasToFile = imageCompression.canvasToFile |
65 | | - // getExifOrientation = imageCompression.getExifOrientation |
66 | | - // handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight |
67 | | - // followExifOrientation = imageCompression.followExifOrientation |
68 | | - // |
69 | | - // getNewCanvasAndCtx = ${getNewCanvasAndCtx} |
70 | | - // |
71 | | - // function compress (){return (${compress}).apply(null, arguments)} |
72 | | - // `) |
73 | | - // } |
74 | 73 | let id = cnt++ |
75 | 74 |
|
76 | 75 | function handler (e) { |
|
0 commit comments