Page 1 of 1

Refresh contextual menu action name

Posted: Fri Mar 11, 2022 2:05 pm
by NicoAMP
Hello,

In Oxygen web Author, I would like to add an action that takes name of current selected element in contextual menu.
Each time the contextual menu is displayed, this action's name will have the name of the current selected element.

So I use this code to get current tag name:

Code: Select all

//display name
DisplayCurrentElementAction.prototype.getDisplayName = function () {
   var oxyNode = this.editor.getSelectionManager().getSelection().getNodeAtCaret().getTagName();
   return oxyNode;
};
And I add action to the contextuel menu:

Code: Select all

goog.events.listen(workspace, sync.api.Workspace.EventType.EDITOR_LOADED, function (e) {
    var editor = e.editor;
    editor.getActionsManager().registerAction('display.current.element.action', new DisplayCurrentElementAction(editor));
    addToContextMenu(editor, 'display.current.element.action');
});    

function addToContextMenu(editor, actionId) {
    editor.listen(sync.api.Editor.EventTypes.ACTIONS_LOADED, function (e) {
        var contextualItems = e.actionsConfiguration.contextualItems;
        if (contextualItems) {
            contextualItems.unshift({
                id: actionId,
                type: "action"
            });
        }
    });
}
It works but just one time, because ACTIONS_LOADED is triggered only once
The action's name in contextual menu is not refreshed when the selection change.

Is there a way to achieve this behavior?

Thanks.

Re: Refresh contextual menu action name

Posted: Fri Mar 11, 2022 4:24 pm
by Gabriel Titerlea
Hello,

Refreshing the displayName of actions is not possible at the moment. I registered a feature request and we'll update this forum post when we have a fix available.

Meanwhile, as a workaround, you can change the DOM of the menu-item yourself, during the isEnabled method execution. Something like this:

Code: Select all

DisplayCurrentElementAction.prototype.isEnabled = function () {
  var tagName = this.editor.getSelectionManager().getSelection().getNodeAtCaret().getTagName();

  let menuItemNode = document.querySelector('.goog-menuitem[name="display.current.element.action"] span');
  if (menuItemNode) {
    menuItemNode.textContent = tagName;
  }

  // isEnabled logic.
  return true;
}
Best,
Gabriel

Re: Refresh contextual menu action name

Posted: Mon Mar 14, 2022 10:04 am
by NicoAMP
Hello Gabriel,
Thanks a lot for this workaround. It works like a charm!
Regards,
Nicolas