Document Builder function not been called by callCommand

Reporting a bug:

Document Builder function is not been called when using the callCommand function in certain cases.

In the following example:

var htmlContent =  `<p>Test</p>
  <p><img src="https://upload.wikimedia.org/wikipedia/fr/c/c1/Logo_onlyoffice.png?20170715090358"></p>`;

	console.debug(1);
	window.Asc.plugin.executeMethod(
		"PasteHtml",
		[htmlContent],
		function(){
			console.debug(2);
			window.Asc.plugin.callCommand(
				function() {
					console.debug(3);
				},
				false,
				true,
				function(){
					console.debug(4);
				}
			);
		}
	);

The expected behavior should be to print the console logs sequentially

1
2
3
4

However the result is :

1
2
4

Where the console.debug(3) is never been called.
I managed to get this issue only when there is an <img> at the htmlContent

Best,

Document Server version:
Installation method: docker image: onlyoffice/documentserver:7.3.2.8

1 Like

Hello,

I managed to find & fix where the problems came from.

First, the Api.prototype["pluginMethod_PasteHtml"] return callback is called right away when the function is finished, and not when the execution is finished (loading the images in this case).

So, I used the same functionality as ReplaceTextSmart:

let guid = window.g_asc_plugins ? window.g_asc_plugins.setPluginMethodReturnAsync() : null;

....

if (guid)
    window.g_asc_plugins.onPluginMethodReturn(guid, true);

The second problem came from the function this.asc_PasteData, this function has a callback that has not been used, so when the images are loaded the callback has been called already.

so here is my fix, let me know what you think:

Api.prototype["pluginMethod_PasteHtml"] = function(htmlText) {
	if (!AscCommon.g_clipboardBase)
		return null;

	if (this.isViewMode)
		return null;

	var _elem = document.getElementById("pmpastehtml");
	if (_elem)
		return;

	let guid = window.g_asc_plugins ? window.g_asc_plugins.setPluginMethodReturnAsync() : null;

	_elem = document.createElement("div");
	_elem.id = "pmpastehtml";

	if (this.editorId == AscCommon.c_oEditorId.Word || this.editorId == AscCommon.c_oEditorId.Presentation) {
		var textPr = this.get_TextProps();
		if (textPr) {
			if (undefined !== textPr.TextPr.FontSize)
				_elem.style.fontSize = textPr.TextPr.FontSize + "pt";

			_elem.style.fontWeight = (true === textPr.TextPr.Bold) ? "bold" : "normal";
			_elem.style.fontStyle = (true === textPr.TextPr.Italic) ? "italic" : "normal";

			var _color = textPr.TextPr.Color;
			if (_color)
				_elem.style.color = "rgb(" + _color.r + "," + _color.g + "," + _color.b + ")";
			else
				_elem.style.color = "rgb(0,0,0)";
		}
	} else if (this.editorId == AscCommon.c_oEditorId.Spreadsheet) {
		var props = this.asc_getCellInfo();

		if (props && props.font) {
			if (undefined != props.font.size)
				_elem.style.fontSize = props.font.size + "pt";

			_elem.style.fontWeight = (true === props.font.bold) ? "bold" : "normal";
			_elem.style.fontStyle = (true === props.font.italic) ? "italic" : "normal";
		}
	}

	_elem.innerHTML = htmlText;
	document.body.appendChild(_elem);
	this.asc_setVisiblePasteButton(false);
	this.incrementCounterLongAction();
	var b_old_save_format = AscCommon.g_clipboardBase.bSaveFormat;
	AscCommon.g_clipboardBase.bSaveFormat = false;
	var self = this;
	this.asc_PasteData(AscCommon.c_oAscClipboardDataFormat.HtmlElement, _elem, undefined, undefined, undefined,
		function() {
			self.decrementCounterLongAction();
			if (true) {
				var fCallback = function() {
					document.body.removeChild(_elem);
					_elem = null;
					AscCommon.g_clipboardBase.bSaveFormat = b_old_save_format;
				};
				if (self.checkLongActionCallback(fCallback, null)) {
					fCallback();
				}
			} else {
				document.body.removeChild(_elem);
				_elem = null;
				AscCommon.g_clipboardBase.bSaveFormat = b_old_save_format;

			}
			if (guid)
				window.g_asc_plugins.onPluginMethodReturn(guid, true);
		}
	);
};

best,

Hi @humbol

We are checking the situation.
I will let you know when I get something.

Hey @humbol

We’ve filed a bug on the issue you provided. Thanks a lot!