add methods for working with ole-objects in plugins

This commit is contained in:
Sergey Luzyanin
2022-02-08 10:14:30 +03:00
parent 95aa1d2991
commit 148ed60fcf
12 changed files with 365 additions and 52 deletions

View File

@ -849,24 +849,10 @@ CShape.prototype.Get_Worksheet = function()
CShape.prototype.Set_CurrentElement = function()
{
var drawing_objects = this.getDrawingObjectsController();
if(drawing_objects)
{
drawing_objects.resetSelection(true);
if(this.group)
{
var main_group = this.group.getMainGroup();
drawing_objects.selectObject(main_group, 0);
main_group.selectObject(this, 0);
main_group.selection.textSelection = this;
drawing_objects.selection.groupSelection = main_group;
}
else
{
drawing_objects.selectObject(this, 0);
drawing_objects.selection.textSelection = this;
}
this.SetControllerTextSelection(drawing_objects, 0);
}
};

View File

@ -2931,6 +2931,68 @@ CBlipFill.prototype =
_ret.rotWithShape = this.rotWithShape;
}
return _ret;
},
getBase64RasterImageId: function (bReduce)
{
var sRasterImageId = this.RasterImageId;
if(typeof sRasterImageId !== "string" || sRasterImageId.length === 0)
{
return null;
}
if(sRasterImageId.indexOf("data:") === 0 && sRasterImageId.index("base64") > 0)
{
return sRasterImageId;
}
var oApi = Asc.editor || editor;
var sDefaultResult = sRasterImageId;
if(!oApi)
{
return sDefaultResult;
}
var oImageLoader = oApi.ImageLoader;
if(!oImageLoader)
{
return sDefaultResult;
}
var oImage = oImageLoader.map_image_index[AscCommon.getFullImageSrc2(sRasterImageId)];
if(!oImage || !oImage.Image || oImage.Status !== AscFonts.ImageLoadStatus.Complete)
{
return sDefaultResult;
}
var sResult = sDefaultResult;
if(!window["NATIVE_EDITOR_ENJINE"])
{
var oCanvas = document.createElement("canvas");
var nW = Math.max(oImage.Image.width, 1);
var nH = Math.max(oImage.Image.height, 1);
if(bReduce)
{
var nMaxSize = 640;
var dWK = nW/nMaxSize;
var dHK = nH/nMaxSize;
var dK = Math.max(dWK, dHK);
if(dK > 1)
{
nW = ((nW / dK) + 0.5 >> 0);
nH = ((nH / dK) + 0.5 >> 0);
}
}
oCanvas.width = nW;
oCanvas.height = nH;
var oCtx = oCanvas.getContext("2d");
oCtx.drawImage(oImage.Image, 0, 0, oCanvas.width, oCanvas.height);
try
{
sResult = oCanvas.toDataURL("image/png");
}
catch (err)
{
sResult = sDefaultResult;
}
return sResult;
}
return sRasterImageId;
}
};

View File

@ -1769,6 +1769,8 @@
};
CGraphicObjectBase.prototype.GetAllContentControls = function(arrContentControls) {};
CGraphicObjectBase.prototype.GetAllDrawingObjects = function(arrDrawingObjects) {};
CGraphicObjectBase.prototype.GetAllOleObjects = function(sPluginId, arrObjects) {
};
CGraphicObjectBase.prototype.CheckContentControlEditingLock = function () {
if(this.group){
this.group.CheckContentControlEditingLock();
@ -2458,6 +2460,36 @@
}
return this;
};
CGraphicObjectBase.prototype.Set_CurrentElement = function(bUpdate, pageIndex) {
//TODO: refactor this
if(AscFormat.CShape.prototype.Set_CurrentElement) {
AscFormat.CShape.prototype.Set_CurrentElement.call(this, bUpdate, pageIndex);
}
};
CGraphicObjectBase.prototype.SetControllerTextSelection = function(drawing_objects, nPageIndex) {
if(drawing_objects) {
var oContent = this.getDocContent && this.getDocContent();
drawing_objects.resetSelection(true);
if(this.group) {
var main_group = this.group.getMainGroup();
drawing_objects.selectObject(main_group, nPageIndex);
main_group.selectObject(this, nPageIndex);
if(oContent) {
main_group.selection.textSelection = this;
}
drawing_objects.selection.groupSelection = main_group;
}
else {
drawing_objects.selectObject(this, nPageIndex);
if(oContent) {
drawing_objects.selection.textSelection = this;
}
}
}
};
CGraphicObjectBase.prototype.hitInBoundingRect = function (x, y) {
if(this.parent && this.parent.kind === AscFormat.TYPE_KIND.NOTES){
return false;
@ -2565,9 +2597,6 @@
}
return false;
};
CGraphicObjectBase.prototype.canEditText = function() {
if(this.getObjectType() === AscDFH.historyitem_type_Shape) {
if(!AscFormat.CheckLinePresetForParagraphAdd(this.getPresetGeom()) && !this.signatureLine) {
@ -2592,7 +2621,6 @@
CGraphicObjectBase.prototype.applySmartArtTextStyle = function() {
};
CGraphicObjectBase.prototype.convertFromSmartArt = function() {
return this;
};

View File

@ -1962,10 +1962,14 @@ function CGroupShape()
this.spTree[nSp].applySmartArtTextStyle();
}
};
CGroupShape.prototype.getTypeName = function() {
return AscCommon.translateManager.getValue("Group");
};
CGroupShape.prototype.GetAllOleObjects = function(sPluginId, arrObjects) {
for(let nSp = 0; nSp < this.spTree.length; ++nSp) {
this.spTree[nSp].GetAllOleObjects(sPluginId, arrObjects);
}
};
//--------------------------------------------------------export----------------------------------------------------
window['AscFormat'] = window['AscFormat'] || {};
window['AscFormat'].CGroupShape = CGroupShape;

View File

@ -378,6 +378,42 @@ function (window, undefined) {
}
}
};
window['AscFormat'] = window['AscFormat'] || {};
window['AscFormat'].COleObject = COleObject;
COleObject.prototype.GetAllOleObjects = function(sPluginId, arrObjects) {
if(typeof sPluginId === "string" && sPluginId.length > 0) {
if(sPluginId === this.m_sApplicationId) {
arrObjects.push(this);
}
}
else {
arrObjects.push(this);
}
};
COleObject.prototype.getDataObject = function() {
var dWidth = 0, dHeight = 0;
if(this.parent && this.parent.Extent) {
var oExtent = this.parent.Extent;
dWidth = oExtent.W;
dHeight = oExtent.H;
}
else {
if(this.spPr && this.spPr.xfrm) {
var oXfrm = this.spPr.xfrm;
dWidth = oXfrm.extX;
dHeight = oXfrm.extY;
}
}
var oBlipFill = this.blipFill;
return {
"Data": this.m_sData,
"ApplicationId": this.m_sApplicationId,
"ImageData": oBlipFill ? oBlipFill.getBase64RasterImageId(false) : "",
"Width": dWidth,
"Height": dHeight,
"WidthPix": this.m_nPixWidth,
"HeightPix": this.m_nPixHeight,
"InternalId": this.Id
}
};
window['AscFormat'] = window['AscFormat'] || {};
window['AscFormat'].COleObject = COleObject;
})(window);

View File

@ -1905,7 +1905,7 @@
var sGuid = oPluginData["guid"];
if (typeof sImgSrc === "string" && sImgSrc.length > 0 && typeof sData === "string"
&& typeof sGuid === "string" && sGuid.length > 0
&& AscFormat.isRealNumber(nWidthPix) && AscFormat.isRealNumber(nHeightPix)
/*&& AscFormat.isRealNumber(nWidthPix) && AscFormat.isRealNumber(nHeightPix)*/
&& AscFormat.isRealNumber(fWidth) && AscFormat.isRealNumber(fHeight)
)

View File

@ -613,18 +613,7 @@ CShape.prototype.getIsSingleBody = function(x, y)
CShape.prototype.Set_CurrentElement = function(bUpdate, pageIndex){
if(this.parent && this.parent.graphicObjects){
var drawing_objects = this.parent.graphicObjects;
drawing_objects.resetSelection(true);
if(this.group){
var main_group = this.group.getMainGroup();
drawing_objects.selectObject(main_group, 0);
main_group.selectObject(this, 0);
main_group.selection.textSelection = this;
drawing_objects.selection.groupSelection = main_group;
}
else{
drawing_objects.selectObject(this, 0);
drawing_objects.selection.textSelection = this;
}
this.SetControllerTextSelection(drawing_objects, 0);
var nSlideNum;
if(this.parent instanceof AscCommonSlide.CNotes){
editor.WordControl.m_oLogicDocument.FocusOnNotes = true;

View File

@ -6731,6 +6731,58 @@ CDocument.prototype.RemoveBeforePaste = function()
else
this.Remove(1, true, true, true);
};
CDocument.prototype.RemoveDrawingObjectById = function(sObjectId)
{
var oState = this.SaveDocumentState();
var oDrawing = AscCommon.g_oTableId.Get_ById(sObjectId);
oDrawing.Set_CurrentElement(false, null);
if (false === this.Document_Is_SelectionLocked(AscCommon.changestype_Remove, null, true, this.IsFormFieldEditing()))
{
this.StartAction(AscDFH.historydescription_Document_BackSpaceButton);
this.Remove(-1, true, false, false, false, true);
this.FinalizeAction();
}
this.LoadDocumentState(oState);
};
CDocument.prototype.RemoveDrawingObjects = function(arrObjectsId)
{
var oState = this.SaveDocumentState();
var oDrawing = AscCommon.g_oTableId.Get_ById(arrObjectsId[0]);
if(!oDrawing)
{
return;
}
oDrawing.Set_CurrentElement(false, null);
var aObjectsToDelete = [];
for(var nObject = 0; nObject < arrObjectsId.length; ++nObject)
{
oDrawing = AscCommon.g_oTableId.Get_ById(arrObjectsId[nObject]);
if(oDrawing)
{
aObjectsToDelete.push(oDrawing);
if(oDrawing.group)
{
oDrawing.getMainGroup().select(this.DrawingObjects);
}
else
{
oDrawing.select(this.DrawingObjects);
}
}
}
if (false === this.Document_Is_SelectionLocked(AscCommon.changestype_Remove, null, true, this.IsFormFieldEditing()))
{
this.StartAction(AscDFH.historydescription_Document_BackSpaceButton);
for(nObject = 0; nObject < aObjectsToDelete.length; ++nObject)
{
oDrawing = aObjectsToDelete[nObject];
oDrawing.Set_CurrentElement(false, null);
this.Remove(-1, true, false, false, false, true);
}
this.FinalizeAction();
}
this.LoadDocumentState(oState);
};
CDocument.prototype.GetCursorPosXY = function()
{
return this.Controller.GetCursorPosXY();

View File

@ -2230,3 +2230,23 @@ CDocumentContentBase.prototype.IsEmptyParagraphAfterTableInTableCell = function(
CDocumentContentBase.prototype.SetThisElementCurrent = function(isUpdateStates)
{
};
/**
* Method for getting all
* @param {string} sPluginId - id of plugin which can edit those ole-objects, if it's not specified return all ole-objects
* @param {Array} arrObjects - create array if not specified
* @returns {Array}
*/
CDocumentContentBase.prototype.GetAllOleObjects = function(sPluginId, arrObjects)
{
if (Array.isArray(arrObjects))
{
arrObjects = [];
}
let arrDrawings = this.GetAllDrawingObjects([]);
for(let nDrawing = 0; nDrawing < arrDrawings.length; ++nDrawing)
{
arrDrawings[nDrawing].GetAllOleObjects(sPluginId, arrObjects)
}
return arrObjects;
};

View File

@ -916,28 +916,18 @@ CShape.prototype.Set_CurrentElement = function(bUpdate, pageIndex)
para_drawing = this.parent;
}
if (para_drawing && para_drawing.DocumentContent)
{
var nPageIndex = AscFormat.isRealNumber(pageIndex) ? pageIndex : para_drawing.PageNum;
var drawing_objects = oLogicDoc.DrawingObjects;
drawing_objects.resetSelection(true);
if (this.group)
{
var main_group = this.group.getMainGroup();
drawing_objects.selectObject(main_group, pageIndex);
main_group.selectObject(this, pageIndex);
main_group.selection.textSelection = this;
drawing_objects.selection.groupSelection = main_group;
}
else
{
drawing_objects.selectObject(this, pageIndex);
drawing_objects.selection.textSelection = this;
}
this.SetControllerTextSelection(drawing_objects, nPageIndex);
var hdr_ftr = para_drawing.DocumentContent.IsHdrFtr(true);
if (hdr_ftr)
{
hdr_ftr.Content.SetDocPosType(docpostype_DrawingObjects);
hdr_ftr.Content.SetDocPosType(AscCommonWord.docpostype_DrawingObjects);
hdr_ftr.Set_CurrentElement(bUpdate);
}
else
@ -946,7 +936,7 @@ CShape.prototype.Set_CurrentElement = function(bUpdate, pageIndex)
var nOldDocPosType = oDocument.GetDocPosType();
drawing_objects.document.SetDocPosType(docpostype_DrawingObjects);
drawing_objects.document.SetDocPosType(AscCommonWord.docpostype_DrawingObjects);
drawing_objects.document.Selection.Use = true;
if (true === bUpdate)
@ -956,7 +946,7 @@ CShape.prototype.Set_CurrentElement = function(bUpdate, pageIndex)
drawing_objects.document.Document_UpdateSelectionState();
}
if (docpostype_HdrFtr === nOldDocPosType && oDocument.Redraw)
if (AscCommonWord.docpostype_HdrFtr === nOldDocPosType && oDocument.Redraw)
oDocument.Redraw(-1, -1);
}
}

View File

@ -271,6 +271,18 @@ ParaDrawing.prototype.GetAllDrawingObjects = function(arrDrawingObjects)
return arrDrawingObjects;
};
ParaDrawing.prototype.GetAllOleObjects = function(sPluginId, arrObjects)
{
if (!Array.isArray(arrObjects))
{
arrObjects = [];
}
if (this.GraphicObj.GetAllOleObjects)
this.GraphicObj.GetAllOleObjects(sPluginId, arrObjects);
return arrObjects;
};
ParaDrawing.prototype.canRotate = function()
{
return AscCommon.isRealObject(this.GraphicObj) && typeof this.GraphicObj.canRotate == "function" && this.GraphicObj.canRotate();

View File

@ -670,6 +670,140 @@
this.asc_AddContentControlDatePicker(oPr, _content_control_pr);
};
/**
* @typedef {Object} OLEObjectData
* @property {string} Data - data which is stored in ole-object
* @property {string} ImageData - image encoded in base64
* @property {string} ApplicationId - identifier of plugin which able edit this ole-object
* @property {string} InternalId - identifier of ole-object which is used for work with ole-object added to document
* @property {number} Width - width of ole object in millimeters
* @property {number} Height - height of ole object in millimeters
* @property {?number} WidthPix - width image of ole-object in pixels
* @property {?number} HeightPix - height image of ole-object in pixels
*/
/**
* This method returns all ole-objects data for objects which can be opened by plugin with sPluginId.
* If sPluginId is not present this method returns all ole-objects contained in this document
* @memberof Api
* @typeofeditors ["CDE"]
* @alias GetAllOleObjects
* @param {?string} sPluginId
* @returns {OLEObjectData[]}
* */
window["asc_docs_api"].prototype["pluginMethod_GetAllOleObjects"] = function (sPluginId)
{
let aDataObjects = [];
let aOleObjects = this.WordControl.m_oLogicDocument.GetAllOleObjects(sPluginId, []);
for(let nObj = 0; nObj < aOleObjects.length; ++nObj)
{
aDataObjects.push(aOleObjects[nObj].getDataObject());
}
return aDataObjects;
};
/**
* Remove ole-object from document by internal id
* @memberof Api
* @typeofeditors ["CDE"]
* @alias RemoveOleObject
* @param {string} sInternalId
* @return {undefined}
* */
window["asc_docs_api"].prototype["pluginMethod_RemoveOleObject"] = function (sInternalId)
{
this.WordControl.m_oLogicDocument.RemoveDrawingObjectById(sInternalId);
};
/**
* This method allows to remove several ole-objects.
* @memberof Api
* @typeofeditors ["CDE"]
* @alias RemoveContentControls
* @param {OLEObjectData[]} arrObjects is a array of InternalId's. example: [{"InternalId": "5_556"}]
* @return {undefined}
* @example
* window.Asc.plugin.executeMethod("RemoveOleObjects", [[{"InternalId": "5_556"}]])
*/
window["asc_docs_api"].prototype["pluginMethod_RemoveOleObjects"] = function (arrObjects)
{
var arrIds = [];
for(var nIdx = 0; nIdx < arrObjects.length; ++nIdx)
{
arrIds.push(arrObjects[nIdx].InternalId);
}
this.WordControl.m_oLogicDocument.RemoveDrawingObjects(arrIds);
};
/**
* Select specified ole-object
* @memberof Api
* @typeofeditors ["CDE"]
* @alias SelectOleObject
* @param {string} id is a InternalId of the content control
* @example
* window.Asc.plugin.executeMethod("SelectOleObject", ["5_665"]);
*/
window["asc_docs_api"].prototype["pluginMethod_SelectOleObject"] = function(id)
{
var oLogicDocument = this.private_GetLogicDocument();
if (!oLogicDocument)
return;
var oDrawing = AscCommon.g_oTableId.Get_ById(id);
if(!oDrawing)
{
return;
}
oDrawing.Set_CurrentElement(true, null);
};
/**
* Add ole-object in current document position
* @memberof Api
* @typeofeditors ["CDE"]
* @alias AddOleObject
* @param {OLEObjectData} NewObject - new object data
* @return {undefined}
*/
window["asc_docs_api"].prototype["pluginMethod_InsertOleObject"] = function(NewObject)
{
var oPluginData = {};
oPluginData["imgSrc"] = NewObject["ImageData"];
oPluginData["widthPix"] = NewObject["WidthPix"];
oPluginData["heightPix"] = NewObject["HeightPix"];
oPluginData["width"] = NewObject["Width"];
oPluginData["height"] = NewObject["Height"];
oPluginData["data"] = NewObject["Data"];
oPluginData["guid"] = NewObject["ApplicationId"];
this.asc_addOleObject(oPluginData);
};
/**
* Change ole-object in current document position
* @memberof Api
* @typeofeditors ["CDE"]
* @alias AddOleObject
* @param {OLEObjectData} ObjectData
* @return {undefined} sInternalId - internal id of new ole-object
* window.Asc.plugin.executeMethod("SelectOleObject", ["5_665"]);
*/
window["asc_docs_api"].prototype["pluginMethod_ChangeOleObject"] = function(ObjectData)
{
var oPluginData = {};
oPluginData["objectId"] = ObjectData["InternalId"];
oPluginData["imgSrc"] = ObjectData["ImageData"];
oPluginData["widthPix"] = ObjectData["WidthPix"];
oPluginData["heightPix"] = ObjectData["HeightPix"];
oPluginData["width"] = ObjectData["Width"];
oPluginData["height"] = ObjectData["Height"];
oPluginData["data"] = ObjectData["Data"];
oPluginData["guid"] = ObjectData["ApplicationId"];
this.asc_editOleObject(oPluginData);
};
function private_ReadContentControlCommonPr(commonPr)
{
var resultPr;