Compare commits

...

10 Commits

5 changed files with 401 additions and 18 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

136
api.js
View File

@ -34,7 +34,6 @@
(function(window, document) {
window['Asc']['Addons'] = window['Asc']['Addons'] || {};
window['Asc']['Addons']['forms'] = true; // register addon
window['Asc']['asc_docs_api'].prototype['asc_AddContentControlCheckBox'] = window['Asc']['asc_docs_api'].prototype.asc_AddContentControlCheckBox = function(oPr, oFormPr, oCommonPr)
{
var oLogicDocument = this.private_GetLogicDocument();
@ -85,6 +84,7 @@
{
oCC.SetFormPr(oFormPr);
oCC.UpdatePlaceHolderTextPrForForm();
private_CheckFormKey(oCC, oLogicDocument);
}
if (oCommonPr)
@ -175,32 +175,34 @@
oLogicDocument.StartAction(AscDFH.historydescription_Document_AddContentControlPicture);
var oCC = oLogicDocument.AddContentControlPicture();
let oFormParaDrawing = null;
if (oCC && oFormPr)
{
oCC.SetFormPr(oFormPr);
oCC.UpdatePlaceHolderTextPrForForm();
oCC.ConvertFormToFixed();
private_CheckFormKey(oCC, oLogicDocument);
oFormParaDrawing = oCC.ConvertFormToFixed();
oCC.SetPictureFormPr(new AscCommon.CSdtPictureFormPr());
oCC.SelectContentControl();
var aDrawings = oCC.GetAllDrawingObjects();
for(var nDrawing = 0; nDrawing < aDrawings.length; ++nDrawing)
for(var nDrawing = 0; nDrawing < aDrawings.length; ++nDrawing)
{
var oDrawing = aDrawings[nDrawing];
var oGraphic = oDrawing.GraphicObj;
if(oGraphic && oGraphic.getObjectType() === AscDFH.historyitem_type_ImageShape)
if(oGraphic && oGraphic.getObjectType() === AscDFH.historyitem_type_ImageShape)
{
var oSpPr = oGraphic.spPr;
if(oSpPr)
if(oSpPr)
{
if(oSpPr.Fill)
if(oSpPr.Fill)
{
oSpPr.setFill(null);
}
if(oSpPr.ln)
if(oSpPr.ln)
{
oSpPr.setLn(null);
}
if(oSpPr.geometry)
if(oSpPr.geometry)
{
oSpPr.setGeometry(null);
}
@ -240,7 +242,15 @@
oLogicDocument.UpdateInterface();
oLogicDocument.Recalculate();
oLogicDocument.FinalizeAction();
if(oFormParaDrawing)
{
let oFormShape = oFormParaDrawing.GraphicObj;
if(oFormShape)
{
oFormShape.Set_CurrentElement(true, null, true);
}
}
oLogicDocument.FinalizeAction();
}
};
window['Asc']['asc_docs_api'].prototype['asc_AddContentControlList'] = window['Asc']['asc_docs_api'].prototype.asc_AddContentControlList = function(isComboBox, oPr, oFormPr, oCommonPr)
@ -264,6 +274,7 @@
{
oCC.SetFormPr(oFormPr);
oCC.UpdatePlaceHolderTextPrForForm();
private_CheckFormKey(oCC, oLogicDocument);
}
if (oCC && oCommonPr)
@ -296,21 +307,32 @@
oLogicDocument.FinalizeAction();
}
};
window['Asc']['asc_docs_api'].prototype['asc_AddContentControlTextForm'] = window['Asc']['asc_docs_api'].prototype.asc_AddContentControlTextForm = function(oPr, oFormPr)
window['Asc']['asc_docs_api'].prototype['asc_AddContentControlTextForm'] = window['Asc']['asc_docs_api'].prototype.asc_AddContentControlTextForm = function(contentControlPr)
{
var oLogicDocument = this.private_GetLogicDocument();
if (!oLogicDocument)
return;
let textFormPr = contentControlPr ? contentControlPr.TextFormPr : null;
let formPr = contentControlPr ? contentControlPr.FormPr : null;
let placeholderText = contentControlPr ? contentControlPr.PlaceholderText : "";
if (!oLogicDocument.IsSelectionLocked(AscCommon.changestype_Paragraph_Content))
{
oLogicDocument.StartAction(AscDFH.historydescription_Document_AddContentControlTextForm);
var oCC = oLogicDocument.AddContentControlTextForm(oPr);
if (oCC && oFormPr)
var oCC = oLogicDocument.AddContentControlTextForm(textFormPr);
if (oCC)
{
oCC.SetFormPr(oFormPr);
oCC.UpdatePlaceHolderTextPrForForm();
if (placeholderText)
oCC.SetPlaceholderText(placeholderText);
if (formPr)
{
oCC.SetFormPr(formPr);
oCC.UpdatePlaceHolderTextPrForForm();
private_CheckFormKey(oCC, oLogicDocument);
}
}
oLogicDocument.UpdateInterface();
@ -318,5 +340,89 @@
oLogicDocument.FinalizeAction();
}
};
window['Asc']['asc_docs_api'].prototype['asc_AddComplexForm'] = window['Asc']['asc_docs_api'].prototype.asc_AddComplexForm = function(json, formPr)
{
let logicDocument = this.private_GetLogicDocument();
if (!logicDocument)
return;
function AddComplexForm()
{
if (!logicDocument.IsSelectionLocked(AscCommon.changestype_Paragraph_Content))
{
logicDocument.StartAction(AscDFH.historydescription_Document_AddComplexForm);
let complexForm = logicDocument.AddComplexForm(new AscWord.CSdtComplexFormPr(), formPr);
private_CheckFormKey(complexForm, logicDocument);
if (json)
AscWord.JsonToForm(json, complexForm);
logicDocument.UpdateInterface();
logicDocument.Recalculate();
logicDocument.FinalizeAction();
}
}
if (json)
{
AscFonts.FontPickerByCharacter.checkText(AscWord.GetUnicodesFromJsonToForm(), this, function() {
AddComplexForm();
}, true);
}
else
{
AddComplexForm();
}
};
window['Asc']['asc_docs_api'].prototype['asc_GetCurrentComplexForm'] = window['Asc']['asc_docs_api'].prototype.asc_GetCurrentComplexForm = function()
{
let logicDocument = this.private_GetLogicDocument();
if (!logicDocument)
return null;
let form = logicDocument.GetContentControl();
if (!form || !form.IsForm())
return null;
let mainForm = form.GetMainForm();
return (mainForm.IsComplexForm() ? mainForm : null);
};
window['Asc']['asc_docs_api'].prototype['asc_ConvertFormToJson'] = window['Asc']['asc_docs_api'].prototype.asc_ConvertFormToJson = function(form)
{
let logicDocument = this.private_GetLogicDocument();
if (!logicDocument)
return null;
if (typeof (form) === "string")
form = AscCommon.g_oTableId.Get_ById(form);
if (!form || !(form instanceof AscWord.CInlineLevelSdt) || !form.IsForm())
return null;
return AscWord.FormToJson(form);
};
function private_CheckFormKey(form, logicDocument)
{
if (!form || !form.IsForm() || !logicDocument)
return;
let key = form.GetFormKey();
if (key && "" !== key.trim())
return;
let formManager = logicDocument.GetFormsManager();
let keyGenerator = formManager.GetKeyGenerator();
let formPr = form.GetFormPr().Copy();
if (!formPr)
return;
key = keyGenerator.GetNewKey(form);
formPr.SetKey(key);
form.SetFormPr(formPr);
}
})(window, window.document);

View File

@ -156,7 +156,7 @@
oPr.SetWidth((GetNumberParameter(oFormPr["cellWidth"], 0) * 72 * 20 / 25.4) | 0);
oCC.ApplyTextFormPr(oPr);
CheckFormKey(oCC);
return new AscBuilder.ApiTextForm(oCC);
};
/**
@ -226,6 +226,7 @@
private_PerformAddCheckBox();
}
CheckFormKey(oCC);
return new AscBuilder.ApiCheckBoxForm(oCC);
};
/**
@ -290,6 +291,7 @@
}
}
CheckFormKey(oCC);
return new AscBuilder.ApiComboBoxForm(oCC);
};
/**
@ -328,6 +330,7 @@
oCC.SetPictureFormPr(oPr);
CheckFormKey(oCC);
return new AscBuilder.ApiPictureForm(oCC);
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -357,6 +360,27 @@
return oCC;
}
function CheckFormKey(form)
{
let logicDocument = editor && editor.WordControl && editor.WordControl.m_oLogicDocument;
if (!form || !form.IsForm() || !logicDocument)
return;
let key = form.GetFormKey();
if (key && "" !== key.trim())
return;
let formManager = logicDocument.GetFormsManager();
let keyGenerator = formManager.GetKeyGenerator();
let formPr = form.GetFormPr().Copy();
if (!formPr)
return;
key = keyGenerator.GetNewKey(form);
formPr.SetKey(key);
form.SetFormPr(formPr);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Export
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

251
apiPlugins.js Normal file
View File

@ -0,0 +1,251 @@
/*
* (c) Copyright Ascensio System SIA 2010-2022
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
"use strict";
(function(window)
{
/**
* Returns information about all the forms that have been added to the document
* @memberof Api
* @typeofeditors ["CDE"]
* @alias GetAllForms
* @returns {[]} - An array of forms
* @example
* window.Asc.plugin.executeMethod("GetAllForms");
*/
window["asc_docs_api"].prototype["pluginMethod_GetAllForms"] = function()
{
let oFormsManager = this.private_GetFormsManager();
if (!oFormsManager)
return [];
let arrForms = oFormsManager.GetAllForms();
let arrResult = [];
for (let nIndex = 0, nCount = arrForms.length; nIndex < nCount; ++nIndex)
{
arrResult.push(arrForms[nIndex].GetContentControlPr().GetEventObject());
}
return arrResult;
};
/**
* Returns information about all the forms that have been added to the document with specified Tag
* @memberof Api
* @typeofeditors ["CDE"]
* @alias GetFormsByTag
* @param {string} tag - Tag of the form
* @returns {[]} - An array of forms
* @example
* window.Asc.plugin.executeMethod("GetFormsByTag");
*/
window["asc_docs_api"].prototype["pluginMethod_GetFormsByTag"] = function(tag)
{
let oFormsManager = this.private_GetFormsManager();
if (!oFormsManager)
return [];
let arrForms = oFormsManager.GetAllForms();
let arrResult = [];
let oForm = null;
for (let nIndex = 0, nCount = arrForms.length; nIndex < nCount; ++nIndex)
{
oForm = arrForms[nIndex];
if (oForm.GetTag() === tag)
arrResult.push(oForm.GetContentControlPr().GetEventObject());
}
return arrResult;
};
/**
* Set value for specified form
* @memberof Api
* @typeofeditors ["CDE"]
* @alias SetFormValue
* @param {string} internalId - A unique internal identifier of the content control.
* @param {*} value - Depends on the type of a form
* @example
* window.Asc.plugin.executeMethod("SetFormValue");
*/
window["asc_docs_api"].prototype["pluginMethod_SetFormValue"] = function(internalId, value)
{
let oLogicDocument = this.private_GetLogicDocument();
if (!AscCommon.g_oTableId
|| !oLogicDocument
|| !oLogicDocument.IsDocumentEditor())
return;
let oForm = AscCommon.g_oTableId.GetClass(internalId);
if (!oForm
|| !(oForm instanceof AscWord.CInlineLevelSdt)
|| !oForm.IsForm())
return;
let oParagraph = oForm.GetParagraph();
oForm.SkipSpecialContentControlLock(true);
if (!oParagraph
|| oLogicDocument.IsSelectionLocked(AscCommon.changestype_None, {
Type : AscCommon.changestype_2_ElementsArray_and_Type,
Elements : [oParagraph],
CheckType : AscCommon.changestype_Paragraph_Content
}, true, oLogicDocument.IsFillingFormMode()))
{
oForm.SkipSpecialContentControlLock(false);
return;
}
oForm.SkipSpecialContentControlLock(false);
oLogicDocument.StartAction(AscDFH.historydescription_Document_FillFormInPlugin);
let isClear = false;
if (null === value)
{
isClear = true;
}
else if (oForm.IsTextForm() || oForm.IsComboBox())
{
let sValue = AscBuilder.GetStringParameter(value, "");
if (!value)
isClear = true;
else
oForm.SetInnerText(sValue);
}
else if (oForm.IsDropDownList())
{
let sValue = AscBuilder.GetStringParameter(value, "");
let oPr = oForm.GetDropDownListPr();
let nIndex = oPr.FindByText(sValue);
if (-1 !== nIndex)
oForm.SelectListItem(oPr.GetItemValue(nIndex));
else
isClear = true;
}
else if (oForm.IsCheckBox())
{
let isChecked = value === "true" ? true : value === "false" ? false : AscBuilder.GetBoolParameter(value, null);
if (null !== isChecked)
oForm.SetCheckBoxChecked(isChecked);
else
isClear = true;
}
else if (oForm.IsPictureForm())
{
let sValue = AscBuilder.GetStringParameter(value, "");
if (!sValue)
return;
let oImg;
let allDrawings = oForm.GetAllDrawingObjects();
for (let nDrawing = 0; nDrawing < allDrawings.length; ++nDrawing)
{
if (allDrawings[nDrawing].IsPicture())
{
oImg = allDrawings[nDrawing].GraphicObj;
break;
}
}
if (oImg)
{
oForm.SetShowingPlcHdr(false);
oImg.setBlipFill(AscFormat.CreateBlipFillRasterImageId(sValue));
}
else
{
isClear = true;
}
}
if (isClear)
oForm.ClearContentControlExt();
oLogicDocument.OnChangeForm(oForm);
oLogicDocument.Recalculate();
oLogicDocument.UpdateTracks();
oLogicDocument.FinalizeAction();
};
/**
* Get form value
* @memberof Api
* @typeofeditors ["CDE"]
* @alias GetFormValue
* @param {string} internalId - A unique internal identifier of the content control.
* @returns {null | string} Value of the form (null means the form is filled with a placeholder)
* @example
* window.Asc.plugin.executeMethod("GetFormValue");
*
*/
window["asc_docs_api"].prototype["pluginMethod_GetFormValue"] = function(internalId)
{
if (!AscCommon.g_oTableId)
return "";
let oForm = AscCommon.g_oTableId.GetClass(internalId);
if (!oForm
|| !(oForm instanceof AscWord.CInlineLevelSdt)
|| !oForm.IsForm())
return "";
if (oForm.IsPlaceHolder())
return null;
if (oForm.IsTextForm() || oForm.IsComboBox() || oForm.IsDropDownList())
{
return oForm.GetInnerText();
}
else if (oForm.IsCheckBox())
{
return oForm.IsCheckBoxChecked();
}
else if (oForm.IsPictureForm())
{
let oImg;
let allDrawings = oForm.GetAllDrawingObjects();
for (let nDrawing = 0; nDrawing < allDrawings.length; ++nDrawing)
{
if (allDrawings[nDrawing].IsPicture())
{
oImg = allDrawings[nDrawing].GraphicObj;
break;
}
}
if (oImg)
return oImg.getBase64Img();
}
return "";
};
})(window);

View File

@ -1,8 +1,9 @@
{
"sdk": {
"min": [
"api.js"
],
"api.js",
"apiPlugins.js"
],
"common": [
"apiBuilder.js"
]