22import mobiledocParsers from '../parsers/mobiledoc' ;
33import HTMLParser from '../parsers/html' ;
44import TextParser from '../parsers/text' ;
5- import HTMLRenderer from 'mobiledoc-html-renderer' ;
6- import TextRenderer from 'mobiledoc-text-renderer' ;
75import Logger from 'mobiledoc-kit/utils/logger' ;
86
97export const MIME_TEXT_PLAIN = 'text/plain' ;
@@ -13,6 +11,9 @@ export const NONSTANDARD_IE_TEXT_TYPE = 'Text';
1311const log = Logger . for ( 'parse-utils' ) ;
1412const MOBILEDOC_REGEX = new RegExp ( / d a t a \- m o b i l e d o c = ' ( .* ?) ' > / ) ;
1513
14+ /**
15+ * @return {Post }
16+ */
1617function parsePostFromHTML ( html , builder , plugins ) {
1718 let post ;
1819
@@ -27,36 +28,26 @@ function parsePostFromHTML(html, builder, plugins) {
2728 return post ;
2829}
2930
31+ /**
32+ * @return {Post }
33+ */
3034function parsePostFromText ( text , builder , plugins ) {
3135 let parser = new TextParser ( builder , { plugins} ) ;
3236 let post = parser . parse ( text ) ;
3337 return post ;
3438}
3539
36- // Sets the clipboard data in a cross-browser way.
37- function setClipboardData ( clipboardData , html , plain ) {
38- if ( clipboardData && clipboardData . setData ) {
39- clipboardData . setData ( MIME_TEXT_HTML , html ) ;
40- clipboardData . setData ( MIME_TEXT_PLAIN , plain ) ;
41- } else if ( window . clipboardData && window . clipboardData . setData ) { // IE
42- // The Internet Explorers (including Edge) have a non-standard way of interacting with the
43- // Clipboard API (see http://caniuse.com/#feat=clipboard). In short, they expose a global window.clipboardData
44- // object instead of the per-event event.clipboardData object on the other browsers.
45- window . clipboardData . setData ( NONSTANDARD_IE_TEXT_TYPE , html ) ;
46- }
47- }
40+ /**
41+ * @return {{html: String, text: String} }
42+ */
43+ export function getContentFromPasteEvent ( event , window ) {
44+ let html = '' , text = '' ;
4845
49- // Gets the clipboard data in a cross-browser way.
50- function getClipboardData ( clipboardData ) {
51- let html ;
52- let text ;
46+ let { clipboardData } = event ;
5347
5448 if ( clipboardData && clipboardData . getData ) {
5549 html = clipboardData . getData ( MIME_TEXT_HTML ) ;
56-
57- if ( ! html || html . length === 0 ) { // Fallback to 'text/plain'
58- text = clipboardData . getData ( MIME_TEXT_PLAIN ) ;
59- }
50+ text = clipboardData . getData ( MIME_TEXT_PLAIN ) ;
6051 } else if ( window . clipboardData && window . clipboardData . getData ) { // IE
6152 // The Internet Explorers (including Edge) have a non-standard way of interacting with the
6253 // Clipboard API (see http://caniuse.com/#feat=clipboard). In short, they expose a global window.clipboardData
@@ -68,67 +59,74 @@ function getClipboardData(clipboardData) {
6859}
6960
7061/**
71- * @param {Event } copyEvent
72- * @param {Editor }
73- * @return null
62+ * @return {{html: String, text: String} }
7463 */
75- export function setClipboardCopyData ( copyEvent , editor ) {
76- const { range , post } = editor ;
64+ function getContentFromDropEvent ( event ) {
65+ let html = '' , text = '' ;
7766
78- const mobiledoc = post . cloneRange ( range ) ;
67+ try {
68+ html = event . dataTransfer . getData ( MIME_TEXT_HTML ) ;
69+ text = event . dataTransfer . getData ( MIME_TEXT_PLAIN ) ;
70+ } catch ( e ) {
71+ // FIXME IE11 does not include any data in the 'text/html' or 'text/plain'
72+ // mimetypes. It throws an error 'Invalid argument' when attempting to read
73+ // these properties.
74+ log ( 'Error getting drop data: ' , e ) ;
75+ }
76+
77+ return { html, text } ;
78+ }
7979
80- const unknownCardHandler = ( ) => { } ; // ignore unknown cards
81- const unknownAtomHandler = ( ) => { } ; // ignore unknown atoms
82- const { result : innerHTML } =
83- new HTMLRenderer ( { unknownCardHandler, unknownAtomHandler} ) . render ( mobiledoc ) ;
80+ /**
81+ * @param {CopyEvent|CutEvent }
82+ * @param {Editor }
83+ * @param {Window }
84+ */
85+ export function setClipboardData ( event , { mobiledoc, html, text} , window ) {
86+ if ( mobiledoc && html ) {
87+ html = `<div data-mobiledoc='${ JSON . stringify ( mobiledoc ) } '>${ html } </div>` ;
88+ }
8489
85- const html =
86- `<div data-mobiledoc='${ JSON . stringify ( mobiledoc ) } '>${ innerHTML } </div>` ;
87- const { result : plain } =
88- new TextRenderer ( { unknownCardHandler, unknownAtomHandler} ) . render ( mobiledoc ) ;
90+ let { clipboardData } = event ;
91+ let { clipboardData : nonstandardClipboardData } = window ;
8992
90- setClipboardData ( copyEvent . clipboardData , html , plain ) ;
93+ if ( clipboardData && clipboardData . setData ) {
94+ clipboardData . setData ( MIME_TEXT_HTML , html ) ;
95+ clipboardData . setData ( MIME_TEXT_PLAIN , text ) ;
96+ } else if ( nonstandardClipboardData && nonstandardClipboardData . setData ) {
97+ // The Internet Explorers (including Edge) have a non-standard way of interacting with the
98+ // Clipboard API (see http://caniuse.com/#feat=clipboard). In short, they expose a global window.clipboardData
99+ // object instead of the per-event event.clipboardData object on the other browsers.
100+ nonstandardClipboardData . setData ( NONSTANDARD_IE_TEXT_TYPE , html ) ;
101+ }
91102}
92103
93104/**
94- * @param {Event } pasteEvent
95- * @param {PostNodeBuilder } builder
96- * @param {Array } plugins parser plugins
105+ * @param {PasteEvent }
106+ * @param {{builder: Builder, _parserPlugins: Array} } options
97107 * @return {Post }
98108 */
99109export function parsePostFromPaste ( pasteEvent , { builder, _parserPlugins : plugins } ) {
100- let post ;
110+ let { html , text } = getContentFromPasteEvent ( pasteEvent , window ) ;
101111
102- const { html, text } = getClipboardData ( pasteEvent . clipboardData ) ;
103- if ( html && html . length > 0 ) {
104- post = parsePostFromHTML ( html , builder , plugins ) ;
105- } else if ( text && text . length > 0 ) {
106- post = parsePostFromText ( text , builder , plugins ) ;
112+ if ( html && html . length ) {
113+ return parsePostFromHTML ( html , builder , plugins ) ;
114+ } else if ( text && text . length ) {
115+ return parsePostFromText ( text , builder , plugins ) ;
107116 }
108-
109- return post ;
110117}
111118
119+ /**
120+ * @param {DropEvent }
121+ * @param {{builder: Builder, _parserPlugins: Array} } options
122+ * @return {Post }
123+ */
112124export function parsePostFromDrop ( dropEvent , { builder, _parserPlugins : plugins } ) {
113- let post ;
125+ let { html , text } = getContentFromDropEvent ( dropEvent ) ;
114126
115- let html , text ;
116- try {
117- html = dropEvent . dataTransfer . getData ( 'text/html' ) ;
118- text = dropEvent . dataTransfer . getData ( 'text/plain' ) ;
119- } catch ( e ) {
120- // FIXME IE11 does not include any data in the 'text/html' or 'text/plain'
121- // mimetypes. It throws an error 'Invalid argument' when attempting to read
122- // these properties.
123- log ( 'Error getting drop data: ' , e ) ;
124- return ;
125- }
126-
127- if ( html && html . length > 0 ) {
128- post = parsePostFromHTML ( html , builder , plugins ) ;
129- } else if ( text && text . length > 0 ) {
130- post = parsePostFromText ( text , builder , plugins ) ;
127+ if ( html && html . length ) {
128+ return parsePostFromHTML ( html , builder , plugins ) ;
129+ } else if ( text && text . length ) {
130+ return parsePostFromText ( text , builder , plugins ) ;
131131 }
132-
133- return post ;
134132}
0 commit comments