[evolution/wip/webkit-composer] Show star instead of bullet in Bulleted list in plain text mode



commit 4df4e84d5f266e62ff96167fcd51d1bdaf9a4cc9
Author: Tomas Popela <tpopela redhat com>
Date:   Wed Mar 5 13:46:59 2014 +0100

    Show star instead of bullet in Bulleted list in plain text mode
    
    Switch between bullets and stars when changing composer mode.

 e-util/e-editor-selection.c |   41 ++++++++--
 e-util/e-editor-widget.c    |  173 +++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 200 insertions(+), 14 deletions(-)
---
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index 0450d8e..1d8b0f0 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -1360,7 +1360,8 @@ static void
 change_list_style (EEditorSelection *selection,
                    WebKitDOMDocument *document,
                    EEditorSelectionBlockFormat from,
-                   EEditorSelectionBlockFormat to)
+                   EEditorSelectionBlockFormat to,
+                   gboolean html_mode)
 {
        WebKitDOMNode *list, *node;
        WebKitDOMRange *range;
@@ -1398,6 +1399,12 @@ change_list_style (EEditorSelection *selection,
                        to == E_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST ? "UL" : "OL",
                        NULL);
 
+               if (to == E_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST && !html_mode) {
+                       element_add_class (new_list, "-x-evo-ul-plain");
+                       webkit_dom_element_set_attribute (
+                               new_list, "style", "margin-left: -3ch;", NULL);
+               }
+
                move_items_from_list_to_list (WEBKIT_DOM_ELEMENT (list), new_list);
 
                list = webkit_dom_node_replace_child (
@@ -1430,18 +1437,23 @@ change_list_style (EEditorSelection *selection,
 static void
 insert_new_list (EEditorSelection *selection,
                  WebKitDOMDocument *document,
-                 gboolean inserting_ordered_list)
+                 EEditorSelectionBlockFormat to,
+                 gboolean html_mode)
 {
+       gboolean inserting_ordered_list = FALSE;
+       gchar *content;
        WebKitDOMRange *range;
        WebKitDOMElement *element;
        WebKitDOMNode *node;
-       gchar *content;
 
        range = editor_selection_get_current_range (selection);
        node = webkit_dom_range_get_end_container (range, NULL);
        if (!WEBKIT_DOM_IS_ELEMENT (node))
                node = WEBKIT_DOM_NODE (webkit_dom_node_get_parent_element (node));
 
+       if (to != E_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST)
+               inserting_ordered_list = TRUE;
+
        /* Sometimes there is UNICODE_ZERO_WIDTH_SPACE so we have to remove it */
        webkit_dom_node_set_text_content (node, "", NULL);
 
@@ -1449,6 +1461,20 @@ insert_new_list (EEditorSelection *selection,
        element = webkit_dom_document_create_element (
                document, inserting_ordered_list ? "OL" : "UL", NULL);
 
+       if (to == E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA) {
+               webkit_dom_element_set_attribute (
+                       WEBKIT_DOM_ELEMENT (element), "type", "A", NULL);
+       } else if (to == E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN) {
+               webkit_dom_element_set_attribute (
+                       WEBKIT_DOM_ELEMENT (element), "type", "I", NULL);
+       }
+
+       if (!html_mode && !inserting_ordered_list) {
+               element_add_class (element, "-x-evo-ul-plain");
+               webkit_dom_element_set_attribute (
+                       element, "style", "margin-left: -3ch;", NULL);
+       }
+
        /* We have to use again the hidden space to move caret into newly
         * inserted list */
        content = g_strconcat ("<li>", UNICODE_ZERO_WIDTH_SPACE, "</li>", NULL);
@@ -1560,7 +1586,7 @@ e_editor_selection_set_block_format (EEditorSelection *selection,
        EEditorSelectionBlockFormat current_format;
        EEditorWidgetCommand command;
        const gchar *value;
-       gboolean inserting_ordered_list = FALSE, has_selection = FALSE;
+       gboolean has_selection = FALSE;
        gboolean from_list = FALSE, to_list = FALSE, html_mode;
        WebKitDOMDocument *document;
        WebKitDOMNode *block = NULL;
@@ -1618,7 +1644,6 @@ e_editor_selection_set_block_format (EEditorSelection *selection,
                case E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA:
                case E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN:
                        command = E_EDITOR_WIDGET_COMMAND_INSERT_ORDERED_LIST;
-                       inserting_ordered_list = TRUE;
                        to_list = TRUE;
                        value = NULL;
                        break;
@@ -1683,7 +1708,8 @@ e_editor_selection_set_block_format (EEditorSelection *selection,
        }
 
        if (from_list && to_list) {
-               change_list_style (selection, document, current_format, format);
+               change_list_style (
+                       selection, document, current_format, format, html_mode);
        } else if (!to_list && !has_selection) {
                gboolean restore_caret = TRUE;
                /* If there is no selection in composer we will change the format of
@@ -1745,7 +1771,7 @@ e_editor_selection_set_block_format (EEditorSelection *selection,
                }
        } else {
                if (!has_selection)
-                       insert_new_list (selection, document, inserting_ordered_list);
+                       insert_new_list (selection, document, format, html_mode);
                else
                        e_editor_widget_exec_command (editor_widget, command, value);
        }
@@ -3274,7 +3300,6 @@ insert_base64_image (EEditorSelection *selection,
                NULL);
 
        e_editor_selection_restore_caret_position (selection);
-
 }
 
 static void
diff --git a/e-util/e-editor-widget.c b/e-util/e-editor-widget.c
index 57d1354..6bf9ca5 100644
--- a/e-util/e-editor-widget.c
+++ b/e-util/e-editor-widget.c
@@ -346,6 +346,34 @@ body_input_event_cb (WebKitDOMElement *element,
                                        -1, 0, "");
                        }
                }
+       } else if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (node)) {
+               WebKitDOMElement *parent;
+
+               if (e_editor_widget_get_html_mode (editor_widget))
+                       return;
+
+               parent = webkit_dom_node_get_parent_element (node);
+               /* Move caret to correct place in unordered list in plain text mode */
+               if (element_has_class (parent, "-x-evo-ul-plain")) {
+                       WebKitDOMDocument *document;
+
+                       document = webkit_web_view_get_dom_document (
+                               WEBKIT_WEB_VIEW (editor_widget));
+
+                       webkit_dom_html_element_set_inner_html (
+                               WEBKIT_DOM_HTML_ELEMENT (node),
+                               UNICODE_ZERO_WIDTH_SPACE,
+                               NULL);
+
+                       webkit_dom_node_append_child (
+                               node,
+                               e_editor_selection_get_caret_position_node (
+                                       document),
+                               NULL);
+
+                       e_editor_selection_restore_caret_position (
+                               e_editor_widget_get_selection (editor_widget));
+               }
        }
 }
 
@@ -1487,6 +1515,86 @@ editor_widget_button_release_event (GtkWidget *widget,
 }
 
 static gboolean
+end_list_on_return_press_in_plain_text_mode (EEditorWidget *editor_widget)
+{
+       EEditorSelection *selection;
+       WebKitDOMDocument *document;
+       WebKitDOMElement *caret, *li, *list, *paragraph;
+       WebKitDOMNode *prev_sibling, *parent;
+
+       if (e_editor_widget_get_html_mode (editor_widget))
+               return FALSE;
+
+       selection = e_editor_widget_get_selection (editor_widget);
+       e_editor_selection_save_caret_position (selection);
+
+       document = webkit_web_view_get_dom_document (
+               WEBKIT_WEB_VIEW (editor_widget));
+
+       caret = webkit_dom_document_get_element_by_id (document, "-x-evo-caret-position");
+       if (!caret)
+               goto out;
+
+       /* Check if item already containes some text */
+       prev_sibling = webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (caret));
+       if (prev_sibling) {
+               gchar *data;
+
+               if (!WEBKIT_DOM_IS_TEXT (prev_sibling))
+                       goto out;
+
+               data = webkit_dom_character_data_get_data (
+                       WEBKIT_DOM_CHARACTER_DATA (prev_sibling));
+
+               /* If text is just zero width space treat is as no text */
+               if (g_strcmp0 (data, UNICODE_ZERO_WIDTH_SPACE) != 0) {
+                       g_free (data);
+                       goto out;
+               }
+
+               g_free (data);
+       }
+
+       li= webkit_dom_node_get_parent_element (WEBKIT_DOM_NODE (caret));
+       if (!WEBKIT_DOM_IS_HTMLLI_ELEMENT (li))
+               goto out;
+       list = webkit_dom_node_get_parent_element (WEBKIT_DOM_NODE (li));
+       if (!(WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (list) ||
+           WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (list)))
+               goto out;
+
+       /* Remove last empty item */
+       webkit_dom_node_remove_child (
+               WEBKIT_DOM_NODE (list),
+               WEBKIT_DOM_NODE (li),
+               NULL);
+
+       paragraph = e_editor_selection_get_paragraph_element (
+               selection, document, -1, 0);
+
+       webkit_dom_html_element_set_inner_html (
+               WEBKIT_DOM_HTML_ELEMENT (paragraph), UNICODE_ZERO_WIDTH_SPACE, NULL);
+
+       webkit_dom_node_append_child (
+               WEBKIT_DOM_NODE (paragraph),
+               e_editor_selection_get_caret_position_node (document),
+               NULL);
+
+       parent = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (list));
+       webkit_dom_node_insert_before (
+               webkit_dom_node_get_parent_node (parent),
+               WEBKIT_DOM_NODE (paragraph),
+               webkit_dom_node_get_next_sibling (parent),
+               NULL);
+
+       e_editor_selection_restore_caret_position (selection);
+       return TRUE;
+ out:
+       e_editor_selection_clear_caret_position_marker (selection);
+       return FALSE;
+}
+
+static gboolean
 editor_widget_key_press_event (GtkWidget *widget,
                                GdkEventKey *event)
 {
@@ -1508,13 +1616,12 @@ editor_widget_key_press_event (GtkWidget *widget,
 
        if ((event->keyval == GDK_KEY_Return) ||
            (event->keyval == GDK_KEY_KP_Enter)) {
+               EEditorSelection *selection;
 
+               selection = e_editor_widget_get_selection (editor);
                /* When user presses ENTER in a citation block, WebKit does
                 * not break the citation automatically, so we need to use
                 * the special command to do it. */
-               EEditorSelection *selection;
-
-               selection = e_editor_widget_get_selection (editor);
                if (e_editor_selection_is_citation (selection)) {
                        gboolean ret_val = e_editor_widget_exec_command (
                                editor,
@@ -1522,12 +1629,10 @@ editor_widget_key_press_event (GtkWidget *widget,
                                NULL);
                        /* If successful we have to put inserted BR into paragraph */
                        if (ret_val) {
-                               EEditorSelection *selection;
                                WebKitDOMDocument *document;
                                WebKitDOMNode *node;
                                WebKitDOMRange *range;
 
-                               selection = e_editor_widget_get_selection (editor);
                                document = webkit_web_view_get_dom_document (
                                        WEBKIT_WEB_VIEW (editor));
 
@@ -1548,6 +1653,9 @@ editor_widget_key_press_event (GtkWidget *widget,
                                }
                        }
                        return ret_val;
+               } else {
+                       if (end_list_on_return_press_in_plain_text_mode (editor))
+                               return TRUE;
                }
        }
 
@@ -3890,6 +3998,35 @@ toggle_smileys (EEditorWidget *widget)
 }
 
 static void
+toggle_ul_style (EEditorWidget *widget)
+{
+       gboolean html_mode;
+       gint ii, length;
+       WebKitDOMDocument *document;
+       WebKitDOMNodeList *ul_lists;
+
+       html_mode = e_editor_widget_get_html_mode (widget);
+
+       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (widget));
+       ul_lists = webkit_dom_document_query_selector_all (document, "ul", NULL);
+
+       length = webkit_dom_node_list_get_length (ul_lists);
+       for (ii = 0; ii < length; ii++) {
+               WebKitDOMElement *ul = WEBKIT_DOM_ELEMENT (
+                       webkit_dom_node_list_item (ul_lists, ii));
+
+               if (html_mode) {
+                       element_remove_class (ul, "-x-evo-ul-plain");
+                       webkit_dom_element_remove_attribute (ul, "style");
+               } else {
+                       element_add_class (ul, "-x-evo-ul-plain");
+                       webkit_dom_element_set_attribute (
+                               ul, "style", "margin-left: -3ch;", NULL);
+               }
+       }
+}
+
+static void
 toggle_paragraphs_style (EEditorWidget *widget)
 {
        EEditorSelection *selection;
@@ -4305,6 +4442,7 @@ e_editor_widget_set_html_mode (EEditorWidget *widget,
 
                toggle_paragraphs_style (widget);
                toggle_smileys (widget);
+               toggle_ul_style (widget);
                remove_wrapping (widget);
 
        } else {
@@ -4321,6 +4459,7 @@ e_editor_widget_set_html_mode (EEditorWidget *widget,
 
                toggle_paragraphs_style (widget);
                toggle_smileys (widget);
+               toggle_ul_style (widget);
                remove_images (widget);
 
                plain = process_content_for_mode_change (widget);
@@ -4816,7 +4955,29 @@ e_editor_widget_update_fonts (EEditorWidget *widget)
                "  margin-bottom: 0.2em;\n"
                "}\n");
 
-       g_string_append(
+       g_string_append (
+               stylesheet,
+               "ul,ol "
+               "{\n"
+               "  -webkit-padding-start: 7ch; \n"
+               "}\n");
+
+       g_string_append (
+               stylesheet,
+               "ul.-x-evo-ul-plain "
+               "{\n"
+               "  list-style: none; \n"
+               "}\n");
+
+       g_string_append (
+               stylesheet,
+               "ul.-x-evo-ul-plain li:before "
+               "{\n"
+               "  content: '*'; \n"
+               "  padding-right: 2ch; \n"
+               "}\n");
+
+       g_string_append (
                stylesheet,
                "ol,ul "
                "{\n"


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