[evolution/wip/webkit-composer] EEditorWidget: Introduce spellchecking function for current paragraph



commit 92392266101974d8c8937b253bfc2b9517d4247f
Author: Tomas Popela <tpopela redhat com>
Date:   Wed Mar 12 14:23:24 2014 +0100

    EEditorWidget: Introduce spellchecking function for current paragraph

 e-util/e-editor-selection.c |   11 ++---
 e-util/e-editor-widget.c    |  103 ++++++++++++++++++++++++++++++++++++++++---
 e-util/e-editor-widget.h    |    2 +
 3 files changed, 104 insertions(+), 12 deletions(-)
---
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index e80b5e6..f96d4ac 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -1809,7 +1809,7 @@ e_editor_selection_set_block_format (EEditorSelection *selection,
        }
 
        if (!has_selection)
-               e_editor_widget_force_spell_check (editor_widget);
+               e_editor_widget_force_spell_check_for_current_paragraph (editor_widget);
 
        g_object_unref (editor_widget);
 
@@ -2182,7 +2182,7 @@ e_editor_selection_indent (EEditorSelection *selection)
                e_editor_widget_exec_command (editor_widget, command, NULL);
        }
 
-       e_editor_widget_force_spell_check (editor_widget);
+       e_editor_widget_force_spell_check_for_current_paragraph (editor_widget);
 
        g_object_unref (editor_widget);
 
@@ -2365,7 +2365,7 @@ e_editor_selection_unindent (EEditorSelection *selection)
                e_editor_widget_exec_command (editor_widget, command, NULL);
        }
 
-       e_editor_widget_force_spell_check (editor_widget);
+       e_editor_widget_force_spell_check_for_current_paragraph (editor_widget);
 
        g_object_unref (editor_widget);
 
@@ -2903,7 +2903,8 @@ e_editor_selection_set_monospaced (EEditorSelection *selection,
                        g_free (outer_html);
                        g_free (tmp);
 
-                       e_editor_widget_force_spell_check (editor_widget);
+                       e_editor_widget_force_spell_check_for_current_paragraph (
+                               editor_widget);
                }
 
                /* Re-set formatting */
@@ -3997,8 +3998,6 @@ e_editor_selection_restore_caret_position (EEditorSelection *selection)
                        webkit_dom_dom_selection_modify (
                                window_selection, "move", "forward", "character");
                }
-
-               webkit_dom_node_normalize (parent_node);
        }
 }
 
diff --git a/e-util/e-editor-widget.c b/e-util/e-editor-widget.c
index 18c102b..63bc9d8 100644
--- a/e-util/e-editor-widget.c
+++ b/e-util/e-editor-widget.c
@@ -199,6 +199,91 @@ editor_widget_should_show_delete_interface_for_element (EEditorWidget *widget,
        return FALSE;
 }
 
+void
+e_editor_widget_force_spell_check_for_current_paragraph (EEditorWidget *widget)
+{
+       EEditorSelection *selection;
+       WebKitDOMDocument *document;
+       WebKitDOMDOMSelection *dom_selection;
+       WebKitDOMDOMWindow *window;
+       WebKitDOMElement *caret, *parent, *element;
+       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);
+       dom_selection = webkit_dom_dom_window_get_selection (window);
+
+       element = webkit_dom_document_query_selector (
+               document, "body[spellcheck=true]", NULL);
+
+       if (!element)
+               return;
+
+       selection = e_editor_widget_get_selection (widget);
+       caret = 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);
+
+       caret = e_editor_selection_save_caret_position (selection);
+
+       parent = webkit_dom_node_get_parent_element (WEBKIT_DOM_NODE (caret));
+       element = caret;
+
+       while (!WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
+               element = parent;
+               parent = webkit_dom_node_get_parent_element (
+                       WEBKIT_DOM_NODE (parent));
+       }
+
+       /* Append some text on the end of the element */
+       text = webkit_dom_document_create_text_node (document, "-x-evo-end");
+       webkit_dom_node_append_child (
+               WEBKIT_DOM_NODE (element), 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 paragraph */
+       actual = webkit_dom_document_create_range (document);
+       webkit_dom_range_select_node_contents (
+               actual, WEBKIT_DOM_NODE (element), NULL);
+       webkit_dom_range_collapse (actual, TRUE, NULL);
+       webkit_dom_dom_selection_remove_all_ranges (dom_selection);
+       webkit_dom_dom_selection_add_range (dom_selection, actual);
+
+       /* 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 */
+       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 paragraph 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 paragraph */
+       webkit_dom_node_remove_child (
+               WEBKIT_DOM_NODE (element), 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);
+}
+
 static void
 refresh_spell_check (EEditorWidget *widget,
                      gboolean enable_spell_check)
@@ -229,7 +314,8 @@ refresh_spell_check (EEditorWidget *widget,
        /* 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);
+       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 */
@@ -239,11 +325,13 @@ refresh_spell_check (EEditorWidget *widget,
 
        /* 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_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");
+       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 */
@@ -251,8 +339,10 @@ refresh_spell_check (EEditorWidget *widget,
        /* 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);
+               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 */
@@ -260,7 +350,8 @@ refresh_spell_check (EEditorWidget *widget,
                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);
+       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);
diff --git a/e-util/e-editor-widget.h b/e-util/e-editor-widget.h
index 37128f0..e71057d 100644
--- a/e-util/e-editor-widget.h
+++ b/e-util/e-editor-widget.h
@@ -139,6 +139,8 @@ void                e_editor_widget_dequote_plain_text
                                                (EEditorWidget *widget);
 void           e_editor_widget_turn_spell_check_off
                                                (EEditorWidget *widget);
+void           e_editor_widget_force_spell_check_for_current_paragraph
+                                               (EEditorWidget *widget);
 void           e_editor_widget_force_spell_check
                                                (EEditorWidget *widget);
 void           e_editor_widget_add_inline_image_from_mime_part


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