Skip to content

Commit d3a7ecd

Browse files
committed
feat: image loader config
1 parent 25d46cd commit d3a7ecd

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Image Tool supports these configuration parameters:
8383
| buttonContent | `string` | Allows to override HTML content of «Select file» button |
8484
| uploader | `{{uploadByFile: function, uploadByUrl: function}}` | Optional custom uploading methods. See details below. |
8585
| actions | `array` | Array with custom actions to show in the tool's settings menu. See details below. |
86+
| imageLoader | `function` | Optional function for resolving the image URL. See details below. |
8687

8788
Note that if you don't implement your custom uploader methods, the `endpoints` param is required.
8889

@@ -281,3 +282,26 @@ var editor = EditorJS({
281282
...
282283
});
283284
```
285+
286+
287+
## Providing custom image loader
288+
289+
If your images are served with a CDN (e.g. imgix) and you prefer to store relative URLs,
290+
you can configure an `imageLoader` function to return URLs to the theird-party image service.
291+
292+
Example:
293+
294+
```js
295+
import ImageTool from '@editorjs/image';
296+
297+
var editor = EditorJS({
298+
...
299+
tools: {
300+
...
301+
image: {
302+
class: ImageTool,
303+
config: {
304+
imageLoader: (src) => `https://example.com/${src}`
305+
...
306+
});
307+
```

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import { IconAddBorder, IconStretch, IconAddBackground, IconPicture } from '@cod
6363
* @property {object} [uploader] - optional custom uploader
6464
* @property {function(File): Promise.<UploadResponseFormat>} [uploader.uploadByFile] - method that upload image by File
6565
* @property {function(string): Promise.<UploadResponseFormat>} [uploader.uploadByUrl] - method that upload image by URL
66+
* @property {function(string): string} [imageLoader] - method to resolve image URL
6667
*/
6768

6869
/**
@@ -150,6 +151,7 @@ export default class ImageTool {
150151
types: config.types || 'image/*',
151152
captionPlaceholder: this.api.i18n.t(config.captionPlaceholder || 'Caption'),
152153
buttonContent: config.buttonContent || '',
154+
imageLoader: config.imageLoader || undefined,
153155
uploader: config.uploader || undefined,
154156
actions: config.actions || [],
155157
};

src/ui.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ export default class Ui {
154154
*/
155155
const tag = /\.mp4$/.test(url) ? 'VIDEO' : 'IMG';
156156

157+
if (this.config.imageLoader) {
158+
url = this.config.imageLoader(url);
159+
}
160+
157161
const attributes = {
158162
src: url,
159163
};

0 commit comments

Comments
 (0)