|
| 1 | + |
| 2 | + |
| 3 | +# Image Tool |
| 4 | + |
| 5 | +Image Block for the [CodeX Editor](https://ifmo.su/editor). |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- Uploading file from the device |
| 12 | +- Pasting copied content from the web |
| 13 | +- Pasting images by drag-n-drop |
| 14 | +- Allows to add border, background |
| 15 | +- Allows to stretch image to the container's full-width |
| 16 | + |
| 17 | +**Note** This Tool requires server-side implementation for file uploading. See [backend response format](#server-format) for more details. |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +### Install via NPM |
| 22 | + |
| 23 | +Get the package |
| 24 | + |
| 25 | +```shell |
| 26 | +npm i --save-dev codex.editor.image |
| 27 | +``` |
| 28 | + |
| 29 | +Include module at your application |
| 30 | + |
| 31 | +```javascript |
| 32 | +const ImageTool = require('codex.editor.image'); |
| 33 | +``` |
| 34 | + |
| 35 | +### Download to your project's source dir |
| 36 | + |
| 37 | +1. Upload folder `dist` from repository |
| 38 | +2. Add `dist/bundle.js` file to your page. |
| 39 | + |
| 40 | +### Load from CDN |
| 41 | + |
| 42 | +You can load specific version of package from [jsDelivr CDN](https://www.jsdelivr.com/package/npm/codex.editor.image). |
| 43 | + |
| 44 | +`https://cdn.jsdelivr.net/npm/[email protected]` |
| 45 | + |
| 46 | +Then require this script on page with CodeX Editor through the `<script src=""></script>` tag. |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +Add a new Tool to the `tools` property of the CodeX Editor initial config. |
| 51 | + |
| 52 | +```javascript |
| 53 | +var editor = CodexEditor({ |
| 54 | + ... |
| 55 | + |
| 56 | + tools: { |
| 57 | + ... |
| 58 | + image: { |
| 59 | + class: ImageTool, |
| 60 | + config: { |
| 61 | + url: 'http://localhost:8008/', // Your backend uploader endpoint |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + ... |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +## Config Params |
| 71 | + |
| 72 | +| Field | Type | Description | |
| 73 | +| ----- | -------- | ------------------ | |
| 74 | +| url | `string` | **Required** Path for file uploading | |
| 75 | +| field | `string` | (default: `image`) Name of uploaded image field in POST request | |
| 76 | +| types | `string` | (default: `image/*`) Mime-types of files that can be [accepted with file selection](https://github.com/codex-team/ajax#accept-string).| |
| 77 | +| captionPlaceholder | `string` | (default: `Caption`) Placeholder for Caption input | |
| 78 | + |
| 79 | +## Tool's settings |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +1. Add border |
| 84 | + |
| 85 | +2. Stretch to full-width |
| 86 | + |
| 87 | +3. Add background |
| 88 | + |
| 89 | +## Output data |
| 90 | + |
| 91 | +This Tool returns `data` with following format |
| 92 | + |
| 93 | +| Field | Type | Description | |
| 94 | +| -------------- | --------- | ------------------------------- | |
| 95 | +| file | `object` | Uploaded file data. Any data got from backend uploader. Always contain the `url` property | |
| 96 | +| caption | `string` | image's caption | |
| 97 | +| withBorder | `boolean` | add border to image | |
| 98 | +| withBackground | `boolean` | need to add background | |
| 99 | +| stretched | `boolean` | stretch image to screen's width | |
| 100 | + |
| 101 | + |
| 102 | +```json |
| 103 | +{ |
| 104 | + "type" : "image", |
| 105 | + "data" : { |
| 106 | + "file": { |
| 107 | + "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg" |
| 108 | + }, |
| 109 | + "caption" : "Roadster // tesla.com", |
| 110 | + "withBorder" : false, |
| 111 | + "withBackground" : false, |
| 112 | + "stretched" : true |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Backend response format <a name="server-format"></a> |
| 118 | + |
| 119 | +This Tool works by following scheme: |
| 120 | + |
| 121 | +1. User select file from the device |
| 122 | +2. Tool sends it to **your** backend |
| 123 | +3. Your backend should save file and return file data with JSON at specified format. |
| 124 | +4. Image tool shows saved image and stores server answer |
| 125 | + |
| 126 | +So, you can implement backend for file saving by your own way. It is a specific and trivial task depending on your |
| 127 | +environment and stack. |
| 128 | + |
| 129 | +Response of your uploader **should** cover following format: |
| 130 | + |
| 131 | +```json5 |
| 132 | +{ |
| 133 | + "success" : 1, |
| 134 | + "file": { |
| 135 | + "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", |
| 136 | + // ... and any additional fields you want |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +**success** - uploading status. 1 for successful, 0 for failed |
| 142 | + |
| 143 | +**file** - uploaded file data. **Must** contain an `url` field with full public path to the uploaded image. |
| 144 | +Also, can contain any additional fields you want to store. For example, width, height, id etc. |
| 145 | +All additional fields will be saved at the `file` object of output data. |
| 146 | + |
| 147 | + |
0 commit comments