Draftail Documentation

repository·main·Indexed 20 days ago

https://github.com/wagtail/draftail

A configurable rich text editor built with Draft.js, designed for a keyboard-centric and extensible editing experience. Draftail features smart pasting, autolists, markdown-like shortcuts, and a comprehensive API for building custom block types, entity controls, and inline styles. It is compatible with the draft-js-plugins ecosystem and provides detailed guides for integration, API usage, and custom extension development.

Tokens
6.5K
Snippets
23
Records
34
Agent score
71%

What's inside Draftail

  1. Overview of Draftail features

    main

    Draftail is a configurable rich text editor built on top of Draft.js. It is designed for a keyboard-centric, mouse-free user experience.

    Key features include:

    • Keyboard Shortcuts: Extensive support for keyboard-driven editing.
    • Smart Pasting: Support for pasting content from Microsoft Word or other editors.
    • Autolists: Automatically creates list items when starting a line with -, *, or 1..
    • Markdown-like Shortcuts: Supports shortcuts for headings (##), code blocks (`), and text formatting (**).
    • Text Types & Styles: Built-in support for headings, paragraphs, quotes, lists, bold, italic, and more.
    • Extensibility: Provides an API to build custom controls (e.g., for links and images) and is compatible with the draft-js-plugins ecosystem.

    Note: While Draftail is actively maintained, the underlying Draft.js library is archived and no longer receives updates.

  2. Run development commands in Draftail

    main

    Once the environment is installed, use the following npm scripts to manage your development workflow, including starting the server, linting, formatting, and running tests.

    # Make sure you use the correct node version.
    nvm use
    # Start the server and the development tools.
    npm run start
    # Runs linting.
    npm run lint
    # Re-formats all of the files in the project (with Prettier).
    npm run format
    # Run tests in a watcher.
    npm run test:watch
    # Run test coverage
    npm run test:coverage
    # Open the coverage report with:
    npm run report:coverage
    # Open the build report with:
    npm run report:build
    # Open the file size report with:
    npm run report:size
    # Open the package contents report with:
    npm run report:package
    # View other available commands with:
    npm run
  3. Access Draftail documentation and guides

    main

    For detailed implementation and usage information, refer to the official documentation at draftail.org:

    • Getting Started: Initial setup and integration steps.
    • API Reference: Detailed technical documentation for the Draftail API.
    • User Guide: Instructions for end-users on how to use the editor.
    • Extensions Guide: How to build and integrate custom extensions.
  4. Release a new version of Draftail

    main

    To release a new version, follow these steps:

    1. Create a new branch for the release.
    2. Update CHANGELOG.md and ensure documentation links point to the correct version.
    3. Update the version number in package.json and package-lock.json following semantic versioning (semver).
    4. Submit a PR and squash merge it.
    5. On the main branch, run the distribution and publishing commands.
    6. Create a release and a tag on GitHub.
    npm run dist
    npm run report:size
    npm run report:package
    npm publish
  5. Get help and ask questions about Draftail

    main

    Before asking a question, ensure you have consulted the official documentation and searched for existing answers online. If you still need assistance, you can use the following channels:

    • Slack: Join the #draftail channel on Wagtail’s Slack to interact with the community.
    • Stack Overflow: Search for existing answers using the draftail query or the draftjs tag.
  6. Install Draftail for development

    main

    To set up a local development environment for Draftail, clone the repository, ensure you have Node.js and nvm installed, and follow these steps:

    1. Install the required Node version using nvm.
    2. Install project dependencies using npm.
    3. Create a .env file to manage local secrets.
    nvm install
    # Then, install all project dependencies.
    npm install
    # Set up a `.env` file with the appropriate secrets.
    touch .env
  7. Understand Entity Component Props

    main

    When building custom UI for entities, you will use one of three prop interfaces depending on the context:

    1. EntitySourceProps

    Used for the UI that manages/creates entities (e.g., a configuration dialog).

    • editorState: The current state of the editor.
    • onComplete: Call this with the nextState to save changes and focus the editor.
    • onClose: Closes the source without refocusing the editor.
    • entityType: The configuration of the entity being edited.
    • entityKey: The specific key of the entity being edited.
    • textDirectionality: Overriding text direction (LTR, RTL, or null).

    2. EntityDecoratorProps

    Used for inline entities rendered within the text flow.

    • onEdit: Shorthand to trigger the entity's source UI.
    • onRemove: Shorthand to remove the entity and its associated block.
    • children: The rich text content of the entity.

    3. EntityBlockProps

    Used for block-level entities.

    • lockEditor / unlockEditor: Methods to make the editor read-only while interacting with the block.
    • onEditEntity / onRemoveEntity: Shorthands for management.
    • onChange: Method to update the editorState with changes made within the block.
  8. How Draftail handles entities and sources

    main

    Draftail supports complex entities (like links or custom blocks) through an entity-source pattern.

    1. Entities: Defined via entityTypes. Some entities are 'blocks' (atomic blocks), while others are inline.
    2. Sources: For entities that require user input (e.g., a URL for a link), you can define a source component in the EntityTypeControl.
    3. Workflow: When a user triggers an entity command, Draftail enters a readOnly state and renders the source component. Once the user completes the input via the onComplete callback, the editor is unlocked and focused again.

    When building custom entities, your source component will receive:

    • editorState: The current editor state.
    • onComplete: A callback that takes the updated EditorState and focuses the editor.
    • onClose: A callback to close the source without updating state.
    • entity / entityKey: The current entity instance and its key.
  9. Configure Prettier for Draftail

    main

    Draftail uses Prettier for code formatting. The project follows these specific formatting rules defined in prettier.config.js. If you are contributing to the codebase or setting up a local development environment, ensure your editor or CLI uses these settings to maintain consistency.

    module.exports = {
      printWidth: 80,
      tabWidth: 2,
      useTabs: false,
      semi: true,
      singleQuote: false,
      trailingComma: "all",
      bracketSpacing: true,
      arrowParens: "always",
      proseWrap: "preserve",
    };
  10. Retrieve control labels with getControlLabel

    main

    Use getControlLabel to determine the text label for a UI control. The function resolves the label based on the following priority:

    1. If config is a boolean, it returns the predefined label from LABELS using the control's type.
    2. If config.label is a string or null, it returns that value.
    3. If config.icon is defined, it returns null (indicating the control is represented by an icon rather than text).
    4. Otherwise, it falls back to the predefined label from LABELS using the control's type.
    import { getControlLabel } from './api/ui';
    
    // Example usage:
    const label = getControlLabel('bold', { type: 'bold', label: 'Bold Text' });