Pages: 1
Print
Author Topic: Inserting element example b(bold) i(Italic) in run time  (Read 4697 times)
karthic2861
Member

Posts: 22


« on: September 17, 2012, 03:34:32 AM »

Hi all,

Is that possible can we able to insert element like bold and other element in run time using script.

Please guide me

Regards
M
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #1 on: September 17, 2012, 11:19:38 AM »

You might use one of the following APIs:
  Selection.ToggleInline()
  Selection.InsertElement()
  Range.InsertElement()
  DOMNode_object.insertBefore()


Here are a few simple examples written in JScript:

Code:
//XMetaL Script Language JScript:
//If selection is not inside "bold" wrap it in "bold" if allowed by DTD.
//If selection is inside "bold" and selection is entire element content then remove the containing element
//or split it (depending on selection and DTD).
Selection.ToggleInline("bold");

Code:
//XMetaL Script Language JScript:
//Insert the element named "bold" at the current selection.
//If not an insertion point the current selection will be replaced, if legal according to the DTD.
Selection.InsertElement("bold");

Code:
//XMetaL Script Language JScript:
//Insert the element named "bold" after the current selection.
//Range is often preferable over Selection as manipulating a Range does not usually move the user's Selection.
var rng = ActiveDocument.Range;
rng.Collapse(sqCollapseEnd);
rng.InsertElement("bold");

Code:
// XMetaL Script Language JScript:
// Insert a new Sect1 element (node) before the first Sect1 in the document.
//Paste this example in a "Journalist" DTD document.

// Get the top level node
var topElem = ActiveDocument.documentElement;
if (topElem) {
// Create the new Sect1 node
var newSect1 = ActiveDocument.createElement("Sect1");
// Visit each child node of the top-level node
// until the desired node (if it exists) is found
var nextChild=topElem.firstChild;
while (nextChild && nextChild.nodeName!="Sect1") {
nextChild = nextChild.nextSibling;
}
// If the desired node was found, insert the new
// node before it. if (nextChild){
var firstSect1 = nextChild;
topElem.insertBefore(newSect1,firstSect1);
}

Depending on your exact needs you may need to add additional logic to those scripts.

Many APIs let you insert elements. One of the following (or others I have missed) might be more suitable depending on your exact need:

  DOMNode_object.appendChild()
  DOMNode_object.insertBefore()
  Selection.Surround()
  Selection.SurroundNS()
  Selection.SplitContainer()
  Selection.InsertElementNS()
  Selection.InsertWithTemplate()
  Selection.InsertWithTemplateNS()
  Selection.InsertWithRequired()
  Selection.InsertWithRequiredNS()
  Selection.InsertCaption()
  Selection.InsertImage()
  Selection.InsertTable()
  Selection.InsertCALSTable()
  Selection.InsertRowsAbove()
  Selection.InsertRowsBelow()
  Selection.InsertColumnsLeft()
  Selection.InsertColumnsRight()
  Selection.TypingSplit()
  Selection.SplitToElementType()
  Selection.SplitToElementTypeNS()
  Range.Surround()
  Range.SurroundNS()
  Range.SplitContainer()
  Range.InsertElementNS()
  Range.InsertWithTemplate()
  Range.InsertWithTemplateNS()
  Range.InsertWithRequired()
  Range.InsertWithRequiredNS()
  Range.InsertCaption()
  Range.InsertImage()
  Range.InsertTable()
  Range.InsertCALSTable()
  Range.InsertRowsAbove()
  Range.InsertRowsBelow()
  Range.InsertColumnsLeft()
  Range.InsertColumnsRight()
  Range.TypingSplit()
  Range.SplitToElementType()
  Range.SplitToElementTypeNS()


Additional useful APIs (that work with or support those listed above):

  Document_object.createElement()
  Document_object.createElementNS()
  Document_object.createDocumentFragment()
  Selection.CanInsert
  Selection.CanInsertList
  Selection.CanSplitContainer
  Selection.CanSurround
  Range.CanInsert
  Range.CanInsertList
  Range.CanSplitContainer
  Range.CanSurround
  Many Selection and Range methods that allow you to move the Selection and Range
  Many properties and methods that allow you to obtain a Selection or Range element name, attribute value, attribute name, etc.
Logged
karthic2861
Member

Posts: 22


« Reply #2 on: September 18, 2012, 03:13:49 AM »

Hi Derek,

How to listen to toolbar button (B , I , U) click events in Xmetal using Macro,Script? To create tags accordingly

for example if I press (B) from the toolbar in the xml i need to create tag as b.

Please guide us.
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #3 on: September 18, 2012, 01:39:36 PM »

Coincidentally, someone else just asked this question. See this post: http://forums.xmetal.com/index.php/topic,2496.0.html
Logged
karthic2861
Member

Posts: 22


« Reply #4 on: September 19, 2012, 04:30:00 AM »

Hi Derek,

Thanks for the reply

Can you please explain me bit more with javascript and i have xmetal author 5.5

Please guide

Regards
M
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #5 on: September 19, 2012, 11:37:35 AM »

Does the Journalist sample do what you want your customization to do?
If so, please have another look at my answer at http://forums.xmetal.com/index.php/topic,2496.0.html
Logged
Pages: 1
Print
Jump to: