[evolution/wip/webkit-composer: 571/966] Fix block formatting



commit 456e8b46e59b7e825cf956ed841371217b64929d
Author: Dan Vrátil <dvratil redhat com>
Date:   Thu Aug 23 14:46:18 2012 +0200

    Fix block formatting

 e-util/e-editor-actions.c   |   19 ++----
 e-util/e-editor-selection.c |  146 ++++++++++++++++++++++++++++++++++---------
 e-util/e-editor-selection.h |    2 +-
 3 files changed, 121 insertions(+), 46 deletions(-)
---
diff --git a/e-util/e-editor-actions.c b/e-util/e-editor-actions.c
index e16f729..c8b239d 100644
--- a/e-util/e-editor-actions.c
+++ b/e-util/e-editor-actions.c
@@ -842,19 +842,6 @@ action_properties_text_cb (GtkAction *action,
 }
 
 static void
-action_style_cb (GtkRadioAction *action,
-                 GtkRadioAction *current,
-                 EEditor *editor)
-{
-       EEditorSelection *selection;
-
-       selection = e_editor_widget_get_selection (
-                       e_editor_get_editor_widget (editor));
-       e_editor_selection_set_block_format (
-               selection, gtk_radio_action_get_current_value (current));
-}
-
-static void
 action_redo_cb (GtkAction *action,
                 EEditor *editor)
 {
@@ -1846,7 +1833,7 @@ editor_actions_init (EEditor *editor)
                action_group, core_style_entries,
                G_N_ELEMENTS (core_style_entries),
                E_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH,
-               G_CALLBACK (action_style_cb), editor);
+               NULL, NULL);
        gtk_ui_manager_insert_action_group (manager, action_group, 0);
 
        /* Synchronize wiget mode with the button */
@@ -1974,6 +1961,10 @@ editor_actions_init (EEditor *editor)
                ACTION (FONT_SIZE_GROUP), "current-value",
                G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
        g_object_bind_property (
+               editor->priv->selection, "block-format",
+               ACTION (STYLE_NORMAL), "current-value",
+               G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
+       g_object_bind_property (
                editor->priv->selection, "indented",
                ACTION (UNINDENT), "sensitive",
                G_BINDING_SYNC_CREATE);
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index 594ea8d..8dee08c 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -339,6 +339,11 @@ e_editor_selection_set_property (GObject *object,
                                selection, g_value_get_boxed (value));
                        return;
 
+               case PROP_BLOCK_FORMAT:
+                       e_editor_selection_set_block_format (
+                               selection, g_value_get_int (value));
+                       return;
+
                case PROP_FONT_NAME:
                        e_editor_selection_set_font_name (
                                selection, g_value_get_string (value));
@@ -483,12 +488,12 @@ e_editor_selection_class_init (EEditorSelectionClass *klass)
        g_object_class_install_property (
                object_class,
                PROP_BLOCK_FORMAT,
-               g_param_spec_uint (
+               g_param_spec_int (
                        "block-format",
                        NULL,
                        NULL,
                        0,
-                       G_MAXUINT,
+                       G_MAXINT,
                        0,
                        G_PARAM_READWRITE));
 
@@ -810,43 +815,63 @@ EEditorSelectionBlockFormat
 e_editor_selection_get_block_format (EEditorSelection *selection)
 {
        WebKitDOMNode *node;
-       gchar *tmp, *node_name;
+       WebKitDOMRange *range;
+       WebKitDOMElement *element;
        EEditorSelectionBlockFormat result;
 
        g_return_val_if_fail (E_IS_EDITOR_SELECTION (selection),
-                             E_EDITOR_SELECTION_BLOCK_FORMAT_NONE);
+                             E_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH);
 
-       node = webkit_dom_range_get_common_ancestor_container (
-                       selection->priv->range, NULL);
+       range = editor_selection_get_current_range (selection);
+       if (!range) {
+               return E_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;
+       }
 
-       tmp = webkit_dom_node_get_node_name (node);
-       node_name = g_ascii_strdown (tmp, -1);
-       g_free (tmp);
+       node = webkit_dom_range_get_start_container (range, NULL);
 
-       if (g_strcmp0 (node_name, "blockquote") == 0)
+       if (e_editor_dom_node_find_parent_element (node, "UL")) {
+               result = E_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;
+       } else if ((element = e_editor_dom_node_find_parent_element (node, "OL")) != NULL) {
+               if (webkit_dom_element_has_attribute (element, "type")) {
+                       gchar *type;
+
+                       type = webkit_dom_element_get_attribute(element, "type");
+                       if (type && ((*type == 'a') || (*type == 'A'))) {
+                               result = E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA;
+                       } else if (type && ((*type == 'i') || (*type == 'I'))) {
+                               result = E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN;
+                       } else {
+                               result = E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST;
+                       }
+
+                       g_free (type);
+               } else {
+                       result = E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST;
+               }
+       } else if (e_editor_dom_node_find_parent_element (node, "BLOCKQUOTE")) {
                result = E_EDITOR_SELECTION_BLOCK_FORMAT_BLOCKQUOTE;
-       else if (g_strcmp0 (node_name, "h1") == 0)
+       } else if (e_editor_dom_node_find_parent_element (node, "PRE")) {
+               result = E_EDITOR_SELECTION_BLOCK_FORMAT_PRE;
+       } else if (e_editor_dom_node_find_parent_element (node, "ADDRESS")) {
+               result = E_EDITOR_SELECTION_BLOCK_FORMAT_ADDRESS;
+       } else if (e_editor_dom_node_find_parent_element (node, "H1")) {
                result = E_EDITOR_SELECTION_BLOCK_FORMAT_H1;
-       else if (g_strcmp0 (node_name, "h2") == 0)
+       } else if (e_editor_dom_node_find_parent_element (node, "H2")) {
                result = E_EDITOR_SELECTION_BLOCK_FORMAT_H2;
-       else if (g_strcmp0 (node_name, "h3") == 0)
+       } else if (e_editor_dom_node_find_parent_element (node, "H3")) {
                result = E_EDITOR_SELECTION_BLOCK_FORMAT_H3;
-       else if (g_strcmp0 (node_name, "h4") == 0)
+       } else if (e_editor_dom_node_find_parent_element (node, "H4")) {
                result = E_EDITOR_SELECTION_BLOCK_FORMAT_H4;
-       else if (g_strcmp0 (node_name, "h5") == 0)
+       } else if (e_editor_dom_node_find_parent_element (node, "H5")) {
                result = E_EDITOR_SELECTION_BLOCK_FORMAT_H5;
-       else if (g_strcmp0 (node_name, "h6") == 0)
+       } else if (e_editor_dom_node_find_parent_element (node, "H6")) {
                result = E_EDITOR_SELECTION_BLOCK_FORMAT_H6;
-       else if (g_strcmp0 (node_name, "p") == 0)
+       } else if (e_editor_dom_node_find_parent_element (node, "P")) {
                result = E_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;
-       else if (g_strcmp0 (node_name, "pre") == 0)
-               result = E_EDITOR_SELECTION_BLOCK_FORMAT_PRE;
-       else if (g_strcmp0 (node_name, "address") == 0)
-               result = E_EDITOR_SELECTION_BLOCK_FORMAT_ADDRESS;
-       else
-               result = E_EDITOR_SELECTION_BLOCK_FORMAT_NONE;
+       } else {
+               result = E_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;
+       }
 
-       g_free (node_name);
        return result;
 }
 
@@ -854,55 +879,114 @@ void
 e_editor_selection_set_block_format (EEditorSelection *selection,
                                     EEditorSelectionBlockFormat format)
 {
+       EEditorSelectionBlockFormat current_format;
        WebKitDOMDocument *document;
+       const gchar *command;
        const gchar *value;
 
        g_return_if_fail (E_IS_EDITOR_SELECTION (selection));
 
+       current_format = e_editor_selection_get_block_format (selection);
+       if (current_format == format) {
+               return;
+       }
+
+       document = webkit_web_view_get_dom_document (selection->priv->webview);
+
        switch (format) {
                case E_EDITOR_SELECTION_BLOCK_FORMAT_BLOCKQUOTE:
+                       command = "formatBlock";
                        value = "BLOCKQUOTE";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_H1:
+                       command = "formatBlock";
                        value = "H1";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_H2:
+                       command = "formatBlock";
                        value = "H2";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_H3:
+                       command = "formatBlock";
                        value = "H3";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_H4:
+                       command = "formatBlock";
                        value = "H4";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_H5:
+                       command = "formatBlock";
                        value = "H5";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_H6:
+                       command = "formatBlock";
                        value = "H6";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH:
+                       command = "formatBlock";
                        value = "P";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_PRE:
+                       command = "formatBlock";
                        value = "PRE";
                        break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_ADDRESS:
+                       command = "formatBlock";
                        value = "ADDRESS";
                        break;
+               case E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST:
+               case E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA:
+               case E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN:
+                       command = "insertOrderedList";
+                       value = "";
+                       break;
+               case E_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST:
+                       command = "insertUnorderedList";
+                       value = "";
+                       break;
                case E_EDITOR_SELECTION_BLOCK_FORMAT_NONE:
                default:
-                       value = NULL;
+                       command = "removeFormat";
+                       value = "";
                        break;
        }
 
-       document = webkit_web_view_get_dom_document (selection->priv->webview);
-       if (value) {
+
+       /* First remove (un)ordered list before changing formatting */
+       if (current_format == E_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST) {
                webkit_dom_document_exec_command (
-                       document, "formatBlock", FALSE, value);
-       } else {
+                       document, "insertUnorderedList", FALSE, "");
+               /*                  ^-- not a typo, "insert" toggles the formatting
+                *                      if already present */
+       } else if (current_format >= E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST) {
                webkit_dom_document_exec_command (
-                       document, "removeFormat", FALSE, "");
+                       document, "insertOrderedList", FALSE ,"");
+       }
+
+       webkit_dom_document_exec_command (
+               document, command, FALSE, value);
+
+       /* Fine tuning - set the specific marker type for ordered lists */
+       if ((format == E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA) ||
+           (format == E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN)) {
+
+               WebKitDOMRange *range = editor_selection_get_current_range (selection);
+               WebKitDOMNode *node;
+               WebKitDOMElement *list;
+
+               node = webkit_dom_range_get_start_container (range, NULL);
+
+               list = e_editor_dom_node_find_child_element (node, "OL");
+               if (!list) {
+                       list = e_editor_dom_node_find_parent_element (node, "OL");
+               }
+
+               if (list) {
+                       webkit_dom_element_set_attribute (
+                               list, "type",
+                               (format == E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA) ?
+                                       "A" : "I", NULL);
+               }
        }
 
        g_object_notify (G_OBJECT (selection), "block-format");
@@ -1534,4 +1618,4 @@ e_editor_selection_wrap_lines (EEditorSelection *selection)
        e_editor_selection_insert_html (selection, html);
 
        g_free (html);
-}
\ No newline at end of file
+}
diff --git a/e-util/e-editor-selection.h b/e-util/e-editor-selection.h
index 043656e..f8e62ac 100644
--- a/e-util/e-editor-selection.h
+++ b/e-util/e-editor-selection.h
@@ -63,7 +63,7 @@ typedef enum {
        E_EDITOR_SELECTION_BLOCK_FORMAT_BLOCKQUOTE,
        E_EDITOR_SELECTION_BLOCK_FORMAT_PRE,
        E_EDITOR_SELECTION_BLOCK_FORMAT_ADDRESS,
-       E_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST, /* FIXME WEBKIT Not implemented VVVVV */
+       E_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST,
        E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST,
        E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN,
        E_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA,


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