From f17806f4e842c691f485d27b2dfef04076acf4a0 Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Sun, 10 Sep 2017 23:03:03 +0200
Subject: [PATCH] Allow dragging tweet links over columns to open them in
 detail view

Closes #167
---
 Core/FormBrowser.cs                 |  1 +
 Core/Handling/DragHandlerBrowser.cs | 19 ++++++++++++++
 Resources/Scripts/code.js           | 40 +++++++++++++++++++++++++++++
 TweetDuck.csproj                    |  1 +
 4 files changed, 61 insertions(+)
 create mode 100644 Core/Handling/DragHandlerBrowser.cs

diff --git a/Core/FormBrowser.cs b/Core/FormBrowser.cs
index 42ff40d3..cf18d179 100644
--- a/Core/FormBrowser.cs
+++ b/Core/FormBrowser.cs
@@ -85,6 +85,7 @@ public FormBrowser(UpdaterSettings updaterSettings){
 
             this.browser = new ChromiumWebBrowser("https://tweetdeck.twitter.com/"){
                 DialogHandler = new FileDialogHandler(),
+                DragHandler = new DragHandlerBrowser(),
                 MenuHandler = new ContextMenuBrowser(this),
                 JsDialogHandler = new JavaScriptDialogHandler(),
                 KeyboardHandler = new KeyboardHandlerBrowser(this),
diff --git a/Core/Handling/DragHandlerBrowser.cs b/Core/Handling/DragHandlerBrowser.cs
new file mode 100644
index 00000000..f6103062
--- /dev/null
+++ b/Core/Handling/DragHandlerBrowser.cs
@@ -0,0 +1,19 @@
+using System.Collections.Generic;
+using CefSharp;
+
+namespace TweetDuck.Core.Handling{
+    sealed class DragHandlerBrowser : IDragHandler{
+        public bool OnDragEnter(IWebBrowser browserControl, IBrowser browser, IDragData dragData, DragOperationsMask mask){
+            if (dragData.IsLink){
+                browserControl.ExecuteScriptAsync("window.TDGF_onGlobalDragStart", "link", dragData.LinkUrl);
+            }
+            else{
+                browserControl.ExecuteScriptAsync("window.TDGF_onGlobalDragStart", "unknown");
+            }
+
+            return false;
+        }
+
+        public void OnDraggableRegionsChanged(IWebBrowser browserControl, IBrowser browser, IList<DraggableRegion> regions){}
+    }
+}
diff --git a/Resources/Scripts/code.js b/Resources/Scripts/code.js
index 17d32254..04c9f9f2 100644
--- a/Resources/Scripts/code.js
+++ b/Resources/Scripts/code.js
@@ -683,6 +683,46 @@
     };
   })();
   
+  //
+  // Block: Allow drag & drop behavior for dropping links on columns to open their detail view.
+  //
+  (function(){
+    let tweetRegex = /^https?:\/\/twitter\.com\/[A-Za-z0-9_]+\/status\/(\d+)\/?$/;
+    let isDraggingValid = false;
+    
+    window.TDGF_onGlobalDragStart = function(type, data){
+      isDraggingValid = type === "link" && tweetRegex.test(data);
+    };
+    
+    app.delegate("section.js-column", {
+      dragover: function(e){
+        e.originalEvent.dataTransfer.dropEffect = isDraggingValid ? "move" : "none";
+        e.preventDefault();
+        e.stopPropagation();
+      },
+      
+      drop: function(e){
+        let match = tweetRegex.exec(e.originalEvent.dataTransfer.getData("URL"));
+        
+        if (match.length === 2){
+          let column = TD.controller.columnManager.get($(this).attr("data-column"));
+          
+          if (column){
+            TD.controller.clients.getPreferredClient().show(match[1], function(chirp){
+              TD.ui.updates.showDetailView(column, chirp, column.findChirp(chirp) || chirp);
+              $(document).trigger("uiGridClearSelection");
+            }, function(){
+              alert("error|Could not retrieve the requested tweet.");
+            });
+          }
+        }
+        
+        e.preventDefault();
+        e.stopPropagation();
+      }
+    });
+  })();
+  
   //
   // Block: Fix scheduled tweets not showing up sometimes.
   //
diff --git a/TweetDuck.csproj b/TweetDuck.csproj
index 433ab4b3..11be4398 100644
--- a/TweetDuck.csproj
+++ b/TweetDuck.csproj
@@ -88,6 +88,7 @@
       <SubType>Component</SubType>
     </Compile>
     <Compile Include="Core\FormManager.cs" />
+    <Compile Include="Core\Handling\DragHandlerBrowser.cs" />
     <Compile Include="Core\Handling\General\BrowserProcessHandler.cs" />
     <Compile Include="Core\Handling\ContextMenuBase.cs" />
     <Compile Include="Core\Handling\ContextMenuBrowser.cs" />