Git Product home page Git Product logo

react-prosemirror's Introduction

react-prosemirror

An unofficial React component for ProseMirror. Build Status

Hello World

import ProseMirror from 'react-prosemirror'

const Hello = React.createClass({
  getInitialState() {
    return {value: 'Hello World!'}
  },
  render() {
    return <ProseMirror value={this.state.value} onChange={this.onChange} options={{docFormat: 'html'}} />
  },
  onChange(value) {
    this.setState({value})
  }
})

Installation

Via NPM (note that you need to install ProseMirror yourself)

npm install --save react-prosemirror prosemirror

If your target environment doesn't natively support Object.assign, you may need to use some sort of polyfill such as babel-polyfill.

Usage

The intent is to provide an API compatible with standard React form elements. react-prosemirror supports defaultValue, value and onChange props as well as valueLink. It additionally supports an options prop which is passed directly to the ProseMirror constructor.

The ProseMirror instance is stored on the component instance as pm. You can gain access to it by putting a ref prop on the component.

render() {
  return <ProseMirror value={value} onChange={callback} ref="editor" />
},
someFunc(posA, posB) {
  this.refs.editor.pm.setSelection(posA, posB)
}

Finally, instances have a getContent method which defaults to the selected docFormat.

Options

react-prosemirror simply passes options into ProseMirror. It will not automatically load other modules. For example, if you wish to use the menubar option or markdown format, you'll need to import those modules in addition to adding the appropriate options.

import 'prosemirror/dist/markdown'
import 'prosemirror/dist/menu/menubar'

Formats

options.docFormat is used to determine the type of value returned to the onChange callback as well as what is expected to be in the value prop.

react-prosemirror will attempt to control the component in the spirit of standard React form elements (see: Why Controlled Components?). To avoid thrashing ProseMirror, it will only call setContent when the new value is not strictly equal (===) to the last value pulled from ProseMirror. This should work for most cases, but be aware of that if you're using a complex type such as json.

Project Status

I would consider this library largely complete within its scope, though clearly not proven in a production setting. I'm open to refinements, especially around the interaction of docFormat and controlled values.

I intend to track ProseMirror API changes as needed.

react-prosemirror's People

Contributors

tgecho avatar ianstormtaylor avatar gnunicorn avatar

Watchers

James Cloos avatar Timothy Gu avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.