[geary/bug/728002-webkit2: 73/140] Reimplement inserting/removing \t chars in the composer.



commit fd97ad28878bd0b8fd887d947cba6cc6d5af2c1f
Author: Michael James Gratton <mike vee net>
Date:   Thu Jan 5 13:12:14 2017 +1100

    Reimplement inserting/removing \t chars in the composer.
    
    * ui/composer-web-view.js (ComposerPageState): Add tabOut/tabIn
      functions, listen to key Tab key pressess on the body and invoke the
      appropriate method if found.
    
    * src/client/web-process/util-composer.vala: Removed old vala
      implementation of same.

 src/client/web-process/util-composer.vala |   28 ---------------------------
 ui/composer-web-view.js                   |   30 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 28 deletions(-)
---
diff --git a/src/client/web-process/util-composer.vala b/src/client/web-process/util-composer.vala
index d9c51b8..2855434 100644
--- a/src/client/web-process/util-composer.vala
+++ b/src/client/web-process/util-composer.vala
@@ -27,34 +27,6 @@ namespace Util.Composer {
         Util.DOM.linkify_document(page.get_dom_document());
     }
 
-    public bool handle_key_press(WebKit.WebPage page, Gdk.EventKey event) {
-        WebKit.DOM.Document document = page.get_dom_document();
-        if (event.keyval == Gdk.Key.Tab) {
-            document.exec_command("inserthtml", false,
-                "<span style='white-space: pre-wrap'>\t</span>");
-            return true;
-        }
-
-        if (event.keyval == Gdk.Key.ISO_Left_Tab) {
-            // If there is no selection and the character before the cursor is tab, delete it.
-            // WebKit.DOM.DOMSelection selection = document.get_default_view().get_selection();
-            // if (selection.is_collapsed) {
-            //     selection.modify("extend", "backward", "character");
-            //     try {
-            //         if (selection.get_range_at(0).get_text() == "\t")
-            //             selection.delete_from_document();
-            //         else
-            //             selection.collapse_to_end();
-            //     } catch (Error error) {
-            //         debug("Error handling Left Tab: %s", error.message);
-            //     }
-            // }
-            return true;
-        }
-
-        return false;
-    }
-
 
     /////////////////////// From WebEditorFixer ///////////////////////
 
diff --git a/ui/composer-web-view.js b/ui/composer-web-view.js
index e54dcce..98324d1 100644
--- a/ui/composer-web-view.js
+++ b/ui/composer-web-view.js
@@ -43,7 +43,19 @@ ComposerPageState.prototype = {
         });
     },
     loaded: function() {
+        let state = this;
+
         this.messageBody = document.getElementById(ComposerPageState.BODY_ID);
+        this.messageBody.addEventListener("keydown", function(e) {
+            if (e.keyCode == 9) {
+                if (!e.shiftKey) {
+                    state.tabOut();
+                } else {
+                    state.tabIn();
+                }
+                e.preventDefault();
+            }
+        });
 
         // Search for and remove a particular styling when we quote
         // text. If that style exists in the quoted text, we alter it
@@ -90,6 +102,24 @@ ComposerPageState.prototype = {
         document.execCommand("redo", false, null);
         this.checkCommandStack();
     },
+    tabOut: function() {
+        document.execCommand(
+            "inserthtml", false, "<span style='white-space: pre-wrap'>\t</span>"
+        );
+    },
+    tabIn: function() {
+        // If there is no selection and the character before the
+        // cursor is tab, delete it.
+        let selection = window.getSelection();
+        if (selection.isCollapsed) {
+            selection.modify("extend", "backward", "character");
+            if (selection.getRangeAt(0).toString() == "\t") {
+                document.execCommand("delete", false, null);
+            } else {
+                selection.collapseToEnd();
+            }
+        }
+    },
     getHtml: function() {
         return this.messageBody.innerHTML;
     },


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]