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

gnunicorn avatar ianstormtaylor avatar tgecho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-prosemirror's Issues

fails to load plugins

for some reason i can't seem to make it load the menu bar or other plugin
the editors shows up, works as expected, if i use ctrl+b/i it changes the text as expected, but no menuBar

import React, {Component, PropTypes} from 'react';
import cx from 'classnames';
import shallowCompare from 'react-addons-shallow-compare';

import ProseMirror from 'react-prosemirror';
import 'prosemirror/dist/inputrules/autoinput';
import 'prosemirror/dist/menu/menubar';
import 'prosemirror/dist/menu/tooltipmenu';
import 'prosemirror/dist/menu/menu';

import '../publishing-platform-widget.scss';

export default class Paragraph extends Component {
  static propTypes = {
    className: PropTypes.string,
    children: PropTypes.node,
    onChange: PropTypes.func,
    onRemove: PropTypes.func,
    value: PropTypes.string,
    toolbar: PropTypes.array,
  };

  static defaultProps = {
    value: '',
  };

  constructor(props) {
    super(props);

    this.state = {
      value: props.value
    };

    this.handleTextChange = this.onTextChange.bind(this);
  }

  onTextChange(value) {
    this.setState({
      text: value,
    });

    if (this.props.onChange) {
      this.props.onChange(value);
    }
  }

  render() {
    const {
      onChange,
      ...props,
      } = this.props;

    return (
      <div >
        <ProseMirror
          options={{
            menuBar:true,
            tooltipMenu: true,
            autoInput: true,
            docFormat: 'html',
          }}
          ref="editor"
          defaultValue={this.props.value}
          onChange={this.handleTextChange}>
        </ProseMirror>
      </div>
    );
  }
}

Trying to set a document with a different schema

I am trying to get the output to go to a Mongo Database, when setting up the getInitialState I keep getting the error "Trying to set a document with a different schema" when inspecting it says that it's react-prosemirror that is giving the error. how do you have your schema set up, I am using this as an article writer, so I have more fields than just the ProseMirror. Does this not play well with other fields?

Deprecation Notice

This repo should have a notice of deprecation or unmaintenance. It's not working with latest ProseMirror and the last commit was 8 months ago.

<embed> tag is cleared...

In html model, although the passed-in value has some tags like that this editor can not render, it should not clear them away when I call getContent.

How to support video?

Display Name is '.'

Hi @tgecho,

When I browse the rendered tree in React Dev Tools, the react-prosemirror component has a name '.'
I am using the ES2015 way for creating components, but I belive you could either add displayName property when calling createClass or let React to automatically figure it out for you, by exporting ProseMirror the following way:

const ProseMirror = React.createClass({...});
export default ProseMirror;

With thanks,
Laszlo

0.0.0 is the latest release on NPM

Thanks for your work on this component,
Just trying to get started but looks like the latest version needs to be released to NPM.

Cheers.

npm install

Hello, npm package seem wrong.

npm install react-prosemirror
ls /node_modules/react-prosemirror

return

..                    
LICENSE               
package.json          
README.md             

No js file !
Ami44

Changing the input value does not update _lastValue

It seems that the .change handler for pm is only called for user initiated changes (setContent doesn't cause it be triggered). This means that the wrapper never sets a _lastValue if a new value is passed into it (until the user changes something).

Flow which causes this:

Start with value1
Modify to value2 (via keyboard)
Set value={value1}
Set value={value2}

value2 never gets repopulated because _lastValue is still set to value2, so setContent is never called.

I 'fixed it' locally by adding the follow after setContent:
this._lastValue = this.pm.getContent(this.props.options.docFormat);

example doesn't work

Thanks for this component!

I am using prosemirror 0.8.0. Looks like the following code doesn't work.

It says cannot read schema of undefined.

import React, {PropTypes, Component} from 'react';
import autobind from 'autobind-decorator';
import ProseMirror from 'react-prosemirror';
import 'prosemirror/dist/inputrules';
import 'prosemirror/dist/menu/menubar';
import 'prosemirror/dist/menu/tooltipmenu';
import 'prosemirror/dist/menu/menu';
import 'prosemirror/dist/markdown';

const options = {
  menuBar: true,
  tooltipMenu: true,
  autoInput: true
};

@autobind
export default class RichTextCard extends Component {

  componentWillMount() {
    this.setState({value: "<h1>What is your story?</h1>"});
  }

  onChange(state) {
    this.setState({value, state});
    console.log(state);
  }

  render() {
    console.log('====>', this.state, options);
    return (
      <div className="card rich-text-card">
        <h1>Rich Text Editor</h1>
        <ProseMirror value={this.state.value} onChange={this.onChange} options={options} ref="pm"></ProseMirror>
      </div>
    );
  }
}

'prosemirror' cannot be found

I just updated to 0.2.0, when I run my compiler it throws out an error saying that 'prosemirror' cannot be found in /node_modules/react-prosemirror/dist looking at the index.js and taking out _prosemirror = require('prosemirror') it runs just fine, but then it can't find _prosemirror when you call it on line 57 for
this.pm = new _prosemirror.ProseMirror(options)

also after updating it couldn't find markdown, or any of the other options that were in the dist folder, it appears they have all been deleted, even after uninstalling it and reinstalling it. this is a great rich text editor, and I hope we can figure out whats going on, especially since the page I am currently doing some work on is the page that has this component.

Uncaught Error: Option 'schema' can not be changed

If I create a react-prosemirror component instance with a custom schema, then any time the parent component' render method runs, I get the above error.

Just a side note, inside componentDidUpdate, when previous and current options are compared, the non-primitive values (schema, place, commands and doc [ if it is in some fancy format]) will be never equal, due to the 'shallow' equality checking, therefore pm.setOption is called unnecessary. I have not dig into this deeper, so not sure how does this affect performance, however it might worth to keep it in mind.

So back to the real issue, since I am not changing the schema in the parent component, I just made a dirty hack in componentDidUpdate, and filtered out schema, like this:

Object.keys(current).filter( k => k!=='schema').forEach(...);

However if someone wants pass in different schema objects to react-prosemirror, based on some state or props of the parent component, then the inability of changing schema of an existing ProseMirror instance, could be problematic. What do you think?

react-prosemirror requires "babbel-loader?optional=runtime"

Since react-prosemirror relies on Object.assign it needs to be compiled with babel-runtime. Hence it would be great to update to update the README for other developers.

{
  test: /\.js?$/,
  include: [/prosemirror/, /react-prosemirror/],
  loaders: ["babel-loader?optional=runtime"]
},

Evernote text not working copy & paste.

Evernote contents copy & paste to the react-prosemirror demo.

First scene my demo.

Only the first line will be copied.

Second scene your demo.

working fine.

react-prosemirror-bug

my repo here

I really wonder why not working fine my sample.

Thanks!

let people bring their own prosemirror dependency?

Just wondering, have you considered removing prosemirror from dependencies, and then having the user npm install --save prosemirror for themselves, so that you don't have to re-peg against new ProseMirror versions? Since it's at 0.3.0 now. Might be easier!

Fails for empty text

When I delete all the text inside the editor, then the editor stops updating itself.
The root cause of this problem is that during the initiation of this._lastValue, this.props.value becomes falsy. I think it could be easily fixed by checking whether this.props.value is undefined instead.

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.