From 0b5ba16400b7908d33a74db4beb2506ae2146f6c Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Mon, 21 Nov 2016 13:36:00 -0800 Subject: [PATCH 1/2] 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. * From 59a69bbea63180c463351200af8a3fd35cd606e5 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Mon, 21 Nov 2016 13:54:43 -0800 Subject: [PATCH 2/2] add rich text checking for plain text too --- src/utils/transfer.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils/transfer.js b/src/utils/transfer.js index b75bda63d..a53acda06 100644 --- a/src/utils/transfer.js +++ b/src/utils/transfer.js @@ -204,10 +204,11 @@ class Transfer { 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) + // Since files are preferred over HTML or text, 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` or `text`. (2016/11/21) if (this.hasRichText() && this.hasHtml()) return 'html' + if (this.hasRichText() && this.hasText()) return 'text' if (this.hasFiles()) return 'files' if (this.hasHtml()) return 'html'