Add support custom functions in builder scripts

This commit is contained in:
Oleg Korshul
2024-08-07 12:48:43 +03:00
parent 673a0fa377
commit da678b8e58
2 changed files with 76 additions and 2 deletions

View File

@ -427,7 +427,6 @@
* @memberof Api
* @typeofeditors ["CSE"]
* @param {Function} fCustom - A new function for calculating.
* @see office-js-api/Examples/{Editor}/Api/Methods/AddCustomFunction.js
*/
// Example with description:
// Calculates the sum of the specified numbers.
@ -522,6 +521,21 @@
this.addCustomFunction(fCustom, parsedJSDoc/*isValidJsDoc ? parsedJSDoc : options*/);
};
/**
* Register a new custom functions library (see SetCustomFunctions plugin method).
* The description of the function parameters and result is specified using JSDoc. The <em>@customfunction</em> tag is required in JSDoc.
* Parameters and results can be specified as the <em>number / string / bool / any / number[][] / string[][] / bool[][] / any[][]</em> types.
* Parameters can be required or optional. A user can also set a default value.
* @memberof Api
* @typeofeditors ["CSE"]
* @param {string} sName - A name of library
* @param {Function} Func - A custom functions library code.
* @see office-js-api/Examples/{Editor}/Api/Methods/AddCustomFunction.js
*/
Api.prototype.AddCustomFunctionLibrary = function(sName, Func) {
this.addCustomFunctionsLibrary(sName, Func);
};
/**
* Removes a custom function.
* @memberof Api
@ -13456,6 +13470,7 @@
Api.prototype["AddCustomFunction"] = Api.prototype.AddCustomFunction;
Api.prototype["RemoveCustomFunction"] = Api.prototype.RemoveCustomFunction;
Api.prototype["AddCustomFunctionLibrary"] = Api.prototype.AddCustomFunctionLibrary;
Api.prototype["GetReferenceStyle"] = Api.prototype.GetReferenceStyle;
Api.prototype["SetReferenceStyle"] = Api.prototype.SetReferenceStyle;

View File

@ -157,7 +157,14 @@
{
// DISABLE FOR NATIVE VERSION
if (window["NATIVE_EDITOR_ENJINE"])
return;
{
if (!window.localStorage)
{
window.localStorage = {};
window.localStorage.getItem = function(key) { return this[key]; };
window.localStorage.setItem = function(key, value) { this[key] = value; };
}
}
if (undefined === obj)
obj = AscCommon.getLocalStorageItem(customFunctionsStorageId);
@ -185,6 +192,58 @@
this.recalculateCustomFunctions();
};
Api.prototype.addCustomFunctionsLibrary = function(sName, Func)
{
// DISABLE FOR NATIVE VERSION
if (window["NATIVE_EDITOR_ENJINE"])
{
if (!window.localStorage)
{
window.localStorage = {};
window.localStorage.getItem = function(key) { return this[key]; };
window.localStorage.setItem = function(key, value) { this[key] = value; };
}
}
let currentValue = AscCommon.getLocalStorageItem(customFunctionsStorageId);
let libraryString = "(" + Func.toString() + ")()";
if (!currentValue)
{
currentValue = {
"macrosArray" : [{
"name": sName,
"value": libraryString
}]
};
}
else
{
let arr = currentValue["macrosArray"];
if (arr)
{
let isChanged = false;
for (let i = 0, len = arr.length; i < len; i++)
{
if (arr[i]["name"] === sName)
{
isChanged = true;
arr[i]["value"] = libraryString;
}
}
if (!isChanged)
{
arr.push({
"name" : sName,
"value" : libraryString
});
}
}
}
AscCommon.setLocalStorageItem(customFunctionsStorageId, currentValue);
this.registerCustomFunctionsLibrary(currentValue);
};
/**
* Returns a library of local custom functions.
* @memberof Api