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

quill-delta-to-react

v1.0.1

Published

Converts Quill's Delta ops to React

Downloads

476

Readme

Render Quill's Delta ops in React

See Quill, Delta

Quickstart

Installation

npm install quill-delta-to-react

Usage

import { RenderDelta } from 'quill-delta-to-react';

const ops = [
  { insert: 'Hello\n' },
  { insert: 'This is colorful', attributes: { color: '#ff0000' } },
  { insert: '\n' },
];

const App = () => {
  return (
    <RenderDelta ops={ops} />
  );
};

Principles

This library is designed to provide a lot of flexibility and extensibility. You can customize nearly anything about the rendering.

At the same time, we take performance seriously. You can have many instances of a RenderDelta component on a page, and it will render quickly and efficiently.

Finally, this library keeps dependencies to a minimum, and the dependencies that it has are kept up-to-date.

Configuration

The RenderDelta component accepts a few configuration options with the options prop as shown below:

| Option | Type | Default | Description | | ------------------------ | -------------------------------------------------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | paragraphTag | string | 'p' | A custom tag to wrap paragraph elements. | | classPrefix | string | 'ql' | A CSS class name to prefix classes for styles such as size and font. | | inlineStyles | boolean or object | false | If true or an object, use inline styles instead of classes. See the Rendering Inline Styles section below for details. | | multiLineBlockquote | boolean | true | Instead of rendering multiple blockquote elements for quotes that are consecutive and have exactly the same attributes, render them into only one. | | multiLineHeader | boolean | true | Same deal as multiLineBlockquote for headers. | | multiLineCodeBlock | boolean | true | Same deal as multiLineBlockquote for code-blocks. | | linkRel | string | none | Specifies a rel attribute's value to put on all links. This can be overridden for any individual link by specifiying a rel property with a value in the respective op's attributes. | | linkTarget | string | none | Specifies a target attribute's value to put on all links. This can be overridden for any individual link by specifiying a target property with a value in the respective op's attributes. | | allowBackgroundClasses | boolean | false | If true, classes will be added for the background attribute. | | urlSanitizer | function (url: string): string | none | A function that is called once per URL in the ops (image, video, link) for you to do sanitization. | | customTag | function (format: string, op: DeltaInsertOp): string | undefined | none | Callback allows to provide custom tag for some formats. | | customAttributes | function (op: DeltaInsertOp): { [key: string]: string } | undefined | none | Allows to provide custom HTML tag attributes. | | customClasses | function (op: DeltaInsertOp): string | string[] | undefined | none | A function that can provide custom classes for any op (except for custom op types). | | customCssStyles | function (op: DeltaInsertOp): string | string[] | undefined | none | A function that can provide custom CSS styles for any op (except for custom op types). |

Rendering Inline Styles

The easiest way to enable this is to pass the option inlineStyles: true.

You can customize styles by passing an object for inlineStyles instead. Below is how the default inline styles are configured:

const DEFAULT_INLINE_STYLES = {
  direction: (value, op) => {
    if (value === 'rtl') {
      if (op.attributes['align']) {
        return {
          direction: 'rtl',
        };
      }
      return {
        direction: 'rtl',
        textAlign: 'inherit',
      };
    }
    return undefined;
  },
  font: (value) => {
    switch (value) {
      case 'serif':
        return { fontFamily: 'Georgia, Times New Roman, serif' };
      case 'monospace':
        return { fontFamily: 'Monaco, Courier New, monospace' };
      default:
        if (typeof value === 'string') {
          return { fontFamily: value };
        }
    }
  },
  size: (value) => {
    switch (value) {
      case 'small':
        return { fontSize: '0.75em' };
      case 'large':
        return { fontSize: '1.5em' };
      case 'huge':
        return { fontSize: '2.5em' };
      default:
        return undefined;
    }
  },
  indent: (value, op) => {
    const indentSize = Number(value) * 3;
    return {
      [op.attributes['direction'] === DirectionType.Rtl
        ? 'paddingRight'
        : 'paddingLeft']: `${indentSize}em`,
    };
  },
};

Keys to this object are the names of attributes from Quill. The values are a function that takes the value of the attribute and the Op object and returns an object of CSS properties (or undefined).

Advanced Custom Rendering

In addition to providing custom HTML element tags, classes, styles, and any other attribute, you can define custom Op types and provide a rendering function for these types with a customRenderer prop.

By default your custom ops will be treated as inlines, but you can set the renderAsBlock: true property in an op's attributes to have it rendered as a block.