UNPKG

react-quill

Version:

The Quill rich-text editor as a React component.

106 lines (89 loc) 2.82 kB
'use strict'; var Quill = require('quill'); var QuillMixin = { /** Creates an editor on the given element. The editor will be passed the configuration, have its events bound, */ createEditor: function($el, config) { var editor = new Quill($el, config); this.hookEditor(editor); return editor; }, hookEditor: function(editor) { // Expose the editor on change events via a weaker, // unprivileged proxy object that does not allow // accidentally modifying editor state. var unprivilegedEditor = this.makeUnprivilegedEditor(editor); this.handleTextChange = function(delta, oldDelta, source) { if (this.onEditorChangeText) { this.onEditorChangeText( editor.root.innerHTML, delta, source, unprivilegedEditor ); this.onEditorChangeSelection( editor.getSelection(), source, unprivilegedEditor ); } }.bind(this); this.handleSelectionChange = function(range, oldRange, source) { if (this.onEditorChangeSelection) { this.onEditorChangeSelection( range, source, unprivilegedEditor ); } }.bind(this); editor.on('text-change', this.handleTextChange); editor.on('selection-change', this.handleSelectionChange); }, unhookEditor: function(editor) { editor.off('selection-change'); editor.off('text-change'); }, setEditorReadOnly: function(editor, value) { value? editor.disable() : editor.enable(); }, /* Replace the contents of the editor, but keep the previous selection hanging around so that the cursor won't move. */ setEditorContents: function(editor, value) { var sel = editor.getSelection(); if (typeof value === 'string') { editor.clipboard.dangerouslyPasteHTML(value); } else { editor.setContents(value); } if (sel) this.setEditorSelection(editor, sel); }, setEditorSelection: function(editor, range) { if (range) { // Validate bounds before applying. var length = editor.getLength(); range.index = Math.max(0, Math.min(range.index, length-1)); range.length = Math.max(0, Math.min(range.length, (length-1) - range.index)); } editor.setSelection(range); }, /* Returns an weaker, unprivileged proxy object that only exposes read-only accessors found on the editor instance, without any state-modificating methods. */ makeUnprivilegedEditor: function(editor) { var e = editor; return { getLength: function(){ return e.getLength.apply(e, arguments); }, getText: function(){ return e.getText.apply(e, arguments); }, getHTML: function(){ return e.root.innerHTML }, getContents: function(){ return e.getContents.apply(e, arguments); }, getSelection: function(){ return e.getSelection.apply(e, arguments); }, getBounds: function(){ return e.getBounds.apply(e, arguments); }, }; } }; module.exports = QuillMixin;