npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

native-json-editor

v0.3.0

Published

Native JSON text editor with indentation and syntax highlighting on the fly.

Downloads

108

Readme

JSON Editor

A HTML text editor for json inputs, with auto indentation and syntax highlight, both on the fly while typing. If this doesn't sound good enough:

  • NO libraries, frameworks or transpilers required.
  • It works with vanilla HTML / JS and it doesn't have any dependency.
  • Easy integration, as it's a single javascript file. Available as a node package as well.
  • FULL styling control with CSS.

Just add in your document:

<json-editor></json-editor>

to render this:

ezgif-com-gif-maker-2.webp

How to install

Setup for vanilla HTML / JS

Just take the file json-editor.js file. Then, add it to your application as usual.

<script src="json-editor.js" />

Setup for Node

First, add the package to your project.

npm install native-json-editor --save

Second and last, add the package to the files for which you will use it.

require('native-json-editor')

How to use it

After the setup, you will have 1 new HTML tag available to use: json-editor.

<json-editor>

Defines a new editor, similar to a vanilla textarea.

Attributes

  • value: You can initialise the editor with some json, just add your json as a STRING using this value.
  • indent: The number of "spaces" used in the indentation, default is 3.

For clarity, consider the following example:

<json-editor value='[ 1, 2, {"hello":"world"} ]' indent="5"></json-editor>

That will render:

json-editor-2.png

API

To control the editor programmatically, you can use the following getters and setters:

  • raw_string: Gets/Sets the editor content as a string. Getter returns the current editor content even if it is invalid json. It includes new line characters (\n). Example:

    // get
    const value = editor.raw_string // returns '[\n1,\n2,\n{\n"hello":"world"\n}\n]' as a string
  • string_value: Gets/Sets the editor content as a string. Getter returns the LAST VALID json string. Example:

    // get
    const value = editor.string_value // returns '[ 1, 2, {"hello":"world"} ]' as a string
    // set
    editor.string_value = '{ "other": "stuff" }' // sets the content of the editor
  • json_value: Gets/Sets the editor content as data. Getter returns the LAST VALID json value. Example:

    // get
    const value = editor.json_value // returns an array [ 1, 2, {"hello":"world"} ]
    // set
    editor.json_value = { "other": "stuff" } // sets the content of the editor from an object
  • value: Value is just an alias for string_value, with no difference but the name. I recommend to use the prefixed getters and setters for clarity, but it's on you.

    // get
    const value = editor.value // returns '[ 1, 2, {"hello":"world"} ]' as a string
    // set
    editor.value = '{ "other": "stuff" }' // sets the content of the editor
  • is_valid(): A method which returns true or false, depending if the current editor content is valid json or not.

    // get
    editor.is_valid() // returns true

Styling

To customize the style, we just need some CSS. As example we can create a "light theme" version of the editor just with:

/* a class name for the editor */
.light {
    background: #FFF;
    border: 1px solid black;
}

/* to customise the different tokens of the json syntax, we just do... */
.light::part(braces)        { color: #00b2e8 } /* curly braces {} */
.light::part(brackets)      { color: #d26a6a } /* brackets [] */
.light::part(colon)         { color: #000000 } /* colon : */
.light::part(comma)         { color: #000000 } /* comma , */
.light::part(string)        { color: #7c0d29; background: rgb(224, 213, 151) } /* strings */
.light::part(string_quotes) { color: #112ba1 } /* quotes wrapping strings */
.light::part(key)           { color: #1d0bbe; background: #20f5ff; } /* object keys */
.light::part(key_quotes)    { color: #ff2032 } /* quotes wrapping object keys */
.light::part(value)         { border: 1px solid #000 } /* object value */
.light::part(number)        { color: #ef33b0 } /* number */
.light::part(null)          { color: #21d6ff; background: #000; } /* null keyword */
.light::part(true)          { color: #ffd6d6; background: #000; } /* true keyword */
.light::part(false)         { color: #d6ffd6; background: #000; } /* false keyword */
<json-editor class="light"></json-editor>

the above will result in:

json-editor-4.png