[evolution/wip/webkit-composer: 812/966] EEditorWidget: Speed up the e_editor_widget_force_spellcheck



commit 44b2b0ab7f8ac47d75320bf6b52a6bb8d51cd01a
Author: Tomas Popela <tpopela redhat com>
Date:   Fri Feb 21 18:17:24 2014 +0100

    EEditorWidget: Speed up the e_editor_widget_force_spellcheck

 e-util/e-editor-selection.c |   22 ++++++++++++++++
 e-util/e-editor-selection.h |    4 +++
 e-util/e-editor-widget.c    |   57 +++++++++++++++++++++++++-----------------
 3 files changed, 60 insertions(+), 23 deletions(-)
---
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index 8231133..3d055df 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -254,6 +254,28 @@ editor_selection_selection_changed_cb (WebKitWebView *webview,
        g_object_thaw_notify (G_OBJECT (selection));
 }
 
+void
+e_editor_selection_block_selection_changed (EEditorSelection *selection)
+{
+       EEditorWidget *widget;
+
+       widget = e_editor_selection_ref_editor_widget (selection);
+       g_signal_handlers_block_by_func (
+               widget, editor_selection_selection_changed_cb, selection);
+       g_object_unref (widget);
+}
+
+void
+e_editor_selection_unblock_selection_changed (EEditorSelection *selection)
+{
+       EEditorWidget *widget;
+
+       widget = e_editor_selection_ref_editor_widget (selection);
+       g_signal_handlers_unblock_by_func (
+               widget, editor_selection_selection_changed_cb, selection);
+       g_object_unref (widget);
+}
+
 static void
 editor_selection_set_editor_widget (EEditorSelection *selection,
                                     EEditorWidget *editor_widget)
diff --git a/e-util/e-editor-selection.h b/e-util/e-editor-selection.h
index 920cab3..47a0523 100644
--- a/e-util/e-editor-selection.h
+++ b/e-util/e-editor-selection.h
@@ -69,6 +69,10 @@ GType                e_editor_selection_get_type     (void) G_GNUC_CONST;
 struct _EEditorWidget *
                e_editor_selection_ref_editor_widget
                                                (EEditorSelection *selection);
+void           e_editor_selection_block_selection_changed
+                                               (EEditorSelection *selection);
+void           e_editor_selection_unblock_selection_changed
+                                               (EEditorSelection *selection);
 gint           e_editor_selection_get_word_wrap_length
                                                (EEditorSelection *selection);
 gboolean       e_editor_selection_has_text     (EEditorSelection *selection);
diff --git a/e-util/e-editor-widget.c b/e-util/e-editor-widget.c
index 0e48aed..6e039bd 100644
--- a/e-util/e-editor-widget.c
+++ b/e-util/e-editor-widget.c
@@ -246,30 +246,16 @@ editor_widget_should_show_delete_interface_for_element (EEditorWidget *widget,
        return FALSE;
 }
 
-static gint
-get_word_count_in_element (WebKitDOMHTMLElement *element) {
-       gchar *inner_text;
-       gchar **words;
-       gint count;
-
-       inner_text = webkit_dom_html_element_get_inner_text (element);
-       words = g_strsplit (inner_text, " ", 0);
-       count = g_strv_length (words);
-       g_strfreev (words);
-       g_free (inner_text);
-
-       return count;
-}
-
 void
 e_editor_widget_force_spellcheck (EEditorWidget *widget)
 {
        EEditorSelection *selection;
-       gint ii, word_count;
        WebKitDOMDocument *document;
        WebKitDOMDOMSelection *dom_selection;
        WebKitDOMDOMWindow *window;
        WebKitDOMHTMLElement *body;
+       WebKitDOMRange *end_range, *actual;
+       WebKitDOMText *text;
 
        document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (widget));
        window = webkit_dom_document_get_default_view (document);
@@ -278,13 +264,9 @@ e_editor_widget_force_spellcheck (EEditorWidget *widget)
        /* Enable spellcheck in composer */
        body = webkit_dom_document_get_body (document);
        webkit_dom_element_set_attribute (
-               WEBKIT_DOM_ELEMENT (body),
-               "spellcheck",
-               "true",
-               NULL);
+               WEBKIT_DOM_ELEMENT (body), "spellcheck", "true", NULL);
 
        selection = e_editor_widget_get_selection (widget);
-
        e_editor_selection_save_caret_position (selection);
 
        if (!webkit_dom_document_get_element_by_id (document, "-x-evo-caret-position")) {
@@ -294,13 +276,42 @@ e_editor_widget_force_spellcheck (EEditorWidget *widget)
                e_editor_selection_save_caret_position (selection);
        }
 
+       /* Block callbacks of selection-changed signal as we don't want to
+        * recount all the block format things in EEditorSelection and here as well
+        * when we are moving with caret */
+       g_signal_handlers_block_by_func (widget, editor_widget_selection_changed_cb, NULL);
+       e_editor_selection_block_selection_changed (selection);
+
+       /* Append some text on the end of the body */
+       text = webkit_dom_document_create_text_node (document, "-x-evo-end");
+       webkit_dom_node_append_child (
+               WEBKIT_DOM_NODE (body), WEBKIT_DOM_NODE (text), NULL);
+
+       /* Create range that's pointing on the end of this text */
+       end_range = webkit_dom_document_create_range (document);
+       webkit_dom_range_select_node_contents (end_range, WEBKIT_DOM_NODE (text), NULL);
+       webkit_dom_range_collapse (end_range, FALSE, NULL);
+
+       /* Move on the beginning of the document */
        webkit_dom_dom_selection_modify (dom_selection, "move", "backward", "documentboundary");
 
        /* Go through all words to spellcheck them. To avoid this we have to wait for
         * http://www.w3.org/html/wg/drafts/html/master/editing.html#dom-forcespellcheck */
-       word_count = get_word_count_in_element (body);
-       for (ii = 0; ii < word_count; ii++)
+       actual = webkit_dom_dom_selection_get_range_at (dom_selection, 0, NULL);
+       /* We are moving forward word by word until we hit the text on the end of
+        * the body that we previously inserted there */
+       while (actual && webkit_dom_range_compare_boundary_points (end_range, 2, actual, NULL) != 0) {
                webkit_dom_dom_selection_modify (dom_selection, "move", "forward", "word");
+               actual = webkit_dom_dom_selection_get_range_at (dom_selection, 0, NULL);
+       }
+
+       /* Remove the text that we inserted on the end of the body */
+       webkit_dom_node_remove_child (
+               WEBKIT_DOM_NODE (body), WEBKIT_DOM_NODE (text), NULL);
+
+       /* Unblock the callbacks */
+       g_signal_handlers_unblock_by_func (widget, editor_widget_selection_changed_cb, NULL);
+       e_editor_selection_unblock_selection_changed (selection);
 
        e_editor_selection_restore_caret_position (selection);
 }


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