React-quill: Calling insertembed on quill instance

Created on 27 Apr 2017  路  3Comments  路  Source: zenoamaro/react-quill

What is the best way to tap into the insertembed with React Quill? I tried a simple call to the component, but no dice:

import ReactQuill, {Quill} from 'react-quill';

Quill.insertEmbed(10, 'image', 'http://quilljs.com/images/cloud.png');

http://quilljs.com/docs/api/#insertembed

Most helpful comment

It works if you change from value to defaultValue: https://codepen.io/alexkrolick/pen/rmjzBN?editors=0010

This is something to do with the controlled component rerendering with textContent - potentially a bug.

All 3 comments

You need a ref to the specific quill instance, which you can get with the getEditor API: https://github.com/zenoamaro/react-quill#methods

@alexkrolick Thanks! I'm trying to insert a text value into the editor that removes completely when you hit the delete key after it, like an embed. Below is my code. It's working great, but the textContent seems to update twice. First it sets the text correctly as 'variable name', then next it just sets the value to true.

Any idea why? Also, is this the best way to handle what I'm trying to do?

import React, { Component } from 'react';
import {connect} from 'react-redux';
import ReactQuill, {Quill} from 'react-quill';
import Store from '../core/store';

let Embed = Quill.import('blots/block/embed');

class Variable extends Embed {
  static create(value) {
    let node = super.create();
    node.textContent = value;
    return node;
  }
}
Variable.blotName = 'variable';
Variable.tagName = 'span';
Variable.className = 'variable';

Quill.register(Variable);

class EditableInput extends Component {
  constructor(props) {
    super(props);
    this.formats = ['align', 'bold', 'italic', 'underline', 'variable'];
    this.modules = {
      toolbar: [
        ['bold', 'italic', 'underline']
      ],
    };
  }

  test = () => {
    this.reactQuillRef.getEditor().insertEmbed(0, 'variable', 'variable name');
  };

  render() {
    const {
      text
    } = this.props;

    return (
      <div>
        <ReactQuill
          ref={(el) => { this.reactQuillRef = el }}
          value={text}
          formats={this.formats}
          modules={this.modules}
        />
        <button onClick={this.test}>Add variable name text</button>
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    text: state.viewState.text,
    variable: state.viewState.variable
  };
}

export default connect(mapStateToProps)(EditableInput);

It works if you change from value to defaultValue: https://codepen.io/alexkrolick/pen/rmjzBN?editors=0010

This is something to do with the controlled component rerendering with textContent - potentially a bug.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexkrolick picture alexkrolick  路  4Comments

Drejerdk picture Drejerdk  路  4Comments

Nazanin1369 picture Nazanin1369  路  4Comments

ycjcl868 picture ycjcl868  路  5Comments

AlexKvazos picture AlexKvazos  路  4Comments