[![npm version](https://badge.fury.io/js/vue3-ts-jsoneditor.svg)](https://badge.fury.io/js/vue3-ts-jsoneditor) ![NPM](https://img.shields.io/npm/l/vue3-ts-jsoneditor) ![npm](https://img.shields.io/npm/dm/vue3-ts-jsoneditor) ## πŸ‡ΊπŸ‡¦πŸ‡ΊπŸ‡¦πŸ‡ΊπŸ‡¦ [Stand With Ukraine](https://www.standwithukraine.how/) πŸ‡ΊπŸ‡¦πŸ‡ΊπŸ‡¦πŸ‡ΊπŸ‡¦ # vue3-ts-jsoneditor #### Powered by [svelte-jsoneditor](https://www.npmjs.com/package/svelte-jsoneditor) ### πŸš€ Advantages - SSR support - Full width button - Automatic dynamic import of query languages - Automatic dynamic import of dark theme ## πŸ•Ή Demo > you can πŸ‘€ a live demo [here](https://bestkolobok.github.io/vue3-jsoneditor/) ## β˜‘οΈ Installation ```bash npm install vue3-ts-jsoneditor ``` ## βœ… Using ### πŸ‘‰ Import globally ```javascript import {createApp} from 'vue'; import App from './App.vue'; import vue3TsJsoneditor from 'vue3-ts-jsoneditor'; const app = createApp(App); app.use(vue3TsJsoneditor, { componentName: '/componentName/', // Default: 'JsonEditor', options: { /** * * SET GLOBAL OPTIONS * * */ } }); app.mount('#app'); ``` ### 🌎 Global options ```typescript interface JSONEditorOptions { readOnly?: boolean; indentation?: number | string; tabSize?: number; mode?: Mode; mainMenuBar?: boolean; navigationBar?: boolean; statusBar?: boolean; escapeControlCharacters?: boolean; escapeUnicodeCharacters?: boolean; validator?: Validator; queryLanguagesIds?: QueryLanguageId[]; queryLanguageId?: QueryLanguageId; onRenderValue?: OnRenderValue; onClassName?: OnClassName; onRenderMenu?: OnRenderMenu; height?: string | number; fullWidthButton?: boolean; darkTheme?: boolean; } type Mode = "text" | "tree"; type QueryLanguageId = 'javascript' | 'lodash' | 'jmespath'; ``` Read more in [svelte-jsoneditor](https://www.npmjs.com/package/svelte-jsoneditor) properties
### πŸ‘‰ Import locally ```javascript ```
### πŸ‘‰ Use in template ```vue // You can use the "v-model:json" and pass json value // or you can use the "v-model:text" and pass json string // or you can use the "v-model" and pass json value. "mode" should be "tree"!!! // or you can use the "v-model" and pass json string. "mode" should be "text"!!! ``` ### ❗️❗️❗️ Important > If you want use v-model (not v-model:json or v-model:text) then the type of data depends on the mode of the editor. > If mode="tree", then the data type in the model is JSON value, if mode="text", > then the data type is JSON string. > Please be aware that in text mode v-model can contain invalid JSON: whilst typing in text mode, > a JSON document will be temporarily invalid, like when the user is typing a new string. > > It is more clear to use v-model:json for tree mode and v-model:text for text mode.
### β˜‘οΈ Slots | Slot | Description | | ---------- | ------------------ | | default | Slot content displayed before the editor is rendered. Useful for SSR |
### β˜‘οΈ Props | Name | Description | type | default | | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------: | :-----------------: | | json (v-model) | JSON value | object | array | true | false | null | number | string | undefined | | text (v-model) | JSON string | string | undefined | | modelValue (v-model) | JSON value or JSON string | object | array | true | false | null | number | string | undefined | | value | Same as modelValue | object | array | true | false | null | number | string | undefined | | mode | Open the editor in 'tree' mode or 'text' mode (formerly: code mode). | string | 'tree' | | mainMenuBar | Show the main menu bar. Default value is true. | boolean | true | | navigationBar | Show the navigation bar with, where you can see the selected path and navigate through your document from there. | boolean | true | | statusBar | Show a status bar at the bottom of the 'text' editor, showing information about the cursor location and selected contents. | boolean | true | | readOnly | Open the editor in read-only mode: no changes can be made, non-relevant buttons are hidden from the menu, and the context menu is not enabled. | boolean | false | | indentation | Number of spaces use for indentation when stringifying JSON, or a string to be used as indentation like '\t' to use a tab as indentation, or ' ' to use 4 spaces (which is equivalent to configuring indentation: 4). See also property tabSize. | number | string | 4 | | tabSize | When indentation is configured as a tab character (indentation: '\t'), tabSize configures how large a tab character is rendered. Default value is 4. Only applicable to text mode. | number | 4 | | escapeControlCharacters | When true, control characters like newline and tab are rendered as escaped characters \n and \t. Only applicable for 'tree' mode, in 'text' mode control characters are always escaped. | boolean | false | | escapeUnicodeCharacters | When true, unicode characters like ☎ and πŸ˜€ are rendered escaped like \u260e and \ud83d\ude00 | boolean | false | | validator | Validate the JSON document. Details in [svelte-jsoneditor](https://www.npmjs.com/package/svelte-jsoneditor) | function (json: JSONValue): ValidationError[] | | | queryLanguagesIds | Configure one or multiple query language that can be used in the Transform modal. The library comes with three languages: javascript, lodash or jmespath | QueryLanguage[] | [javascript] | | queryLanguageId | The id of the currently selected query language javascript, lodash or jmespath | string | | | onClassName | Add a custom class name to specific nodes, based on their path and/or value. | function (path: Path, value: any): string | undefined | | | onRenderValue | Details in [svelte-jsoneditor](https://www.npmjs.com/package/svelte-jsoneditor) | function (props: RenderValueProps) : RenderValueComponentDescription[] | | | onRenderMenu | Details in [svelte-jsoneditor](https://www.npmjs.com/package/svelte-jsoneditor) | function (mode: 'tree' | 'text', items: MenuItem[]) : MenuItem[] | undefined | | | fullWidthButton | Whether full screen switching is added | boolean | true | | height | Default height | string | number | undefined | | darkTheme | Switch to dark theme | boolean | false |
### β˜‘οΈ Events | Name | Description | Arguments | | ------------- | -------------------------------------------------------------------- | ---------------------------------------------------------------------------- | | change | Which is invoked on every change made in the JSON document. The parameter patchResult is only available in tree mode, and not in text mode, since a change in arbitrary text cannot be expressed as a JSON Patch document.| (content: Content, previousContent: Content, patchResult: JSONPatchResult | null) | | error | Event fired when an error occurs. Default implementation is to log an error in the console and show a simple alert message to the user. | (err: Error) | | change-mode | Invoked when the mode is changed. | (mode: 'tree' | 'text') | | change-query-language | Invoked when the user changes the selected query language in the TransformModal via the configuration button top right. | (queryLanguageId: string) | | focus | Fired when the editor got focus. | () | | blur | Fired when the editor lost focus. | () |
### β˜‘οΈ Use expose functions - $collapseAll - collapse all nodes - $expandAll - expand all nodes - $expand - Expand or collapse paths in the editor. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about expand() - $get - Get the current JSON document. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about get() - $set - Replace the current content. Will reset the state of the editor. See also method update(). [See more](https://www.npmjs.com/package/svelte-jsoneditor) about set() - $update - Update the loaded content, keeping the state of the editor (like expanded objects). [See more](https://www.npmjs.com/package/svelte-jsoneditor) about update() - $updateProps - Update some or all of the properties [See more](https://www.npmjs.com/package/svelte-jsoneditor) about updateProps() - $refresh - Refresh rendering of the contents, for example after changing the font size. This is only available in text mode. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about refresh() - $focus - Give the editor focus. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about focus() - $patch - Apply a JSON patch document to update the contents of the JSON document. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about patch() - $transform - Programmatically trigger clicking of the transform button in the main menu, opening the transform model. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about transform() - $scrollTo - Scroll the editor vertically such that the specified path comes into view. The path will be expanded when needed. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about scrollTo() - $findElement - Find the DOM element of a given path. Returns null when not found. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about findElement() - $acceptAutoRepair - In tree mode, invalid JSON is automatically repaired when loaded. [See more](https://www.npmjs.com/package/svelte-jsoneditor) about acceptAutoRepair()
```vue ```
### πŸŸ₯🟧🟨🟩🟦πŸŸͺ️ Styling The editor can be styled using the available CSS variables. A full list with all variables can be found [here](https://github.com/josdejong/svelte-jsoneditor/blob/main/src/lib/themes/jse-theme-default.css) ```vue ```
## πŸ”¨Build Setup ``` bash # install dependencies npm install # serve with hot reload at localhost:3080 npm run dev # build for production with minification npm run build