Web Inspector: Elements: allow nodes to be copied and pasted
authordrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Jul 2019 16:18:09 +0000 (16:18 +0000)
committerdrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Jul 2019 16:18:09 +0000 (16:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=199182

Reviewed by Matt Baker.

Copy is already supported, so there's nothing that needs to be done there.

Pasting is as simple as calling `insertAdjacentHTML("afterend", ...)` on the selected node.

* UserInterface/Base/Main.js:
(WI.contentLoaded):
(WI._paste): Added.

* UserInterface/Views/DOMTreeContentView.js:
(WI.DOMTreeContentView.prototype.handlePasteEvent): Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247054 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Base/Main.js
Source/WebInspectorUI/UserInterface/Views/DOMTreeContentView.js

index 2d21b5d..c4f19a7 100644 (file)
@@ -1,5 +1,23 @@
 2019-07-02  Devin Rousso  <drousso@apple.com>
 
+        Web Inspector: Elements: allow nodes to be copied and pasted
+        https://bugs.webkit.org/show_bug.cgi?id=199182
+
+        Reviewed by Matt Baker.
+
+        Copy is already supported, so there's nothing that needs to be done there.
+
+        Pasting is as simple as calling `insertAdjacentHTML("afterend", ...)` on the selected node.
+
+        * UserInterface/Base/Main.js:
+        (WI.contentLoaded):
+        (WI._paste): Added.
+
+        * UserInterface/Views/DOMTreeContentView.js:
+        (WI.DOMTreeContentView.prototype.handlePasteEvent): Added.
+
+2019-07-02  Devin Rousso  <drousso@apple.com>
+
         Web Inspector: DOM Debugger: descendant breakpoints should be able to be enabled/disabled/deleted from a collapsed parent
         https://bugs.webkit.org/show_bug.cgi?id=199332
 
index eeda649..e9e6acd 100644 (file)
@@ -269,6 +269,7 @@ WI.contentLoaded = function()
     // Register for global events.
     document.addEventListener("beforecopy", WI._beforecopy);
     document.addEventListener("copy", WI._copy);
+    document.addEventListener("paste", WI._paste);
 
     document.addEventListener("click", WI._mouseWasClicked);
     document.addEventListener("dragover", WI._handleDragOver);
@@ -2620,6 +2621,34 @@ WI._copy = function(event)
     event.preventDefault();
 };
 
+WI._paste = function(event)
+{
+    let selection = window.getSelection();
+
+    // If there is no selection, pass the paste event on to the focused element or focused ContentView.
+    if (!selection.isCollapsed || WI.isEventTargetAnEditableField(event))
+        return;
+
+    let focusedPasteHandler = WI.currentFocusElement && WI.currentFocusElement.pasteHandler;
+    if (focusedPasteHandler && focusedPasteHandler.handlePasteEvent) {
+        focusedPasteHandler.handlePasteEvent(event);
+        if (event.defaultPrevented)
+            return;
+    }
+
+    let focusedContentView = WI._focusedContentView();
+    if (focusedContentView && focusedContentView.handlePasteEvent) {
+        focusedContentView.handlePasteEvent(event);
+        return;
+    }
+
+    let tabContentView = WI.tabBrowser.selectedTabContentView;
+    if (tabContentView && tabContentView.handlePasteEvent) {
+        tabContentView.handlePasteEvent(event);
+        return;
+    }
+};
+
 WI._increaseZoom = function(event)
 {
     const epsilon = 0.0001;
index b018897..5db19b8 100644 (file)
@@ -251,6 +251,19 @@ WI.DOMTreeContentView = class DOMTreeContentView extends WI.ContentView
         selectedDOMNode.copyNode();
     }
 
+    handlePasteEvent(event)
+    {
+        let selectedDOMNode = this._domTreeOutline.selectedDOMNode();
+        if (!selectedDOMNode)
+            return;
+
+        let text = event.clipboardData.getData("text/plain");
+        if (!text)
+            return;
+
+        selectedDOMNode.insertAdjacentHTML("afterend", text);
+    }
+
     get supportsSave()
     {
         return WI.canArchiveMainFrame();