From 5269fc79fb9789c2a1f232065a366af9743a4960 Mon Sep 17 00:00:00 2001 From: Nikita Khromov Date: Tue, 18 Feb 2025 13:53:33 +0700 Subject: [PATCH] Actions --- pdf/src/forms/base/base.js | 84 +++++++++++++++++++++++++++++---- pdf/src/forms/formActions.js | 22 +++++++++ pdf/src/history/formsChanges.js | 84 +++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 10 deletions(-) diff --git a/pdf/src/forms/base/base.js b/pdf/src/forms/base/base.js index 5d266e57a6..0ac41f1053 100644 --- a/pdf/src/forms/base/base.js +++ b/pdf/src/forms/base/base.js @@ -454,37 +454,42 @@ break; } } + + const oNewTrigger = aActions.length != 0 ? new AscPDF.CFormTrigger(nTriggerType, aActions) : null; + const aCurActionsInfo = this.GetActions(); + + AscCommon.History.Add(new CChangesPDFFormActions(this, aCurActionsInfo, aActionsInfo, nTriggerType)); switch (nTriggerType) { case AscPDF.FORMS_TRIGGERS_TYPES.MouseUp: - this._triggers.MouseUp = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.MouseUp = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.MouseDown: - this._triggers.MouseDown = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.MouseDown = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.MouseEnter: - this._triggers.MouseEnter = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.MouseEnter = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.MouseExit: - this._triggers.MouseExit = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.MouseExit = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.OnFocus: - this._triggers.OnFocus = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.OnFocus = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.OnBlur: - this._triggers.OnBlur = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.OnBlur = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.Keystroke: - this._triggers.Keystroke = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.Keystroke = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.Validate: - this._triggers.Validate = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.Validate = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.Calculate: - this._triggers.Calculate = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.Calculate = oNewTrigger; break; case AscPDF.FORMS_TRIGGERS_TYPES.Format: - this._triggers.Format = new AscPDF.CFormTrigger(nTriggerType, aActions); + this._triggers.Format = oNewTrigger; break; } @@ -494,6 +499,65 @@ return aActions; }; + CBaseField.prototype.GetActions = function(nTriggerType) { + // Get the trigger by type + let oTrigger = this.GetTrigger(nTriggerType); + if (!oTrigger || !oTrigger.Actions) { + return []; + } + + let aActionsInfo = []; + // Iterate through all actions associated with the trigger + for (let i = 0; i < oTrigger.Actions.length; i++) { + let oAction = oTrigger.Actions[i]; + let actionInfo = {}; + + // Determine the action type and populate the object with information + switch (oAction.GetType()) { + case AscPDF.ACTIONS_TYPES.JavaScript: + actionInfo["S"] = AscPDF.ACTIONS_TYPES.JavaScript; + actionInfo["JS"] = oAction.GetScript(); + break; + case AscPDF.ACTIONS_TYPES.ResetForm: + actionInfo["S"] = AscPDF.ACTIONS_TYPES.ResetForm; + actionInfo["Fields"] = oAction.GetNames(); + actionInfo["Flags"] = Number(oAction.GetNeedAllExcept()); + break; + case AscPDF.ACTIONS_TYPES.URI: + actionInfo["S"] = AscPDF.ACTIONS_TYPES.URI; + actionInfo["URI"] = oAction.GetURI(); + break; + case AscPDF.ACTIONS_TYPES.HideShow: + actionInfo["S"] = AscPDF.ACTIONS_TYPES.HideShow; + actionInfo["H"] = oAction.GetHidden(); + actionInfo["T"] = oAction.GetNames(); + break; + case AscPDF.ACTIONS_TYPES.GoTo: + actionInfo["S"] = AscPDF.ACTIONS_TYPES.GoTo; + actionInfo["page"] = oAction.GetPage(); + actionInfo["kind"] = oAction.GetKind(); + actionInfo["zoom"] = oAction.GetZoom(); + let oRect = oAction.GetRect(); + actionInfo["top"] = oRect.top; + actionInfo["right"] = oRect.right; + actionInfo["bottom"] = oRect.bottom; + actionInfo["left"] = oRect.left; + break; + case AscPDF.ACTIONS_TYPES.Named: + actionInfo["S"] = AscPDF.ACTIONS_TYPES.Named; + actionInfo["N"] = oAction.GetName(); + break; + default: + // If the type is not recognized, add handling or skip + break; + } + + aActionsInfo.push(actionInfo); + } + + return aActionsInfo; + }; + /** * Sets the JavaScript action of the field for a given trigger. diff --git a/pdf/src/forms/formActions.js b/pdf/src/forms/formActions.js index c9a51d4aa0..6c085da6f2 100644 --- a/pdf/src/forms/formActions.js +++ b/pdf/src/forms/formActions.js @@ -268,6 +268,10 @@ return this.goToType; }; + CActionGoTo.prototype.GetRect = function() { + return this.rect; + }; + CActionGoTo.prototype.Do = function() { let oViewer = editor.getDocumentRenderer(); let oDoc = this.field.GetDocument(); @@ -493,6 +497,13 @@ oDoc.HideShowForms(this.hidden, this.names); }; + CActionHideShow.prototype.GetNames = function() { + return this.names; + }; + CActionHideShow.prototype.GetHidden = function() { + return this.hidden; + }; + CActionHideShow.prototype.WriteToBinary = function(memory) { memory.WriteByte(this.GetType()); if (this.hidden) @@ -531,6 +542,13 @@ oDoc.ResetForms(this.names, this.bAllExcept); }; + CActionReset.prototype.GetNames = function() { + return this.names; + }; + CActionReset.prototype.GetNeedAllExcept = function() { + return this.bAllExcept; + }; + CActionReset.prototype.WriteToBinary = function(memory) { memory.WriteByte(this.GetType()); @@ -600,6 +618,10 @@ } }; + CActionRunScript.prototype.GetScript = function() { + return this.script; + } + CActionRunScript.prototype.WriteToBinary = function(memory) { memory.WriteByte(this.GetType()); memory.WriteString(this.script); diff --git a/pdf/src/history/formsChanges.js b/pdf/src/history/formsChanges.js index cfeb2f4f90..678c26aab7 100644 --- a/pdf/src/history/formsChanges.js +++ b/pdf/src/history/formsChanges.js @@ -466,6 +466,90 @@ CChangesPDFFormRect.prototype.private_SetValue = function(Value) oField.SetRect(Value); }; +/** + * @constructor + * @extends {AscDFH.CChangesBaseStringProperty} + */ +function CChangesPDFFormActions(Class, oOldActionsInfo, oNewActionsInfo, nTriggerType, Color) +{ + AscDFH.CChangesBaseStringProperty.call(this, Class, JSON.stringify(oOldActionsInfo), JSON.stringify(oNewActionsInfo), Color); + this.TriggerType = nTriggerType; +} +CChangesPDFFormActions.prototype = Object.create(AscDFH.CChangesBaseStringProperty.prototype); +CChangesPDFFormActions.prototype.constructor = CChangesPDFFormActions; +CChangesPDFFormActions.prototype.Type = AscDFH.historyitem_Pdf_Pushbutton_Image; +CChangesPDFFormActions.prototype.CreateReverseChange = function() { + return new this.constructor(this.Class, this.New, this.Old, this.TriggerType, this.Color); +}; +CChangesPDFFormActions.prototype.private_SetValue = function(Value) +{ + let oField = this.Class; + oField.SetActions(this.TriggerType, JSON.parse(Value)); +}; + +CChangesPDFFormActions.prototype.WriteToBinary = function(Writer) +{ + let nFlags = 0; + + if (false !== this.Color) + nFlags |= 1; + + if (undefined === this.TriggerType) + nFlags |= 2; + + if (undefined === this.New) + nFlags |= 4; + + if (undefined === this.Old) + nFlags |= 8; + + + Writer.WriteLong(nFlags); + + if (undefined !== this.TriggerType) + Writer.WriteLong(this.TriggerType); + + if (undefined !== this.New) + Writer.WriteString2(this.New); + + if (undefined !== this.Old) + Writer.WriteString2(this.Old); +}; +CChangesPDFFormActions.prototype.ReadFromBinary = function(Reader) +{ + this.FromLoad = true; + + // Long : Flag + // 1-bit : Подсвечивать ли данные изменения + // 2-bit : IsUndefined New + // 3-bit : IsUndefined Old + // long : New + // long : Old + + + var nFlags = Reader.GetLong(); + + if (nFlags & 1) + this.Color = true; + else + this.Color = false; + + if (nFlags & 2) + this.TriggerType = undefined; + else + this.TriggerType = Reader.GetLong(); + + if (nFlags & 4) + this.New = undefined; + else + this.New = Reader.GetString2(); + + if (nFlags & 8) + this.Old = undefined; + else + this.Old = Reader.GetString2(); +}; + //------------------------------------------------------------------------------------------------------------------ // // Text Form