Page 1 of 1

Source code for PromoteDemoteItemOperation

Posted: Wed Dec 05, 2018 11:57 am
by Bruno.Ballarin
Hi,

I could not find the source code for the recently introduced PromoteDemoteItemOperation.

I am interested in finding an javascript example showing how to select a subset of similar elements either partially or totally included in the current selection at the caret position.

For instance if I make the following selection in Oxygen Dita Author mode:

Image

and run the PromoteDemoteItemOperation on this selection, I obtain this:

Image

So I am interested in this piece of code that is able to select all the similar elements (here <li> elements) that are completely or partially belonging to the current selection at the caret position, in order to perform some operations on them.

Cheers,

Bruno

Re: Source code for PromoteDemoteItemOperation

Posted: Wed Dec 05, 2018 12:11 pm
by Radu
Hi Bruno,

You should try this API:

Code: Select all

authorAccess.getDocumentController().getNodesToSelect(authorAccess.getEditorAccess().getSelectionStart(), authorAccess.getEditorAccess().getSelectionEnd());
https://www.oxygenxml.com/InstData/Edit ... t-int-int-

Regards,
Radu

Re: Source code for PromoteDemoteItemOperation

Posted: Wed Dec 05, 2018 3:59 pm
by Bruno.Ballarin
Thank you Radu,

with your help, I have been able to put together the JSOperation (provided below) that does the following:

After selecting a range of cells in a table row, or an entire row:
  • If any cell in the selection has attribute rotate = "...", the attribute "rotate" is removed for the entire row
  • If none of the cells in the selection have the attribute "rotate", the attribute rotate = "1" is set for the entire selection
Now if selecting a range of cells across multiple rows (example here 4 cells = columns indexes 2 and 3, 2nd and 3rd rows), only the last row is affected by the JSOperation:
Image

I assume this has to do with the fact that the selection correspond to 2 discontinued areas in the text view and that the getSelectionStart() and getSelectionEnd() return only the offset of the last xml fragment of the selection.

Maybe the function getSelectionIntervals() "Get the selection [start offset, end offset] intervals list" would do the trick but when I try to use it I got an error: "Could not find function getSelectionIntervals() ...". Do I need to update my Dita framework to make use of this function? How?

Code: Select all

function doOperation() {
if(authorAccess.getEditorAccess().hasSelection()) {
startOffset = authorAccess.getEditorAccess().getSelectionStart();
endOffset = authorAccess.getEditorAccess().getSelectionEnd();
results = authorAccess.getDocumentController().findNodesByXPath("../descendant-or-self::entry", true, true, true);
rotated = false;
for(i = 0; i < results.length; i++){
if(results[i].getEndOffset() > startOffset && results[i].getStartOffset() < endOffset && results[i].getAttribute("rotate") != null){
rotated = true;
}
}
if (rotated == true){
for(i = 0; i < results.length; i++){
if(results[i].getEndOffset() > startOffset && results[i].getStartOffset() < endOffset){
authorAccess.getDocumentController().removeAttribute("rotate", results[i]);
}
}
}
if (rotated == false){
for(i = 0; i < results.length; i++){
if(results[i].getEndOffset() > startOffset && results[i].getStartOffset() < endOffset){
authorAccess.getDocumentController().setAttribute("rotate", new Packages.ro.sync.ecss.extensions.api.node.AttrValue("1"), results[i]);
}
}
}
}
}

Re: Source code for PromoteDemoteItemOperation

Posted: Thu Dec 06, 2018 8:54 am
by Radu
Hi,

Indeed, and mostly when it comes to tables, you may have multiple selection intervals.
The method can be accessed like:

Code: Select all


authorAccess.getEditorAccess().getAuthorSelectionModel().getSelectionIntervals();
https://www.oxygenxml.com/InstData/Edit ... Model.html

Regards,
Radu

Re: Source code for PromoteDemoteItemOperation

Posted: Mon Dec 10, 2018 12:25 pm
by Bruno.Ballarin
Thank you Radu,

I got it to work now: Set/clear Attribute rotate = "1" on:
  • an arbitrary selection of cells (including multiple cells belonging to multiple rows)
  • a single cell at cursor position

Code: Select all

function doOperation() {
if(authorAccess.getEditorAccess().hasSelection()) {
offsetList = authorAccess.getEditorAccess().getAuthorSelectionModel().getSelectionIntervals();
results = authorAccess.getDocumentController().findNodesByXPath("ancestor-or-self::table/descendant-or-self::entry", true, true, true);
rotated = false;
for(i = 0; i < results.length; i++){
for(j = 0; j < offsetList.size(); j++){
if(results[i].getEndOffset() >= offsetList.get(j).getStartOffset() && results[i].getStartOffset() < offsetList.get(j).getEndOffset() && results[i].getAttribute("rotate") != null){
rotated = true;
}
}
}
if (rotated == true){
for(i = 0; i < results.length; i++){
for(j = 0; j < offsetList.size(); j++){
if(results[i].getEndOffset() >= offsetList.get(j).getStartOffset() && results[i].getStartOffset() < offsetList.get(j).getEndOffset()){
authorAccess.getDocumentController().removeAttribute("rotate", results[i]);
}
}
}
}
if (rotated == false){
for(i = 0; i < results.length; i++){
for(j = 0; j < offsetList.size(); j++){
if(results[i].getEndOffset() >= offsetList.get(j).getStartOffset() && results[i].getStartOffset() < offsetList.get(j).getEndOffset()){
authorAccess.getDocumentController().setAttribute("rotate", new Packages.ro.sync.ecss.extensions.api.node.AttrValue("1"), results[i]);
}
}
}
}
} else {
results = authorAccess.getDocumentController().findNodesByXPath("ancestor-or-self::entry", true, true, true);
rotated = false;
for(i = 0; i < results.length; i++){
if(results[i].getAttribute("rotate") != null){
rotated = true;
}
}
if (rotated == true){
for(i = 0; i < results.length; i++){
authorAccess.getDocumentController().removeAttribute("rotate", results[i]);
}
}
if (rotated == false){
for(i = 0; i < results.length; i++){
authorAccess.getDocumentController().setAttribute("rotate", new Packages.ro.sync.ecss.extensions.api.node.AttrValue("1"), results[i]);
}
}
}
}
Image

Re: Source code for PromoteDemoteItemOperation

Posted: Mon Dec 10, 2018 12:31 pm
by Radu
Hi Bruno,

Great, thanks for updating the forum thread. Probably in the future we might also need to add a default operation in Oxygen (we have a Table Properties operation which can be used to edit a table's configuration) to do the same thing. I will add an issue on our side.

Regards,
Radu