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-editor-wysyvig

v0.0.21

Published

react,wysyvig,editor,javascript

Downloads

912

Readme

react-editor-wysyvig v0.0.x

Simple wysyvig editor on React .

Attention ! Editor for only desktop browser not mobile !!
This is a simple editor, developed for internal needs. Support for single-level nesting tags is assumed.
  1. Install
  2. Struct data of Editor component
  3. Simple use Editor
  4. Custom Editor panel
  5. Add custom tags

Install

$ npm i react-editor-wysyvig

Struct data of Editor component

The editor works with an array of data, each element of which completely describes one displayed element (symbol, image, link, etc.). See the example draw "ok":

import React, { useCallback, useState } from 'react';
import _ from 'lodash';
import Editor from 'react-editor-wysyvig';
import 'react-editor-wysyvig/style/Editor.scss';

const  DATAS = [
    {id:'1',type:'char',value:'o',style:{}},
    {id:'2',type:'char',value:'k',style:{}},
];

function App() {
    const [data, setData] = useState(DATAS);

    const doChange = (newData) => {
        setData(newData);
    };

    return (
        <div>
            <Editor onChange={doChange} data = {data}/>
        </div>
    );
}

|name|type|notes| |---|---|---| |id|string|ident| |type|string|type of tag component| |value|string|displayed value| |style|object|react css style for draw|

Simple use

The editor package includes a utility for converting simple HTML into editor data and back.

import React, { useCallback, useState } from 'react';
import _ from 'lodash';
import Editor from 'react-editor-wysyvig';
import Html from 'react-editor-wysyvig/utils/Html.js';
import 'react-editor-wysyvig/style/Editor.scss';

const HTML = 'Simple<br>text';// init html text';

function App() {
    const [data, setData] = useState(Html.toData(HTML));
    const [code, setCode] = useState('');

    const doDecode = (from) => {
        setCode(Html.fromData(from));
    };

    const decode = useCallback(_.debounce(doDecode, 1000), []);

    const doChange = (newData) => {
        setData(newData);
        decode(newData);
    };

    return (
        <>
            <div>
                <Editor onChange={doChange} data = {data}/>
            </div>
            <code>
                {code}
            </code>
        </>
    );
}

Custom Editor panel

To replace the built-in style editing panel, add your own component inside the editor.

import React, { useCallback, useState } from 'react';
import _ from 'lodash';
import Editor from 'react-editor-wysyvig';
import 'react-editor-wysyvig/style/Editor.scss';
import Panel from './Panel.jsx';

const  DATAS = [
    {id:'1',type:'char',value:'o',style:{}},
    {id:'2',type:'char',value:'k',style:{}},
];

function App() {
    const [data, setData] = useState(DATAS);

    const doChange = (newData) => {
        setData(newData);
    };

    return (
        <div>
            <Editor onChange={doChange} data = {data}>
                <Panel/>
            </Editor>
        </div>
    );
}

Example of custom Panel component

import React, {
    useCallback, useState,
} from 'react';
import Style from 'react-editor-wysyvig/utils/Style.js';
import EditorTagClass from './EditorTags/EditorTagClass.js';
import eq from 'react-editor-wysyvig/jsx/js/eq';

const Panel=({
    data,
    selects,
    onChange,
    cursor,
    onSelect,
}) =>{

    const change = (newData) => {
        if (onChange) {
            onChange(newData);
        }
    };
    const changeStyle = (modif) => {
        if (onChange) {
            let define;

            const newData = data.map((it) => {
                if (selects.find((sid) => eq.id(it.id, sid))) {
                    define = define || modif(it.style || {});
                    return {
                        ...it,
                        style: {
                            ...it.style,
                            ...define,
                        },
                    };
                }
                return { ...it };
            });

            onChange(newData);
        }
    };
    const bold = () => {
        changeStyle((style) => Style.toggle({ fontWeight: ['bold', ''] }, style));
    };

    const underline = () => {
        changeStyle((style) => Style.toggle({ textDecoration: ['underline', ''] }, style));
    };

    const italic = () => {
        changeStyle((style) => Style.toggle({ fontStyle: ['italic', ''] }, style));
    };


    return (
        <>
            <div className='editor-panel'>
                <button onClick={bold} >B</button>
                <button onClick={underline} >U</button>
                <button onClick={italic} >I</button>
            </div>
        </>
    );
}

export default Panel;

Add custom tags

To add your custom tag, you need to create a component corresponding to it and register it.

/* eslint-disable camelcase */
import React from 'react';
import get from 'react-editor-wysyvig/jsx/js/get';
import getid from 'react-editor-wysyvig/jsx/js/getid';

function Block({
    id,
    type,
    value,
    style = {},
    cursor = false,
    select = false,
    onClick,
}) {
    const doClick = (sender) => {
        if (onClick) {
            onClick({
                id, type, value, sender,
            });
        }
    };

    return (
        <div
            id={id}
            style={{ ...style }}
            className={`${cursor ? 'cursor' : ''} ${select ? 'select' : ''}` }
            onMouseDown={doClick}
        >
            {value}
        </div>
    );
}

Block.createData = (data = {}) => ({
    id: getid(),
    type: 'block',

    value: '',
    ...data,
    style: { ...get(data, ['style'], {}) },
});

export default Block;

rigister it

import React from 'react';
import EditorTagClass from 'react-editor-wysyvig/jsx/EditorTags/EditorTagClass';
//register Block
EditorTagClass.add([{block:Block}]);