Skip to content

Commit

Permalink
Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
KhromovNikita committed Feb 18, 2025
1 parent 679ca9e commit 5269fc7
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 10 deletions.
84 changes: 74 additions & 10 deletions pdf/src/forms/base/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions pdf/src/forms/formActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -600,6 +618,10 @@
}
};

CActionRunScript.prototype.GetScript = function() {
return this.script;
}

CActionRunScript.prototype.WriteToBinary = function(memory) {
memory.WriteByte(this.GetType());
memory.WriteString(this.script);
Expand Down
84 changes: 84 additions & 0 deletions pdf/src/history/formsChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5269fc7

Please sign in to comment.