[evolution/wip/mcrha/webkit-jsc-api] blockquote pre-post processing



commit 3daa6d52f9e2927bc700af58bf5a377307c719c8
Author: Milan Crha <mcrha redhat com>
Date:   Tue Mar 10 19:15:04 2020 +0100

    blockquote pre-post processing

 data/webkit/e-editor.js | 78 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 68 insertions(+), 10 deletions(-)
---
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index 75b6d332fc..5f11d96523 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -2381,6 +2381,24 @@ EvoEditor.findSmileys = function(text, unicodeSmileys)
        return res;
 }
 
+EvoEditor.maybeUpdateParagraphWidth = function(topNode)
+{
+       if (EvoEditor.mode == EvoEditor.MODE_PLAIN_TEXT) {
+               var node = topNode, citeLevel = 0;
+
+               while (node && node.tagName != "BODY") {
+                       if (node.tagName == "BLOCKQUOTE")
+                               citeLevel++;
+
+                       node = node.parentElement;
+               }
+
+               if (citeLevel * 2 < EvoEditor.NORMAL_PARAGRAPH_WIDTH) {
+                       topNode.style.width = (EvoEditor.NORMAL_PARAGRAPH_WIDTH - citeLevel * 2) + "ch";
+               }
+       }
+}
+
 EvoEditor.AfterInputEvent = function(inputEvent, isWordDelim)
 {
        var isInsertParagraph = inputEvent.inputType == "insertParagraph";
@@ -2404,19 +2422,36 @@ EvoEditor.AfterInputEvent = function(inputEvent, isWordDelim)
                // paragraph can inherit styles from it, which is also negative text-indent
                selection.anchorNode.textIndent = "";
                EvoEditor.removeEmptyStyleAttribute(selection.anchorNode);
+               EvoEditor.maybeUpdateParagraphWidth(selection.anchorNode);
+       }
 
-               if (EvoEditor.mode == EvoEditor.MODE_PLAIN_TEXT) {
-                       var node = selection.anchorNode, citeLevel = 0;
+       // inserting paragraph in BLOCKQUOTE creates a new BLOCKQUOTE without <DIV> inside it
+       if (isInsertParagraph && selection.isCollapsed && selection.anchorNode && 
(selection.anchorNode.tagName == "BLOCKQUOTE" ||
+           (selection.anchorNode.nodeType == selection.anchorNode.TEXT_NODE && 
selection.anchorNode.parentElement &&
+            selection.anchorNode.parentElement.tagName == "BLOCKQUOTE"))) {
+               var blockquoteNode = selection.anchorNode;
 
-                       while (node && node.tagName != "BODY") {
-                               if (node.tagName == "BLOCKQUOTE")
-                                       citeLevel++;
+               if (blockquoteNode.nodeType == blockquoteNode.TEXT_NODE)
+                       blockquoteNode = blockquoteNode.parentElement;
 
-                               node = node.parentElement;
-                       }
+               if (!blockquoteNode.firstChild || !EvoEditor.IsBlockNode(blockquoteNode.firstChild)) {
+                       EvoUndoRedo.StartRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, "blockquoteFix", 
blockquoteNode, blockquoteNode,
+                               EvoEditor.CLAIM_CONTENT_FLAG_SAVE_HTML);
+
+                       try {
+                               var divNode = document.createElement("DIV");
 
-                       if (citeLevel * 2 < EvoEditor.NORMAL_PARAGRAPH_WIDTH) {
-                               selection.anchorNode.style.width = (EvoEditor.NORMAL_PARAGRAPH_WIDTH - 
citeLevel * 2) + "ch";
+                               while (blockquoteNode.firstChild) {
+                                       divNode.appendChild(blockquoteNode.firstChild);
+                               }
+
+                               blockquoteNode.appendChild(divNode);
+                               EvoEditor.maybeUpdateParagraphWidth(divNode);
+                       } finally {
+                               EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_CUSTOM, "blockquoteFix");
+                               EvoUndoRedo.GroupTopRecords(2, "insertParagraph::blockquoteFix");
+                               EvoEditor.maybeUpdateFormattingState(EvoEditor.FORCE_MAYBE);
+                               EvoEditor.EmitContentChanged();
                        }
                }
        }
@@ -4099,7 +4134,7 @@ EvoEditor.InsertContent = function(text, isHTML, quote)
                                                                        clone.insertBefore(from, 
clone.firstChild);
                                                                }
 
-                                                               if (parent === parentBlock.parentElement || 
parent.parentElement.tagName == "BLOCKQUOTE") {
+                                                               if (parent === parentBlock.parentElement || 
(parent.parentElement && parent.parentElement.tagName == "BLOCKQUOTE")) {
                                                                        break;
                                                                }
 
@@ -4207,6 +4242,29 @@ EvoEditor.processLoadedContent = function()
                node = list[ii];
                node.removeAttribute("class");
        }
+
+       // require blocks under BLOCKQUOTE
+       list = document.querySelectorAll("BLOCKQUOTE");
+
+       for (ii = list.length - 1; ii >= 0; ii--) {
+               var blockquoteNode = list[ii], addingTo = null, next;
+
+               for (node = blockquoteNode.firstChild; node; node = next) {
+                       next = node.nextSibling;
+
+                       if (!EvoEditor.IsBlockNode(node)) {
+                               if (!addingTo) {
+                                       addingTo = document.createElement("DIV");
+                                       blockquoteNode.insertBefore(addingTo, node);
+                                       EvoEditor.maybeUpdateParagraphWidth(addingTo);
+                               }
+
+                               addingTo.appendChild(node);
+                       } else {
+                               addingTo = null;
+                       }
+               }
+       }
 }
 
 EvoEditor.LoadHTML = function(html)


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