Add feature to fix link drag&drop in CC

This change adds an option to fix the drag&drop bug that exists in
CKEditor (https://dev.ckeditor.com/ticket/13569), which is the library
used for the rich text editor in the Community Console.

Fixes: #20
Change-Id: Ic82bf6ee6d583876858f21291723dacb5e74debe
diff --git a/src/content_scripts/console_inject.js b/src/content_scripts/console_inject.js
index b5e86f8..4dba614 100644
--- a/src/content_scripts/console_inject.js
+++ b/src/content_scripts/console_inject.js
@@ -32,6 +32,16 @@
   node.appendChild(container);
 }
 
+function applyDragAndDropFix(node) {
+  console.debug('Adding link drag&drop fix to ', node);
+  node.addEventListener('drop', e => {
+    if (e.dataTransfer.types.includes('text/uri-list')) {
+      e.stopImmediatePropagation();
+      console.debug('Stopping link drop event propagation.');
+    }
+  }, true);
+}
+
 function mutationCallback(mutationList, observer) {
   mutationList.forEach((mutation) => {
     if (mutation.type == 'childList') {
@@ -93,6 +103,17 @@
               node.querySelector('.main-card-content').appendChild(container);
             }
           }
+
+          // We target both tags because in different contexts different
+          // elements containing the text editor get added to the DOM structure.
+          // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
+          // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
+          // directly.
+          if (options.ccdragndropfix && ('tagName' in node) &&
+              (node.tagName == 'EC-MOVABLE-DIALOG' ||
+               node.tagName == 'EC-RICH-TEXT-EDITOR')) {
+            applyDragAndDropFix(node);
+          }
         }
       });
     }