[evolution] EHTMLEditor - Improve lists formats



commit d41e9fb03f7f4ef975dd41ff49067c96b487ef24
Author: Tomas Popela <tpopela redhat com>
Date:   Thu Jul 2 16:02:54 2015 +0200

    EHTMLEditor - Improve lists formats
    
    With this change the "Bulleted" list will have star character as a bullet
    instead of a default HTML one. This applies for Plain Text mode, the HTML one
    is left unchanged and the list bullets are given by WebKit.
    
    With this change the lists are more compact as the indentation level is now set
    to 3 characters (before 4) and the list level is set to 3 (from 8) as well. For
    ordered lists (Numbered, Roman Numeral, Alphabetical) the initial indentation is
    set to 6 as there won't be enough space for counters (especially for Roman numerals).
    
    This also removes some definitions around lists alignments as they are not needed
    anymore.

 e-util/e-html-editor-selection.c |   93 ++++++++++-------------
 e-util/e-html-editor-view.c      |  157 ++++++++++++++++++++++++++++++++------
 e-util/e-html-editor-view.h      |    5 +-
 3 files changed, 175 insertions(+), 80 deletions(-)
---
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index 01c375a..a3a0bfb 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -1156,14 +1156,12 @@ e_html_editor_selection_replace (EHTMLEditorSelection *selection,
 EHTMLEditorSelectionAlignment
 e_html_editor_selection_get_list_alignment_from_node (WebKitDOMNode *node)
 {
-       if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-list-item-align-left"))
-               return E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
-       if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-list-item-align-center"))
+       if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-align-center"))
                return E_HTML_EDITOR_SELECTION_ALIGNMENT_CENTER;
-       if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-list-item-align-right"))
+       if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-align-right"))
                return E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT;
-
-       return E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
+       else
+               return E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
 }
 
 static EHTMLEditorSelectionAlignment
@@ -1284,22 +1282,31 @@ create_list_element (EHTMLEditorSelection *selection,
                     gint level,
                      gboolean html_mode)
 {
+       gboolean inserting_unordered_list;
        WebKitDOMElement *list;
-       gint offset = -SPACES_PER_LIST_LEVEL;
-       gboolean inserting_unordered_list =
-               format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;
+
+       inserting_unordered_list = format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;
 
        list = webkit_dom_document_create_element (
                document, inserting_unordered_list  ? "UL" : "OL", NULL);
 
-       set_ordered_list_type_to_element (list, format);
+       if (!inserting_unordered_list)
+               set_ordered_list_type_to_element (list, format);
+
+       if (level >= 0 && !html_mode) {
+               gint offset;
 
-       if (level >= 0)
-               offset = (level + 1) * -SPACES_PER_LIST_LEVEL;
+               offset = (level + 1) * SPACES_PER_LIST_LEVEL;
+
+               offset += !inserting_unordered_list ?
+                       SPACES_ORDERED_LIST_FIRST_LEVEL - SPACES_PER_LIST_LEVEL: 0;
 
-       if (!html_mode)
                e_html_editor_selection_set_paragraph_style (
-                       selection, list, -1, offset, "");
+                       selection, list, -1, -offset, "");
+
+               if (inserting_unordered_list)
+                       webkit_dom_element_set_attribute (list, "data-evo-plain-text", "", NULL);
+       }
 
        return list;
 }
@@ -1544,7 +1551,7 @@ e_html_editor_selection_set_alignment (EHTMLEditorSelection *selection,
        EHTMLEditorView *view;
        EHTMLEditorViewHistoryEvent *ev = NULL;
        gboolean after_selection_end = FALSE;
-       const gchar *class = "", *list_class = "";
+       const gchar *class = "";
        WebKitDOMDocument *document;
        WebKitDOMElement *selection_start_marker, *selection_end_marker;
        WebKitDOMNode *block;
@@ -1562,7 +1569,6 @@ e_html_editor_selection_set_alignment (EHTMLEditorSelection *selection,
        switch (alignment) {
                case E_HTML_EDITOR_SELECTION_ALIGNMENT_CENTER:
                        class = "-x-evo-align-center";
-                       list_class = "-x-evo-list-item-align-center";
                        break;
 
                case E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT:
@@ -1570,7 +1576,6 @@ e_html_editor_selection_set_alignment (EHTMLEditorSelection *selection,
 
                case E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT:
                        class = "-x-evo-align-right";
-                       list_class = "-x-evo-list-item-align-right";
                        break;
        }
 
@@ -1615,51 +1620,31 @@ e_html_editor_selection_set_alignment (EHTMLEditorSelection *selection,
                after_selection_end = webkit_dom_node_contains (
                        block, WEBKIT_DOM_NODE (selection_end_marker));
 
-               if (node_is_list (block)) {
-                       WebKitDOMNode *item = webkit_dom_node_get_first_child (block);
+               if (element_has_class (WEBKIT_DOM_ELEMENT (block), "-x-evo-indented")) {
+                       gint ii, length;
+                       WebKitDOMNodeList *list;
 
-                       while (item && WEBKIT_DOM_IS_HTMLLI_ELEMENT (item)) {
-                               element_remove_class (
-                                       WEBKIT_DOM_ELEMENT (item),
-                                       "-x-evo-list-item-align-center");
-                               element_remove_class (
-                                       WEBKIT_DOM_ELEMENT (item),
-                                       "-x-evo-list-item-align-right");
+                       list = webkit_dom_element_query_selector_all (
+                               WEBKIT_DOM_ELEMENT (block),
+                               ".-x-evo-indented > *:not(.-x-evo-indented):not(li)",
+                               NULL);
+                       length = webkit_dom_node_list_get_length (list);
+
+                       for (ii = 0; ii < length; ii++) {
+                               WebKitDOMNode *item = webkit_dom_node_list_item (list, ii);
+
+                               set_block_alignment (WEBKIT_DOM_ELEMENT (item), class);
 
-                               element_add_class (WEBKIT_DOM_ELEMENT (item), list_class);
                                after_selection_end = webkit_dom_node_contains (
                                        item, WEBKIT_DOM_NODE (selection_end_marker));
+                               g_object_unref (item);
                                if (after_selection_end)
                                        break;
-                               item = webkit_dom_node_get_next_sibling (item);
                        }
-               } else {
-                       if (element_has_class (WEBKIT_DOM_ELEMENT (block), "-x-evo-indented")) {
-                               gint ii, length;
-                               WebKitDOMNodeList *list;
-
-                               list = webkit_dom_element_query_selector_all (
-                                       WEBKIT_DOM_ELEMENT (block),
-                                       ".-x-evo-indented > *:not(.-x-evo-indented):not(li)",
-                                       NULL);
-                               length = webkit_dom_node_list_get_length (list);
 
-                               for (ii = 0; ii < length; ii++) {
-                                       WebKitDOMNode *item = webkit_dom_node_list_item (list, ii);
-
-                                       set_block_alignment (WEBKIT_DOM_ELEMENT (item), class);
-
-                                       after_selection_end = webkit_dom_node_contains (
-                                               item, WEBKIT_DOM_NODE (selection_end_marker));
-                                       g_object_unref (item);
-                                       if (after_selection_end)
-                                               break;
-                               }
-
-                               g_object_unref (list);
-                       } else {
-                               set_block_alignment (WEBKIT_DOM_ELEMENT (block), class);
-                       }
+                       g_object_unref (list);
+               } else {
+                       set_block_alignment (WEBKIT_DOM_ELEMENT (block), class);
                }
 
                block = next_block;
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index adcd83e..647e0dc 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -3428,6 +3428,48 @@ body_keyup_event_cb (WebKitDOMElement *element,
                e_html_editor_selection_restore (selection);
        } else if (key_code == HTML_KEY_CODE_CONTROL)
                html_editor_view_set_links_active (view, FALSE);
+       else if (key_code == HTML_KEY_CODE_RETURN) {
+               WebKitDOMDocument *document;
+               WebKitDOMElement *selection_start_marker, *selection_end_marker;
+               WebKitDOMNode *parent;
+
+               /* If the return is pressed in an unordered list in plain text mode
+                * the caret is moved to the "*" character before the the newly inserted
+                * item. It looks like it is not enough that the item has BR element
+                * inside, but we have to again use the zero width space character
+                * to fix the situation. */
+               if (e_html_editor_view_get_html_mode (view))
+                       return;
+
+               e_html_editor_selection_save (selection);
+
+               document = webkit_dom_node_get_owner_document (WEBKIT_DOM_NODE (element));
+
+               e_html_editor_selection_save (selection);
+
+               selection_start_marker = webkit_dom_document_get_element_by_id (
+                       document, "-x-evo-selection-start-marker");
+               selection_end_marker = webkit_dom_document_get_element_by_id (
+                       document, "-x-evo-selection-end-marker");
+
+               parent = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (selection_start_marker));
+               if (!WEBKIT_DOM_IS_HTMLLI_ELEMENT (parent) ||
+                   !WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (webkit_dom_node_get_parent_node (parent))) {
+                       e_html_editor_selection_restore (selection);
+                       return;
+               }
+
+               if (!webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (selection_start_marker)) &&
+                   (!webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE (selection_end_marker)) ||
+                    WEBKIT_DOM_IS_HTMLBR_ELEMENT (webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE 
(selection_end_marker)))))
+                       webkit_dom_html_element_insert_adjacent_text (
+                               WEBKIT_DOM_HTML_ELEMENT (parent),
+                               "afterbegin",
+                               UNICODE_ZERO_WIDTH_SPACE,
+                               NULL);
+
+               e_html_editor_selection_restore (selection);
+       }
 }
 
 static void
@@ -8012,6 +8054,8 @@ process_list_to_plain_text (EHTMLEditorView *view,
                                                /* put spaces before line characters -> wordwraplength - 
indentation */
                                                for (ii = 0; ii < level; ii++)
                                                        g_string_append (line, indent_per_level);
+                                               if (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (element))
+                                                       g_string_append (line, indent_per_level);
                                                g_string_append (item_value, line->str);
                                                g_string_erase (line, 0, -1);
                                        }
@@ -8027,6 +8071,8 @@ process_list_to_plain_text (EHTMLEditorView *view,
 
                                        fill_length = word_wrap_length - g_utf8_strlen (line->str, -1);
                                        fill_length -= ii * SPACES_PER_LIST_LEVEL;
+                                       if (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (element))
+                                               fill_length += SPACES_PER_LIST_LEVEL;
                                        fill_length /= 2;
 
                                        if (fill_length < 0)
@@ -8080,7 +8126,7 @@ process_list_to_plain_text (EHTMLEditorView *view,
                                if (tmp == 1)
                                        length++;
 
-                               space = g_strnfill (SPACES_PER_LIST_LEVEL - 2 - length, ' ');
+                               space = g_strnfill (SPACES_ORDERED_LIST_FIRST_LEVEL - 2 - length, ' ');
                                item_str = g_strdup_printf (
                                        "%s%d. %s", space, counter, item_value->str);
                                g_free (space);
@@ -8094,8 +8140,7 @@ process_list_to_plain_text (EHTMLEditorView *view,
                                else
                                        value = get_roman_value (counter, FALSE);
 
-                               /* Value already containes dot and space */
-                               space = g_strnfill (SPACES_PER_LIST_LEVEL - strlen (value), ' ');
+                               space = g_strnfill (SPACES_ORDERED_LIST_FIRST_LEVEL - strlen (value), ' ');
                                item_str = g_strdup_printf (
                                        "%s%s%s", space, value, item_value->str);
                                g_free (space);
@@ -8106,6 +8151,9 @@ process_list_to_plain_text (EHTMLEditorView *view,
                                for (ii = 0; ii < level - 1; ii++) {
                                        g_string_append (output, indent_per_level);
                                }
+                               if (WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (element))
+                                       if (e_html_editor_dom_node_find_parent_element (item, "OL"))
+                                               g_string_append (output, indent_per_level);
                                g_string_append (output, item_str);
                        }
 
@@ -8144,6 +8192,8 @@ process_list_to_plain_text (EHTMLEditorView *view,
 
                                        fill_length = word_wrap_length - g_utf8_strlen (item_str, -1);
                                        fill_length -= ii * SPACES_PER_LIST_LEVEL;
+                                       if (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (element))
+                                               fill_length += SPACES_PER_LIST_LEVEL;
                                        fill_length /= 2;
 
                                        if (fill_length < 0)
@@ -8206,6 +8256,7 @@ remove_evolution_attributes (WebKitDOMElement *element)
        webkit_dom_element_remove_attribute (element, "data-name");
        webkit_dom_element_remove_attribute (element, "data-new-message");
        webkit_dom_element_remove_attribute (element, "data-user-wrapped");
+       webkit_dom_element_remove_attribute (element, "data-evo-plain-text");
        webkit_dom_element_remove_attribute (element, "spellcheck");
 }
 
@@ -9706,6 +9757,32 @@ toggle_tables (EHTMLEditorView *view)
        g_object_unref (list);
 }
 
+static void
+toggle_unordered_lists (EHTMLEditorView *view)
+{
+       WebKitDOMDocument *document;
+       WebKitDOMNodeList *list;
+       gint ii, length;
+
+       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
+       list = webkit_dom_document_query_selector_all (document, "ul", NULL);
+       length = webkit_dom_node_list_get_length (list);
+
+       for (ii = 0; ii < length; ii++) {
+               WebKitDOMNode *node = webkit_dom_node_list_item (list, ii);
+
+               if (view->priv->html_mode) {
+                       webkit_dom_element_remove_attribute (
+                               WEBKIT_DOM_ELEMENT (node), "data-evo-plain-text");
+               } else {
+                       webkit_dom_element_set_attribute (
+                               WEBKIT_DOM_ELEMENT (node), "data-evo-plain-text", "", NULL);
+               }
+               g_object_unref (node);
+       }
+       g_object_unref (list);
+}
+
 /**
  * e_html_editor_view_set_html_mode:
  * @view: an #EHTMLEditorView
@@ -9781,6 +9858,7 @@ e_html_editor_view_set_html_mode (EHTMLEditorView *view,
                toggle_paragraphs_style (view);
                toggle_smileys (view);
                toggle_tables (view);
+               toggle_unordered_lists (view);
                remove_wrapping_from_element (WEBKIT_DOM_ELEMENT (body));
 
                g_object_notify (G_OBJECT (selection), "font-color");
@@ -9798,6 +9876,7 @@ e_html_editor_view_set_html_mode (EHTMLEditorView *view,
                toggle_paragraphs_style (view);
                toggle_smileys (view);
                toggle_tables (view);
+               toggle_unordered_lists (view);
                remove_images (view);
                remove_background_images_in_document (document);
 
@@ -10868,61 +10947,91 @@ e_html_editor_view_update_fonts (EHTMLEditorView *view)
                "  display : inline-block;\n"
                "}\n");
 
-       g_string_append (
+       g_string_append_printf (
                stylesheet,
-               "ul,ol "
+               "ul[data-evo-plain-text]"
                "{\n"
-               "  -webkit-padding-start: 7ch; \n"
-               "}\n");
+               "  list-style: outside none;\n"
+               "  -webkit-padding-start: %dch; \n"
+               "}\n", SPACES_PER_LIST_LEVEL);
+
+       g_string_append_printf (
+               stylesheet,
+               "ul[data-evo-plain-text] > li"
+               "{\n"
+               "  list-style-position: outside;\n"
+               "  text-indent: -%dch;\n"
+               "}\n", SPACES_PER_LIST_LEVEL - 1);
 
        g_string_append (
                stylesheet,
-               ".-x-evo-align-left "
+               "ul[data-evo-plain-text] > li::before "
                "{\n"
-               "  text-align: left; \n"
+               "  content: \"*"UNICODE_NBSP"\";\n"
                "}\n");
 
+       g_string_append_printf (
+               stylesheet,
+               "ul[data-evo-plain-text].-x-evo-indented "
+               "{\n"
+               "  -webkit-padding-start: %dch; \n"
+               "}\n", SPACES_PER_LIST_LEVEL);
+
        g_string_append (
                stylesheet,
-               ".-x-evo-align-center "
+               "ul:not([data-evo-plain-text]),ol > li.-x-evo-align-center"
                "{\n"
-               "  text-align: center; \n"
+               "  list-style-position: inside;\n"
                "}\n");
 
        g_string_append (
                stylesheet,
-               ".-x-evo-align-right "
+               "ul:not([data-evo-plain-text]),ol > li.-x-evo-align-right"
                "{\n"
-               "  text-align: right; \n"
+               "  list-style-position: inside;\n"
                "}\n");
 
+       g_string_append_printf (
+               stylesheet,
+               "ul:not([data-evo-plain-text]),ol > li"
+               "{\n"
+               "  text-indent: -%dch;\n"
+               "  list-style-position: inside;\n"
+               "}\n", SPACES_PER_LIST_LEVEL);
+
+       g_string_append_printf (
+               stylesheet,
+               "ol"
+               "{\n"
+               "  -webkit-padding-start: %dch; \n"
+               "}\n", SPACES_ORDERED_LIST_FIRST_LEVEL);
+
+       g_string_append_printf (
+               stylesheet,
+               "ol.-x-evo-indented"
+               "{\n"
+               "  -webkit-padding-start: %dch; \n"
+               "}\n", SPACES_PER_LIST_LEVEL);
+
        g_string_append (
                stylesheet,
-               ".-x-evo-list-item-align-left "
+               ".-x-evo-align-left "
                "{\n"
                "  text-align: left; \n"
                "}\n");
 
        g_string_append (
                stylesheet,
-               ".-x-evo-list-item-align-center "
+               ".-x-evo-align-center "
                "{\n"
                "  text-align: center; \n"
-               "  -webkit-padding-start: 0ch; \n"
-               "  margin-left: -3ch; \n"
-               "  margin-right: 1ch; \n"
-               "  list-style-position: inside; \n"
                "}\n");
 
        g_string_append (
                stylesheet,
-               ".-x-evo-list-item-align-right "
+               ".-x-evo-align-right "
                "{\n"
                "  text-align: right; \n"
-               "  -webkit-padding-start: 0ch; \n"
-               "  margin-left: -3ch; \n"
-               "  margin-right: 1ch; \n"
-               "  list-style-position: inside; \n"
                "}\n");
 
        g_string_append (
diff --git a/e-util/e-html-editor-view.h b/e-util/e-html-editor-view.h
index e1fc64f..bf5ad64 100644
--- a/e-util/e-html-editor-view.h
+++ b/e-util/e-html-editor-view.h
@@ -56,8 +56,9 @@
 #define UNICODE_ZERO_WIDTH_SPACE "\xe2\x80\x8b"
 #define UNICODE_NBSP "\xc2\xa0"
 
-#define SPACES_PER_INDENTATION 4
-#define SPACES_PER_LIST_LEVEL 8
+#define SPACES_PER_INDENTATION 3
+#define SPACES_PER_LIST_LEVEL 3
+#define SPACES_ORDERED_LIST_FIRST_LEVEL 6
 #define MINIMAL_PARAGRAPH_WIDTH 5
 #define TAB_LENGTH 8
 


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