From cf84d35b625316dbab417187c3469cb682391f02 Mon Sep 17 00:00:00 2001
From: "Ilya.Kirillov" <Ilya.Kirillov@OnlyOffice.com>
Date: Fri, 31 Oct 2014 09:05:18 +0000
Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?=
 =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B1=D0=B0=D0=B3=D0=B8=20=D0=B2=20Undo/R?=
 =?UTF-8?q?edo=20=D0=B8=20=D1=81=D0=BE=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BD?=
 =?UTF-8?q?=D0=BE=D0=BC=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80?=
 =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B8=20=D0=BF=D0=BE=D1=81=D0=BB?=
 =?UTF-8?q?=D0=B5=20=D0=BE=D1=82=D1=80=D0=B0=D0=B1=D0=B0=D1=82=D1=8B=D0=B2?=
 =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F=20enter=20=D0=B2=D0=BD=D1=83=D1=82?=
 =?UTF-8?q?=D1=80=D0=B8=20=D1=84=D0=BE=D1=80=D0=BC=D1=83=D0=BB=D1=8B.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@59370 954022d7-b5bf-4e40-9824-e11837661b57
---
 Word/Editor/Math.js      | 40 +++++++++++++++++++++++++---------------
 Word/Editor/Paragraph.js | 13 ++++++++-----
 Word/Math/base.js        | 28 +++++++++++++++++++---------
 Word/Math/mathContent.js | 15 +++++++++------
 Word/Math/matrix.js      | 12 +-----------
 5 files changed, 62 insertions(+), 46 deletions(-)

diff --git a/Word/Editor/Math.js b/Word/Editor/Math.js
index f68f20758..22370cce8 100644
--- a/Word/Editor/Math.js
+++ b/Word/Editor/Math.js
@@ -1693,11 +1693,15 @@ ParaMath.prototype.Handle_AddNewLine = function()
     var ContentPos = new CParagraphContentPos();
 
     var CurrContent = this.GetSelectContent().Content;
+
+    if (true === CurrContent.bRoot)
+        return false;
+
     CurrContent.Get_ParaContentPos(this.bSelectionUse, true, ContentPos);
 
     var NeedRecalculate = false;
 
-    if(CurrContent.Parent.kind == MATH_EQ_ARRAY)
+    if(MATH_EQ_ARRAY === CurrContent.ParentElement.kind)
     {
         var NewContent = CurrContent.Parent.addRow();
         CurrContent.SplitContent(NewContent, ContentPos, 0);
@@ -1706,14 +1710,14 @@ ParaMath.prototype.Handle_AddNewLine = function()
 
         NeedRecalculate = true;
     }
-    else if(CurrContent.bRoot == false && CurrContent.Parent.kind !== MATH_MATRIX)
+    else if(MATH_MATRIX !== CurrContent.ParentElement.kind)
     {
         var ctrPrp = CurrContent.Parent.CtrPrp.Copy();
         var props = {row: 2, ctrPrp: ctrPrp};
         var EqArray = new CEqArray(props);
 
-        var FirstContent = EqArray.getElement(0),
-            SecondContent = EqArray.getElement(1);
+        var FirstContent  = EqArray.getElementMathContent(0);
+        var SecondContent = EqArray.getElementMathContent(1);
 
         CurrContent.SplitContent(SecondContent, ContentPos, 0);
         CurrContent.CopyTo(FirstContent, false);
@@ -1740,10 +1744,10 @@ ParaMath.prototype.Handle_AddNewLine = function()
         NeedRecalculate = true;
     }
 
-    this.SetNeedResize();
+    if (true === NeedRecalculate)
+        this.SetNeedResize();
 
     return NeedRecalculate;
-
 };
 
 /**
@@ -1947,36 +1951,42 @@ function CChangesMathAddItems(Pos, Items)
 CChangesMathAddItems.prototype.Type = historyitem_Math_AddItems_ToMathBase;
 CChangesMathAddItems.prototype.Undo = function(Class)
 {
-
+    Class.raw_RemoveFromContent(this.Pos, this.Items.length);
 };
 CChangesMathAddItems.prototype.Redo = function(Class)
 {
-    Class.raw_Internal_Content_Add(this.Pos, this.Items, false);
+    Class.raw_AddToContent(this.Pos, this.Items, false);
 };
 CChangesMathAddItems.prototype.Save_Changes = function(Writer)
 {
+    // Long : Count
+    // Long : Pos
+    // Array of String : Id
+
     var Count = this.Items.length;
     Writer.WriteLong(Count);
+    Writer.WriteLong(this.Pos);
 
     for(var Index = 0; Index < Count; Index++)
     {
-        Writer.WriteLong(this.Pos + Index);
         Writer.WriteString2(this.Items[Index].Get_Id());
     }
 };
 CChangesMathAddItems.prototype.Load_Changes = function(Reader, Class)
 {
-    var Count = Reader.GetLong();
-    this.Pos = Reader.GetLong();
+    // Long : Count
+    // Long : Pos
+    // Array of String : Id
 
-    if(this.Items == undefined)
-        this.Items = [];
+    var Count = Reader.GetLong();
+    this.Pos  = Reader.GetLong();
 
+    this.Items = [];
     for(var Index = 0; Index < Count; Index++)
     {
-        var Element = g_oTableId.Get_ById( Reader.GetString2() );
+        var Element = g_oTableId.Get_ById(Reader.GetString2());
 
-        if ( null != Element )
+        if (null !== Element)
             this.Items.push(Element);
     }
 
diff --git a/Word/Editor/Paragraph.js b/Word/Editor/Paragraph.js
index 9f7ff8df5..807d7b791 100644
--- a/Word/Editor/Paragraph.js
+++ b/Word/Editor/Paragraph.js
@@ -12140,12 +12140,15 @@ Paragraph.prototype.private_CorrectCurPosRangeLine = function()
         var RangeIndex = Ranges[Index].Range;
         var LineIndex  = Ranges[Index].Line;
 
-        var Range = this.Lines[LineIndex].Ranges[RangeIndex];
-        if (Range.W > 0)
+        if (undefined !== this.Lines[LineIndex] && undefined !== this.Lines[LineIndex].Ranges[RangeIndex])
         {
-            this.CurPos.Line  = LineIndex;
-            this.CurPos.Range = RangeIndex;
-            break;
+            var Range = this.Lines[LineIndex].Ranges[RangeIndex];
+            if (Range.W > 0)
+            {
+                this.CurPos.Line = LineIndex;
+                this.CurPos.Range = RangeIndex;
+                break;
+            }
         }
     }
 };
diff --git a/Word/Math/base.js b/Word/Math/base.js
index 8d46f6c24..37f19830d 100644
--- a/Word/Math/base.js
+++ b/Word/Math/base.js
@@ -1041,23 +1041,33 @@ CMathBase.prototype.Draw_HighLights = function(PDSH, bAll)
     PDSH.X = this.pos.x + this.ParaMath.X + this.size.width;
 
 };
-CMathBase.prototype.Internal_Content_Add = function(Pos, Items, bUpdatePosition)
+CMathBase.prototype.protected_AddToContent = function(Pos, Items, bUpdatePosition)
 {
     History.Add(this, new CChangesMathAddItems(Pos, Items));
-    //History.Add( this, { Type : historyitem_Math_AddItems_ToMathBase, Pos : Pos, EndPos : Pos, Items : Items } );
-    this.raw_Internal_Content_Add(Pos, Items, bUpdatePosition);
+    this.raw_AddToContent(Pos, Items, bUpdatePosition);
+    this.private_UpdatePosOnAdd(Pos, bUpdatePosition);
 };
-CMathBase.prototype.raw_Internal_Content_Add = function(Pos, Items, bUpdatePosition)
+CMathBase.prototype.raw_AddToContent = function(Pos, Items, bUpdatePosition)
 {
-    for(var Index = 0; Index < Items.length; Index++)
-        this.Content.splice(Pos + Index, 0, Items[Index]);
+    for(var Index = 0, Count = Items.length; Index < Count; Index++)
+    {
+        var Item = Items[Index];
+        this.Content.splice(Pos + Index, 0, Item);
+        Item.ParentElement = this;
+    }
 
-    this.Update_Pos_After_AddItems(Pos, bUpdatePosition);
     this.fillContent();
-
     this.private_SetNeedResize();
 }
-CMathBase.prototype.Update_Pos_After_AddItems   = CMathContent.prototype.Update_Pos_After_AddItems;
+CMathBase.prototype.raw_RemoveFromContent = function(Pos, Count)
+{
+    this.Content.splice(Pos, Count);
+
+    this.fillContent();
+    this.private_SetNeedResize();
+};
+CMathBase.prototype.private_UpdatePosOnAdd      = CMathContent.prototype.private_UpdatePosOnAdd;
+CMathBase.prototype.private_UpdatePosOnRemove   = CMathContent.prototype.private_UpdateOnRemove;
 CMathBase.prototype.private_CorrectSelectionPos = CMathContent.prototype.private_CorrectSelectionPos;
 CMathBase.prototype.private_SetNeedResize       = CMathContent.prototype.private_SetNeedResize;
 
diff --git a/Word/Math/mathContent.js b/Word/Math/mathContent.js
index 147d6f764..690a85dcf 100644
--- a/Word/Math/mathContent.js
+++ b/Word/Math/mathContent.js
@@ -1564,9 +1564,10 @@ CMathContent.prototype =
         History.Add( this, { Type : historyitem_Math_AddItem, Pos : Pos, EndPos : Pos, Items : [ Item ] } );
         this.Content.splice( Pos, 0, Item );
 
-        this.Update_Pos_After_AddItems(Pos, bUpdatePosition);
+        this.private_UpdatePosOnAdd(Pos, bUpdatePosition);
     },
-    Update_Pos_After_AddItems: function(Pos, bUpdatePosition)
+
+    private_UpdatePosOnAdd: function(Pos, bUpdatePosition)
     {
         if(bUpdatePosition !== false)
         {
@@ -1631,13 +1632,12 @@ CMathContent.prototype =
     {
         var Pos = ContentPos.Get(Depth);
 
-        if(this.Content[Pos].Type == para_Math_Run)
+        if(para_Math_Run === this.Content[Pos].Type)
         {
             var NewRun = this.Content[Pos].Split(ContentPos, Depth+1);
             NewContent.Add_ToContent(0, NewRun);
 
             var len = this.Content.length;
-
             if(Pos < len - 1)
             {
                 NewContent.Concat_ToContent( this.Content.slice(Pos + 1) );
@@ -1645,8 +1645,7 @@ CMathContent.prototype =
             }
         }
 
-        this.ParaMath.SetNeedResize();
-
+        this.private_SetNeedResize();
     },
     Add_ToContent : function(Pos, Item)
     {
@@ -1672,7 +1671,11 @@ CMathContent.prototype =
             this.CurPos = Pos;
 
         this.private_CorrectCurPos();
+        this.private_UpdatePosOnRemove(Pos, Count);
+    },
 
+    private_UpdatePosOnRemove : function(Pos, Count)
+    {
         // Обновим начало и конец селекта
         if (true === this.Selection.Use)
         {
diff --git a/Word/Math/matrix.js b/Word/Math/matrix.js
index a3279efa8..4d5f36e68 100644
--- a/Word/Math/matrix.js
+++ b/Word/Math/matrix.js
@@ -687,20 +687,10 @@ CEqArray.prototype.addRow = function()
 {
     this.bDecreaseRow = true;
     var NewContent = new CMathContent();
-    this.Internal_Content_Add(this.CurPos + 1, [NewContent], true);
+    this.protected_AddToContent(this.CurPos + 1, [NewContent], true);
 
     return NewContent;
 }
-/*CEqArray.prototype.addRow = function()
-{
-    this.Content.splice( this.CurPos + 1, 0, new CMathContent() );
-    this.Content[this.CurPos + 1].ParentElement = this;
-
-    //this.Pr.DecreaseCountRow();
-    //this.bDecreaseRow = true;
-
-    return this.Content[this.CurPos + 1];
-}*/
 CEqArray.prototype.fillContent = function()
 {
     var nRowsCount = this.Content.length;
-- 
2.30.9