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

Custom download function #224

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Image Tool supports these configuration parameters:
| captionPlaceholder | `string` | (default: `Caption`) Placeholder for Caption input |
| buttonContent | `string` | Allows to override HTML content of «Select file» button |
| uploader | `{{uploadByFile: function, uploadByUrl: function}}` | Optional custom uploading methods. See details below. |
| downloader | `{{download: function}}` | Optional custom for downloading image. See details below. |
| actions | `array` | Array with custom actions to show in the tool's settings menu. See details below. |

Note that if you don't implement your custom uploader methods, the `endpoints` param is required.
Expand Down Expand Up @@ -296,3 +297,45 @@ var editor = EditorJS({
...
});
```

## Providing custom download method

As mentioned at the Config Params section, you have an ability to provide own custom downloading methods.
It is a quite simple: implement `download` method and pass them via `downloader` config param.


| Method | Arguments | Return value | Description |
| -------------- | --------- | ------------- | ------------|
| download | `url`, `File` | `url | blobUrl` | Custom download function to get image data |

Example:

```js
import ImageTool from '@editorjs/image';

var editor = EditorJS({
...

tools: {
...
image: {
class: ImageTool,
config: {
/**
* Custom downloader
*/
downloader: {
async download(url: string, file: any) {
const data = await getImageData();
const blob = new Blob([data]);
const blobUrl = URL.createObjectURL(blob);
return blobUrl;
},
},
}
}
}

...
});
```
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { IconAddBorder, IconStretch, IconAddBackground, IconPicture } from '@cod
* @property {object} additionalRequestHeaders - allows to pass custom headers with Request
* @property {string} buttonContent - overrides for Select File button
* @property {object} [uploader] - optional custom uploader
* @property {object} [downloader] - optional custom downloader
* @property {function(File): Promise.<UploadResponseFormat>} [uploader.uploadByFile] - method that upload image by File
* @property {function(string): Promise.<UploadResponseFormat>} [uploader.uploadByUrl] - method that upload image by URL
*/
Expand Down Expand Up @@ -149,6 +150,7 @@ export default class ImageTool {
captionPlaceholder: this.api.i18n.t(config.captionPlaceholder || 'Caption'),
buttonContent: config.buttonContent || '',
uploader: config.uploader || undefined,
downloader: config.downloader || undefined,
actions: config.actions || [],
};

Expand Down Expand Up @@ -381,7 +383,7 @@ export default class ImageTool {
this._data.file = file || {};

if (file && file.url) {
this.ui.fillImage(file.url);
this.ui.fillImage(file.url, file);
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,23 @@ export default class Ui {
* @param {string} url - image source
* @returns {void}
*/
fillImage(url) {
async fillImage(url, file) {
let resultUrl;

// Check if downloader function exists in the config
if (this.config.downloader) {
resultUrl = await this.config.downloader.download(url, file);
} else {
resultUrl = url;
}

/**
* Check for a source extension to compose element correctly: video tag for mp4, img — for others
*/
const tag = /\.mp4$/.test(url) ? 'VIDEO' : 'IMG';
const tag = /\.mp4$/.test(resultUrl) ? 'VIDEO' : 'IMG';

const attributes = {
src: url,
src: resultUrl,
};

/**
Expand Down