[evolution] Fix handling of the selection in the composer when saving the message draft



commit 6a0592918ac4bf109d67b619cdf3835239b700d4
Author: Tomas Popela <tpopela redhat com>
Date:   Wed Aug 27 16:06:07 2014 +0200

    Fix handling of the selection in the composer when saving the message draft
    
    When saving the draft don't lose the active selection in the web view.
    Also restore the selection when the draft is again opened. Also fix the
    situations when the spell check was not activated when the composer was
    opened.

 composer/e-composer-private.c    |   12 +++----
 composer/e-msg-composer.c        |    4 +-
 e-util/e-html-editor-selection.c |   70 ++++++++++++++++++++------------------
 e-util/e-html-editor-selection.h |    3 ++
 e-util/e-html-editor-view.c      |   20 +++++------
 5 files changed, 56 insertions(+), 53 deletions(-)
---
diff --git a/composer/e-composer-private.c b/composer/e-composer-private.c
index 79bba3d..69d6db4 100644
--- a/composer/e-composer-private.c
+++ b/composer/e-composer-private.c
@@ -867,10 +867,8 @@ composer_move_caret (EMsgComposer *composer)
                                "data-edit-as-new",
                                "",
                                NULL);
-               e_html_editor_selection_restore_caret_position (editor_selection);
-               e_html_editor_selection_scroll_to_caret (editor_selection);
 
-               e_html_editor_view_force_spell_check (view);
+               e_html_editor_selection_scroll_to_caret (editor_selection);
                return;
        }
 
@@ -930,7 +928,6 @@ composer_move_caret (EMsgComposer *composer)
                        e_html_editor_selection_restore_caret_position (editor_selection);
                        if (!html_mode)
                                e_html_editor_view_quote_plain_text (view);
-                       e_html_editor_view_force_spell_check (view);
 
                        input_start = webkit_dom_document_get_element_by_id (
                                document, "-x-evo-input-start");
@@ -979,8 +976,6 @@ composer_move_caret (EMsgComposer *composer)
                        }
                }
 
-               e_html_editor_view_force_spell_check (view);
-
                webkit_dom_range_select_node_contents (
                        new_range,
                        WEBKIT_DOM_NODE (
@@ -995,6 +990,8 @@ composer_move_caret (EMsgComposer *composer)
        g_object_unref (list);
        g_object_unref (blockquotes);
 
+       e_html_editor_view_force_spell_check (view);
+
        e_html_editor_selection_unblock_selection_changed (editor_selection);
 }
 
@@ -1253,7 +1250,8 @@ e_composer_update_signature (EMsgComposer *composer)
 
        g_return_if_fail (E_IS_MSG_COMPOSER (composer));
 
-       /* Do nothing if we're redirecting a message or we disabled the signature * on purpose */
+       /* Do nothing if we're redirecting a message or we disabled
+        * the signature on purpose */
        if (composer->priv->redirect || composer->priv->disable_signature)
                return;
 
diff --git a/composer/e-msg-composer.c b/composer/e-msg-composer.c
index 8c1748c..a728263 100644
--- a/composer/e-msg-composer.c
+++ b/composer/e-msg-composer.c
@@ -1239,12 +1239,12 @@ composer_build_message (EMsgComposer *composer,
                data = g_byte_array_new ();
 
                e_html_editor_view_embed_styles (view);
-               e_html_editor_selection_save_caret_position (selection);
+               e_html_editor_selection_save (selection);
 
                text = e_html_editor_view_get_text_html_for_drafts (view);
 
                e_html_editor_view_remove_embed_styles (view);
-               e_html_editor_selection_restore_caret_position (selection);
+               e_html_editor_selection_restore (selection);
 
                g_byte_array_append (data, (guint8 *) text, strlen (text));
 
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index e691b6f..84f6fbc 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -3552,28 +3552,6 @@ e_html_editor_selection_is_monospaced (EHTMLEditorSelection *selection)
        return ret_val;
 }
 
-static void
-move_caret_into_element (WebKitDOMDocument *document,
-                        WebKitDOMElement *element)
-{
-       WebKitDOMDOMWindow *window;
-       WebKitDOMDOMSelection *window_selection;
-       WebKitDOMRange *new_range;
-
-       if (!element)
-               return;
-
-       window = webkit_dom_document_get_default_view (document);
-       window_selection = webkit_dom_dom_window_get_selection (window);
-       new_range = webkit_dom_document_create_range (document);
-
-       webkit_dom_range_select_node_contents (
-                       new_range, WEBKIT_DOM_NODE (element), NULL);
-       webkit_dom_range_collapse (new_range, FALSE, NULL);
-       webkit_dom_dom_selection_remove_all_ranges (window_selection);
-       webkit_dom_dom_selection_add_range (window_selection, new_range);
-}
-
 /**
  * e_html_editor_selection_set_monospaced:
  * @selection: an #EHTMLEditorSelection
@@ -3666,7 +3644,8 @@ e_html_editor_selection_set_monospaced (EHTMLEditorSelection *selection,
                        webkit_dom_range_insert_node (
                                range, WEBKIT_DOM_NODE (monospace), NULL);
 
-                       move_caret_into_element (document, monospace);
+                       e_html_editor_selection_move_caret_into_element (
+                               document, monospace);
                }
        } else {
                gboolean is_bold, is_italic, is_underline, is_strikethrough;
@@ -4750,6 +4729,28 @@ e_html_editor_selection_replace_image_src (EHTMLEditorSelection *selection,
                image_load_and_insert_async (selection, element, image_uri);
 }
 
+void
+e_html_editor_selection_move_caret_into_element (WebKitDOMDocument *document,
+                                                 WebKitDOMElement *element)
+{
+       WebKitDOMDOMWindow *window;
+       WebKitDOMDOMSelection *window_selection;
+       WebKitDOMRange *new_range;
+
+       if (!element)
+               return;
+
+       window = webkit_dom_document_get_default_view (document);
+       window_selection = webkit_dom_dom_window_get_selection (window);
+       new_range = webkit_dom_document_create_range (document);
+
+       webkit_dom_range_select_node_contents (
+                       new_range, WEBKIT_DOM_NODE (element), NULL);
+       webkit_dom_range_collapse (new_range, FALSE, NULL);
+       webkit_dom_dom_selection_remove_all_ranges (window_selection);
+       webkit_dom_dom_selection_add_range (window_selection, new_range);
+}
+
 /**
  * e_html_editor_selection_clear_caret_position_marker:
  * @selection: an #EHTMLEditorSelection
@@ -4940,14 +4941,14 @@ e_html_editor_selection_restore_caret_position (EHTMLEditorSelection *selection)
                        if (element_has_class (WEBKIT_DOM_ELEMENT (next_sibling), "-x-evo-paragraph")) {
                                remove_node (WEBKIT_DOM_NODE (element));
 
-                               move_caret_into_element (
+                               e_html_editor_selection_move_caret_into_element (
                                        document, WEBKIT_DOM_ELEMENT (next_sibling));
 
                                goto out;
                        }
                }
 
-               move_caret_into_element (document, element);
+               e_html_editor_selection_move_caret_into_element (document, element);
 
                if (fix_after_quoting) {
                        prev_sibling = webkit_dom_node_get_previous_sibling (
@@ -6221,16 +6222,19 @@ e_html_editor_selection_scroll_to_caret (EHTMLEditorSelection *selection)
        EHTMLEditorView *view;
        WebKitDOMDocument *document;
        WebKitDOMDOMWindow *window;
-       WebKitDOMElement *caret;
+       WebKitDOMElement *selection_start_marker;
 
-       caret = e_html_editor_selection_save_caret_position (selection);
-       if (!caret)
-               return;
+       e_html_editor_selection_save (selection);
 
        view = e_html_editor_selection_ref_html_editor_view (selection);
        document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
        g_object_unref (view);
 
+       selection_start_marker = webkit_dom_document_get_element_by_id (
+               document, "-x-evo-selection-start-marker");
+       if (!selection_start_marker)
+               return;
+
        window = webkit_dom_document_get_default_view (document);
 
        window_top = webkit_dom_dom_window_get_scroll_y (window);
@@ -6238,14 +6242,14 @@ e_html_editor_selection_scroll_to_caret (EHTMLEditorSelection *selection)
        window_bottom = window_top + webkit_dom_dom_window_get_inner_height (window);
        window_right = window_left + webkit_dom_dom_window_get_inner_width (window);
 
-       element_left = webkit_dom_element_get_offset_left (caret);
-       element_top = webkit_dom_element_get_offset_top (caret);
+       element_left = webkit_dom_element_get_offset_left (selection_start_marker);
+       element_top = webkit_dom_element_get_offset_top (selection_start_marker);
 
        /* Check if caret is inside viewport, if not move to it */
        if (!(element_top >= window_top && element_top <= window_bottom &&
             element_left >= window_left && element_left <= window_right)) {
-               webkit_dom_element_scroll_into_view (caret, TRUE);
+               webkit_dom_element_scroll_into_view (selection_start_marker, TRUE);
        }
 
-       e_html_editor_selection_clear_caret_position_marker (selection);
+       e_html_editor_selection_restore (selection);
 }
diff --git a/e-util/e-html-editor-selection.h b/e-util/e-html-editor-selection.h
index 2bef77f..3290dfe 100644
--- a/e-util/e-html-editor-selection.h
+++ b/e-util/e-html-editor-selection.h
@@ -183,6 +183,9 @@ void                e_html_editor_selection_replace_image_src
 void           e_html_editor_selection_insert_image
                                                (EHTMLEditorSelection *selection,
                                                 const gchar *image_uri);
+void           e_html_editor_selection_move_caret_into_element
+                                               (WebKitDOMDocument *document,
+                                                WebKitDOMElement *element);
 void           e_html_editor_selection_clear_caret_position_marker
                                                (EHTMLEditorSelection *selection);
 WebKitDOMNode *
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index 90a75de..e0028ff 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -1202,6 +1202,14 @@ html_editor_view_load_status_changed (EHTMLEditorView *view)
        move_elements_to_body (document);
        repair_gmail_blockquotes (document);
 
+       if (webkit_dom_element_get_attribute (WEBKIT_DOM_ELEMENT (body), "data-evo-draft")) {
+               /* Restore the selection how it was when the draft was saved */
+               e_html_editor_selection_move_caret_into_element (
+                       document, WEBKIT_DOM_ELEMENT (body));
+               e_html_editor_selection_restore (
+                       e_html_editor_view_get_selection (view));
+       }
+
        /* Register on input event that is called when the content (body) is modified */
        register_input_event_listener_on_body (view);
 
@@ -6185,17 +6193,11 @@ process_content_for_saving_as_draft (EHTMLEditorView *view)
 {
        WebKitDOMDocument *document;
        WebKitDOMHTMLElement *body;
-       WebKitDOMElement *element, *document_element;
+       WebKitDOMElement *document_element;
        gchar *content;
 
        document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
        body = webkit_dom_document_get_body (document);
-       element = webkit_dom_document_get_element_by_id (
-               document, "-x-evo-caret-position");
-
-       if (element)
-               webkit_dom_element_set_attribute (
-                       element, "style", "display: none; color: red;", NULL);
 
        webkit_dom_element_set_attribute (
                WEBKIT_DOM_ELEMENT (body), "data-evo-draft", "", NULL);
@@ -6207,10 +6209,6 @@ process_content_for_saving_as_draft (EHTMLEditorView *view)
        webkit_dom_element_remove_attribute (
                WEBKIT_DOM_ELEMENT (body), "data-evo-draft");
 
-       if (element)
-               webkit_dom_element_set_attribute (
-                       element, "style", "color: red;", NULL);
-
        return content;
 }
 


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