SamyPesse / draft-js-prism

Code highlighting for DraftJS using Prism
http://samypesse.github.io/draft-js-prism/
Apache License 2.0
275 stars 36 forks source link

Consider the same API as draftjs-plugins #1

Closed okonet closed 7 years ago

okonet commented 8 years ago

There is a cool project https://github.com/draft-js-plugins/draft-js-plugins that has a very nice plugin API for draft-js. I really like how you can just define an array of plugins you want to use:

import React, { Component } from 'react';
import Editor from 'draft-js-plugins-editor';
import createHashtagPlugin from 'draft-js-hashtag-plugin';
import createLinkifyPlugin from 'draft-js-linkify-plugin';
import { EditorState } from 'draft-js';

const hashtagPlugin = createHashtagPlugin();
const linkifyPlugin = createLinkifyPlugin();

const plugins = [
  hashtagPlugin,
  linkifyPlugin,
];

export default class UnicornEditor extends Component {

  state = {
    editorState: EditorState.createEmpty(),
  };

  onChange = (editorState) => {
    this.setState({
      editorState,
    });
  };

  render() {
    return (
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
        plugins={plugins}
        ref="editor"
      />
    );
  }
}

Would you consider adapting this API?

okonet commented 8 years ago

cc @nikgraf

SamyPesse commented 8 years ago

I prefer to keep a low level API, but we can easily also expose a plugin for draftjs-plugins (in this plugin or in another one).

But I think one of the main advantage of Draft is that its API can be low level and really customisable, and APIs like the draftjs-plugins one should stay an abstraction on top of that instead of being the main convention.

okonet commented 8 years ago

I'm wondering if you checked out the draftjs-plugins API since this is exactly what they are trying to achieve: a low-level but very developer-friendly API to add several plugins to DraftJS instance without using tons of HoCs.

nikgraf commented 8 years ago

@SamyPesse cool tool. Is there a way to get out a decorator that has a Strategy and Component to be used within the CompositeDecorator https://facebook.github.io/draft-js/docs/advanced-topics-decorators.html#compositedecorator provided by DraftJS? If yes, creating a plugin (which would be really cool) wouldn't take any member for the plugins team more than an hour or two 😄

@okonet thx for mentioning me

SamyPesse commented 8 years ago

@nikgraf CompositeDecorator are a subset of that i's possible with decorators an Draft, the problem is that CompositeDecorator limits the component/props to be global to the decorator.

In the case of the prism decorator, we need different props according to the type of tokens. So basically, it's not possible to build the prism highlighting as a CompositeDecorator.

But I've build draftjs-multidecorators to accept multiple decorators (composite or not) and return one.

Do draftjs-plugins only accept composite decorator for now?

nikgraf commented 8 years ago

@SamyPesse yes, at least for now. https://github.com/draft-js-plugins/draft-js-plugins/blob/master/draft-js-plugins-editor/src/Editor/index.js#L55-L59

I didn't really considered creating something else until there is a valid use-case (I didn't imagine there would be soon). draftjs-prism now is 😄

Might be interesting to use the multidecorators in the core. Are there any downsides using it that you have encountered?

mxstbr commented 7 years ago

I've wrapped this decorator to be compatible with draft-js-plugins in draft-js-prism-plugin and that works perfectly fine, so I'll close this issue!