diff --git a/src/modules/paste-manager.coffee b/src/modules/paste-manager.coffee index 7427746faf..6f9ba77c1b 100644 --- a/src/modules/paste-manager.coffee +++ b/src/modules/paste-manager.coffee @@ -5,10 +5,24 @@ dom = Quill.require('dom') Delta = Quill.require('delta') class PasteManager - constructor: (@quill, @options) -> + @DEFAULTS: + onConvert: null + + constructor: (@quill, options) -> @container = @quill.addContainer('ql-paste-manager') @container.setAttribute('contenteditable', true) dom(@quill.root).on('paste', _.bind(this._paste, this)) + @options = _.defaults(options, PasteManager.DEFAULTS) + @options.onConvert ?= this._onConvert; + + _onConvert: (container) => + doc = new Document(container, @quill.options) + delta = doc.toDelta() + lengthAdded = delta.length() + if lengthAdded == 0 + return delta + # Need to remove trailing newline so paste is inline, losing format is expected and observed in Word + return delta.compose(new Delta().retain(lengthAdded - 1).delete(1)) _paste: -> oldDocLength = @quill.getLength() @@ -16,12 +30,9 @@ class PasteManager return unless range? @container.focus() _.defer( => - doc = new Document(@container, @quill.options) - delta = doc.toDelta() - lengthAdded = Math.max(0, delta.length() - 1) + delta = @options.onConvert(@container) + lengthAdded = delta.length() if lengthAdded > 0 - # Need to remove trailing newline so paste is inline, losing format is expected and observed in Word - delta = delta.compose(new Delta().retain(lengthAdded).delete(1)) delta.ops.unshift({ retain: range.start }) if range.start > 0 delta.delete(range.end - range.start) @quill.updateContents(delta, 'user') diff --git a/test/unit/modules/paste-manager.coffee b/test/unit/modules/paste-manager.coffee index bedc072700..90c1c2498c 100644 --- a/test/unit/modules/paste-manager.coffee +++ b/test/unit/modules/paste-manager.coffee @@ -23,4 +23,42 @@ describe('PasteManager', -> ) ) ) + + it('optionally allows custom delta creation', (done) -> + container = $('#editor-container').get(0) + container.innerHTML = ' +
+
0123
+
' + quill = new Quill(container.firstChild, { + modules: { + 'paste-manager': { + onConvert: -> + new Quill.Delta().insert('something else', { + italic: true + }) + } + } + }) + pasteManager = quill.getModule('paste-manager') + quill.setSelection(2, 2) + pasteManager._paste() + pasteManager.container.innerHTML = ' + Pasted +
Text
+ ' + quill.on(Quill.events.TEXT_CHANGE, (delta) -> + expect(delta).toEqualDelta( + new Quill.Delta() + .retain(2) + .insert('something else', { italic: true }) + ) + _.defer( -> + range = quill.getSelection() + expect(range.start).toEqual(16) + expect(range.end).toEqual(16) + done() + ) + ) + ) )