[evolution/wip/webkit2] EHTMLEditorView - Add a way how to insert new block above table where there is none



commit 7f28af3d98d895867886996a4fc48d201b1ffe28
Author: Tomas Popela <tpopela redhat com>
Date:   Mon Mar 30 11:04:45 2015 +0200

    EHTMLEditorView - Add a way how to insert new block above table where there is none
    
    Return pressed in the the begining of the first cell will insert new block
    before the table (and move the caret there) if none is already there,
    otherwise it will act as normal return. This behaviour is taken from
    LibreOffice Writer.

 web-extensions/e-html-editor-view-dom-functions.c |   73 ++++++++++++++++++---
 1 files changed, 64 insertions(+), 9 deletions(-)
---
diff --git a/web-extensions/e-html-editor-view-dom-functions.c 
b/web-extensions/e-html-editor-view-dom-functions.c
index 30b7dec..db7e8a8 100644
--- a/web-extensions/e-html-editor-view-dom-functions.c
+++ b/web-extensions/e-html-editor-view-dom-functions.c
@@ -6359,7 +6359,9 @@ fix_structure_after_delete_before_quoted_content (WebKitDOMDocument *document)
 }
 
 static gboolean
-selection_is_in_table (WebKitDOMDocument *document)
+selection_is_in_table (WebKitDOMDocument *document,
+                       gboolean *first_cell,
+                       WebKitDOMNode **table_node)
 {
        WebKitDOMDOMWindow *window;
        WebKitDOMDOMSelection *selection;
@@ -6369,6 +6371,12 @@ selection_is_in_table (WebKitDOMDocument *document)
        window = webkit_dom_document_get_default_view (document);
        selection = webkit_dom_dom_window_get_selection (window);
 
+       if (first_cell != NULL)
+               *first_cell = FALSE;
+
+       if (table_node != NULL)
+               *table_node = NULL;
+
        if (webkit_dom_dom_selection_get_range_count (selection) < 1)
                return FALSE;
 
@@ -6377,16 +6385,34 @@ selection_is_in_table (WebKitDOMDocument *document)
 
        parent = node;
        while (parent && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
-               if (WEBKIT_DOM_IS_HTML_TABLE_ROW_ELEMENT (parent))
-                       return TRUE;
-               if (WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (parent))
-                       return TRUE;
-               if (WEBKIT_DOM_IS_HTML_TABLE_ELEMENT (parent))
-                       return TRUE;
+               if (WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (parent)) {
+                       if (first_cell != NULL) {
+                               if (!webkit_dom_node_get_previous_sibling (parent)) {
+                                       gboolean on_start = TRUE;
+                                       WebKitDOMNode *tmp;
+
+                                       tmp = webkit_dom_node_get_previous_sibling (node);
+                                       if (!tmp && WEBKIT_DOM_IS_TEXT (node))
+                                               on_start = webkit_dom_range_get_start_offset (range, NULL) == 
0;
+                                       else if (tmp)
+                                               on_start = FALSE;
+
+                                       if (on_start) {
+                                               node = webkit_dom_node_get_parent_node (parent);
+                                               if (node && WEBKIT_DOM_HTML_TABLE_ROW_ELEMENT (node))
+                                                       if (!webkit_dom_node_get_previous_sibling (node))
+                                                               *first_cell = TRUE;
+                                       }
+                               }
+                       }
+               }
+               if (WEBKIT_DOM_IS_HTML_TABLE_ELEMENT (parent)) {
+                       *table_node = parent;
+               }
                parent = webkit_dom_node_get_parent_node (parent);
        }
 
-       return FALSE;
+       return *table_node != NULL;
 }
 
 static void
@@ -6477,7 +6503,7 @@ dom_process_on_key_press (WebKitDOMDocument *document,
                           guint key_val)
 {
        if (key_val == GDK_KEY_Tab || key_val == GDK_KEY_ISO_Left_Tab) {
-               if (selection_is_in_table (document)) {
+               if (selection_is_in_table (document, NULL, NULL)) {
                        jump_to_next_table_cell (document, key_val == GDK_KEY_ISO_Left_Tab);
                        return TRUE;
                } else if (key_val == GDK_KEY_Tab)
@@ -6489,6 +6515,35 @@ dom_process_on_key_press (WebKitDOMDocument *document,
 
        if (is_return_key (key_val)) {
                EHTMLEditorSelectionBlockFormat format;
+               gboolean first_cell = FALSE;
+               WebKitDOMNode *table = NULL;
+
+               /* Return pressed in the the begining of the first cell will insert
+                * new block before the table (and move the caret there) if none
+                * is already there, otherwise it will act as normal return. */
+               if (selection_is_in_table (document, &first_cell, &table) && first_cell) {
+                       WebKitDOMNode *node;
+
+                       node = webkit_dom_node_get_previous_sibling (table);
+                       if (!node) {
+                               node = webkit_dom_node_get_next_sibling (table);
+                               node = webkit_dom_node_clone_node (node, FALSE);
+                               webkit_dom_node_append_child (
+                                       node,
+                                       WEBKIT_DOM_NODE (webkit_dom_document_create_element (
+                                               document, "br", NULL)),
+                                       NULL);
+                               add_selection_markers_into_element_start (
+                                       document, WEBKIT_DOM_ELEMENT (node), NULL, NULL);
+                               webkit_dom_node_insert_before (
+                                       webkit_dom_node_get_parent_node (table),
+                                       node,
+                                       table,
+                                       NULL);
+                               dom_selection_restore (document);
+                               return TRUE;
+                       }
+               }
 
                /* When user presses ENTER in a citation block, WebKit does
                 * not break the citation automatically, so we need to use


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