From 0b5ba16400b7908d33a74db4beb2506ae2146f6c Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Mon, 21 Nov 2016 13:36:00 -0800 Subject: [PATCH] add logic to handle rich text pastes --- src/utils/transfer.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/utils/transfer.js b/src/utils/transfer.js index 27965e87c..b75bda63d 100644 --- a/src/utils/transfer.js +++ b/src/utils/transfer.js @@ -157,6 +157,24 @@ class Transfer { return node } + /** + * Get the rich text content of the data transfer. + * + * @return {String|Void} + */ + + getRichText() { + if ('richtext' in this.cache) return this.cache.richtext + + let richtext + const string = this.data.getData('text/rtf') + + if (string != '') richtext = string + + this.cache.richtext = richtext + return richtext + } + /** * Get the text content of the data transfer. * @@ -184,6 +202,13 @@ class Transfer { getType() { if (this.hasFragment()) return 'fragment' if (this.hasNode()) return 'node' + + // COMPAT: Microsoft Word adds an image of the selected text to the data. + // Since files are preferred over HTML, this would cause the type to be + // considered `files`. But it also adds rich text data so we can check for + // that and properly set the type to `html`. (2016/11/21) + if (this.hasRichText() && this.hasHtml()) return 'html' + if (this.hasFiles()) return 'files' if (this.hasHtml()) return 'html' if (this.hasText()) return 'text' @@ -210,6 +235,16 @@ class Transfer { return this.getHtml() != null } + /** + * Check whether the data transfer has rich text content. + * + * @return {Boolean} + */ + + hasRichText() { + return this.getRichText() != null + } + /** * Check whether the data transfer has text content. *