1.1.0 • Published 8 months ago

quill-delta-to-react v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

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:

OptionTypeDefaultDescription
paragraphTagstring'p'A custom tag to wrap paragraph elements.
classPrefixstring'ql'A CSS class name to prefix classes for styles such as size and font.
inlineStylesboolean or objectfalseIf true or an object, use inline styles instead of classes. See the Rendering Inline Styles section below for details.
multiLineBlockquotebooleantrueInstead of rendering multiple blockquote elements for quotes that are consecutive and have exactly the same attributes, render them into only one.
multiLineHeaderbooleantrueSame deal as multiLineBlockquote for headers.
multiLineCodeBlockbooleantrueSame deal as multiLineBlockquote for code-blocks.
linkRelstringnoneSpecifies 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.
linkTargetstringnoneSpecifies 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.
allowBackgroundClassesbooleanfalseIf true, classes will be added for the background attribute.
urlSanitizerfunction (url: string): stringnoneA function that is called once per URL in the ops (image, video, link) for you to do sanitization.
customTagfunction (format: string, op: DeltaInsertOp): string | undefinednoneCallback allows to provide custom tag for some formats.
customAttributesfunction (op: DeltaInsertOp): { key: string: string } | undefinednoneAllows to provide custom HTML tag attributes.
customClassesfunction (op: DeltaInsertOp): string | string[] | undefinednoneA function that can provide custom classes for any op (except for custom op types).
customCssStylesfunction (op: DeltaInsertOp): string | string[] | undefinednoneA 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.

1.1.0

8 months ago

1.0.1

1 year ago

1.0.0

1 year ago

0.1.0

1 year ago