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

draft-regex

v1.3.0

Published

Helpers for draft-js

Downloads

153

Readme

Draft Regex

NPM version npm install size Greenkeeper badge FlowType compatible

These helpers are written for Draft.js to improve Editor capabilities.

Requirements

Getting started

npm install draft-regex

or

yarn add draft-regex

How to use

import * as React from 'react';
import { EditorState, Editor } from 'draft-js';
import { clearEmptyBlocks, clearPastedStyle, replaceTextRegex } from 'draft-regex';

type State = {
  editorState: EditorState,
};

class MyEditor extends React.Component<void, State> {
  state: State = {
    editorState: EditorState.createEmpty(),
  };

  onChange = (editorState: EditorState) => {
    this.setState({ editorState: clearEmptyBlocks(editorState) });
  };

  handlePastedText = (text: ?string, html: ?string, editorState: EditorState) => {
    this.setState({ editorState: clearPastedStyle(editorState) });
  };

  onSave = () => {
    const { editorState } = this.state;
    this.setState({ editorState: replaceTextRegex(editorState) });
  };

  render() {
    const { editorState } = this.state;
    return (
      <>
        <Editor
          editorState={editorState}
          onChange={this.onChange}
          handlePastedText={this.handlePastedText}
        />
        <button onClick={this.onSave}>Save</button>
      </>
    );
  }
}

API

All helpers are taken by EditorState and options as an argument and returned EditorState.

clearEmptyBlocks

Prevents the ability to add blank lines more than 3 (varies in settings).

A second argument can take a number for remove blank lines (number + 1).

function clearEmptyBlocks(editorState: EditorState, maxEmptyLines?: number = 2): EditorState

replaceTextRegex

Apply regular expressions to the entire text, in the process of typing or after copy/pasting text.

A second argument can take an array of rules. typoRules is a set of basic rules that you can not use.

All regular expressions are used once in the entire text. If you use a lot of regular expressions, the performance of the editor can drop noticeably.

function replaceTextRegex(
  editorState: EditorState,
  rulesArray?: Array<Rule> = typoRules,
): EditorState

The rule looks like this: { reg: new RegExp(), shift: '' }

clearPastedStyle

Clears styles of copy/pasted text to those that you have.

A second argument can take an object that can contain options.

blockTypes is an array of strings that contains all the types of blocks that you use in your editor. This is useful if you want to clear all styles, except those you can already ask yourself.

A list of all types can be found here.

If you do not want to use all six kinds of headings, you can bring the headers to the same view using shiftHeader.

The same applies to lists.

function clearPastedStyle(
  editorState: EditorState,
  options?: {
    blockTypes?: Array<string>,
    shiftHeader?: string,
    shiftList?: string,
  }
): EditorState

Hints

To improve performance, use clearPastedStyle to handlePastedText method, and replaceTextRegex to save the text editor.