Skip to content

Commit

Permalink
[pdf] Refactor pages
Browse files Browse the repository at this point in the history
  • Loading branch information
KhromovNikita authored and KirillovIlya committed Oct 7, 2024
1 parent db37d38 commit 6e6d501
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 39 deletions.
3 changes: 2 additions & 1 deletion common/HistoryCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,8 @@
window['AscDFH'].historyitem_type_Pdf_Pushbutton = 2227 << 16;
window['AscDFH'].historyitem_type_Pdf_List_Form = 2228 << 16;
window['AscDFH'].historyitem_type_Pdf_Drawing = 2229 << 16;

window['AscDFH'].historyitem_type_Pdf_Page = 2230 << 16;

window['AscDFH'].historyitem_type_CustomProperties = 2300 << 16;


Expand Down
1 change: 1 addition & 0 deletions common/TableId.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_Underline] = AscPDF.CAnnotationUnderline;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_Strikeout] = AscPDF.CAnnotationStrikeout;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_FreeText] = AscPDF.CAnnotationFreeText;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Page] = AscPDF.CPageInfo;
}

this.m_oFactoryClass[AscDFH.historyitem_type_DocumentMacros] = AscCommon.CDocumentMacros;
Expand Down
18 changes: 16 additions & 2 deletions pdf/src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2195,10 +2195,21 @@ var CPresentation = CPresentation || function(){};
Image : undefined
});

let oPageInfo;
// from history
if (oPage.Id) {
oPageInfo = AscCommon.g_oTableId.GetById(oPage.Id);
}
// to history
else {
oPageInfo = new AscPDF.CPageInfo();
oPage.Id = oPageInfo.Id;
}

if (oViewer.pagesInfo.pages.length == 0)
oViewer.pagesInfo.setCount(1);
else
oViewer.pagesInfo.pages.splice(nPos, 0, new AscPDF.CPageInfo());
oViewer.pagesInfo.pages.splice(nPos, 0, oPageInfo);

// can be uninitialized on Apply_Changes
if (oViewer.thumbnails) {
Expand Down Expand Up @@ -2279,8 +2290,11 @@ var CPresentation = CPresentation || function(){};
// убираем информацию о странице
let aPages = oFile.removePage(nPos);
oViewer.drawingPages.splice(nPos, 1);
oViewer.pagesInfo.pages.splice(nPos, 1);
let aPagesInfo = oViewer.pagesInfo.pages.splice(nPos, 1);

// to history
aPages[0].Id = aPagesInfo[0].Id;

// can be uninitialized on Apply_Changes
if (oViewer.thumbnails) {
oViewer.thumbnails._deletePage(nPos);
Expand Down
104 changes: 68 additions & 36 deletions pdf/src/history/documentChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ CChangesPDFDocumentAnnotsContent.prototype.IsContentChange = function(){
return true;
};

CChangesPDFDocumentAnnotsContent.prototype.ReadFromBinary = function (reader) {
this.PageId = reader.GetString2();
AscDFH.CChangesDrawingsContent.prototype.ReadFromBinary.call(this, reader);
};
CChangesPDFDocumentAnnotsContent.prototype.WriteToBinary = function (writer) {
let oViewer = Asc.editor.getDocumentRenderer();
if (!this.PageId) {
let nPage = this.Items[0].GetPage();
this.PageId = oViewer.pagesInfo.pages[nPage].GetId();
}
writer.WriteString2(this.PageId);

AscDFH.CChangesDrawingsContent.prototype.WriteToBinary.call(this, writer);
};

CChangesPDFDocumentAnnotsContent.prototype.Undo = function()
{
let oDocument = this.Class;
Expand Down Expand Up @@ -150,11 +165,10 @@ CChangesPDFDocumentAnnotsContent.prototype.private_InsertInArrayLoad = function(
let oDocument = this.Class;
let oViewer = Asc.editor.getDocumentRenderer();
let oContentChanges = this.private_GetContentChanges();
let pageChanges = oDocument.pagesContentChanges;
let oPage = AscCommon.g_oTableId.Get_ById(this.PageId);

for (let i = 0; i < this.Items.length; ++i) {
let oItem = this.Items[i];
let nPage = oItem.GetPage();

// Adjust position based on content changes
let nPos = oContentChanges.Check(AscCommon.contentchanges_Add, true !== this.UseArray ? this.Pos + i : this.PosArray[i]);
Expand All @@ -163,13 +177,12 @@ CChangesPDFDocumentAnnotsContent.prototype.private_InsertInArrayLoad = function(
// Ensure position is within bounds
nPos = Math.min(nPos, oDocument.annots.length);

nPage = pageChanges.GetPos(nPage);
if (false !== nPage) {
if (oPage) {
// Insert into document annots array
oDocument.annots.splice(nPos, 0, oItem);

// Insert into viewer annots array
let annotsArray = oViewer.pagesInfo.pages[nPage].annots;
let annotsArray = oPage.annots;
nPos = Math.min(nPos, annotsArray.length);
annotsArray.splice(nPos, 0, oItem);
oItem.AddToRedraw();
Expand All @@ -193,27 +206,25 @@ CChangesPDFDocumentAnnotsContent.prototype.private_RemoveInArrayLoad = function(
let oDocument = this.Class;
let oViewer = Asc.editor.getDocumentRenderer();
let oContentChanges = this.private_GetContentChanges();
let pageChanges = oDocument.pagesContentChanges;
let oPage = AscCommon.g_oTableId.Get_ById(this.PageId);

// Remove items in reverse order to maintain indices
for (let i = this.Items.length - 1; i >= 0; --i) {
let oItem = this.Items[i];
let nPage = oItem.GetPage();

// Adjust position based on content changes
let nPos = oContentChanges.Check(AscCommon.contentchanges_Remove, true !== this.UseArray ? this.Pos + i : this.PosArray[i]);
if (nPos === false) continue;

// Remove from viewer annots array
nPage = pageChanges.GetPos(nPage);
if (false !== nPage) {
if (oPage) {
oItem.AddToRedraw();
// Remove from document annots array
let indexInAnnots = oDocument.annots.indexOf(oItem);
if (indexInAnnots !== -1)
oDocument.annots.splice(indexInAnnots, 1);

let annotsArray = oViewer.pagesInfo.pages[nPage].annots;
let annotsArray = oPage.annots;
let indexInPageAnnots = annotsArray.indexOf(oItem);
if (indexInPageAnnots !== -1)
annotsArray.splice(indexInPageAnnots, 1);
Expand Down Expand Up @@ -250,6 +261,21 @@ CChangesPDFDocumentFieldsContent.prototype.IsContentChange = function(){
return true;
};

CChangesPDFDocumentFieldsContent.prototype.ReadFromBinary = function (reader) {
this.PageId = reader.GetString2();
AscDFH.CChangesDrawingsContent.prototype.ReadFromBinary.call(this, reader);
};
CChangesPDFDocumentFieldsContent.prototype.WriteToBinary = function (writer) {
let oViewer = Asc.editor.getDocumentRenderer();
if (!this.PageId) {
let nPage = this.Items[0].GetPage();
this.PageId = oViewer.pagesInfo.pages[nPage].GetId();
}
writer.WriteString2(this.PageId);

AscDFH.CChangesDrawingsContent.prototype.WriteToBinary.call(this, writer);
};

CChangesPDFDocumentFieldsContent.prototype.Undo = function()
{
let oDocument = this.Class;
Expand Down Expand Up @@ -337,9 +363,8 @@ CChangesPDFDocumentFieldsContent.prototype.private_InsertInArrayLoad = function(
return;

let oDocument = this.Class;
let oViewer = Asc.editor.getDocumentRenderer();
let oContentChanges = this.private_GetContentChanges();
let pageChanges = oDocument.pagesContentChanges;
let oPage = AscCommon.g_oTableId.Get_ById(this.PageId);

for (let i = 0; i < this.Items.length; ++i) {
let oItem = this.Items[i];
Expand All @@ -350,15 +375,12 @@ CChangesPDFDocumentFieldsContent.prototype.private_InsertInArrayLoad = function(

if (oItem.IsForm()) {
if (oItem.IsWidget()) {
let nPage = oItem.GetPage();

// Insert into document widgets array
nPos = Math.min(nPos, oDocument.widgets.length);
nPage = pageChanges.GetPos(nPage);
if (false !== nPage) {
if (oPage) {
oDocument.widgets.splice(nPos, 0, oItem);
// Insert into viewer fields array
let fieldsArray = oViewer.pagesInfo.pages[nPage].fields;
let fieldsArray = oPage.fields;
nPos = Math.min(nPos, fieldsArray.length);
fieldsArray.splice(nPos, 0, oItem);
}
Expand All @@ -380,9 +402,8 @@ CChangesPDFDocumentFieldsContent.prototype.private_RemoveInArrayLoad = function(
return;

let oDocument = this.Class;
let oViewer = Asc.editor.getDocumentRenderer();
let oContentChanges = this.private_GetContentChanges();
let pageChanges = oDocument.pagesContentChanges;
let oPage = AscCommon.g_oTableId.Get_ById(this.PageId);

// Remove items in reverse order to maintain indices
for (let i = this.Items.length - 1; i >= 0; --i) {
Expand All @@ -394,10 +415,7 @@ CChangesPDFDocumentFieldsContent.prototype.private_RemoveInArrayLoad = function(

if (oItem.IsForm()) {
if (oItem.IsWidget()) {
let nPage = oItem.GetPage();

nPage = pageChanges.GetPos(nPage);
if (false !== nPage) {
if (oPage) {
oItem.AddToRedraw();

// Remove from document widgets array
Expand All @@ -406,7 +424,7 @@ CChangesPDFDocumentFieldsContent.prototype.private_RemoveInArrayLoad = function(
oDocument.widgets.splice(indexInWidgets, 1);

// Remove from viewer fields array
let fieldsArray = oViewer.pagesInfo.pages[nPage].fields;
let fieldsArray = oPage.fields;
let indexInFields = fieldsArray.indexOf(oItem);
if (indexInFields !== -1)
fieldsArray.splice(indexInFields, 1);
Expand Down Expand Up @@ -442,6 +460,20 @@ CChangesPDFDocumentDrawingsContent.prototype.IsContentChange = function(){
return true;
};

CChangesPDFDocumentDrawingsContent.prototype.ReadFromBinary = function (reader) {
this.PageId = reader.GetString2();
AscDFH.CChangesDrawingsContent.prototype.ReadFromBinary.call(this, reader);
};
CChangesPDFDocumentDrawingsContent.prototype.WriteToBinary = function (writer) {
let oViewer = Asc.editor.getDocumentRenderer();
if (!this.PageId) {
let nPage = this.Items[0].GetPage();
this.PageId = oViewer.pagesInfo.pages[nPage].GetId();
}
writer.WriteString2(this.PageId);

AscDFH.CChangesDrawingsContent.prototype.WriteToBinary.call(this, writer);
};
CChangesPDFDocumentDrawingsContent.prototype.Undo = function()
{
let oDocument = this.Class;
Expand Down Expand Up @@ -526,23 +558,21 @@ CChangesPDFDocumentDrawingsContent.prototype.private_InsertInArrayLoad = functio
let pdfDocument = this.Class;
let oViewer = Asc.editor.getDocumentRenderer();
let drawingChanges = pdfDocument.drawingsContentChanges;
let pageChanges = pdfDocument.pagesContentChanges;
let oPage = AscCommon.g_oTableId.Get_ById(this.PageId);

for (let i = 0; i < this.Items.length; ++i)
{
let oItem = this.Items[i];
let nPage = oItem.GetPage();

// Adjust position based on content changes
let nPos = drawingChanges.Check(AscCommon.contentchanges_Add, true !== this.UseArray ? this.Pos + i : this.PosArray[i]);
if (nPos === false) continue;

nPos = Math.min(nPos, pdfDocument.drawings.length);
nPage = pageChanges.GetPos(nPage);
if (false !== nPage)

if (oPage)
{
pdfDocument.drawings.splice(nPos, 0, oItem);
let drawingsArray = oViewer.pagesInfo.pages[nPage].drawings;
let drawingsArray = oPage.drawings;

nPos = Math.min(nPos, drawingsArray.length);
drawingsArray.splice(nPos, 0, oItem);
Expand All @@ -563,25 +593,23 @@ CChangesPDFDocumentDrawingsContent.prototype.private_RemoveInArrayLoad = functio
let pdfDocument = this.Class;
let oViewer = Asc.editor.getDocumentRenderer();
let drawingChanges = pdfDocument.drawingsContentChanges;
let pageChanges = pdfDocument.pagesContentChanges;
let oPage = AscCommon.g_oTableId.Get_ById(this.PageId);

// Remove items in reverse order to maintain indices
for (let i = this.Items.length - 1; i >= 0; --i)
{
let oItem = this.Items[i];
let nPage = oItem.GetPage();

let nPos = drawingChanges.Check(AscCommon.contentchanges_Remove, true !== this.UseArray ? this.Pos + i : this.PosArray[i]);
if (nPos === false) continue;

nPage = pageChanges.GetPos(nPage);
if (false !== nPage)
if (oPage)
{
let indexInDrawings = pdfDocument.drawings.indexOf(oItem);
if (indexInDrawings !== -1)
pdfDocument.drawings.splice(indexInDrawings, 1);

let drawingsArray = oViewer.pagesInfo.pages[nPage].drawings;
let drawingsArray = oPage.drawings;
let indexInDrawingsArray = drawingsArray.indexOf(oItem);
if (indexInDrawingsArray !== -1)
drawingsArray.splice(indexInDrawingsArray, 1);
Expand Down Expand Up @@ -647,11 +675,12 @@ CChangesPDFDocumentAddPage.prototype.Load = function()
{
let nPos = true !== this.UseArray ? this.Pos : this.PosArray[nIndex];
nPos = pageChanges.Check(AscCommon.contentchanges_Add, nPos);
pdfDocument.AddPage(nPos);
pdfDocument.AddPage(nPos, this.Items[nIndex]);
}
};
CChangesPDFDocumentAddPage.prototype.private_WriteItem = function(Writer, oPage)
{
Writer.WriteString2(oPage.Id);
Writer.WriteLong(oPage.Rotate);
Writer.WriteLong(oPage.Dpi);
Writer.WriteLong(oPage.W);
Expand All @@ -660,6 +689,7 @@ CChangesPDFDocumentAddPage.prototype.private_WriteItem = function(Writer, oPage)
CChangesPDFDocumentAddPage.prototype.private_ReadItem = function(Reader)
{
return {
Id: Reader.GetString2(),
Rotate: Reader.GetLong(),
Dpi: Reader.GetLong(),
W: Reader.GetLong(),
Expand Down Expand Up @@ -724,6 +754,7 @@ CChangesPDFDocumentRemovePage.prototype.Load = function()
};
CChangesPDFDocumentRemovePage.prototype.private_WriteItem = function(Writer, oPage)
{
Writer.WriteString2(oPage.Id);
Writer.WriteLong(oPage.Rotate);
Writer.WriteLong(oPage.Dpi);
Writer.WriteLong(oPage.W);
Expand All @@ -732,6 +763,7 @@ CChangesPDFDocumentRemovePage.prototype.private_WriteItem = function(Writer, oPa
CChangesPDFDocumentRemovePage.prototype.private_ReadItem = function(Reader)
{
return {
Id: Reader.GetString2(),
Rotate: Reader.GetLong(),
Dpi: Reader.GetLong(),
W: Reader.GetLong(),
Expand Down
8 changes: 8 additions & 0 deletions pdf/src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
// страницы на экране в приоритете.
function CPageInfo()
{
this.Id = null;
if ((AscCommon.g_oIdCounter.m_bLoad || AscCommon.History.CanAddChanges())) {
this.Id = AscCommon.g_oIdCounter.Get_NewId();
AscCommon.g_oTableId.Add(this, this.Id);
}

this.isPainted = false;
this.links = null;
this.fields = [];
Expand All @@ -112,6 +118,8 @@
this.needRedrawDrawings = true;
this.needRedrawAnnots = true;
}
AscFormat.InitClass(CPageInfo, AscFormat.CBaseNoIdObject, AscDFH.historyitem_type_Pdf_Page);
CPageInfo.prototype.constructor = CPageInfo;

function CDocumentPagesInfo()
{
Expand Down

0 comments on commit 6e6d501

Please sign in to comment.