[evolution/wip/mcrha/webkit-jsc-api] A little bit code shuffling and a fix of EvoUndoRedo.before_input_cb()



commit 8a3cc0c4da1d476b19349715f9f00e6db0288402
Author: Milan Crha <mcrha redhat com>
Date:   Thu Oct 31 15:25:32 2019 +0100

    A little bit code shuffling and a fix of EvoUndoRedo.before_input_cb()

 data/webkit/e-editor.js    | 14 +++++++++----
 data/webkit/e-selection.js |  2 +-
 data/webkit/e-undo-redo.js | 49 ++++++++++------------------------------------
 3 files changed, 21 insertions(+), 44 deletions(-)
---
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index 8378341a7a..ab3d5ae969 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -42,6 +42,11 @@ var EvoEditor = {
        E_CONTENT_EDITOR_BLOCK_FORMAT_ORDERED_LIST_ROMAN : 12,
        E_CONTENT_EDITOR_BLOCK_FORMAT_ORDERED_LIST_ALPHA : 13,
 
+       /* Flags for ClaimAffectedContent() */
+       CLAIM_CONTENT_FLAG_NONE : 0,
+       CLAIM_CONTENT_FLAG_USE_PARENT_BLOCK_NODE : 1 << 0,
+       CLAIM_CONTENT_FLAG_SAVE_HTML : 1 << 1,
+
        htmlFormat : false,
        storedSelection : null
 };
@@ -179,10 +184,11 @@ EvoEditor.GetDirectChild = function(parent, child)
        return child;
 }
 
-EvoEditor.ClaimAffectedContent = function(startNode, endNode, useParentBlockNode, withHtml)
+EvoEditor.ClaimAffectedContent = function(startNode, endNode, flags)
 {
        var commonParent, startChild, endChild;
        var firstChildIndex = -1, html = "", ii;
+       var withHtml = (flags & EvoEditor.CLAIM_CONTENT_FLAG_SAVE_HTML) != 0;
 
        if (!startNode) {
                startNode = document.getSelection().baseNode;
@@ -197,7 +203,7 @@ EvoEditor.ClaimAffectedContent = function(startNode, endNode, useParentBlockNode
                endNode = startNode;
        }
 
-       if (useParentBlockNode) {
+       if ((flags & EvoEditor.CLAIM_CONTENT_FLAG_USE_PARENT_BLOCK_NODE) != 0) {
                while (startNode && !(startNode === document.body)) {
                        if (EvoEditor.IsBlockNode(startNode)) {
                                break;
@@ -339,7 +345,7 @@ EvoEditor.SetAlignment = function(alignment)
                }
        };
 
-       var affected = EvoEditor.ClaimAffectedContent(null, null, true, false);
+       var affected = EvoEditor.ClaimAffectedContent(null, null, 
EvoEditor.CLAIM_CONTENT_FLAG_USE_PARENT_BLOCK_NODE);
 
        if (alignment == EvoEditor.E_CONTENT_EDITOR_ALIGNMENT_NONE)
                traversar.toSet = "";
@@ -354,7 +360,7 @@ EvoEditor.SetAlignment = function(alignment)
        else
                throw "EvoEditor.SetAlignment: Unknown alignment value: '" + alignment + "'";
 
-       traversar.record = EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, "setAlignment", null, 
null, true, false);
+       traversar.record = EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, "setAlignment", null, 
null, EvoEditor.CLAIM_CONTENT_FLAG_USE_PARENT_BLOCK_NODE);
 
        try {
                EvoEditor.ForeachChildInAffectedContent(affected, traversar);
diff --git a/data/webkit/e-selection.js b/data/webkit/e-selection.js
index 32574667fd..f48b9e1161 100644
--- a/data/webkit/e-selection.js
+++ b/data/webkit/e-selection.js
@@ -65,7 +65,7 @@ EvoSelection.FindElementByPath = function(parent, path)
                var idx = path[ii];
 
                if (idx < 0 || idx >= child.children.length) {
-                       throw "EvoSelection.FindElementByPath:: Index '" + idx + "' out of range '" + 
parent.children.length + "'";
+                       throw "EvoSelection.FindElementByPath:: Index '" + idx + "' out of range '" + 
child.children.length + "'";
                }
 
                child = child.children.item(idx);
diff --git a/data/webkit/e-undo-redo.js b/data/webkit/e-undo-redo.js
index 0f49336cbd..38c03f2180 100644
--- a/data/webkit/e-undo-redo.js
+++ b/data/webkit/e-undo-redo.js
@@ -25,11 +25,6 @@ var EvoUndoRedo = {
        E_UNDO_REDO_STATE_CAN_UNDO : 1 << 0,
        E_UNDO_REDO_STATE_CAN_REDO : 1 << 1,
 
-       /* Flags for StartRecord() */
-       FLAG_NONE : 0,
-       FLAG_USE_PARENT_BLOCK_NODE : 1 << 0,
-       FLAG_SAVE_HTML : 1 << 1,
-
        stack : {
                // to not claim changes when none being made
                state : -1,
@@ -270,32 +265,10 @@ EvoUndoRedo.before_input_cb = function(inputEvent)
                return;
        }
 
-       var opType = inputEvent.inputType, useParentBlockNode = false;
-
-       if (opType == "" || // some WebKit-specific editing commands use this
-           opType.startsWith("format") ||
-           opType == "insertLineBreak" ||
-           opType == "insertParagraph") {
-               useParentBlockNode = true;
-               var startNode;
-
-               startNode = document.getSelection().baseNode;
+       var opType = inputEvent.inputType;
 
-               if (!startNode) {
-                       startNode = document.body;
-               }
-
-               while (startNode && !(startNode === document.body)) {
-                       if (EvoEditor.IsBlockNode(startNode)) {
-                               break;
-                       }
-
-                       startNode = startNode.parentElement;
-               }
-       }
-
-       EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_EVENT, opType, startNode, null,
-               EvoUndoRedo.FLAG_SAVE_HTML | (useParentBlockNode ? EvoUndoRedo.FLAG_USE_PARENT_BLOCK_NODE : 
EvoUndoRedo.FLAG_NONE));
+       EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_EVENT, opType, null, null,
+               EvoEditor.CLAIM_CONTENT_FLAG_SAVE_HTML | EvoEditor.CLAIM_CONTENT_FLAG_USE_PARENT_BLOCK_NODE);
 }
 
 EvoUndoRedo.input_cb = function(inputEvent)
@@ -389,11 +362,11 @@ EvoUndoRedo.applyRecord = function(record, isUndo, withSelection)
                                tmpNode.innerHTML = record.htmlAfter;
                        }
 
-                       if (first + 1 < commonParent.children.length) {
-                               first = commonParent.children.item(first + 1);
+                       if (first < commonParent.children.length) {
+                               first = commonParent.children.item(first);
 
-                               for (ii = tmpNode.children.length - 1; ii >= 0; ii--) {
-                                       commonParent.insertBefore(tmpNode.children.item(ii), first);
+                               while(tmpNode.firstElementChild) {
+                                       commonParent.insertBefore(tmpNode.firstElementChild, first);
                                }
                        } else {
                                while(tmpNode.children.length) {
@@ -420,9 +393,7 @@ EvoUndoRedo.StartRecord = function(kind, opType, startNode, endNode, flags)
                return null;
        }
 
-       var record = {}, saveHTML;
-
-       saveHTML = (flags & EvoUndoRedo.FLAG_SAVE_HTML) != 0;
+       var record = {};
 
        record.kind = kind;
        record.opType = opType;
@@ -433,13 +404,13 @@ EvoUndoRedo.StartRecord = function(kind, opType, startNode, endNode, flags)
        } else if (kind != EvoUndoRedo.RECORD_KIND_GROUP) {
                var affected;
 
-               affected = EvoEditor.ClaimAffectedContent(startNode, endNode, (flags & 
EvoUndoRedo.FLAG_USE_PARENT_BLOCK_NODE) != 0, saveHTML);
+               affected = EvoEditor.ClaimAffectedContent(startNode, endNode, flags);
 
                record.path = affected.path;
                record.firstChildIndex = affected.firstChildIndex;
                record.restChildrenCount = affected.restChildrenCount;
 
-               if (saveHTML)
+               if ((flags & EvoEditor.CLAIM_CONTENT_FLAG_SAVE_HTML) != 0)
                        record.htmlBefore = affected.html;
        }
 


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