<commit_before>import Delta from 'rich-text/lib/delta';
import Emitter from '../core/emitter';
import Module from '../core/module';


class Clipboard extends Module {
  constructor(quill, options) {
    super(quill, options);
    this.quill.root.addEventListener('paste', this.onPaste.bind(this));
    this.container = this.quill.addContainer('ql-clipboard');
    this.container.setAttribute('contenteditable', true);
  }

  onPaste() {
    let range = this.quill.getSelection();
    if (range == null) return;
    let updateDelta = new Delta().retain(range.index).delete(range.length);
    this.container.focus();
    setTimeout(() => {
      let pasteDelta = this.options.sanitize(this.container);
      this.container.innerHTML = '';
      let lengthAdded = pasteDelta.length();
      if (lengthAdded > 0) {
        this.quill.updateContents(updateDelta.concat(pasteDelta), Emitter.sources.USER);
      }
      this.quill.setSelection(range.index + lengthAdded, Emitter.sources.SILENT);
      this.quill.selection.scrollIntoView();
    }, 0);
  }
}
Clipboard.DEFAULTS = {
  sanitize: function(container) {
    return new Delta().insert(container.innerText);
  }
};


export default Clipboard;
<commit_msg>Added support for rich copy and paste of delta json within quill editors<commit_after>import Delta from 'rich-text/lib/delta';
import Emitter from '../core/emitter';
import Module from '../core/module';


class Clipboard extends Module {
  constructor(quill, options) {
    super(quill, options);
    this.quill.root.addEventListener('copy', this.onCopy.bind(this));
    this.quill.root.addEventListener('cut', this.onCut.bind(this));
    this.quill.root.addEventListener('paste', this.onPaste.bind(this));
  }

  onCopy(e) {
    let range = this.quill.getSelection();
    if (!range.length || e.defaultPrevented) {
      return;
    }
    e.clipboardData.setData('text', this.quill.getText(range));
    e.clipboardData.setData('application/json', JSON.stringify(this.quill.getContents(range)));
    e.preventDefault();
  }

  onCut(e) {
    if (e.defaultPrevented) {
      return;
    }
    this.onCopy(e);
    this.quill.deleteText(this.quill.getSelection(), Emitter.sources.USER);
  }

  onPaste(e) {
    if (e.defaultPrevented) {
      return;
    }
    let range = this.quill.getSelection();
    let delta = new Delta().retain(range.index);
    if (range.length > 0) {
      delta.delete(range.length);
    }
    if (e.clipboardData.types.indexOf('application/json') !== -1) {
      let deltaJsonStr = e.clipboardData.getData('application/json');
      let deltaJson = JSON.parse(deltaJsonStr);
      if (Array.isArray(deltaJson.ops)) { // isDelta?
        delta = delta.concat(deltaJson);
      } else {
        delta.insert(deltaJsonStr);
      }
    } else {
      delta.insert(e.clipboardData.getData('text'));
    }
    this.quill.editor.applyDelta(delta, Emitter.sources.USER);
    this.quill.setSelection(this.length(delta), Emitter.sources.SILENT);
    this.quill.selection.scrollIntoView();
    e.preventDefault();
  }

  length(delta) {
    return delta.ops.reduce((memo, op) => {
      if (typeof op['delete'] === 'number') {
        return memo - op['delete'];
      } else if (typeof op.retain === 'number') {
        return memo + op.retain;
      } else {
        return memo + (typeof op.insert === 'string' ? op.insert.length : 1);
      }
    }, 0);
  }
}

export default Clipboard;

{'fraction_non_alphanumeric': 0.15260691271236088, 'fraction_numerical': 0.0017574692442882249, 'mean_word_length': 3.25280199252802, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5768662', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1086, 'n_words': 194}