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

react-jycm-viewer

v0.1.0

Published

React rendering tool for JYCM (Json diff).

Downloads

80

Readme

React Json Diff Viewer

This is the renderer for JYCM.

It is very easy to use thanks to the [React Context] (https://reactjs.org/docs/context.html)

GIF-Show

gif-show

Good-to-go Example project

Here's a working demo project react-jycm-viewer-example

Usage

dependencices

yarn add react-jycm-viewer react-monacor-editor monaco-editor
yarn add -D monaco-editor-webpack-plugin

webpack config

{
    plugins: [
        // ...
        new MonacoWebpackPlugin({
            languages: ["json"],
        })
    ]
}

Use in your component

import React, { FC, useState } from 'react';
import ReactDOM from 'react-dom';

import {
  JYCMRender,
  JYCMContext,
  IUseJYCMProps,
  IJYCMRenderProps,
  useJYCM,
} from "react-jycm-viewer";

const SimpleForm: FC<{
    label: string,
}> = ({ label, children }) => {
    return <div style={{
        height: "100%",
        width: "100%",
        display: "flex",
        flexDirection: "column",
        justifyContent: "space-between",
        alignItems: "center",
        position: "relative",
        padding: "0 15px"
    }}
    >
        <div style={{ display: "inline-block" }}>{label}:</div>
        <div style={{ width: "100%" }}>{children}</div>
    </div>
}

const safeJSONCallback = (value: string, cb: (v: string) => void) => {
    try {
        JSON.parse(value);
        return cb(value)
    } catch (e) {
        return false;
    }
}

const App = () => {

    const [leftJSONStr, setLeftJSONStr] = useState(JSON.stringify(leftJson))
    const [rightJSONStr, setRightJSONStr] = useState(JSON.stringify(rightJson))
    const [jycmResultStr, setJYCMResultStr] = useState(JSON.stringify(diffResult))

    // use this can ave your time! see provider below
    const jycmContextValue = useJYCM({
        leftJsonStr,
        rightJsonStr,
        diffResult,
    });

    // In your component you can use all properties from jycm
    // using code 
    // const jycmContext = useContext(JYCMContext)!;


    return <div style={{ height: "100%", width: "100%" }}>
        <h1>Demo For JYCM render</h1>
        <div style={{ height: "100%", width: "100%", display: "flex" }}>
            <SimpleForm label="left JSON">
                <textarea
                    style={{ width: "100%", wordBreak: "break-all" }}
                    rows={5}
                    defaultValue={leftJSONStr}
                    onChange={e => { safeJSONCallback(e.target.value, setLeftJSONStr) }} />
            </SimpleForm>
            <SimpleForm label="right JSON">
                <textarea
                    style={{ width: "100%", wordBreak: "break-all" }}
                    rows={5}
                    defaultValue={rightJSONStr}
                    onChange={e => { safeJSONCallback(e.target.value, setRightJSONStr) }} />
            </SimpleForm>
            <SimpleForm label="JYCM Result">
                <textarea
                    style={{ width: "100%", wordBreak: "break-all" }}
                    rows={5}
                    defaultValue={jycmResultStr}
                    onChange={e => { safeJSONCallback(e.target.value, setJYCMResultStr) }} />
            </SimpleForm>
        </div>

        <div style={{ height: "800px", width: "100%", border: "1px solid red", marginTop: "15px" }}>
            {/********** any component under this provider can have access to the diff etc. 
            You can control the editor very easy.   **********/}
            <JYCMContext.Provider value={jycmContextValue}>
                <JYCMRender leftTitle="BenchMark" rightTitle="Actual"/>
            </JYCMContext.Provider>
        </div>
    </div>
}

ReactDOM.render(
    <App />,
    document.getElementById('root') as HTMLElement
)

API

JYCMContext

JYCMContext Shared state across the context.

It gives you

You can as follow


import React, { FC, useState } from 'react';
import ReactDOM from 'react-dom';

import {
  JYCMRender,
  JYCMContext,
  IUseJYCMProps,
  IJYCMRenderProps,
  useJYCM,
} from "react-jycm-viewer";

const CustomApp: React.FC<any> = () => {
  const { pairInfo, activeLeftJsonPath, activeRightJsonPath } =
    useJYCMContext();
  return (
    <div>
      <FormBlock
        label="activeLeftJsonPath"
        content={activeLeftJsonPath.join("->")}
      />
      <FormBlock
        label="activeRightJsonPath"
        content={activeRightJsonPath.join("->")}
      />
      <FormBlock label="Pair Info" content={pairInfo} />
    </div>
  );
};

const App: React.FC<any> = ({ leftJsonStr, rightJsonStr, diffResult, ...args }) => {
  const jycmContextValue = useJYCM({
    leftJsonStr,
    rightJsonStr,
    diffResult,
  });

  return (
    <div style={{ height: "100%" }}>
      <JYCMContext.Provider value={jycmContextValue}>
        <div style={{ display: "flex", alignItems: "center", height: "100%" }}>
          <div style={{ flexGrow: 1, height: "100%" }}>
            <JYCMRender {...args} />
          </div>
          <div style={{ width: "200px" }}>
            <CustomApp />
          </div>
        </div>
      </JYCMContext.Provider>
    </div>
  );
};

ReactDOM.render(
    <App />,
    document.getElementById('root') as HTMLElement
)