[evolution/wip/webkit2] EHTMLEditor - Improve lists formats



commit a1c8c49113eb2f42cd9fa7f421c40e1537edbf1c
Author: Tomas Popela <tpopela redhat com>
Date:   Fri Feb 26 09:23:00 2016 +0100

    EHTMLEditor - Improve lists formats

 e-util/e-html-editor-view.c                        |   76 +++++++++++-----
 .../e-html-editor-selection-dom-functions.c        |   95 ++++++++-----------
 .../e-html-editor-selection-dom-functions.h        |    5 +-
 .../composer/e-html-editor-view-dom-functions.c    |   85 +++++++++++++++++-
 4 files changed, 180 insertions(+), 81 deletions(-)
---
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index 0e10e7a..31c7939 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -38,6 +38,10 @@
        (G_TYPE_INSTANCE_GET_PRIVATE \
        ((obj), E_TYPE_HTML_EDITOR_VIEW, EHTMLEditorViewPrivate))
 
+#define UNICODE_NBSP "\xc2\xa0"
+#define SPACES_PER_LIST_LEVEL 3
+#define SPACES_ORDERED_LIST_FIRST_LEVEL 6
+
 /**
  * EHTMLEditorView:
  *
@@ -2554,61 +2558,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/web-extensions/composer/e-html-editor-selection-dom-functions.c 
b/web-extensions/composer/e-html-editor-selection-dom-functions.c
index 156e96c..7c5d7a1 100644
--- a/web-extensions/composer/e-html-editor-selection-dom-functions.c
+++ b/web-extensions/composer/e-html-editor-selection-dom-functions.c
@@ -584,21 +584,30 @@ create_list_element (WebKitDOMDocument *document,
                     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;
+
+               offset = (level + 1) * SPACES_PER_LIST_LEVEL;
+
+               offset += !inserting_unordered_list ?
+                       SPACES_ORDERED_LIST_FIRST_LEVEL - SPACES_PER_LIST_LEVEL: 0;
 
-       if (level >= 0)
-               offset = (level + 1) * -SPACES_PER_LIST_LEVEL;
+               dom_set_paragraph_style (document, extension, list, -1, -offset, "");
 
-       if (!html_mode)
-               dom_set_paragraph_style (document, extension, list, -1, offset, "");
+               if (inserting_unordered_list)
+                       webkit_dom_element_set_attribute (list, "data-evo-plain-text", "", NULL);
+       }
 
        return list;
 }
@@ -5530,7 +5539,7 @@ dom_selection_set_alignment (WebKitDOMDocument *document,
        EHTMLEditorUndoRedoManager *manager;
        EHTMLEditorHistoryEvent *ev = NULL;
        gboolean after_selection_end = FALSE;
-       const gchar *class = "", *list_class = "";
+       const gchar *class = "";
        WebKitDOMElement *selection_start_marker, *selection_end_marker;
        WebKitDOMNode *block;
 
@@ -5542,7 +5551,6 @@ dom_selection_set_alignment (WebKitDOMDocument *document,
        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:
@@ -5550,7 +5558,6 @@ dom_selection_set_alignment (WebKitDOMDocument *document,
 
                case E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT:
                        class = "-x-evo-align-right";
-                       list_class = "-x-evo-list-item-align-right";
                        break;
        }
 
@@ -5590,51 +5597,31 @@ dom_selection_set_alignment (WebKitDOMDocument *document,
                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;
+
+                       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);
 
-                       while (item && WEBKIT_DOM_IS_HTML_LI_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");
-
-                               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;
@@ -5843,14 +5830,12 @@ dom_selection_has_text (WebKitDOMDocument *document)
 EHTMLEditorSelectionAlignment
 dom_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;
 }
 
 WebKitDOMElement *
diff --git a/web-extensions/composer/e-html-editor-selection-dom-functions.h 
b/web-extensions/composer/e-html-editor-selection-dom-functions.h
index 855b3b7..670d46a 100644
--- a/web-extensions/composer/e-html-editor-selection-dom-functions.h
+++ b/web-extensions/composer/e-html-editor-selection-dom-functions.h
@@ -43,8 +43,9 @@
 
 #define QUOTE_SYMBOL ">"
 
-#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 TAB_LENGTH 8
 #define MINIMAL_PARAGRAPH_WIDTH 5
 
diff --git a/web-extensions/composer/e-html-editor-view-dom-functions.c 
b/web-extensions/composer/e-html-editor-view-dom-functions.c
index 600fffc..b1595cc 100644
--- a/web-extensions/composer/e-html-editor-view-dom-functions.c
+++ b/web-extensions/composer/e-html-editor-view-dom-functions.c
@@ -2836,6 +2836,49 @@ body_keyup_event_cb (WebKitDOMElement *element,
                dom_selection_restore (document);
        } else if (key_code == HTML_KEY_CODE_CONTROL)
                dom_set_links_active (document, 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_web_extension_get_html_mode (extension))
+                       return;
+
+               /* FIXME WK2 - the below is called twice, the second time two lines below */
+               /*dom_selection_save (document);*/
+
+               document = webkit_dom_node_get_owner_document (WEBKIT_DOM_NODE (element));
+
+               dom_selection_save (document);
+
+               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_HTML_LI_ELEMENT (parent) ||
+                   !WEBKIT_DOM_IS_HTML_U_LIST_ELEMENT (webkit_dom_node_get_parent_node (parent))) {
+                       dom_selection_restore (document);
+                       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_HTML_BR_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);
+
+               dom_selection_restore (document);
+       }
 }
 
 static void
@@ -5530,6 +5573,8 @@ process_list_to_plain_text (EHTMLEditorWebExtension *extension,
                                                /* put spaces before line characters -> wordwraplength - 
indentation */
                                                for (ii = 0; ii < level; ii++)
                                                        g_string_append (line, indent_per_level);
+                                               if (WEBKIT_DOM_IS_HTML_O_LIST_ELEMENT (element))
+                                                       g_string_append (line, indent_per_level);
                                                g_string_append (item_value, line->str);
                                                g_string_erase (line, 0, -1);
                                        }
@@ -5545,6 +5590,8 @@ process_list_to_plain_text (EHTMLEditorWebExtension *extension,
 
                                        fill_length = word_wrap_length - g_utf8_strlen (line->str, -1);
                                        fill_length -= ii * SPACES_PER_LIST_LEVEL;
+                                       if (WEBKIT_DOM_IS_HTML_O_LIST_ELEMENT (element))
+                                               fill_length += SPACES_PER_LIST_LEVEL;
                                        fill_length /= 2;
 
                                        if (fill_length < 0)
@@ -5598,7 +5645,7 @@ process_list_to_plain_text (EHTMLEditorWebExtension *extension,
                                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);
@@ -5612,8 +5659,7 @@ process_list_to_plain_text (EHTMLEditorWebExtension *extension,
                                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);
@@ -5624,6 +5670,9 @@ process_list_to_plain_text (EHTMLEditorWebExtension *extension,
                                for (ii = 0; ii < level - 1; ii++) {
                                        g_string_append (output, indent_per_level);
                                }
+                               if (WEBKIT_DOM_IS_HTML_U_LIST_ELEMENT (element))
+                                       if (dom_node_find_parent_element (item, "OL"))
+                                               g_string_append (output, indent_per_level);
                                g_string_append (output, item_str);
                        }
 
@@ -5662,6 +5711,8 @@ process_list_to_plain_text (EHTMLEditorWebExtension *extension,
 
                                        fill_length = word_wrap_length - g_utf8_strlen (item_str, -1);
                                        fill_length -= ii * SPACES_PER_LIST_LEVEL;
+                                       if (WEBKIT_DOM_IS_HTML_O_LIST_ELEMENT (element))
+                                               fill_length += SPACES_PER_LIST_LEVEL;
                                        fill_length /= 2;
 
                                        if (fill_length < 0)
@@ -5724,6 +5775,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");
 }
 
@@ -8192,6 +8244,31 @@ toggle_tables (WebKitDOMDocument *document,
        g_object_unref (list);
 }
 
+static void
+toggle_unordered_lists (WebKitDOMDocument *document,
+                       gboolean html_mode)
+{
+       WebKitDOMNodeList *list;
+       gint ii, length;
+
+       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 (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);
+}
+
 void
 dom_process_content_after_mode_change (WebKitDOMDocument *document,
                                        EHTMLEditorWebExtension *extension)
@@ -8214,6 +8291,7 @@ dom_process_content_after_mode_change (WebKitDOMDocument *document,
                toggle_paragraphs_style (document, extension);
                toggle_smileys (document, extension);
                toggle_tables (document, html_mode);
+               toggle_unordered_lists (document, html_mode);
 
                body = webkit_dom_document_get_body (document);
 
@@ -8232,6 +8310,7 @@ dom_process_content_after_mode_change (WebKitDOMDocument *document,
                toggle_paragraphs_style (document, extension);
                toggle_smileys (document, extension);
                toggle_tables (document, html_mode);
+               toggle_unordered_lists (document, html_mode);
                remove_images (document);
                remove_background_images_in_document (document);
 


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