Editor API allows the user to perform different operations on the currently opened document. They are available within the Context Object, when the user is editing a Story.
|
Editor API are to be considered as a feature in progress. Changes and additions may be made without warning. |
|
In the source code, we will refer to the Context Object as a generic "ctx" object. |
activeDocument
The activeDocument is the entry point of the Editor API. It contains a set of different methods to analyze and manage the currently opened document.
Methods of the activeDocument object
getXmlContent
Returns the complete XML content of the document.
It can be called as
ctx.activeDocument.getXmlContent(options);
Method |
|
Parameter
* Optional
If options is not specied or if it’s an empty object the xml of the entire document will be returned. |
Returns |
|
Example
// Obtains the XML of the document.
var xml = ctx.activeDocument.getXmlContent();
// For example, we can parse the XML with jQuery and use it somehow.
var parsedXML = $.parseXML( xml );
// ...
getTextContent
Returns the complete text content of the document.
It can be called as
ctx.activeDocument.getTextContent(options);
Method |
|
Parameter |
Optional
|
Returns |
|
Example
// Obtains the text content of the document.
var options = {};
// Uncomment this line if you want to include witespace nodes
options.includeWhitespaceNodes = true;
var text = ctx.activeDocument.getTextContent(options);
// ...
getMetadata
Returns the complete Metadata of the document as a string.
It can be called as
ctx.activeDocument.getMetadata( callbacks );
Method |
|
Parameter |
|
Returns |
|
|
This call is asynchronous. The Metadata string is available only as the parameter of the success callback. |
Example
// Obtains the Metadata of the document
ctx.activeDocument.getMetadata({
success: function( metadata ) {
// Do something with this...
},
error: function( err ) {
// For example.
ctx.showError( err.message );
}
});
getSelection
Returns the current Selection in the active document.
|
The current selection is an object of type Selection. See Selection object for further information on available methods. |
It can be called as
ctx.activeDocument.getSelection();
Method |
|
Parameter |
None |
Returns |
|
Example
// Obtains the current selection.
var sel = ctx.activeDocument.getSelection();
// sel is now a Selection object.
// For example, we can verify that the current selection is a ContentItem
var selNode = sel.getNode();
if ( selNode.nodeType === ctx.activeDocument.CONSTANTS.CONTENTITEM_TYPE ) {
// Perform a specific action...
}
insertXML
Insert an XML in a specified part of the document.
It can be called as
ctx.activeDocument.insertXML(xml, xpathOptions, insertOptions);
Method |
|
Parameter |
e.g. |
Parameter |
See example below. |
Parameter |
Otherwise, it is possible to directly pass the insertType as a string ( |
Returns |
|
|
At the moment, if the specified xpath in the xpathOptions corresponds to more than one xPath, only the first is taken. |
// Prepares the xml.
var xml = '<p>Prepend me</p>';
ctx.activeDocument.insertXml( xml, {
xpath: 'doc/story/text/superbyline',
criteria: {
[
param: 'xsm-preserve',
condition: 'equal',
value: 'true'
]
} },
'prepend' );
// the xml will be inserted only in the first superbyline tag that satisfy xpath and the additional (optional) criteria
// Prepares the xml.
var xml = '<h1><?EM-dummyText lorem ipsum?></h1>';
ctx.activeDocument.insertXml( xml, {xpath: 'cursorPosition'});
// the xml will be inserted at the cursor position, replacing the current selection if not collapsed.
getDocumentInfo
Returns a subset of information on the current document. The information includes the following information : channel, workfolder, issueDate, templateName, language.
It can be called as
ctx.activeDocument.getDocumentInfo();
Method |
|
Parameter |
None |
Returns |
|
Example
// Obtains the document information.
var documentInfo = ctx.activeDocument.getDocumentInfo();
/*
documentInfo is now:
{
"id": "id",
"readonly": true,
"channel": "CHANNEL",
"workfolder": "WORKFOLDER",
"issueDate": "ISSUE DATE",
"templateName": "TEMPLATE NAME",
"language": "en/uk"
}
*/
if ( documentInfo.channel === 'Globe-Web' ) {
// ... do something, for example it can remove a specific custom button.
}
isReadonly
Returns the readonly status of the story
It can be called as
ctx.activeDocument.isReadonly();
Method |
|
Parameter |
None |
Returns |
|
Example
// Obtains the document information.
var isReadonly = ctx.activeDocument.isReadonly();
getSelectionPathInfo
Returns an object containing basic information on the selected path.
It can be called as
ctx.activeDocument.getSelectionPathInfo();
Method |
|
Parameter |
None |
Returns |
|
Example
// Obtains the document information.
var pathInfo = ctx.activeDocument.getSelectionPathInfo();
console.log(pathInfo);
->
{
txtPath: "/doc/story/text/p",
contentItem: "text",
contentItemCfg: "text",
editingType: "text",
collapsed: true,
isDummyText: false,
isEmpty: false,
isAnchor: false,
isInCrossReference: false,
isInTable: false,
sameTable: false,
isInContainer: false,
isInComponent: false,
isInSection: true,
isNote: null,
isInFootnote: false,
annotationInfo: {
isInAnnotation: false,
content: ""
},
isEditingAllowed: true,
isEditingCaption: false,
isEditingCollage: false,
isPictureDesk: false,
isEditingGallery: false
}
saveDocument
Execute the save of the current document.
It can be called as
ctx.activeDocument.saveDocument();
Method |
|
Parameter |
Optional {Function} callback - A function invoked if the the document is successfully saved |
|
If the document is not dirty, the save is not executed, but the callback, if defined, is always invoked. |
Example
// Save the current document.
ctx.activeDocument.saveDocument();
getNode
Returns a specific node, as a DocumentNode object, upon the information contained in the XPath
It can be called as
ctx.activeDocument.getNode(xpathOptions);
Method |
|
Parameter |
|
Return |
|
|
At the moment, if the specified xpath in the xpathOptions corresponds to more than one node, only the first is taken. |
Example
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/text',
criteria: {
[ param: 'channel',
condition: 'equal',
value: 'Globe-Print'
]
}
};
// Obtains the node.
var node = ctx.activeDocument.getNode(xpathOptions);
// Perform actions on the found node
// ...
if (node) {
...
}
insertExternalUrl
Insert an external URL in the document, upon the information contained in the object to insert and in the provided options
It can be called as
ctx.activeDocument.insertExternalUrl(item, insertOptions);
Method |
|
Parameter |
|
Parameter |
|
Return |
|
Example
// prepare the object to insert
var item = {
'url': 'https://images-assets.nasa.gov/image/GSFC_20171208_Archive_e001240/GSFC_20171208_Archive_e001240~orig.jpg',
'type': 'web::image'
};
// Perform the insert.
ctx.activeDocument.insertExternalUrl(item, {});
insertObject
Insert a list of Methode Object in the document, upon the information contained in the reference object to insert and in the provided insertOptions
It can be called as
ctx.activeDocument.insertObject(reference, insertOptions);
Method |
|
Parameter |
|
Parameter |
|
Return |
|
Example
// prepare the object to insert
// Two image objects will be inserted using the paths
const reference = {
full: false,
referenceListIds: ['/Globe/Images/Foreign/11.obama_family.jpeg', '/Globe/Images/Foreign/1420087334.jpg'],
withPath: true
}
// Example One
const insertOptionsOne = {
insertiontype: "insert"
};
// Perform the insert.
ctx.activeDocument.insertObject(reference, insertOptionsOne);
// Example Two
const insertOptionsTwo = {
insertiontype: "insert",
templateInfo: {name: "", type: eomeditor.publicapi.TEMPLATE_TYPE.IMAGE}
};
// Perform the insert.
ctx.activeDocument.insertObject(reference, insertOptionsTwo);
// Example Three
const insertOptionsThree = {
insertiontype: "insert",
templateInfo: {name: "image web square", type: eomeditor.publicapi.TEMPLATE_TYPE.IMAGE}
};
// Perform the insert.
ctx.activeDocument.insertObject(reference, insertOptionsThree);
setDocumentToBeRefreshed
Some events in Swing are not handled. This API could be useful when some actions are fired and they change the document server side. This way, it’s possible to warn the user that the document should be refresh.
It can be called as
ctx.activeDocument.setDocumentToBeRefreshed(options);
Method |
|
Parameter |
|
Return |
|
Example
action: function(ctx, params, callback) {
var editorApi = ctx.activeDocument;
editorApi.setDocumentToBeRefreshed();
}
switchToChannel(channel)
Switch the document to the specified channel
It can be called as
ctx.activeDocument.getCollectionSelectedItems(attrOptions)
Method |
|
Parameter |
Required |
// Switch to channel
ctx.activeDocument.switchToChannel(channel);
Constants of the activeDocument object
The activeDocument object comes with a set of constants to simplify the management of the return values of the different methods.
The constants are available under
activeDocument.CONSTANTS
Currently the following constants are visible:
ctx.activeDocument.CONSTANTS = {
DUMMY_TYPE: 'DummyText', // Document Node type
CONTENTITEM_TYPE: 'ContentItem', // Document Node type
CONTENTBLOCK_TYPE: 'ContentBlock', // Document Node type
CONTENTNODE_TYPE: 'ContentNode', // Document Node type,
BLOCK_IMAGE: 'image', // ContentBlock of type Image
BLOCK_VIDEO: 'video', // ContentBlock of type Video
};
activeDocumentV2
The activeDocumentV2 is an entry point of the Editor API starting form version 5.2020.11. It contains a set of different methods to analyze and manage the currently opened document in an asynchronous way that are cross platform compatible (Swing, Swing Mobile, Prime). Starting from the version 5.2020.11, we reccomend to use activeDocumentV2 and evaluating the migration for existing extensions.
Methods of the activeDocumentV2 object
getXmlContent
Returns the complete XML content of the document or a part of it based on the options parameter.
It can be called as
ctx.activeDocumentV2.getXmlContent(options);
Method |
|
Parameter |
If options is not specied or if it’s an empty object the xml of the entire document will be returned. |
Returns |
|
Example
// Obtains the XML of the document.
ctx.activeDocumentV2.getXmlContent({}).then(result => {
console.log(`V2 async document result: ${result}`);
}).catch(error => {
console.log(`V2 async document error: ${error}`);
});
// Obtains the XML of the current selection
ctx.activeDocumentV2.getXmlContent({useSelection: true}).then(result => {
console.log(`V2 async result selection: ${result}`);
}).catch(error => {
console.log(`V2 async error selection: ${error}`);
});
// Obtains the XML of a specific node
var xpathOptions = {
'xpath': 'doc/story/text'
};
ctx.activeDocumentV2.getXmlContent({xpathOptions}).then(result => {
console.log(`V2 async xpath result: ${result}`);
}).catch(error => {
console.log(`V2 async xpath error: ${error}`);
});
// ...
getTextContent
Returns the complete text content of the document.
It can be called as
ctx.activeDocumentV2.getTextContent();
Method |
|
Returns |
|
Example
// Obtains the text of the document.
ctx.activeDocumentV2.getTextContent().then(result => {
console.log(`V2 async document result: ${result}`);
}).catch(error => {
console.log(`V2 async document error: ${error}`);
});
// ...
replaceXmlContent
Replaces the XML content of a node.
It can be called as
ctx.activeDocumentV2.replaceXmlContent(options);
Method |
|
Parameter |
|
Return |
|
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/summary',
criteria: {
[
param: 'channel',
condition: 'equal',
value: 'Globe-Print'
]
}
};
// REPLACE XML content
ctx.activeDocumentV2.getXmlContent({xpathOptions}).then( content => {
var newContent = '<summary id="U26664270086oOb"><p>New summary content</p></summary>';
ctx.activeDocumentV2.replaceXmlContent({newContent, xpathOptions}).then(result => {
console.log('content replaced')
});
})
...
//
saveDocument
Execute the save of the current document.
It can be called as
ctx.activeDocumentV2.saveDocument();
Method |
|
Return |
|
|
If the document is not dirty, the save is not executed, but the promise is always resolved. |
Example
// Save the current document.
ctx.activeDocumentV2.saveDocument().then(result => {
console.log(`save operation successfully excuted`);
}).catch(error => {
console.log(`an error occurred saving the document`);
});
insertXmlContent
Insert an XML in a specified part of the document..
It can be called as
ctx.activeDocumentV2.insertXmlContent(options);
Method |
|
Parameter |
|
Return |
|
var xpathOptions = {
xpath: 'cursorPosition'
};
var xmlContent = '<h1><?EM-dummyText lorem ipsum?></h1>';
activeDocumentV2.insertXmlContent({xmlContent, xpathOptions}).then(result => {
console.log('content has been inserted');
}).catch(error => {
console.log('content has not been inserted');
});
...
//
Selection object
The Selection Object allows to manipulate the selection. It contains a set of properties and methods described below.
Methods
getNode
Returns the current selected node, as a DocumentNode object.
It can be called as
{SelectionObject}.getNode();
Method |
|
Parameter |
Optional - Set options.getWrappedSection = true if it’s needed to get the section wrapped node |
Return |
|
// Obtains the document selection
var sel = ctx.activeDocument.getSelection();
var selectedNode = sel.getNode();
// Perform actions on the selected node
// ...
getXmlContent
Returns the XML of the current Selection.
It can be called as
{SelectionObject}.getXmlContent();
Method |
|
Parameter |
None |
Return |
|
// Obtains the document selection
var sel = ctx.activeDocument.getSelection();
var selectedXml = sel.getXmlContent();
// Perform actions on the selected node
// ...
|
Calling |
insertXml
Insert an XML in a specified part of the current selection or replaces the current selection with the new XML.
It can be called as
{SelectionObject}.insertXML(xml, insertOptions);
Method |
|
Parameter |
e.g. |
Parameter |
Otherwise, it is possible to directly pass the insertType as a string ( |
Returns |
|
// Obtains the document information.
var xml = '<p>Prepend me</p>';
var sel = ctx.activeDocument.getSelection();
sel.insertXml( xml, 'prepend' );
// Example with closestContainer
var xml = '<newsletter-widget><p>This is a new section part</p><p>Next paragraph content</p></newsletter-widget>';
var sel = ctx.activeDocument.getSelection();
sel.insertXml( xml, {insertionType: 'prepend', closestContainer: 'section'} );
prepend, append
Insert an XML in a specified part of the current selection. They are shortcuts method for insertXml.
It can be called as
{SelectionObject}.prepend(xml);
{SelectionObject}.append(xml);
See insertXml for further details.
getTextContent
Returns the text content of the current Selection.
It can be called as
{SelectionObject}.getTextContent();
Method |
|
Parameter |
Optional
|
Return |
|
// Obtains the document selection
var sel = ctx.activeDocument.getSelection();
var options = {};
// Uncomment this line if you want to include witespace nodes
// options.includeWhitespaceNodes = true;
var text = sel.getTextContent(options);
// ...
|
Calling |
DocumentNode interface
It is the lowest-level object available for manipulating the Active Document. It represents a node within the document. It contains different methods and properties to interact with the node.
|
The DocumentNode object is NOT a DOM node, so operation as $(<DocumentNode>) will not work. |
Properties
nodeType
Return the node type. Can be one of the following:
-
ContentItem
-
ContentNode
-
ContentBlock
-
DummyText
|
Verify the value of this property against the Constants of the activeDocument object to perform the best check. |
|
Each Document Node type has specific methods. See Types for details on each document node. |
var sel = ctx.activeDocument.getSelection();
var selectedNode = sel.getNode();
if ( selectedNode.nodeType === ctx.activeDocument.CONSTANTS.DUMMY_TYPE ) {
// It is a dummy text...
sel.append('<p>New text here</p>');
}
contentItem
Returns the contentItem in which the element is contained. It can be one of two values:
-
null, if the element is not part of any contentItem, or it is a contentItem itself. -
{DocumentNode} ContentItemthe document node of type 'ContentItem'. See ContentItem for further details.
Methods
getXmlContent
Returns the XML of the node.
It can be called as
{DocumentNode}.getXmlContent();
Method |
|
Parameter |
None |
Return |
|
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/text',
criteria: {
[
param: 'channel',
condition: 'equal',
value: 'Globe-Print'
]
}
};
// Obtains the node.
var node = ctx.activeDocument.getNode(xpathOptions);
// Perform actions on the found node
// ...
if (node) {
var xmlContent = node.getXmlContent();
}
replaceXmlContent
Replaces the XML content of the current node.
It can be called as
{DocumentNode}.replaceXmlContent();
Method |
|
Parameter |
|
Return |
|
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/summary',
criteria: {
[
param: 'channel',
condition: 'equal',
value: 'Globe-Print'
]
}
};
// Obtains the node.
var node = ctx.activeDocument.getNode(xpathOptions);
// Perform actions on the found node
// ...
if (node) {
var repxml = '<summary id="U26664270086oOb"><p>New summary content</p></summary>';
node.replaceXmlContent(repxml);
}
getTextContent
Returns the text content of the node.
It can be called as
{DocumentNode}.getTextContent();
Method |
|
Parameter |
Optional
|
Return |
|
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/text',
criteria: {
[
param: 'channel',
condition: 'equal',
value: 'Globe-Print'
]
}
};
// Obtains the node.
var node = ctx.activeDocument.getNode(xpathOptions);
// Perform actions on the found node
// ...
if (node) {
var options = {};
// Uncomment this line if you want to include witespace nodes
// options.includeWhitespaceNodes = true;
var text = node.getTextContent(options);
// ....
}
replaceTextContent
Replace the text of the node with another text.
It can be called as
{DocumentNode}.replaceTextContent(text);
Method |
|
Parameter |
|
Parameter |
|
Returns |
|
|
Right now to execute the replace the API use the information in the current selection. |
// Obtains the current selection.
var sel = ctx.activeDocument.getSelection();
var selectedNode = sel.getNode();
// Replace the text of the current node with simple text
selectedNode.replaceTextContent( 'Simple text' );
// Replace the text of the current node with an xml format
selectedNode.replaceTextContent('<a href="http://www.eidosmedia.com" title="Eidosmedia">Eidosmedia</a>', {'format': 'xml'});
getContentInfo
Returns an object that contains a set of different methods to analyze and manage the current node.
|
This method is valid only for content nodes that refers to Methode Content: Images, External content items. |
API functions to get and set system attributes and metadata of an image linked to a story
It can be called as
{DocumentNode}.getContentInfo();
Method |
|
Parameter |
None |
Return |
|
// Obtains the current selection.
var sel = ctx.activeDocument.getSelection();
var selectedNode = sel.getNode();
// Perform actions on the found node
// ...
if (selectedNode) {
var ctx = selectedNode.getContentInfo();
var id = ctx.getId();
var type = ctx.getType();
var sysAttributes = ctx.getSysAttributes();
var usageTickets = ctx.getUsageTickets();
}
getAttributes
Get node attributes.
It can be called as
{DocumentNode}.getAttributes();
Method |
|
Parameter |
None |
Return |
|
// Obtains the current selection.
var sel = ctx.activeDocument.getSelection();
var selectedNode = sel.getNode();
// Perform actions on the found node
if (selectedNode) {
var attributes = selectedNode.getAttributes();
// ...
}
setAttributes
Set attributes to a node.
It can be called as
{DocumentNode}.setAttributes();
Method |
|
Parameter |
|
Return |
|
|
Don’t use this API to set the channel attribute. |
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/summary',
criteria: {
[
param: 'channel',
condition: 'equal',
value: 'Globe-Print'
]
}
};
// Obtains the node by x path.
var node = ctx.activeDocument.getNode(xpathOptions);
// To obtain the node by selection uncomment the following lines
// var sel = editorApi.getSelection();
// var node = sel.getNode();
var attrOptions = {};
var attributes = [{'name': 'testattr', 'value': 'testvalue'}];
attrOptions.attributes = attributes;
// Perform actions on the found node
// ...
if (node) {
node.setAttributes(attrOptions);
}
setExternalReference
Set a refence of a Methode object to a node.
It can be called as
{DocumentNode}.setExternalReference();
Method |
|
Parameter |
Required |
Parameter |
Optional
|
Return |
|
|
cover, hardcrop, tmx and xtransform settings are used inserting only images or graphics |
var editorApi = ctx.activeDocument;
var sel = editorApi.getSelection();
var node = sel.getNode();
var reference = path o loid of the objects to insert
const settings = {
cover: true
hardCrop: false
tmx: null
xtransform: null
}
node.setExternalReference( reference, settings );
setChannels
Set channels to a node.
It can be called as
{DocumentNode}.setChannels();
Method |
|
Parameter |
|
Return |
|
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/summary',
criteria: {
[
param: 'highlight',
condition: 'equal',
value: 'yes'
]
}
};
// Obtains the node by x path.
var node = ctx.activeDocument.getNode(xpathOptions);
// To obtain the node by selection uncomment the following lines
// var sel = editorApi.getSelection();
// var node = sel.getNode();
var channels = ['Globe-Web'];
// Perform actions on the found node
// ...
if (node) {
node.setChannels(channels);
}
Types
There are four different DocumentNode types.
ContentItem
Its nodeType can be checked against ctx.activeDocument.CONSTANTS.CONTENTITEM_TYPE.
It has some specific methods:
duplicate
Duplicates a contentItem.
It can be called as
{ContentItem}.duplicate(channel);
Method |
|
Parameter |
e.g. |
Returns |
|
// Obtains the current selection.
var sel = ctx.activeDocument.getSelection();
var selectedNode = sel.getNode();
if ( selectedNode.nodeType === ctx.activeDocument.CONSTANTS.CONTENTITEM_TYPE ) {
// it is a ContentItem node. Duplicate itself.
selectedNode.duplicate( 'Globe-Web,Globe-Print' );
} else {
// Duplicates the container ContentItem.
selectedNode.contentItem.duplicate( 'Globe-Web,Globe-Print' );
}
duplicateContent
Copy the content of the ContentItem node into another node.
It can be called as
{ContentItem}.duplicateContent(xpathOptions);
Method |
|
Parameter |
See example below. |
Returns |
|
|
At the moment, if the specified xpath in the xpathOptions corresponds to more than one node, only the first is taken. |
// Obtains the current selection.
var sel = ctx.activeDocument.getSelection();
var selectedNode = sel.getNode();
var xpathOptions = {
xpath: 'doc/story/text',
criteria: {
[
param: 'xsm-preserve',
condition: 'equal',
value: 'true'
]
}
};
if ( selectedNode.nodeType === ctx.activeDocument.CONSTANTS.CONTENTITEM_TYPE ) {
// it is a ContentItem node. Duplicate its own content.
selectedNode.duplicateContent( xpathOptions );
} else {
// Duplicates the content of the container ContentItem.
selectedNode.contentItem.duplicateContent( xpathOptions );
}
ContentNode
Its nodeType can be checked against ctx.activeDocument.CONSTANTS.CONTENTNODE_TYPE.
It has no specific methods at the moment.
ContentBlock
Its nodeType can be checked against ctx.activeDocument.CONSTANTS.CONTENTBLOCK_TYPE.
Its blockType can be checked against ctx.activeDocument.CONSTANTS.BLOCK_*.
Methods common to all the ContentBlocks
getXmlContent
Returns the XML of the current node.
It can be called as
{ContentBlock}.getXmlContent();
Method |
|
Parameter |
None |
Return |
|
// Obtains the document selection
var sel = ctx.activeDocument.getSelection();
var selectedNode = self.getNode();
if ( selectedNode.nodeType === ctx.activeDocument.CONSTANTS.CONTENTITEM_TYPE ) {
var xml = selectedNode.getXmlContent();
// ...
}
replaceXmlContent
Replaces the XML content of the current node.
It can be called as
{ContentBlock}.replaceXmlContent();
Method |
|
Parameter |
|
Return |
|
|
If the specified xml doesn’t contain a special element an error occurred. A block content can be replaced only with another block content. Right now we can replace only imageElement, videoElement and objectElement |
// Obtains the document selection
var sel = ctx.activeDocument.getSelection();
var selectedNode = self.getNode();
if ( selectedNode.nodeType === ctx.activeDocument.CONSTANTS.CONTENTBLOCK_TYPE ) {
var xml = '<photo-web><fw-photo width="200" height="200"></fw-photo>' +
'<photo-caption channel="Globe-Web"><?EM-dummyText Insert caption first here?><p><?EM-dummyText Insert caption here?></p></photo-caption></photo-web>';
var result = selectedNode.replaceXmlContent(xml);
// ...
}
Image Block
It represents an Image Block. Its blockType can be checked against ctx.activeDocument.CONSTANTS.BLOCK_IMAGE.
copyImage( xpathOptions, settings )
Copies the image into other image elements, maintaining the crop and the transformation information.
It can be called as
{ContentBlock}.copyImage(xpathOptions, settings)
Method |
|
Parameter |
See example below. |
Parameter |
|
Returns |
|
|
At the moment, if the specified xpath in the xpathOptions corresponds to more than one xPath, only the first is taken. |
// Obtains the current selection
var sel = ctx.activeDocument.getSelection();
var selNode = sel.getNode();
// Check if it is an image block
if ( selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_IMAGE ) {
var xpathOptions = {
'xpath': 'doc/story/text/photo-web/fw-photo',
'criteria': [
{'param': 'eomattr',
'condition': 'equal',
'value': 'true'}
]
};
selNode.copyImage(xpathOptions, { 'cover': true, 'hardCrop': false, 'copy': true });
}
changeAlignment( align, settings )
Change the alignment of a block component.
It can be called as
{ContentBlock}.changeAlignment(align, settings)
Method |
|
Parameter |
Required |
Parameter |
Optional See example below. |
// Obtains the current selection
var sel = ctx.activeDocument.getSelection();
var selNode = sel.getNode();
// Check if it is an image block
if ( selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_IMAGE ) {
selNode.changeAlignment('center');
}
changeBlockDimensions( dimensions, settings )
Change width and height of a block.
It can be called as
{ContentBlock}.changeBlockDimensions(xpathOptions, settings)
Method |
|
Parameter |
Required |
Parameter |
Optional See example below. |
// Obtains the current selection
var sel = ctx.activeDocument.getSelection();
var selNode = sel.getNode();
// Check if it is an image block
if ( selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_IMAGE ) {
var dimensions = {'width': 300, 'height': 300};
selNode.changeBlockDimensions(dimensions);
}
Video Block
It has no specific methods at the moment.
Collection Block
It represents a Collection Block. It can be of three types: insert, embed or link. Its blockType can be checked against ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_INSERT, ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_EMBED, ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_LINK.
setItemAttributes(attrOptions)
Set attributes to an item of a collection .
It can be called as
{ContentBlock}.setItemAttributes(attrOptions)
Method |
|
Parameter |
|
Returns |
|
|
At least selected or index must be specified, otherwise the attributes will be not applied. |
// Obtains the current selection
var sel = ctx.activeDocument.getSelection();
var selNode = sel.getNode();
// Check if it is a collection block
if ( selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_INSERT ||
selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_EMBED ||
selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_LINK) {
var options = {};
var attributes = [{'name': 'eomtest', 'value': 'test'}, {'name': 'class', 'value': 'testclass'}];
options.attributes = attributes;
options.selected = true; // the attributes will be applied to the selected item
selNode.setItemAttributes(options);
}
getCollectionSelectedItems(attrOptions)
Get the selected items of a collection .
It can be called as
{ContentBlock}.getCollectionSelectedItems(attrOptions)
Method |
|
Parameter |
|
Returns |
|
// Obtains the current selection
var sel = ctx.activeDocument.getSelection();
var selNode = sel.getNode();
// Check if it is a collection block
if ( selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_INSERT ||
selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_EMBED ||
selNode.blockType === ctx.activeDocument.CONSTANTS.BLOCK_COLLECTION_LINK) {
var options = {};
// Uncomment this lines if you want to get items with a specific attribute value
/* var attributes = [{'name': 'lead', 'value': 'test'}];
options.attributes = attributes; */
var selectedItems = selNode.getCollectionSelectedItems(options);
}
DummyText
Its nodeType can be checked against ctx.activeDocument.CONSTANTS.DUMMY_TYPE.
It has no specific methods at the moment.
From activeDocument to activeDocumentV2
Follows some examples to compare the use of activeDocument and activeDocumentV2 to perform the same operation.
getXmlContent
Example
// ***** activeDocument ******
// Obtains the current selection.
var sel = ctx.activeDocument.getSelection();
// get the node
var selNode = sel.getNode();
var xmlContent = node.getXmlContent();
// ***** activeDocumentV2 ******
// Obtains the XML of the current selection
ctx.activeDocumentV2.getXmlContent({useSelection: true}).then(result => {
console.log(`V2 async result selection: ${result}`);
}).catch(error => {
console.log(`V2 async error selection: ${error}`);
});
// ...
getTextContent
Example
// ***** activeDocument ******
// Obtains the text
var textContent = ctx.activeDocument.getTextContent();
// ***** activeDocumentV2 ******
// Obtains the XML of the current selection
ctx.activeDocumentV2.getTextContent().then(result => {
console.log(`V2 async result selection: ${result}`);
}).catch(error => {
console.log(`V2 async error selection: ${error}`);
});
// ...
replaceXmlContent
Example
// ***** activeDocument ******
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/summary',
criteria: {
[
param: 'channel',
condition: 'equal',
value: 'Globe-Print'
]
}
};
// Obtains the node.
var node = ctx.activeDocument.getNode(xpathOptions);
// Perform actions on the found node
// ...
if (node) {
var repxml = '<summary id="U26664270086oOb"><p>New summary content</p></summary>';
node.replaceXmlContent(repxml);
}
// ***** activeDocumentV2 ******
// prepare the xpath options
var xpathOptions = {
xpath: 'doc/story/summary',
criteria: {
[
param: 'channel',
condition: 'equal',
value: 'Globe-Print'
]
}
};
// REPLACE XML content
ctx.activeDocumentV2.getXmlContent({xpathOptions}).then( content => {
var newContent = '<summary id="U26664270086oOb"><p>New summary content</p></summary>';
ctx.activeDocumentV2.replaceXmlContent({newContent, xpathOptions}).then(result => {
console.log('content replaced')
});
})
// ...
saveDocument
Example
// ***** activeDocument ******
ctx.activeDocument.saveDocument();
// ***** activeDocumentV2 ******
// Save the current document.
ctx.activeDocumentV2.saveDocument().then(result => {
console.log(`save operation successfully excuted`);
}).catch(error => {
console.log(`an error occurred saving the document`);
});
// ...
insertXmlContent
Example
// ***** activeDocument ******
var newXmlContent = '<h1><?EM-dummyText lorem ipsum?></h1>';
var retValue = ctx.activeDocument.insertXml(newXmlContent, {xpath: 'cursorPosition'});
// ***** activeDocumentV2 ******
var xpathOptions = {
xpath: 'cursorPosition'
};
var xmlContent = '<h1><?EM-dummyText lorem ipsum?></h1>';
activeDocumentV2.insertXmlContent({xmlContent, xpathOptions}).then(result => {
console.log(`content has not been inserted`);
}).catch(error => {
console.log(`content has not been inserted ${error}`);
});
// ...