export default (editor, options) => {
  editor.TraitManager.addType('textarea', {

    onChange() {
      const value = this.getLineValue(this.getInputEl().val());
      this.model.set('value', value);
    },

    getInputEl() {
      if (!this.$input) {
        const { model, ppfx } = this;
        let val = this.getLineValue(model.getTargetValue());
        try {
          val = JSON.parse(val);
          val = JSON.stringify(val).replace(/\"/g, '\\"');
        } catch(err) { }
        const className = `${ppfx}-textarea`;
        const input = `<textarea class=${className} style="min-height: 80px"></textarea>`;
        this.input = input;
        this.$input = $(input);
        val && this.$input.val(val);
      }
  
      return this.$input;
    },

    getLineValue(value) {
      return value.replace(/[\n+]|[\t+]|[\r+]/g, '');
    },

    renderField() {
      if (!this.$input) {
        this.$el.append(this.tmpl);
        const el = this.getInputEl().get(0);
        this.$el.find('.' + this.ppfx + 'field').append(el);
      }
    },
  })
}