Skip to main content

Connector class

The Connector class allows editing text documents, spreadsheets, presentations, PDFs, and fillable forms from an external source. It has the same interface as plugins.

Below you can find methods available for this class.

addContextMenuItem

The function called to add an item to the context menu. The process of working with the context menu is the same as for plugins except for the onClick method, which is used instead of subscribing by ID.

Parameters

NameTypeDescription
itemsContextMenuItem[]An array containing the context menu item parameters.

ContextMenuItem

NameTypeDescription
idstringThe item ID.
textstringThe item caption.
datastringThe item data (this data will be sent to the click event callback).
disabledbooleanSpecifies if the current item is disabled or not.
iconsstringThe item icons (see the plugins config documentation).
onClickfunctionThe click event callback. Only available for the addContextMenuItem method.
itemsContextMenuItem[]An array containing the context menu items for the current item.

Example

const connector = docEditor.createConnector();
connector.attachEvent("onContextMenuShow", (options) => {
connector.addContextMenuItem([{
text: "mainItem",
onClick: () => {
console.log("[CONTEXTMENUCLICK] menuSubItem1");
},
}]);
});

addToolbarMenuItem

The function called to add an item to the toolbar menu. The process of working with the toolbar menu is the same as for plugins except for the onClick method, which is used instead of subscribing by ID.

Parameters

NameTypeDescription
itemsToolbarMenuMainItemThe toolbar main menu item parameters.

ToolbarMenuMainItem

NameTypeDescription
guidstringThe plugin guid.
tabsToolbarMenuTab[]An array containing the toolbar menu tabs for the current item.

ToolbarMenuTab

NameTypeDescription
idstringThe tab ID.
textstringThe tab text.
itemsToolbarMenuItem[]An array containing the toolbar menu items for the current tab.

ToolbarMenuItem

NameTypeDescription
idstringThe item ID.
typeToolbarMenuItemTypeThe item type.
textstringThe item caption. If this field is "", the toolbar button is displayed only with an icon, without a caption.
hintstringThe item hint.
iconsstring | objectThe item icons (see the plugins config documentation).
disabledbooleanSpecifies whether the current item is locked.
enableTogglebooleanSpecifies whether the toolbar menu item (when "split == false") or its top part (when "split == true") can be toggled.
lockInViewModebooleanSpecifies whether the toolbar menu item is automatically locked in the view modes (when previewing, viewing forms, disconnecting, etc.).
separatorbooleanSpecifies whether a separator is used between the toolbar menu items.
splitbooleanSpecifies whether the toolbar menu item is split into two parts and includes the drop-down menu.
onClickfunctionThe click event callback.
itemsToolbarMenuItem[]An array containing the context menu items for the current item.

ToolbarMenuItemType

TypeDescription
"button" | "big-button"The toolbar menu item type. The button and big-button values are the same and can be equally used to specify the toolbar button.

Example

const connector = docEditor.createConnector();
connector.addToolbarMenuItem({
tabs: [
{
text: "Connector",
items: [
{
id: "toolConnector1",
type: "button",
text: "Meaning",
hint: "Meaning",
lockInViewMode: true,
icons: "./icon.svg",
items: [
{
id: "toolC1",
text: "Text",
data: "Hello",
onClick: (data) => {
console.log(`[TOOLBARMENUCLICK]: ${data}`);
},
},
],
},
],
},
],
});

attachEvent

The function called to add an event listener, a function that will be called whenever the specified event is delivered to the target. The list of all the available events is the same as for the plugins:

Parameters

NameTypeDescription
namestringThe event name.
callbackfunctionThe event listener.

Example

const connector = docEditor.createConnector();
connector.attachEvent("onChangeContentControl", (obj) => {
console.log(`[EVENT] onChangeContentControl: ${JSON.stringify(obj)}`);
});

callCommand

To call commands and send the data back to the editor, define the callCommand method. It allows the connector to send structured data that can be inserted into the resulting document file (formatted paragraphs, tables, text parts, and separate words, etc.).

Parameters

NameTypeDescription
funcfunctionDefines the command written in JavaScript which purpose is to form structured data which can be inserted into the resulting document file (formatted paragraphs, tables, text parts, and separate words, etc.). Then the data is sent to the editors. The command must be compatible with Office JavaScript API syntax.
callbackfunctionThe result that the method returns. Only the js standard types are available (any objects will be replaced with undefined).
isNoCalcbooleanDefines whether the document will be recalculated or not. The true value will not recalculate the document (use it only when your edits surely will not require document recalculation). The false value is used to recalculate the document after executing the function in the func parameter. The default value is false.

This method is executed in its context isolated from other JavaScript data. If some parameters or other data need to be passed to this method, use Asc.scope object.

Example

const connector = docEditor.createConnector();
connector.callCommand(() => {
const oDocument = Api.GetDocument();
const oParagraph = Api.CreateParagraph();
oParagraph.AddText("Hello");
oDocument.InsertContent([oParagraph]);
}, () => {
console.log("[COMMAND] Callback command");
});

connect

The function called to connect the connector to the editor.

note

Please note that this method should only be called if you have disconnected the connector with the disconnect method and need to connect it to the editor again. When creating a connector, you do not need to use the connect method, as it is called automatically along with the createConnector method.

Example

const connector = docEditor.createConnector();
connector.connect();

createWindow

The function called to create the connector modal window to display the additional information inside the editor.

Example

const connector = docEditor.createConnector();
const testConnectorWindow = connector.createWindow();

detachEvent

The function called to remove an event listener.

Parameters

NameTypeDescription
namestringThe event name.

Example

const connector = docEditor.createConnector();
connector.detachEvent("onChangeContentControl");

disconnect

The function called to disconnect the connector from the editor.

Example

const connector = docEditor.createConnector();
connector.disconnect();

executeMethod

The function called to execute certain editor methods using the connector. The full list of these methods is the same as for the plugins:

Parameters

NameTypeDescription
namestringThe name of the specific method that must be executed.
argsarrayThe arguments that the method in use has (if it has any).
callbackfunctionThe result that the method returns. It is an optional parameter.

Example

const connector = docEditor.createConnector();
connector.executeMethod("GetCurrentWord", [], (word) => {
console.log(`[METHOD] GetCurrentWord: ${word}`);
});

updateContextMenuItem

The function called to update an item in the context menu with the specified items.

Parameters

NameTypeDescription
itemsContextMenuItem[]An array containing the context menu item parameters.

Example

const connector = docEditor.createConnector();
const items = [
{
id: "onConvert",
text: "Convert to Markdown or HTML",
},
];

connector.updateContextMenuItem(items);