Copy link to clipboard
Copied
I have a "lettuce" element that is not wrapped by a "bread" element. The task is given the selected "lettuce" element to wrap it in a "bread" element.
Documentation has that the ElementDef.WrapElement() returns void. When I wrap the "lettuce" element, the resulting parent element is also "lettuce". The "lettuce" element is now wrapped by a "lettuce" element.
Thoughts on how to impose a "bread" element that wraps around the "lettuce" element?
var doc = app.ActiveDoc;
var elementRange = doc.ElementSelection;
var elementLoc = elementRange.beg;
var parentElement = elementLoc.parent; // not "bread" element
var childElement = elementLoc.child; // "lettuce" element
PreChecksAndProceed(parentElement, childElement);
function PreChecksAndProceed(parentElement, childElement) {
var elemDef, name;
if (!childElement.ObjectValid())
{
alert ("AciveDoc has no ElementSelection");
return;
}
elemDef = childElement.ElementDef;
if (!elemDef.ObjectValid())
{
alert ("Selected element's (elemDef.ObjectValid() == false)");
return;
}
name = elemDef.Name;
if (name != "Lettuce")
{
alert ("Selected element is not Lettuce.");
return;
}
if (!parentElement.ObjectValid())
{
alert ("Selected element's (parentElement.ObjectValid() == false)");
return;
}
elemDef = parentElement.ElementDef;
if (!elemDef.ObjectValid())
{
alert ("Parent element's (elemDef.ObjectValid() == false)");
return;
}
name = elemDef.Name;
if (name == "Bread")
{
alert ("Selected Lettuce element is already wrapped by a Bread element.");
return;
}
Wrap_Element(childElement);
return;
};
function Wrap_Element(elementToWrap) {
var elemDef = childElement.ElementDef;
if (!elemDef.ObjectValid())
{
alert ("Selected element's (elemDef.ObjectValid() == false)");
return;
}
elemDef.WrapElement();
// todo the selected element is "Lettuce" that wraps the initial "Lettuce" element.
return;
};
Copy link to clipboard
Copied
Hi Joseph,
I don't have time to fully analyze the flow of this code sample, but I do notice right away that you don't ever ask the document for the <bread> element definition, so there is no way to wrap using a <bread> element. It seems that you are asking for the <lettuce> element definition with:
var elemDef = childElement.ElementDef;
...so with that, you get the <lettuce> element definition and therefore should expect more lettuce.
Here is a sample that wraps a specified element in a specified tag, then returns the new element. Take a look at this... it should provide you the answers you need.
Russ
//Wraps the specified element in the specified
//tag and returns the new wrapping element object
function wrapElement(doc, childElem, tag)
{
//Set the selection in the document to select
//the child element, in preparation for the wrap
var er = new ElementRange();
er.beg.parent = er.end.parent = childElem.ParentElement;
er.beg.child = childElem;
er.beg.offset = er.end.offset = 0;
doc.ElementSelection = er;
//Get the element definition for the wrap
//element tag
var elementDef = doc.GetNamedElementDef(tag);
//Wrap the element
elementDef.WrapElement();
//WrapElement() returns null, so we have to use a little trickery
//to find the new parent element. The new element will be
//completely selected, so we can get the element range
//and check the child of the beginning element location.
er = doc.ElementSelection;
return er.beg.child;
}
Copy link to clipboard
Copied
Russ,
I thank you for your reply and insightful answer. I had not considered GetNamedElementDef().
Given a selected element (Lettuce) on FrameMaker page, the use of GetNamedElementDef() is the equivalant of selecting an available element (Bread) from the listings in the Element Catalog dialog.
Joseph