|
| 1 | +# Editron Options |
| 2 | + |
| 3 | +> You can customize most forms for the bundled _editron editors_ in your _json-schema_. For this, follow the [docs/doc-editor-options](./doc-editor-options.md). When you need to adjust the list of used [editors](./docs/doc-editron-editor.md), add [plugins](./doc-plugin.md) or modify [services](./howto-work-with-editron#services) you can add _options_ to the _editron_ constructor which is described here. |
| 4 | +
|
| 5 | +- [Options](#options) |
| 6 | +- [Option: i18n](#option-i18n) |
| 7 | +- [Option: services](#option-services) |
| 8 | +- [Option: proxy](#option-proxy) |
| 9 | + |
| 10 | + |
| 11 | +## Options |
| 12 | + |
| 13 | +Creating a new _editron_ instance you may pass an optional _options_ object |
| 14 | + |
| 15 | +```ts |
| 16 | +import { Editron } from "editron"; |
| 17 | + |
| 18 | +const options = {} |
| 19 | +const jsonSchema = { type: "object" }; |
| 20 | +const editron = new Editron(jsonSchema, data || {}, options); |
| 21 | +``` |
| 22 | + |
| 23 | +With the options supporting the following settings |
| 24 | + |
| 25 | +option | type | description |
| 26 | +----------|-----------------|---------------------- |
| 27 | +editors | EditorPlugin[] | a list of additional _editors_ that may be used to display your json-schema |
| 28 | +addDefaultEditors | boolean | if set to false, _editron_ will not add the bundled _editors_ to the list of available _editors_. |
| 29 | +i18n | object | an object containing key-string translations |
| 30 | +proxy | object | an object with proxy handlers for custom requests |
| 31 | +locationService | object | a configuration object for the location-service |
| 32 | + |
| 33 | + |
| 34 | +For example, adding a custom editor: |
| 35 | + |
| 36 | +```ts |
| 37 | +import { Editron } from "editron"; |
| 38 | +import { MyImageEditor } from "./MyImageEditor"; |
| 39 | + |
| 40 | +const jsonSchema = { type: "object" }; |
| 41 | +const editron = new Editron(jsonSchema, data || {}, { |
| 42 | + editors: [MyImageEditor] |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +### Option i18n |
| 48 | + |
| 49 | +_Editron_ uses a simple key to string translation helper, that is exposed as `translate`. This method will return a string for a unique key and performs placeholder replacement for a data-object. Example: |
| 50 | + |
| 51 | +For a key |
| 52 | + |
| 53 | +```ts |
| 54 | +{ "custom:title": "Title of {{id}}" } |
| 55 | +``` |
| 56 | + |
| 57 | +you can call translate with |
| 58 | + |
| 59 | +```ts |
| 60 | +import { translate } from "editron"; |
| 61 | +const string = translate("custom:title", { id: "Test" }); |
| 62 | +console.log(string); // "title of Test" |
| 63 | +``` |
| 64 | + |
| 65 | +You can add your own key-string pairs and overwrite strings used by _editron_ using the i8n setting: |
| 66 | + |
| 67 | +```ts |
| 68 | +import { Editron } from "editron"; |
| 69 | +const editron = new Editron(jsonSchema, data || {}, { |
| 70 | + i18n: { |
| 71 | + strings: { |
| 72 | + "toolbar:undo:tooltip": "Undo. Letzte Änderung rückgängig machen", |
| 73 | + "toolbar:redo:tooltip": "Redo. Letzte Änderung wiederherstellen", |
| 74 | + } |
| 75 | + } |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +The _i18n_ object supports translation of generated errors. Each error has its own error-code, which may be referenced for string translation like in: |
| 80 | + |
| 81 | +```ts |
| 82 | +import { Editron } from "editron"; |
| 83 | +const editron = new Editron(jsonSchema, data || {}, { |
| 84 | + i18n: { |
| 85 | + errors: { |
| 86 | + "minimum-error": "Die Zahl muss größer oder gleich {{minimum}} sein" |
| 87 | + } |
| 88 | + } |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +When a simple string-translation is not sufficient you may also pass a function |
| 93 | + |
| 94 | +```ts |
| 95 | +import { Editron, render } from "editron"; |
| 96 | +const editron = new Editron(jsonSchema, data || {}, { |
| 97 | + i18n: { |
| 98 | + errors: { |
| 99 | + "min-length-error": (editron, error) => { |
| 100 | + if (error.data.minLength === 1) { |
| 101 | + return "Es wird eine Eingabe benötigt"; |
| 102 | + } |
| 103 | + return render("Der Text muss eine Mindestlänge von {{minLength}} haben (aktuell {{length}}).", error.data); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | + |
| 111 | +### Option Services |
| 112 | + |
| 113 | +Currently, only the [LocationService](./howto-work-with-editron#locationservice) has configuration settings exposed on `locationService`: |
| 114 | + |
| 115 | + |
| 116 | +```ts |
| 117 | +import { Editron } from "editron"; |
| 118 | +const editron = new Editron(jsonSchema, data || {}, { |
| 119 | + locationService: { |
| 120 | + /** additional offset in px, to scroll into view. In case you want to adjust scroll-position on focus (e.g. skip a header). Defaults to 0 */ |
| 121 | + scrollTopOffset: 50 |
| 122 | + /** default root element, where any json-pointers (editron widgets) are searched for. Defaults to `document.body` */ |
| 123 | + rootElement: $mainFormElement; |
| 124 | + /** regular expression to identify page-target within a json-pointer. The service will emit an event `location:page` when it was changed */ |
| 125 | + pagePattern?: string; |
| 126 | + /** a scoll callback event is fired when the desired position has completed scrolling */ |
| 127 | + scrollCallback?: boolean; |
| 128 | + } |
| 129 | +}); |
| 130 | +``` |
| 131 | + |
| 132 | + |
| 133 | +### Option Proxy |
| 134 | + |
| 135 | +_Editron_ uses a frontend adapter [Foxy](https://github.com/sueddeutsche/foxy/) to configure requests outside of _editron_ to be used within _editron_ by custom _editors_. You may pass Foxy-Handlers to the _editron_ constructor, like: |
| 136 | + |
| 137 | +```ts |
| 138 | +import { Editron } from "editron"; |
| 139 | +const editron = new Editron(jsonSchema, data || {}, { |
| 140 | + proxy: { |
| 141 | + handlers: [ |
| 142 | + { |
| 143 | + use: () => true, |
| 144 | + getImageData: ({ source }) => imageService.get(source) |
| 145 | + } |
| 146 | + ] |
| 147 | + } |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +which can be accessed from any _editron_ instance by |
| 152 | + |
| 153 | +```ts |
| 154 | +const imageData = await editron.proxy().get("getImageData", { source: 123 }); |
| 155 | +``` |
| 156 | + |
| 157 | +For more details about the handlers and api, refer to the [Foxy Documentation](https://github.com/sueddeutsche/foxy/). |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +You can read [docs/howto-work-with-editron](./howto-work-with-editron.md) next |
| 163 | + |
| 164 | +[Back to README](../README.md) |
0 commit comments