• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

josdejong/svelte-jsoneditor: A web-based tool to view, edit, format, repair, que ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

josdejong/svelte-jsoneditor

开源软件地址:

https://github.com/josdejong/svelte-jsoneditor

开源编程语言:

TypeScript 50.5%

开源软件介绍:

svelte-jsoneditor

A web-based tool to view, edit, format, transform, and validate JSON

The library is written with Svelte, but can be used in any framework (React, Vue, Angular, plain JavaScript).

JSONEditor tree mode screenshot JSONEditor text mode screenshot

Features

  • View and edit JSON
  • Has a low level text mode and high level tree mode
  • Format (beautify) and compact JSON
  • Sort, query, filter, and transform JSON
  • Repair JSON
  • JSON schema validation and pluggable custom validation
  • Color highlighting, undo/redo, search and replace
  • Utilities like a color picker and timestamp tag
  • Handles large JSON documents up to 500 MB

Install

Install via npm:

npm install svelte-jsoneditor

Remark: you may notice that svelte is a dependency in the project and ask yourself why. This is necessary when using it in a Svelte project or when using it in a TypeScript project. In the latter case, the project depends on types that are defined in the svelte package.

Use

Examples

Svelte usage

Create a JSONEditor with two-way binding bind:json:

<script>
  import { JSONEditor } from 'svelte-jsoneditor'

  let content = {
    text: undefined, // used when in text mode
    json: {
      array: [1, 2, 3],
      boolean: true,
      color: '#82b92c',
      null: null,
      number: 123,
      object: { a: 'b', c: 'd' },
      string: 'Hello World'
    }
  }
</script>

<div>
  <JSONEditor bind:content />
</div>

Or one-way binding:

<script>
  import { JSONEditor } from 'svelte-jsoneditor'

  let content = {
    text: undefined, // used when in text mode
    json: {
      greeting: 'Hello World'
    }
  }

  function handleChange(updatedContent, previousContent, patchResult) {
    // content is an object { json: JSONData } | { text: string }
    console.log('onChange: ', updatedContent, previousContent, patchResult)
    content = updatedContent
  }
</script>

<div>
  <JSONEditor {content} onChange="{handleChange}" />
</div>

Standalone bundle (use in React, Vue, Angular, plain JavaScript, ...)

The library provides a standalone bundle of the editor which can be used in any browser environment and framework. In a framework like React, Vue, or Angular, you'll need to write some wrapper code around the class interface.

Browser example loading the ES module:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JSONEditor</title>
  </head>
  <body>
    <div id="jsoneditor"></div>

    <script type="module">
      import { JSONEditor } from 'svelte-jsoneditor/dist/jsoneditor.js'

      let content = {
        text: undefined,
        json: {
          greeting: 'Hello World'
        }
      }

      const editor = new JSONEditor({
        target: document.getElementById('jsoneditor'),
        props: {
          content,
          onChange: (updatedContent, previousContent, patchResult) => {
            // content is an object { json: JSONData } | { text: string }
            console.log('onChange', updatedContent, previousContent, patchResult)
            content = updatedContent
          }
        }
      })

      // use methods get, set, update, and onChange to get data in or out of the editor.
      // Use updateProps to update properties.
    </script>
  </body>
</html>

API

constructor

Svelte component:

<script>
  import { JSONEditor } from 'svelte-jsoneditor'
</script>

<div>
  <JSONEditor {content} />
</div>

JavasScript class:

import { JSONEditor } from 'svelte-jsoneditor/dist/jsoneditor.js'

const editor = new JSONEditor({
  target: document.getElementById('jsoneditor'),
  props: {
    content,
    onChange: (updatedContent, previousContent, patchResult) => {
      // content is an object { json: JSONData } | { text: string }
      console.log('onChange', updatedContent, previousContent, patchResult)
    }
  }
})

properties

  • content: Content Pass the JSON contents to be rendered in the JSONEditor. Contents is an object containing a property json and text. Only one of the two must be defined. In case of tree mode, json is used. In case of text mode, text is used.

  • mode: 'tree' | 'text'. Open the editor in 'tree' mode (default) or 'text' mode (formerly: code mode).

  • mainMenuBar: boolean Show the main menu bar. Default value is true.

  • navigationBar: boolean Show the navigation bar with, where you can see the selected path and navigate through your document from there. Default value is true.

  • statusBar: boolean Show a status bar at the bottom of the 'text' editor, showing information about the cursor location and selected contents. Default value is true.

  • readOnly: boolean 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. Default value is false.

  • indentation: number | string 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.

  • tabSize: number 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.

  • escapeControlCharacters: boolean. False by default. 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.

  • escapeUnicodeCharacters: boolean. False by default. When true, unicode characters like and


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ohler55/ojg: Optimized JSON for Go发布时间:2022-07-09
下一篇:
krg7880/json-schema-generator: Generates draft v4 schema from a local file or a ...发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap