Skip to content

Commit

Permalink
[pdf] Fixes for saving annots
Browse files Browse the repository at this point in the history
  • Loading branch information
KhromovNikita authored and K0R0L committed Oct 10, 2023
1 parent 4d938a4 commit 4d81954
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 106 deletions.
1 change: 1 addition & 0 deletions common/HistoryCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4012,6 +4012,7 @@
AscDFH.historyitem_Pdf_Annot_Pos = AscDFH.historyitem_type_Pdf_Annot | 2;
AscDFH.historyitem_Pdf_Annot_Contents = AscDFH.historyitem_type_Pdf_Annot | 3;
AscDFH.historyitem_Pdf_Annot_Page = AscDFH.historyitem_type_Pdf_Annot | 4;
AscDFH.historyitem_Pdf_Annot_Replies = AscDFH.historyitem_type_Pdf_Annot | 5;

// Comment
AscDFH.historyitem_Pdf_Comment_Data = AscDFH.historyitem_type_Pdf_Comment | 1;
Expand Down
22 changes: 20 additions & 2 deletions pdf/src/annotations/annotsChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ AscDFH.changesFactory[AscDFH.historyitem_Pdf_Ink_Points] = CChangesPDFInkPoints
AscDFH.changesFactory[AscDFH.historyitem_Pdf_Ink_FlipV] = CChangesPDFInkFlipV;
AscDFH.changesFactory[AscDFH.historyitem_Pdf_Ink_FlipH] = CChangesPDFInkFlipH;
AscDFH.changesFactory[AscDFH.historyitem_Pdf_Annot_Rect] = CChangesPDFAnnotRect;
AscDFH.changesFactory[AscDFH.historyitem_Pdf_Annot_Pos] = CChangesPDFAnnotPos;
AscDFH.changesFactory[AscDFH.historyitem_Pdf_Annot_Contents] = CChangesPDFAnnotContents;
AscDFH.changesFactory[AscDFH.historyitem_Pdf_Annot_Pos] = CChangesPDFAnnotPos;
AscDFH.changesFactory[AscDFH.historyitem_Pdf_Annot_Page] = CChangesPDFAnnotPage;
AscDFH.changesFactory[AscDFH.historyitem_Pdf_Annot_Replies] = CChangesPDFAnnotReplies;

/**
* @constructor
Expand Down Expand Up @@ -169,6 +170,23 @@ CChangesPDFAnnotContents.prototype.private_SetValue = function(Value)
oAnnot.SetContents(Value);
};

/**
* @constructor
* @extends {AscDFH.CChangesBaseProperty}
*/
function CChangesPDFAnnotReplies(Class, Old, New, Color)
{
AscDFH.CChangesBaseProperty.call(this, Class, Old, New, Color);
}
CChangesPDFAnnotReplies.prototype = Object.create(AscDFH.CChangesBaseProperty.prototype);
CChangesPDFAnnotReplies.prototype.constructor = CChangesPDFAnnotReplies;
CChangesPDFAnnotReplies.prototype.Type = AscDFH.historyitem_Pdf_Annot_Replies;
CChangesPDFAnnotReplies.prototype.private_SetValue = function(Value)
{
let oAnnot = this.Class;
oAnnot.SetReplies(Value);
};

/**
* @constructor
* @extends {AscDFH.CChangesBaseProperty}
Expand All @@ -179,7 +197,7 @@ function CChangesPDFAnnotPage(Class, Old, New, Color)
}
CChangesPDFAnnotPage.prototype = Object.create(AscDFH.CChangesBaseProperty.prototype);
CChangesPDFAnnotPage.prototype.constructor = CChangesPDFAnnotPage;
CChangesPDFAnnotPage.prototype.Type = AscDFH.historyitem_Pdf_Annot_Pos;
CChangesPDFAnnotPage.prototype.Type = AscDFH.historyitem_Pdf_Annot_Page;
CChangesPDFAnnotPage.prototype.private_SetValue = function(Value)
{
let oAnnot = this.Class;
Expand Down
127 changes: 102 additions & 25 deletions pdf/src/annotations/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
this._rectDiff = undefined;
this._popupIdx = undefined;

this._reply = undefined; // тут будут храниться ответы (text аннотация)
this._replies = []; // тут будут храниться ответы (text аннотации)

// internal
this._bDrawFromStream = false; // нужно ли рисовать из стрима
Expand Down Expand Up @@ -466,6 +466,7 @@

return true;
};


CAnnotationBase.prototype.GetRect = function() {
return this._rect;
Expand Down Expand Up @@ -554,18 +555,13 @@
oReply.SetReplyTo(this);
oReply.SetApIdx(this.GetDocument().GetMaxApIdx() + 2);

if (this.IsComment())
this._replies.push(oReply);
else {
if (this._reply == null)
this._reply = oReply;
else
this._reply._replies.push(oReply);
}
this._replies.push(oReply);
};
CAnnotationBase.prototype._OnAfterSetReply = function() {
let oAscCommData = this.IsComment() ? this.GetAscCommentData() : this._reply.GetAscCommentData();
editor.sendEvent("asc_onAddComment", this.GetId(), oAscCommData);
if (this.IsInDocument()) {
let oAscCommData = this.GetAscCommentData();
editor.sendEvent("asc_onAddComment", this.GetId(), oAscCommData);
}
};
CAnnotationBase.prototype.SetContents = function(contents) {
if (this.GetContents() == contents)
Expand All @@ -578,30 +574,111 @@
this._contents = contents;

if (oDoc.History.UndoRedoInProgress == false && oViewer.IsOpenAnnotsInProgress == false) {
oDoc.CreateNewHistoryPoint();
oDoc.History.Add(new CChangesPDFAnnotContents(this, oCurContents, contents));
oDoc.TurnOffHistory();
}

this.SetWasChanged(true);
if (contents != null)
this._OnAfterSetReply();
else if (this.IsInDocument())
editor.sync_RemoveComment(this.GetId());
};
CAnnotationBase.prototype.AddReply = function(oReply) {
this._reply = oReply;
oReply.SetReplyTo(this);
this._OnAfterSetReply();
let oDoc = this.GetDocument();

oDoc.CreateNewHistoryPoint();
if (this._replies.length == 0) {
this.SetContents(oReply.GetContents());
}
else {
oReply.SetReplyTo(this);
let aNewReplies = [].concat(this._replies);
aNewReplies.push(oReply);
this.SetReplies(aNewReplies);
}

oDoc.TurnOffHistory();
};
CAnnotationBase.prototype.SetReplies = function(aReplies) {
let oDoc = this.GetDocument();
let oViewer = editor.getDocumentRenderer();

if (oDoc.History.UndoRedoInProgress == false && oViewer.IsOpenAnnotsInProgress == false) {
oDoc.History.Add(new CChangesPDFAnnotReplies(this, this._replies, aReplies));
}
this._replies = aReplies;
};
CAnnotationBase.prototype.GetReply = function() {
return this._reply;
CAnnotationBase.prototype.GetReply = function(nPos) {
return this._replies[nPos];
};
CAnnotationBase.prototype.GetAscCommentData = function() {
if (this._reply)
return this._reply.GetAscCommentData();
CAnnotationBase.prototype.RemoveComment = function() {
let oDoc = this.GetDocument();

return null;
oDoc.CreateNewHistoryPoint();
this.SetContents(null);
this.SetReplies([]);
oDoc.TurnOffHistory();
};
CAnnotationBase.prototype.EditCommentData = function(oCommentData) {
let oFirstCommToEdit;
if (this.GetApIdx() == oCommentData.m_sUserData)
oFirstCommToEdit = this;
else {
oFirstCommToEdit = this._replies.find(function(oReply) {
return oCommentData.m_sUserData == oReply.GetApIdx();
});
}

oFirstCommToEdit.SetContents(oCommentData.m_sText);
oFirstCommToEdit.SetModDate(oCommentData.m_sOOTime);

let aReplyToDel = [];
let oReply, oReplyCommentData;
for (let i = 0; i < this._replies.length; i++) {
oReply = this._replies[i];
if (oFirstCommToEdit == oReply)
continue;

oReplyCommentData = oCommentData.m_aReplies.find(function(item) {
return item.m_sUserData == oReply.GetApIdx();
});

if (oReplyCommentData) {
oReply.EditCommentData(oReplyCommentData);
}
else {
aReplyToDel.push(oReply);
}
}

for (let i = aReplyToDel.length - 1; i >= 0; i--) {
this._replies.splice(this._replies.indexOf(aReplyToDel[i]), 1);
}

for (let i = 0; i < oCommentData.m_aReplies.length; i++) {
oReplyCommentData = oCommentData.m_aReplies[i];
if (!this._replies.find(function(reply) {
return oReplyCommentData.m_sUserData == reply.GetApIdx();
})) {
AscPDF.CAnnotationText.prototype.AddReply.call(this, oReplyCommentData);
}
}
};
CAnnotationBase.prototype.EditCommentData = function(CommentData) {
if (this._reply)
this._reply.EditCommentData(CommentData);
CAnnotationBase.prototype.GetAscCommentData = function() {
let oAscCommData = new Asc["asc_CCommentDataWord"](null);
oAscCommData.asc_putText(this.GetContents());
oAscCommData.asc_putOnlyOfficeTime(this.GetModDate().toString());
oAscCommData.asc_putUserId(editor.documentUserId);
oAscCommData.asc_putUserName(this.GetAuthor());
oAscCommData.asc_putSolved(false);
oAscCommData.asc_putQuoteText("");
oAscCommData.m_sUserData = this.GetApIdx();

this._replies.forEach(function(reply) {
oAscCommData.m_aReplies.push(reply.GetAscCommentData());
});

return oAscCommData;
};
CAnnotationBase.prototype.GetContents = function() {
return this._contents;
Expand Down
6 changes: 3 additions & 3 deletions pdf/src/annotations/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@
memory.WriteLong(nEndPos - nStartPos);
memory.Seek(nEndPos);

let oReply = this.GetReply();
if (oReply)
oReply.WriteToBinary(memory);
this._replies.forEach(function(reply) {
reply.WriteToBinary(memory);
});
};

function TurnOffHistory() {
Expand Down
72 changes: 69 additions & 3 deletions pdf/src/annotations/freeText.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,72 @@
this.content.Recalculate_Page(0, true);
}
};
CAnnotationFreeText.prototype.AddReply = function(oReply) {
oReply.SetReplyTo(this);

let aNewReplies = [].concat(this._replies);
aNewReplies.push(oReply);
this.SetReplies(aNewReplies);
};
CAnnotationFreeText.prototype.RemoveComment = function() {
let oDoc = this.GetDocument();

oDoc.CreateNewHistoryPoint();
this.SetReplies([]);
oDoc.TurnOffHistory();
};
CAnnotationFreeText.prototype.SetContents = function(contents) {
if (this.GetContents() == contents)
return;

let oViewer = editor.getDocumentRenderer();
let oDoc = this.GetDocument();
let oCurContents = this.GetContents();

this._contents = contents;

if (oDoc.History.UndoRedoInProgress == false && oViewer.IsOpenAnnotsInProgress == false) {
oDoc.History.Add(new CChangesPDFAnnotContents(this, oCurContents, contents));
}

this.SetWasChanged(true);
};
CAnnotationFreeText.prototype.SetReplies = function(aReplies) {
let oDoc = this.GetDocument();
let oViewer = editor.getDocumentRenderer();

if (oDoc.History.UndoRedoInProgress == false && oViewer.IsOpenAnnotsInProgress == false) {
oDoc.History.Add(new CChangesPDFAnnotReplies(this, this._replies, aReplies));
}
this._replies = aReplies;
if (aReplies.length != 0)
this._OnAfterSetReply();
else
editor.sync_RemoveComment(this.GetId());
};
CAnnotationFreeText.prototype.GetAscCommentData = function() {
let oAscCommData = new Asc["asc_CCommentDataWord"](null);
if (this._replies.length == 0)
return oAscCommData;

let oMainComm = this._replies[0];
oAscCommData.asc_putText(oMainComm.GetContents());
oAscCommData.asc_putOnlyOfficeTime(oMainComm.GetModDate().toString());
oAscCommData.asc_putUserId(editor.documentUserId);
oAscCommData.asc_putUserName(oMainComm.GetAuthor());
oAscCommData.asc_putSolved(false);
oAscCommData.asc_putQuoteText("");
oAscCommData.m_sUserData = oMainComm.GetApIdx();

this._replies.forEach(function(reply, index) {
if (index == 0)
return;

oAscCommData.m_aReplies.push(reply.GetAscCommentData());
});

return oAscCommData;
};
CAnnotationFreeText.prototype.WriteToBinary = function(memory) {
memory.WriteByte(AscCommon.CommandType.ctAnnotField);

Expand Down Expand Up @@ -208,9 +274,9 @@
memory.WriteLong(nEndPos - nStartPos);
memory.Seek(nEndPos);

let oReply = this.GetReply();
if (oReply)
oReply.WriteToBinary(memory);
this._replies.forEach(function(reply) {
reply.WriteToBinary(memory);
});
};

function TurnOffHistory() {
Expand Down
12 changes: 6 additions & 6 deletions pdf/src/annotations/highlights.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@
memory.WriteLong(nEndPos - nStartPos);
memory.Seek(nEndPos);

let oReply = this.GetReply();
if (oReply)
oReply.WriteToBinary(memory);
this._replies.forEach(function(reply) {
reply.WriteToBinary(memory);
});
};
/**
* Class representing a highlight annotation.
Expand Down Expand Up @@ -634,9 +634,9 @@
memory.WriteLong(nEndPos - nStartPos);
memory.Seek(nEndPos);

let oReply = this.GetReply();
if (oReply)
oReply.WriteToBinary(memory);
this._replies.forEach(function(reply) {
reply.WriteToBinary(memory);
});
};

function findMaxSideWithRotation(x1, y1, x2, y2, x3, y3, x4, y4) {
Expand Down
6 changes: 3 additions & 3 deletions pdf/src/annotations/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,9 @@
memory.WriteLong(nEndPos - nStartPos);
memory.Seek(nEndPos);

let oReply = this.GetReply();
if (oReply)
oReply.WriteToBinary(memory);
this._replies.forEach(function(reply) {
reply.WriteToBinary(memory);
});
};

function generateShapeByPoints(arrOfArrPoints, aShapeRect, oParentAnnot) {
Expand Down
6 changes: 3 additions & 3 deletions pdf/src/annotations/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@
memory.WriteLong(nEndPos - nStartPos);
memory.Seek(nEndPos);

let oReply = this.GetReply();
if (oReply)
oReply.WriteToBinary(memory);
this._replies.forEach(function(reply) {
reply.WriteToBinary(memory);
});
};

function TurnOffHistory() {
Expand Down
6 changes: 3 additions & 3 deletions pdf/src/annotations/polyLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@
memory.WriteLong(nEndPos - nStartPos);
memory.Seek(nEndPos);

let oReply = this.GetReply();
if (oReply)
oReply.WriteToBinary(memory);
this._replies.forEach(function(reply) {
reply.WriteToBinary(memory);
});
};
function TurnOffHistory() {
if (AscCommon.History.IsOn() == true)
Expand Down
6 changes: 3 additions & 3 deletions pdf/src/annotations/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@
memory.WriteLong(nEndPos - nStartPos);
memory.Seek(nEndPos);

let oReply = this.GetReply();
if (oReply)
oReply.WriteToBinary(memory);
this._replies.forEach(function(reply) {
reply.WriteToBinary(memory);
});
};

function TurnOffHistory() {
Expand Down
Loading

0 comments on commit 4d81954

Please sign in to comment.