Quill Rich Text Editor

repository·main·Indexed 13 days ago

https://github.com/slab/quill

A modern, extensible rich text editor for web applications. Version 2.0.3 provides a customizable API to embed text editing capabilities, featuring a Delta system for content manipulation, a Range class for selection management, and support for custom blots, formats, themes, and modules.

Tokens
37.7K
Snippets
137
Records
178
Agent score
98%

What's inside Quill

  1. Overview of Quill 1.0 New Features and Modules

    main

    Quill 1.0 introduces several enhancements to formats, themes, and module configuration:

    New Formats

    • Text Formats: Superscript, subscript, inline code, blocks, headers, and blockquotes.
    • Advanced Support: Text direction and video support.
    • Lists: Support for nested lists.
    • Styling: Formats that previously used inline styles can now use CSS classes.
    • Optional Modules: Syntax highlighted code and LaTeX formulas can be added via optional modules.

    Themes

    • Snow: The standard theme.
    • Bubble: A new theme based on a floating toolbar.
    • Icons: Both themes use SVG icons that are added directly to the DOM, allowing for easy customization of active colors.

    Enhanced Modules

    • Clipboard: Allows customization of how pasted content is interpreted before reaching the editor.
    • Keyboard: Adds a context option for granular control over when keyboard bindings are triggered.
    • Toolbar: Can be configured using a simple array and allows overwriting its handlers.
  2. What is Parchment and how is it used in Quill

    main
    Parchment is a document model used by Quill to scalably support various formats. It provides the underlying architecture for Quill's formatting and content capabilities. While Parchment can be used independently, it is the core engine that allows Quill to implement features like headers, blockquotes, nested lists, and video embeds. Developers looking to understand how to implement custom formats in Quill can examine the Parchment integration within the Quill source code.
  3. What is Parchment and how to define custom nodes

    main

    Parchment is Quill's document model, providing an abstraction over the DOM. It allows developers to define new nodes or overwrite existing ones by handing control of subtrees back to the user. To exist within a Parchment document, custom nodes must implement certain methods such as getValue() and getFormat().

    Custom nodes are typically created by extending Parchment.Embed. Once defined, you must register the format with Quill using Quill.registerFormat() to make it available in the editor.

    class Equation extends Parchment.Embed {
      constructor(value) {
        super(value);
        this.value = value;
        this.domNode.setAttribute('contenteditable', false);
        katex.render(value, this.domNode);
      }
    
      getValue() {
        return this.value;
      }
    }
    
    Quill.registerFormat(Equation);
  4. What is the Delta format?

    main

    Deltas are a strict subset of JSON used to describe both the content of a Quill document and the changes made to it. Instead of using HTML, Deltas use a sequence of operations (ops) to represent text, formatting, and embeds.

    When describing a document, a Delta represents the instructions required to build that document starting from an empty state. When describing changes, a Delta represents the transformations applied to an existing document.

    Key characteristics:

    • Human-readable and machine-parsable.
    • Avoids the ambiguity of HTML.
    • Suitable for Operational Transformation (OT) in real-time applications.
    • Implemented as a standalone library.
  5. Representing line formatting and newlines

    main

    Line attributes (like align) affect the entire line. To maintain the compact and canonical constraints, Quill treats the newline character (\n) as the atomic unit for line formatting.

    Crucially, all Deltas must end with a newline (\n).

    To apply an attribute to a line, apply it to the newline character that terminates that line:

    // Hello World on two lines
    const content = [
      { text: "Hello" },
      { text: "\n", attributes: { align: "center" } },
      { text: "World" },
      { text: "\n", attributes: { align: "right" } }   // Deltas must end with newline
    ];
  6. Understand Quill's API-driven design

    main

    Unlike editors that rely directly on the DOM, Quill uses a text-centric document model. This allows you to interact with content using character indexes and lengths rather than traversing DOM nodes.

    Key benefits include:

    • Index-based access: You can query or modify text using arbitrary indexes and lengths (e.g., checking if a specific character range is bold).
    • Intuitive Formats: Instead of parsing HTML tags like <b> or <strong>, you use methods like getFormat(index, length) to retrieve formatting state.
    • JSON-based Events: The event API reports changes in a structured JSON format, eliminating the need to manually diff DOM trees or parse HTML strings.
  7. Representing document changes with Deltas

    main

    When listening to Quill's text-change event, the provided Delta describes the transformation. A change Delta uses three types of operations:

    1. insert: Adds new content at the current position.
    2. delete: Removes a specific number of characters. The delete operation takes a number and does not include the content being deleted.
    3. retain: Keeps the next $N$ characters without modification. If attributes are provided, it keeps the characters but applies the new formatting. To remove a format, set the attribute key to null.

    Important: Delta instructions always start from the beginning of the document, which is why delete and insert operations do not require an explicit index.

    Example: Applying changes

    Starting from the document: { insert: 'Gandalf', attributes: { bold: true } }, { insert: ' the ' }, { insert: 'Grey', attributes: { color: '#cccccc' } }

    To unbold "Gandalf", italicize it, keep " the ", insert "White" in white, and delete "Grey", the Delta would be:

    {
      ops: [
        { retain: 7, attributes: { bold: null, italic: true } }, // Unbold and italicize "Gandalf"
        { retain: 5 },                                          // Keep " the " as is
        { insert: 'White', attributes: { color: '#fff' } },     // Insert "White" formatted
        { delete: 4 }                                           // Delete "Grey"
      ]
    }
    {
      ops: [
        { retain: 7, attributes: { bold: null, italic: true } },
        { retain: 5 },
        { insert: 'White', attributes: { color: '#fff' } },
        { delete: 4 }
      ]
    }
  8. How the Clipboard module processes pasted content

    main

    The Clipboard module manages copy, cut, and paste operations between Quill and external applications.

    The Processing Pipeline:

    1. DOM Traversal: When HTML is pasted, the Clipboard traverses the DOM tree in post-order.
    2. Delta Construction: As it traverses, it builds a Delta representation of the subtrees.
    3. Matcher Execution: At each descendant node, matcher functions are executed. Matchers are prioritized by nodeType first, then by CSS selector.
    4. Transformation: Each matcher receives the current node and the delta accumulated so far, allowing it to return a modified Delta that influences the final output.
  9. Understand the Delta format for document changes

    main
    Deltas are the representation of changes or document states in Quill. They are designed to be intuitive, human-readable, and expressive. The format uses a series of operations (ops) to describe how to transform one document into another. The modern Delta format focuses on rich text and supports explicit operations like retain, insert, and delete.
  10. Configure allowed formats in Quill

    main

    Quill supports various formats for both UI controls and API calls. By default, all formats are enabled and allowed. You can restrict which formats are permitted in an editor using the formats configuration option.

    Note: Configuring allowed formats is distinct from adding controls to the Toolbar. For example, you can allow bold content to be pasted into an editor even if there is no bold button in the toolbar.

  11. Extend Quill with custom content and formatting

    main

    Quill uses its own document model as an abstraction over the DOM. This model allows for unlimited extension beyond standard text formatting. Because it is not strictly tied to HTML structures, you can embed interactive or complex media such as:

    • Images and videos
    • Interactive checklists
    • Embedded slide decks
    • 3D models
    • Tweets or interactive graphs